Presentation is loading. Please wait.

Presentation is loading. Please wait.

6.1.3 Graph representation.

Similar presentations


Presentation on theme: "6.1.3 Graph representation."— Presentation transcript:

1 6.1.3 Graph representation

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

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

4 Adjacency lists n個linked list 2 1 G3 1 2 #define MAX_VERTICES 50
G3 1 2 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 */ 3 1 2 1 2 3 G1

5 Adjacency lists, by array
2 1 G3 1 2

6 Adjacency multilists 1 2 3 G1 m vertex1 vertex2 list1 list2 N0
1 2 3 G1 Adjacency multilists m vertex1 vertex2 list1 list2 N0 N1 N3 N1 N2 N3 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]; N2 NIL N4 N3 N4 N5 N4 NIL N5 N5 NIL NIL

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 Spanning Tree A spanning tree is a minimal subgraph G’, such that V(G’)=V(G) and G’ is connected Weight and MST 3 1 2 1 2 3 G1 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 A connected graph and its biconnected components
1 2 3 4 5 7 6 8 9 1 7 8 7 9 1 2 3 4 5 7 6 3 5 為何沒有任一個邊可能存在於兩個或多個biconnected component中?

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

17 dfn() and low() Observation low(u): u及後代,其back edge可達vertex之最小dfn()
若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 A example: dfs() and low()
1 2 3 4 5 7 6 8 9 10 1 2 3 4 5 7 6 8 9 10 請自行Trace Biconnected() Hint: 將Unvisited edge跟back edge送入Stack, 到Articulation Point 再一次輸出


Download ppt "6.1.3 Graph representation."

Similar presentations


Ads by Google