1 / 18

資料結構實習 - 十四

資料結構實習 - 十四. Graph. Graph. 0. 1. V ( G 1 )={0, 1, 2, 3, 4, 5, 6, 7, 8, 9} E ( G 1 )={(0, 2), (0, 3), (1, 4), (2, 3), (2, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5, 8), (6, 9), (7, 9),

wood
Download Presentation

資料結構實習 - 十四

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. 資料結構實習-十四 Graph

  2. Graph 0 1 V(G1)={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}E(G1)={(0, 2), (0, 3), (1, 4), (2, 3), (2, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5, 8), (6, 9), (7, 9), } 2 3 4 5 6 7 8 9

  3. Adjacency matrix 0 • 以右圖為例: • Adjacency matrices表示 • 輸出結果: 1 2 3 0 1 2 3 0 1 2 3 • (0) (1) (2) (3) • (0) 0 0 0 1 • (1) 0 0 1 1 • 0 1 0 1 • 1 1 1 0

  4. Adjacency matrix – 範例 0 1 2 3 0 0 0 1 2 3 1 2 1 3 G1 The adjacency matrix of G1 2 0 1 2 0 1 2 G3 The adjacency matrix of G3

  5. Adjacency list 0 3 1 2 3 2 1 3 3 0 1 2 0 1 2 3 • (0) -> 3 • (1) -> 2 -> 3 • -> 1 -> 3 • -> 0 -> 1 -> 2 • 以右圖為例: • Adjacency List表示 • 輸出結果:

  6. Adjacency list – 範例 adj_list data link 0 3 1 2 2 3 0 1 2 1 3 0 3 G1 0 1 2 Adjacent list ofG1 0 Adj_list data link 1 1 2 0 G3 Adjacent list ofG3 2

  7. Depth First Search 0 0 • 類似樹的先序尋訪。 • 必須紀錄之前的路徑。 • Stack • Recursive 1 2 1 2 4 3 4 5 6 3 5 6 7 7 0 -> 1 -> 3 -> 7 -> 4 -> 5 -> 2 -> 6

  8. Biconnected Graph 8 • Articulation point • 如果將v 以及所有附著於v 的邊刪除,會產生兩個以上的連通元件(connected graph),我們就稱v 為Articulation point(連接點)。 • biconnected graph • 二連通圖是一個沒有連接點的連通圖形

  9. Biconnected Graph – 範例 8 0 9 7 1 7 1 2 3 5 3 5 4 6 9

  10. A Connected Graph and Its Biconnected Components 0 8 9 8 0 9 1 7 7 1 7 1 7 2 3 5 2 3 3 5 5 4 6 4 6 (a) A connected graph (b) Biconnected components 10

  11. Biconnected Graph – 範例 0 2 1 3 3 0 1 2 3 Biconnected Graph(List) 1:<0,3> 2:<1,2><1,3><2,3> • 以右圖為例: • Biconnected Graph表示 • 程式預期輸出結果:

  12. Biconnected Graph – 演算法 • depth first numbers (dfn) • 在頂點在深度優先搜尋時,頂點被拜訪的順序。 • low(u)是利用 u 的後代以及至多一個後退邊(back edge)可達到的頂點之最小深度優先序號 • low(u) = min {dfn(u), min {low(w) | w is a child of u}, min {dfn(w) | (u, w) is a back edge}} • back edge:在DFS時不被經過的邊。 • u 是一個連接點若且唯若 • u 是一個根節點且至少有兩個子節點 • 或 u 不是根節點但有一個子節點 w 使得 low(w)  >=dfn(u))

  13. Biconnected Graph – 演算法 dfn:0 dfn:9 dfn:8 0 0 0 8 8 low:9 0 9 8 9 9 9 low:8 low:0 dfn:1 7 , 9 7 , 8 1 1 1 1 7 7 7 7 7 7 low:1 7 , 5 dfn:7 low:7 low:5 6 , 7 5 , 6 <7,8> dfn:2 3 , 5 <7,9> low:1 low:2 2 3 2 3 3 5 2 2 3 5 5 3 , 1 5 <7,5> <6,7> <5,6> 4 , 3 <3,5> dfn:4 dfn:5 <2,4> <1,2> <3,1> <4,3> 2 , 4 low:4 low:1 low:5 <0,1> 1 , 2 4 6 6 6 4 4 6 6 4 0 , 1 dfn:3 dfn:6 low:1 low:3 low:6 low:5 Stack 13

  14. A Connected Graph and Its Biconnected Components 0 8 9 8 0 9 1 7 7 1 7 1 7 2 3 5 2 3 3 5 5 4 6 4 6 (a) A connected graph (b) Biconnected components 14

  15. 0 1 2 3 4 5 6 7 8 9 練習 • 右圖為本次練習的題目 • 請自教學網站下載graph.txt。 • 共有十個vertex,以0-9表示。 • 每一行代表一條edge • vertex 之間以tab(\t)隔開

  16. 練習-1 將該圖形資料轉換成Adjacency matrices的格式,並輸出矩陣格式。 針對練習一中完成的矩陣格式,做Depth First Search的動作,並將結果輸出。 針對練習一中完成的矩陣格式,找出該圖形中的所有 Biconnected Componentsgraph,並將結果輸出。

  17. 練習-2 將該圖形資料轉換成Adjacency list的格式,並輸出矩陣格式。 針對練習一中完成的串列格式,做Depth First Search的動作,並將結果輸出。 針對練習一中完成的串列格式,找出該圖形中的所有 Biconnected Componentsgraph,並將結果輸出。

  18. 練習 • DFS: • 0 2 3 6 5 8 9 7 4 1 • Biconnected Components graph • <5,8> • <4,1> • <7,4> • <7,3> <9,7> <6,9> <5,2> <6,5> <6,2> <3,6> <3,0> <2,3><0,2>

More Related