1 / 18

6.1.3 Graph representation

6.1.3 Graph representation. 0. 1. 2. 3. G1. 0. 1. 2. 0. 4. G3. 2. 1. 5. 6. 3. 7. G4. 常見的表示法. Adjacency matrices Adjacency lists Adjacency multilists. 0. 1. 2. 3. G1. 0. 1. 2. G3. Adjacency matrix. n*n 陣列 n*(n-1), 即 O(n 2 ) If the matrix is sparse ? 大部分元素是 0

liza
Télécharger la présentation

6.1.3 Graph representation

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. 6.1.3 Graph representation

  2. 0 1 2 3 G1 0 1 2 0 4 G3 2 1 5 6 3 7 G4 常見的表示法 • Adjacency matrices • Adjacency lists • Adjacency multilists

  3. 0 1 2 3 G1 0 1 2 G3 Adjacency matrix • n*n 陣列 • n*(n-1),即 O(n2) • If the matrix is sparse ? • 大部分元素是0 • e << (n2/2)

  4. 0 1 2 3 1 G1 2 0 0 1 2 3 1 2 2 G3 3 0 3 1 0 0 1 2 Adjacency lists • n個linked list #define MAX_VERTICES 50 typedef struct node *node_ptr; typedef struct node { int vertex; node_ptr link; } node; node_ptr graph[MAX_VERTICES]; int n = 0; /* number of nodes */

  5. 1 2 0 0 1 2 G3 Adjacency lists, by array

  6. 0 1 2 3 G1 Adjacency multilists list1 m vertex1 vertex2 list2 0 1 N1 N3 N0 0 2 N2 N3 N1 typedef struct edge *edge_ptr; Typedef struct edge { int marked; int vertex1; int vertex2; edge_ptr path1; edge_ptr path2; } edge; edge_ptr graph[MAX_VERTICES]; 0 3 NIL N4 N2 1 2 N4 N5 N3 1 3 NIL N5 N4 2 3 NIL NIL N5

  7. Weighted edges • Cost • Weight field • Network

  8. 6.2 Elementary graph operations

  9. Outlines • Operations similar to tree traversals • Depth-First Search (DFS) • Breadth-First Search (BFS) • Is it a connected graph? • Spanning trees • Biconnected components

  10. Depth-First Search • Adjacency list: O(e) • Adjacency Mtx: O(n2) • 例如:老鼠走迷宮 int visited[MAX_VERTICES]; void dfs(int v) { node_ptr w; visited[v] = TRUE; printf(“%5d”, v); for (w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); }

  11. 例如:地毯式搜索 Breadth-First Search void bfs(int v) { node_ptr w; queue_ptr front, rear; front=rear=NULL; printf(“%5d”,v); visited[v]=TRUE; addq(&front, &rear, v); while(front) { v = deleteq(&front); for(w=graph[v]; w; w=w->link) if(!visited[w->vertex]) { printf(“%5d”, w->vertex); addq(&front, &rear, w->vertex); visited[w->vertex] = TRUE; } } } } typedef struct queue *queue_ptr; typedef struct queue { int vertex; queue_ptr link; }; void addq(queue_ptr *, queue_ptr *, int); Int deleteq(queue_ptr);

  12. Connected component • Is it a connected graph? • BFS(v) or DFS(v) • Find out connected component void connected(void){ int i; for (i=0;i<n;i++){ if(!visited[i]){ dfs(i); printf(“\n”); } }

  13. 0 1 2 3 1 2 3 2 3 0 G1 3 1 0 0 1 2 Spanning Tree • A spanning tree is a minimal subgraph G’, such that V(G’)=V(G) and G’ is connected • Weight and MST 0 0 1 2 1 2 3 3 DFS(0) BFS(0)

  14. Biconnected components • Definition: Articulation point (關節點) • 原文請參閱課本 • 如果將vertex v以及連接的所有edge去除,產生 graph G’, G’至少有兩個connected component, 則v稱為 articulation point • Definition: Biconnected graph • 定義為無Articulation point的connected graph • Definition: Biconnected component • Graph G中的Biconnected component H, 為G中最大的biconnected subgraph; 最大是指G中沒有其他subgraph是biconnected且包含入H

  15. 9 0 8 9 0 7 8 1 7 1 7 2 3 5 1 7 4 6 2 3 3 5 5 4 6 A connected graph and its biconnected components • 為何沒有任一個 點/邊 可能存在於兩個或多個biconnected component中?

  16. 1 3 2 6 4 5 5 9 10 0 8 9 3 7 2 6 4 1 7 4 8 8 1 7 3 1 6 2 3 5 10 5 9 7 0 8 9 4 6 2 DFS spanning tree of the graph in 6.19(a) • Root at 3 • Back edge and cross edge

  17. dfn() and low() • Observation • 若root有兩個以上child, 則為articulation point • 若vertex u有任一child w, 使得w及w後代無法透過back edge到u的祖先, 則為 articulation point • low(u): u及後代,其back edge可達vertex之最小dfn() • low(u) = min{ dfn(u), min{low(w)|w是u的child}, min{dfn(w)|(u,w)是back edge}}

  18. 1 3 2 6 4 5 5 9 10 0 8 9 3 7 2 6 4 1 7 4 8 8 1 7 3 1 6 2 3 5 10 5 9 7 0 8 9 4 6 2 A example: dfs() and low() • 請自行Trace Biconnected() • Hint: 將Unvisited edge跟back edge送入Stack, 到Articulation Point 再一次輸出

More Related