Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Data Structures in C Copyright(c) 2000, Sungkyunkwan University Chapter #6: GRAPHS Fundamentals of Data Structures in C Horowitz, Sahni, and Anderson-Freed Computer Science Press Revised by H. Choo, November 2000. Networking Laboratory Chap. 6-1 Copyright(c) 2000, Sungkyunkwan University Data Structures in C The Graph Abstract Data Type Introduction c C d g C g D A e Kneiphof c d e A a D b f B a b B f (a) (b) the bridges of Koenigsberg Networking Laboratory Chap. 6-2 Copyright(c) 2000, Sungkyunkwan University Data Structures in C The Graph Abstract Data Type Def. A graph G = (V,E) where V(G): nonempty finite set of vertices, E(G): finite set of edges possibly empty undirected graph: unordered (vi,vj) = (vj,vi) directed graph ordered:<vi,vj> <vj,vi> 0 0 0 1 1 2 3 3 2 4 5 1 6 2 G1 G2 G3 Networking Laboratory Chap. 6-3 Copyright(c) 2000, Sungkyunkwan University Data Structures in C The Graph Abstract Data Type restrictions on graphs 1)no edge from a vertex, i, back to itself (no self loop) - not allowed (vi,vi) or <vi,vi> 2)no multiple occurrences of the same edge ( multigraph) 0 0 2 1 3 1 2 (a) (b) examples of a graph with feedback loops and a multigraph Networking Laboratory Chap. 6-4 Data Structures in C Copyright(c) 2000, Sungkyunkwan University The Graph Abstract Data Type complete graph - the maximum number of edges - undirected graph with n vertices max number of edges = n(n-1)/2 - directed graph with n vertices max number of edges = n(n-1) adjacent - vi and vj are adjacent if (vi,vj) E(G) adjacent to(from) for digraphs - <v0,v1>: a directed edge - vertex v0 is adjacent to vertex v1 - vertex v1 is adjacent from vertex v0 incident - an edge e = (vi,vj) is incident on vertices vi and vj Networking Laboratory Chap. 6-5 Copyright(c) 2000, Sungkyunkwan University Data Structures in C The Graph Abstract Data Type subgraph G’ of G - V(G’) V(G) and E(G’) E(G) 0 1 0 0 2 1 1 2 2 3 3 (i) (ii) (iii) (iv) some of the subgraphs of G1 0 1 2 G1 3 Networking Laboratory Chap. 6-6 Data Structures in C Copyright(c) 2000, Sungkyunkwan University The Graph Abstract Data Type 0 1 0 (i) 0 1 (ii) 2 some of the subgraphs of G3 0 0 1 1 2 2 (iii) (iv) G3 path (from vertex vp to vertex vq): a sequence of vertices, vp,vi1,vi2,···,vin,vq such that (vp,vi1), (vi1,vi2), ···, (vin,vq) are edges in an undirected graph or <vp,vi1>,<vi1,vi2>,···,<vin,vq> are edges in a directed graph length of path: # of edges on the path Networking Laboratory Chap. 6-7 Data Structures in C Copyright(c) 2000, Sungkyunkwan University The Graph Abstract Data Type Simple path: a path in which all vertices, except possibly the first and the last, are distinct Cycle: a path in which the first and last vertices are the same - simple cycle for directed graph: add the prefix “directed” to the terms cycle and path - simple directed path - directed cycle, simple directed cycle Connected - vertex v0 and v1 is connected, if there is a path from v0 to v1 in an undirected graph G - an undirected graph is connected if, for every pair of vertices vi and vj, there is a path from vi to vj Networking Laboratory Chap. 6-8 Copyright(c) 2000, Sungkyunkwan University Data Structures in C The Graph Abstract Data Type connected component (of an undirected graph) - maximal connected subgraph 4 H1 H2 0 1 5 2 6 3 7 G4 a graph with two connected components Networking Laboratory Chap. 6-9 Copyright(c) 2000, Sungkyunkwan University Data Structures in C The Graph Abstract Data Type strongly connected (in a directed graph) - for every pair of vertices vi, vj in V(G) there is a directed path from vi to vj and also from vj to vi 1 4 strongly connected directed graph 3 2 strongly connected component - maximal subgraph that is strongly connected 0 2 1 strongly connected components of G3 Networking Laboratory Chap. 6-10 Data Structures in C Copyright(c) 2000, Sungkyunkwan University The Graph Abstract Data Type degree (of a vertex): number of edges incident to that vertex in-degree (of a vertex v): number of edges that have v as the head (for directed graphs) out-degree (of a vertex v): number of edges that have v as the tail (for directed graphs) special types of graphs Tree: an acyclic connected graph bipartite garph planar graph complete graph Networking Laboratory Chap. 6-11 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Graph Representations Adjacency matrix - G = (V,E) with |V| = n(1) - two-dimensional n n array, say adj_mat[][] - adj_mat[i][j] = “1” if (vi,vj) is adjacent “0” otherwise 2 - space complexity: S(n) = n - symmetric for undirected graphs - asymmetric for directed graphs Networking Laboratory Chap. 6-12 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Graph Representations 0 1 2 3 0 0 1 1 1 2 1 1 0 1 1 1 0 1 1 3 1 1 1 0 0 0 1 0 0 1 2 G1 0 1 2 3 4 5 6 7 1 1 0 0 2 0 1 0 G3 0 0 1 1 0 0 0 0 0 1 1 0 0 1 0 0 0 0 2 1 0 0 1 0 0 0 0 3 0 1 1 0 0 0 0 0 4 0 0 0 0 0 1 0 0 5 0 0 0 0 1 0 1 0 6 0 0 0 0 0 1 0 1 7 0 0 0 0 0 0 1 0 G4 adjacency matrices for G1, G3, and G4 Networking Laboratory Chap. 6-13 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Graph Representations Adjacency lists - replace n rows of adjacency matirx with n linked lists - every vertex i in G has one list #define MAX_VERTICES 50 typedef struct node *node_ptr; typdef struct node { int vertex; node_ptr link; }; node_ptr graph[MAX_VERTICES]; int n = 0; /* vertices currently in use */ vertex link Networking Laboratory Chap. 6-14 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Graph Representations headnode vertex link 0 1 2 3 1 0 2 3 2 0 1 3 3 0 1 2 G1 0 1 1 0 2 2 G3 Networking Laboratory Chap. 6-15 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Graph Representations 0 1 2 1 0 3 2 0 3 3 1 2 4 5 5 4 6 6 5 7 7 6 G4 adjacency lists for G1, G3, and G4 Networking Laboratory Chap. 6-16 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Graph Representations Inverse adjacency lists - useful for finding in-degree of a vertex in digraphs - contain one list for each vertex - each list contains a node for each vertex adjacent to the vertex that the list represents 0 1 1 0 2 1 inverse adjacency list for G3 Networking Laboratory Chap. 6-17 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Graph Representations Orthogonal representation - change the node structure of the adjacency lists tail head headnodes (shown twice) column link for head 0 2 0 1 0 1 1 row link for tail 1 0 1 2 2 orthogonal representation for graph G3 Networking Laboratory Chap. 6-18 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Graph Representations vertices may appear in any order headnode vertex link 0 3 1 2 1 2 0 3 2 3 0 1 3 2 1 0 alternate order adjacency list for G1 Networking Laboratory Chap. 6-19 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Graph Representations weighted edges - assign weights to edges of a graph 1)distance from one vertex to another, or 2)cost of going from one vertex to an adjacent vertex - modify representation to signify an edge with the weight of the edge for adj matrix : weight instead of 1 : weight field for adj list network - a graph with weighted edges Networking Laboratory Chap. 6-20 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Elementary Graph Operations graph traversals - visit every vertex in a graph - what order? DFS(Depth First Search): similar to a preorder tree traversal BFS(Breath First Search): similar to a level-order tree traversal v0 v1 v3 v2 v4 v5 v6 (a) v7 Networking Laboratory Chap. 6-21 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Elementary Graph Operations 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 (b) graph G and its adjacency lists Networking Laboratory Chap. 6-22 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Elementary Graph Operations DFS (depth first search) easy to implement recursively - stack a global array visited[MAX_VERTICES] - initialized to FALSE - change visited[i] to TRUE when a vertex i is visited #define FALSE 0 #define TRUE 1 short int visited[MAX_VERTICES]; Networking Laboratory Chap. 6-23 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Elementary Graph Operations EX) visited: v0 0 1 2 v1 3 v2 4 5 v3 v4 v5 v6 6 7 v7 v4 v0 v7 v7 v7 v3 v3 v3 v1 v1 v1 v1 v0 v0 v0 v0 Networking Laboratory Chap. 6-24 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Elementary Graph Operations void dfs(int v) { /* depth first search of a graph beginning with vertex 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); } DFS(depth first search) - time complexity representation: - time complexity representation: for adj list O(e) for adj matrix O(n2) Networking Laboratory Chap. 6-25 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Elementary Graph Operations BFS(breadth first search) use a dynamically linked queue - each queue node contains vertex and link fields typedef struct queue *queue_ptr; typedef struct queue { int vertex; queue_ptr link; }; void insert(queue_ptr *, queue_ptr *, int); void delete(queue_ptr *); Networking Laboratory Chap. 6-26 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Elementary Graph Operations v0 v1 v1 v2 v2 v2 v3 v4 v3 v4 v 5 v6 front v3 v4 v4 v5 v6 v7 v6 v5 v5 v6 v7 v6 v7 v7 v7 Networking Laboratory Chap. 6-27 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Elementary Graph Operations void bfs(int v) { node_ptr w; queue_ptr front, rear; front = rear = NULL;/* initialize queue */ printf(“%5d”, v); visited[v] = TRUE; insert(&front, &rear, v); while (front) { v = delete(&front); for (w = graph[v]; w; w = w->link) if (!visited[w->vertex]) { printf(“%5d”, w->vertex); add(&front, &rear, w->vertex); visited[w->vertex] = TRUE; } } } breath first search of a graph time complexity for bfs() - time complexity for adj list: O(e) - time complexity for adj matrix: O(n2) Networking Laboratory Chap. 6-28 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Elementary Graph Operations connected components determine whether or not an undirected graph is connected - simply calling dfs(0) or bfs(0) and then determine if there are unvisited vertices list the connected components of a graph - make repeated calls to either dfs(v) or bfs(v) where v is an unvisited vertex void connected(void) { /* determine the connected components of a graph */ int i; for (i = 0; i < n; i++) if (!visited[i]) { dfs(i); printf(“\n”); } } time complexity: O(n+e) total time by dfs: O(e), for loop: O(n) Networking Laboratory Chap. 6-29 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Elementary Graph Operations spanning trees when graph G is connected dfs or bfs implicitly partitions the edges in G into two sets: - T(for tree edges): set of edges used or traversed during the search - N(for nontree edges): set of remaining edges - edges in T form a tree that includes all vertices of G Def) A spanning tree is any tree that consists solely of edges in G and that include all the vertices in G Networking Laboratory Chap. 6-30 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Elementary Graph Operations a complete graph and three spanning trees depth first spanning tree - use dfs to create a spanning tree breadth first spanning tree - use bfs to create a spanning tree Networking Laboratory Chap. 6-31 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Elementary Graph Operations v0 v0 v1 v3 v2 v4 v5 v1 v6 v3 v2 v4 v5 v6 v7 v7 (a) dfs(0) spanning tree (b) bfs(0) spanning tree dfs and bfs spanning trees properties of spanning trees 1) if we add a nontree edge into a spanning tree cycle 2) spanning tree is a minimal subgraph G’ of G such that V(G’) = V(G) and G’ is connected 3) |E(G’)| = n - 1 where |V(G)| = n minimum cost spanning treesNetworking Laboratory Chap. 6-32 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Elementary Graph Operations biconnected components and articulation points (cut-points) articulation point - a vertex v of G - deletion of v, together with all edges incident on v, produce a graph, G’, that has at least two connected components biconnected graph - a connected graph that has no articulation points Example of biconnected graph v0 v1 v3 v2 v4 v5 v6 v7 Networking Laboratory Chap. 6-33 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Elementary Graph Operations 0 8 1 7 2 3 4 9 5 6 a connected graph which is not biconnected articulation points are 1,3,5,7 biconnected component - maximal biconnected subgraph,H ,of connected undirected graph G - two biconnected components of the same graph have no more than one vertex in common - no edge can be in two or more biconnected components of a graph - biconnected components of a graph G partition the edges of G Networking Laboratory Chap. 6-34 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Elementary Graph Operations 0 8 1 7 2 0 3 9 5 4 8 6 (a) connected graph 1 7 1 2 7 7 3 4 9 3 5 (b) biconnected components 5 6 a connected graph and its biconnected components Networking Laboratory Chap. 6-35 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Elementary Graph Operations depth first number, or dfn - sequence in which the vertices are visited during the depth first search - if u is an ancestor of v in the df spanning tree, dfn(u) < dfn(v) 0 2 4 0 3 1 2 4 1 9 8 0 5 3 5 9 8 3 5 1 4 7 7 5 2 6 2 6 6 (a) depth first spanning tree dfs(3) 6 7 3 1 7 4 8 0 9 9 8 (b) Networking Laboratory Chap. 6-36 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Minimum Cost Spanning Trees cost of a spanning tree of a weighted undirected graph - sum of the costs(weights) of the edges in the spanning tree - a spanning tree of least cost - Kruskal’s, Prim’s, and Sollin’s algorithms - greedy method: construct an optimal solution in stages, make the best decision at each stage using some criterion for spanning tree: least cost criterion - use only edges within the graph - use exactly n-1 edges - may not use edges that would produce a cycle Networking Laboratory Chap. 6-37 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Minimum Cost Spanning Trees (1) Kruskal’s algorithm - select the edges for inclusion in T in nondecreasing order of their cost - an edge is added to T if it does not form a cycle with the edges that are already in T - exactly n-1 edges are selected Networking Laboratory Chap. 6-38 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Minimum Cost Spanning Trees Kruskal’s algorithm T = {}; while (T contains less than n-1 edges && E is not empty) { choose a least cost edge (v,w) from E; delete (v,w) from E; if ((v,w) does not create a cycle in T) add (v,w) to T; else discard (v,w); } if (T contains fewer than n-1 edges) printf(“no spanning tree\n”); Networking Laboratory Chap. 6-39 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Minimum Cost Spanning Trees choosing a least cost edge(v,w) from E - a min heap - determine and delete the next least cost edge: O(log2e) - construction of the heap: O(e) checking that the new edge,(v,w), does not form a cycle in T - use the union-find operations Networking Laboratory Chap. 6-40 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Minimum Cost Spanning Trees 0 0 28 10 0 1 1 14 16 6 2 5 5 6 10 2 1 5 6 2 24 25 18 4 12 22 3 (a) 4 4 3 (b) 3 (c) stages in Kruskal’s algorithm Networking Laboratory Chap. 6-41 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Minimum Cost Spanning Trees 0 0 10 1 0 10 1 10 1 14 5 6 2 4 12 3 (d) 5 6 2 4 12 5 14 16 6 2 4 12 3 (e) 3 (f) stages in Kruskal’s algorithm Networking Laboratory Chap. 6-42 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Minimum Cost Spanning Trees 0 0 10 1 10 14 16 6 2 5 4 12 22 3 (g) 1 14 16 6 2 5 25 4 12 22 3 (h) stages in Kruskal’s algorithm Networking Laboratory Chap. 6-43 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Minimum Cost Spanning Trees edge ----(0,5) (2,3) (1,6) (1,2) (3,6) (3,4) (4,6) (4,5) (0,1) weight ----10 12 14 16 18 22 24 25 28 result initial added to tree added added added discarded added discarded added not considered figure (b) (c) (d) (e) (f) (g) (h) summary of the Kruskal’s algorithm Networking Laboratory Chap. 6-44 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Minimum Cost Spanning Trees (2) Prim’s algorithm - begins with a tree, T, that contains a single vertex - add a least cost edge (u,v) to T such that T {(u,v)} is also a tree and repeat this step until T contains n-1 edges set of of the - form - form selected edges at each stage algorithm a tree in Prim’s alg. a forest in Kruskal’s alg. Networking Laboratory Chap. 6-45 Data Structures in C Copyright(c) 2000, Sungkyunkwan University Minimum Cost Spanning Trees Prim’s algorithm T = {}; TV = {0}; /* start with vertex 0 and no edge*/ while (T contains fewer than n-1 edges) { let (u,v) be a least cost edge such that u TV and v TV; if (there is no such edge) break; add v to TV; add (u,v) to T; } if (T contains fewer than n-1 edges) printf(“no spanning tree\n”); Networking Laboratory Chap. 6-46 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Minimum Cost Spanning Trees 0 0 10 1 5 6 10 2 3 (a) 1 5 25 4 0 6 10 2 5 25 4 3 1 6 2 4 22 (b) 3 (c) stages in Prim’s algorithm Networking Laboratory Chap. 6-47 Copyright(c) 2000, Sungkyunkwan University Data Structures in C Minimum Cost Spanning Trees 0 0 10 1 0 10 1 10 1 16 5 25 6 2 4 12 22 3 (d) 5 25 6 2 4 12 22 3 14 16 6 2 5 25 4 12 22 (e) 3 (f) stages in Prim’s algorithm Networking Laboratory Chap. 6-48