1 / 27

Graph

連載 : 學生上課睡覺姿勢大全. http://www.wretch.cc/blog/chi771027/26489957. Graph. Michael Tsai 2010/11/26. 本日行程. 期中考檢討 DFS & BFS Spanning tree Biconnected components & Articulation points. 分佈圖. 複習 : 表示 法 I 用 Array. 1. 2. 3. 本方法叫做 adjacency matrix index 當作 vertex 號碼 edge 用 matrix 的值來表示 :

johnda
Télécharger la présentation

Graph

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 連載: 學生上課睡覺姿勢大全 http://www.wretch.cc/blog/chi771027/26489957 Graph Michael Tsai 2010/11/26

  2. 本日行程 • 期中考檢討 • DFS & BFS • Spanning tree • Biconnected components &Articulation points

  3. 分佈圖

  4. 複習: 表示法I 用Array 1 2 3 • 本方法叫做adjacency matrix • index當作vertex號碼 • edge用matrix的值來表示: • 如果有(i,j)這條edge, 則a[i][j]=1, a[j][i]=1. (graph) • 如果有<i,j>這條edge, 則a[i][j]=1. (digraph) 4 5 6

  5. 複習: 表示法II 用linked list • 本方法叫做adjacency lists • 建立一個list arraya[n](n為vertex數目) • 每個list裡面紀錄通往別的vertex的edge 0 1 2 3 4 5 [0] [1] [2] [3] [4] [5]

  6. Depth first search • 給一個vertex v in graph G • 和v連接的其他vertex全部都走一遍 • 走的順序有很多種 • 有點類似之前的preorder tree traversal • visit自己以後, 每次都先走自己的children (而不是走自己的sibling) • 問: 應該是用queue還是stack? • 答: stack.

  7. 詳細一點 dfs(vertex v) { 紀錄v來過了; 印出v; for v的每一個edge (v,u) { if u還沒去過 dfs(u); } } • 問: 如果要改成用iterative的寫法呢? • 請同學來講 • 答: 下一頁

  8. Iterative version of dfs dfs(vertex v ) { push(v, stack s); while(stack s is not empty) { v=pop(stack s); mark that v is already visited; print v; for each edge incident on v: (u,v) { if u is not visited push(u, stack s); } }

  9. 來一個例子 dfs(0) • Adjacency lists: • 0: 1 2 • 1: 0 3 4 • 2: 0 5 6 • 3: 1 7 • 4: 1 7 • 5: 2 7 • 6: 2 7 • 7: 3 4 5 6 0 0 6 1 2 1 4 5 7 2 3 4 5 6 7 3 問: 如果做dfs(5)呢? 答: 5 2 0 1 3 7 4 6

  10. Depth First Search分析 • 問: 做完一次depth first search的time complexity=? • 如果使用adjacency lists, 則最多每條edge被看一次. • 所以為O(e), e為edge數目 • 如果使用adjacency matrix, 則每個二維陣列的entry都要檢查一遍 • 所以為

  11. Breadth First Search • 那麼Breadth First Search呢? • 猜一猜, 要使用什麼資料結構? • 跟哪一種tree traversal相像? • 答: queue, 和level-order traversal很像 • 距離開始的vertex相同距離的vertex會同時( 在附近)visit完畢

  12. 來一個例子 dfs(0) • Adjacency lists: • 0: 1 2 • 1: 0 3 4 • 2: 0 5 6 • 3: 1 7 • 4: 1 7 • 5: 2 7 • 6: 2 7 • 7: 3 4 5 6 0 0 2 1 2 1 4 3 5 6 3 4 5 6 7 7 問: 如果做bfs(5)呢? 答:5 2 7 0 3 4 6 1

  13. BFS怎麼寫? bfs(vertex v){ mark that v is visited. add(v, queue q); while(queue q is not empty) { print v; for all edges incident on v: (v,u) if u is not visited { mark that u is visited. add(u, queue q); } } } time complexity 和 dfs的一樣: 和

  14. 萬用BFS & DFS • 可以用來幹嘛呢? • 1. 看某graph G是不是connected • 複習: 什麼是connected graph • 怎麼做? • 2. 列出所有的connected component • 複習: 什麼是connected component • 怎麼做? • (課本p283) • 後面還有

  15. 0 Spanning Tree 2 1 • Spanning Tree: • 是任何一種tree, • 並且是原本的graph的subgraph, G’=(V, E’) • (包含了原本所有的vertex及原本某些edge) • 在spanning tree中的edges稱為tree edges • 在原本的graph中但不在tree中的edges稱為nontree edges • 練習一下: 隨便畫出幾種spanning tree 3 4 5 6 7

  16. DFS & BFS Tree • 注意: dfs和bfs產生出來的結果都是tree喔! • 叫做dfs tree & bfstree • 所以他們可以視為一種產生spanning tree的方法 • 如何修改剛剛的code, 分辨tree & nontree edges? • (課本p284)

  17. Spanning Tree的一些特性 • Spanning Tree是minimal subgraph, 且V(G’)=V(G) & G’ is connected. • 在這邊minimal的意思, 變成是說edge數目是最少的. • 為什麼? • 想想看連接n個vertex (node)最少要多少edge? • n-1個 • 正好就是有n個node的tree的branch數目 • 所以spanning tree是minimal subgraph

  18. Biconnected Components相關名詞 • Articulation point: • 如果在connected graph G中的的一個vertex v被移除以後(包含v和所有incident在它上面的edge), 新的graph G’會變成有兩塊以上的connected components • (複習: 什麼是 connected components) • Biconnected graph: 沒有articulation point的graph • Biconnected component: maximal biconnectedsubgraph • 這邊maximal是說, 沒有一個可以包含它的subgraph是biconnected的

  19. 例子 Biconnected components: 9 0 8 9 0 8 1 7 1 7 1 7 3 5 2 3 5 2 3 5 6 4 6 4 猜一猜哪邊是articulation point? (有沒有articulation point?) 加了什麼邊可以讓它變成不是articulation point?

  20. 例子: 資訊系館網路 實驗室 各樓層研討室 cisco 2960 1G 網路線 cisco 3750 cisco 2960 215 機房 1G 光纖 NTU CC Where is the articulation point(s)? How do we eliminate them? PC PC PC

  21. DFS另一用途 • 可以用來尋找articulation point & biconnected components • 怎麼用呢? 首先先把graph做一次dfs, 並標上順序 • 從哪個vertex開始不重要, edge順序也不重要 • (真的嗎? 自己驗證看看) 9 0 8 9 8 4 3 1 7 7 2 5 0 2 3 5 6 6 1 4

  22. 尋找articulation point • 如果是root的話(開始做dfs的地方), 且有超過一個的children 是articulation point • 如果不是root: • 當有一個以上的小孩, 無法沿著它自己的小孩及一條nontree edge (back edge)到達它的祖先的時候, 則為articulation point • back edge: 一條edge (u,v), u是v的祖先或者v是u的祖先. • dfs tree中所有nontree edge都是back edge (為什麼???) 0 3 1 5 4 5 2 6 2 6 3 7 7 1 9 8 4 0 9 8 答: 如果有不是back edge的nontree edge, 則在dfs裡面就會變成tree edge (先visit)

  23. 尋找articulation point • 定義一些function來找articulation point • dfn(v) dfs的順序, 由小到大 • 注意如果u是v的祖先, 則dfn(u)<dfn(v) • low(u)=min{dfn(u), min{low(w)|w is a child of u}, min{dfn(w)| (u,w) is a back edge}}

  24. 尋找articulation point 0 • 當某vertex v有任何一個child的low值大於dfn(v)時, 則v為articulation point • 問: 怎麼output articulation point和biconnected component呢? • (program 6.6 on p290) 3 1 5 4 5 2 6 2 6 3 7 7 1 9 8 4 0 9 8

  25. 下課時間

  26. 下課時間

  27. 下周再見! • 作業四今天晚點或者明天會上線 • 對於考試的分數有疑問的同學, 歡迎下課來找我或者多利用office hour • 成績比較不理想的同學, 請盡早來找我, 作業提早開始寫

More Related