Chap : Graphs (Page ) Chapter. GRAPHS. THE GRAPH ABSTRACT DATA TYPE. ELEMENTARY GRAPH OPERATIONS. MINIMUM COST SPANNING TREES. SHORTEST PATHS AND TRANSITIVE CLOSURE. ACTIVITY NETWORKS Chap : Graphs (Page ) take bridge g to C take bridge d to A take bridge b to B take bridge f to D c C d g C c d e g A Kneiphof e D A D a b f B B f a b (a) (b) Figure. : The bridges of Koenigsberg
Chap : Graphs (Page ). The Graph Abstract Data Type A graph, G, consists of two sets: a finite, nonempty set of vertices, and a finite, possibly empty set of edges. V(G) and E(G) represent the sets of vertices and edges of G, respectively. Alternately, we may write G = (V, E) to represent a graph. Undirected graph is one in which the pair of vertices representing any edge is unordered. (v, v ), (v, v ) represent the same edge. Directed graph is one in which we represent each edge as a directed pair of vertices. <v, v > represents an edge in which v is the tail and v is the head. Chap : Graphs (Page ) Sample Graphs G G G V(G ) = {,,, } E(G ) = {(,), (,), (,), (,), (,), (,)} V(G ) = {,,,,,, } E(G ) = {(,), (,), (,), (,), (,), (,)} V(G ) = {,, } E(G ) = {<,>, <,>, <,>}
Chap : Graphs (Page ) Restriction on Graphs. A graph may not have an edge from a vertex, i, back to itself. self loops.. A graph may not have multiple occurrences of the same edge. if we allow, multigraph. Chap : Graphs (Page ) Definitions complete graph graph that has the maximum number of edges. adjacent, incident if (v, v ) is an edge in an undirected graph, then the vertices v and v are adjacent and the edge (v, v ) is incident on vertices v and v <v, v >: v is adjacent to v, while v is adjacent from v subgraph (Figure.) a subgraph of G is a graph G such that V(G ) Í V(G) and E(G ) Í E(G) path a path from vertex v p to vertex v q in a graph, G, is a sequence of vertices, v p, v i, v i,..., v in, v q such that (v p, v i ), (v i, vi ),..., (v in, v q ) are edges in an undirected graph.
Chap : Graphs (Page ) The length of a path is the number of edges on it. Simple (directed) path : path in which all vertices, except possibly the first and the last, are distinct. Cycle : simple path in which the first and the last vertices are the same. (Strongly) Connected In an undirected graph G, two vertices, v and v, are connected if there is a path in G from v to v. (Strongly) Connected Component : maximal connected subgraph Tree : graph that is connected and acyclic (it has no cycles). The degree of a vertex is the number of edges incident to the vertex. in-degree & out-degree digraph Chap : Graphs (Page 8) Graph Representations Adjacency Matrix The adjacency matrix of G is a two-dimensional n n array, say adj_mat. If the edge (v i, v j ) (<v i, v j > for a digraph) is in E(G), adj_mat[i][j] =. If there is no edge in E(G), adj_mat[i][j] =. ex) Undirected Graph: Symmetric Matrix
Chap : Graphs (Page 9) Adjacency Lists Replace the n rows of the adjacency matrix with n linked lists, one for each vertex in G. ex) headnodes vertex link null null null null null null null Chap : Graphs (Page ) Adjacency Multilists maintaining the lists as multilists, that is, lists in which nodes are shared among several lists, facilitates this operation. marked vertex vertex path path ex) headnodes The lists are: vertex : N -> N -> N vertex : N -> N -> N vertex : N -> N -> N vertex : N -> N -> N N N N N N N N null N N N N N null N N null null edge(, ) edge(, ) edge(, ) edge(, ) edge(, ) edge(, )
Chap : Graphs (Page ) Analysis of Graph Representations How many edges are there in G? or Is G connected? Adjacency Matrix: O(n ) time to search (n -n)/ Adjacency List: O(e+n) Good if e << n / (sparse graphs) Finding in-degree of a vertex in a digraph Adjacency Matrx: O(n) Adjacency List: O(e+n) maintaining two lists (original adjacency list & inverse adjacency list) using alternate node structure for adjacency list (Figure.) tail head column link for head row link for tail Chap : Graphs (Page ). Elementary Graph Operations. Depth First Search. Breadth First Search. Connected Components. Spanning Trees. Biconnected Components and Articulation Points
Chap : Graphs (Page ) Depth First Search Algorithm. begin the search by visiting the start vertex, v.. select an unvisited vertex, w, from v s adjacency list and carry out a depth first search on w. (using recursion) #define FALSE #define TRUE short int visited[max_vertices]; void dfs(int v) { node_pointer w; visited[v] = TRUE; printf( d, v); for (w = graph[v]; w; w = w->link) if (!visited[w->vertex]) dfs(w->vertex); } Chap : Graphs (Page ) ex) V V V V V V V V null null null null null null null null depth first search order : v, v, v, v, v, v, v, v
Chap : Graphs (Page ) Breadth First Search Algorithm. starts at vertex v and marks it as visited.. visits each of the vertices on v s adjacency list.. when we have visited all the vertices on v s adjacency list, we visit all the unvisited vertices that are adjacent to the first vertex on v s adjacency list (using queue). typedef struct queue *queue_pointer; typedef struct queue { int vertex; queue_pointer link; }; void addq(queue_pointer *, queue_pointer *, int); int deleteq(queue_pointer *); Chap : Graphs (Page ) void bfs(int v) { /* breadth first traversal of a graph, starting with node v the global array visited is initialized to, the queue */ node_pointer w; queue_pointer front =, rear = ; printf( %d, 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( %d, w->vertex); addq(&front,&rear,w->vertex); visited[w->vertex] = TRUE; } } }
Chap : Graphs (Page ) Connected Components whether or not an undirected graph is connected? calling either dfs() or bfs() and then determining if there are any unvisited vertices. listing the connected components of a graph. void connected (void) { /* determine the connected components of a graph */ int i for (i = ; i < n; i++) if (!visited[i]) { dfs(i); printf( n ); } } Chap : Graphs (Page 8) Spanning Trees A spanning tree is any tree that consists solely of edges in G and that includes all vertices in G. use either dfs or bfs to create a spanning tree. depth first spanning tree breadth first spanning tree V V V V V V V V V V V V V V V depth first spanning tree V breadth first spanning tree
Chap : Graphs (Page 9) Biconnected Components and Articulation Points Definition An articulation point is a vertex v of G such that the deletion of v, together with all edges incident on v, produces a graph, G that has at least two connected components. A biconnected graph is a connected graph that has no articulation points. A biconnected component of a connected undirected graph is a maximal biconnected subgraph, H, of G. The maximal biconnected subgraph means that G contains no other subgraph that is both biconnected and properly contains H. Chap : Graphs (Page ) ex) 8 9 8 9 (a) Connected graph (b) Biconnected components Articulation point :,,,
Chap : Graphs (Page ) Finding Articulation Point Two Rules Root of a DFS tree is an articulation point iff it has at least two children. Any other vertex u is an articulation point iff it has at least one child w such that we cannot reach an ancestor of u using a path that consists of only w, descendants of w, and a single back edge 9 8 9 8 (a) depth first spanning tree (b) 8 9 8 9 Chap : Graphs (Page ) Minimum Cost Spanning Trees Definition cost of a spanning tree of a weighted undirected graph : the sum of the costs (weights) of the edges in the spanning tree Minimum cost spanning tree : a spanning tree of least cost Three algorithms for minimum cost spanning tree Kruskal s, Prim s, Sollin s algorithms All three use an greedy method. Constraints for minimum cost spanning tree algorithms. must use only edges within the graph.. must use exactly n- edges.. may not use edges that would produce a cycle.
Chap : Graphs (Page ) Kruskal s Algorithm Methods Builds a minimum cost tree T by adding edges to T one at a time. Selects the edges for inclusion in T in non-decreasing order of their cost. An edge is added in T if it does not form a cycle. Graph G is connected and has n > vertices, exactly n - edges will be selected Chap : Graphs (Page ) Kruskal s Algorithm (Cont d) T = {}; while ( T contains less than n- edges && E is not empty ) { choose a least cost edge (v,w) form E; delete (v,w) form 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- edges ) printf( No spanning tree\n );
Chap : Graphs (Page ) (Ex) Kruskal s Algorithm 8 8 (a) (b) (c) (d) (e) (f) Chap : Graphs (Page ) Discussion of Kruskal s Algorithm determine an edge with minimum cost edge maintain the edges in E as a sorted sequential : O(e log e) better method : use min heap finding the next cost edge : O(log e) construction of heap : O(e) Cycle checking use union-find operation Analysis union-find operations require less than choosing and deleting an edge, the latter operations determine the total computing time Total Computing Time : O(e log e)
Chap : Graphs (Page ) Theorem.: Let G be an undirected connected graph. Kruskal s algorithm generates a minimum cost spanning tree. proof) We shall show that a) Kruskal s method produces a spanning tree whenever a spanning tree exists. b) The spanning tree generated is of minimum cost. Chap : Graphs (Page 8) Prim s Algorithm Method: Complexity = O(n ) Constructs the minimum cost spanning tree one edge at a time. At each stage, the set of selected edges forms a tree. 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.
Chap : Graphs (Page 9) Prim s Algorithm (Cont d) T = {}; TV = {}; /* start with vertex and no edges */ while ( T contains fewer than n- 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- edges ) printf( No spanning tree\n ); Chap : Graphs (Page ) (Ex) Prim s Algorithm 8 8 (a) (b) (c) (d) (e) (f)
Chap : Graphs (Page ) Sollin s Algorithm Methods Aelects several edges for inclusion in T at each stage. At the start of the stage, the selected edges, together with all n graph vertices, form a spanning forest. During a stage, select one edge for each tree in the forest. Since two trees in the forest could select the same edge, need to eliminate multiple copies of edges. At start of the first stage the set of selected edges is empty. Terminates when there is only tree at the end of a stage or no edges remain for selection. Chap : Graphs (Page ) (Ex) Sollin s Algorithm 8 8 (a) (b) (c) (d)
Chap : Graphs (Page ). Shortest paths and Transitive closure Problem. Is there a path from A to B?. If there is more than one path from A to B, which path is the shortest? Assume The length of a path is the sum of the weights of the edges on that path rather than the number of edges on the path. Since one-way streets are possible, the graphs are directed. Assume that all weights are positive. Chap : Graphs (Page ) Single Source All Destinations Problem ex) a directed graph, G = (V, E), a weighting function, w(e), w(e) >, for the edges of G, and a source vertex, v. Determine a shortest path from v to each of the remaining vertices of G. V V V V V V path length v v v v v v v v v v v
Chap : Graphs (Page ) Greedy algorithm Notation S : the set of vertices, including v, whose shortest paths have been found. For w not in S, let distance[w] be the length of the shortest path starting from v, going through vertices only in S, and ending in w. Observations. If the next shortest path is to vertex u, then the path from v to u goes through only those vertices that are in S.. Vertex u is chosen so that it has the minimum distance, distance[u], among all the vertices not in S. (from the definition of distance and observation.). Once we have selected u and generated the shortest path from v to u, u becomes a member of S. Adding u to S can change the distance[w](w S). If the distance changes, the length of the shorter path is distance[u] + length(<u,w>). Chap : Graphs (Page ) Dijkstra s algorithm Implement Dijkstra s algorithm Assume that the n vertices are numbered from to n- Maintain the set S as an array, found, with found[i] = FALSE if vertex i is not in S and found[i] = TRUE if vertex i is in S. Represent the graph by its cost adjacency matrix, with cost[i][j] being the weight of edge <i, j> If the edge <i, j> is not in G, we set cost[i][j] to some large number the number must be larger than any of the values in the cost matrix. the number must be chosen so that the statement distance[u] + cost[u][w] does not produce an overflow into the sign bit.
Chap : Graphs (Page ) #define MAX_VERTICES int cost[][max_vertices] = {{,,,,, }, {,,,,, }, {,,,,, }, {,,,,, }, {,,,,, }, {,,,,, }}; int distance[max_vertices], n = MAX_VERTICES; short int found[max_vertices]; int choose(int distance[], int n, short int found[]) { /* find the smallest distance not yet checked */ int i, min, minpos; min = INT_MAX; minpos = -; for (i = ; i < n; i++) if (distance[i] < min &&!found[i]) { min = distance[i]; minpos = i; } return minpos; } Chap : Graphs (Page 8) void shortestpath(int v, int cost[][max_vertices], int distance[], int n, short int found[]) { /* Implement Dijkstra s Algorithm */ int i, u, w; for (i = ; i < n; i++) { found[i] = FALSE; distance[i] = cost[v][i]; } Complexity = found[v] = TRUE; distance[v] = ; for (i = ; i < n - ; i++) { u = choose(distance, n, found); found[u] = TRUE; for (w = ; w < n; w++) if (!found[w]) if (distance[u] + cost[u][w] < distance[w]) distance[w] = distance[u] + cost[u][w]; } }
Chap : Graphs (Page 9) San Frnacisco Los Angeles 8 Denver Boston Chicago NewYork 9 (a) Digraph of hypothetical airline routes New Orleans Miami 8 9 Figure. : Digraph of airline routes (b) Cost adjacency matrix Chap : Graphs (Page ) Distance Iteration S Vertax selected LA [] SF [] DEN [] CHI [] BOST [] NY [] MIA [] NO [] Initial -- ---- + + + + + { } + + + {,} + + + {,,} + + {,,,} + {,,,,} {,,,,,} {,,,,,,} Figure. : Action of shortestpath on the digraph of Figure.
Chap : Graphs (Page ) All Pairs Shortest Paths Problem find the shortest paths between all pairs of vertices, v i, v j, i j. Solution. using shortestpath with each of the vertices in V(G) as the source : O(n ).. Dynamic programming method : O(n ) with a smaller constant factor. Chap : Graphs (Page ) Dynamic programming algorithm A k [i][j] : cost of the shortest path from i to j, using only those intermediate vertices with an index k. cost of the shortest path from i to j is A n- [i][j]. A - [i][j] = cost[i][j]. successively generate the matrices A, A, A,..., A n-. Calculate A k with A k-. the shortest path does not go through vertex k : A k [i][j] = A k- [i][j].. the shortest path does go through vertex k : A k [i][j] = A k- [i][k] + A k- [k][j]. A k [i][j] = min {A k- [i][j], A k [i][j] = A k- [i][k] + A k- [k][j] }, k. A - [i][j] = cost[i][j].
Chap : Graphs (Page ) void allcosts (int cost[ ] [MAX_VERTICES], int distance[] [MAX_VERTICES], int n) { /* determine the distance from each vertex to every other vertex, cost is the adjacency matrix, distance is the matrix of distances */ int i, j, k; for (i = ; i < n; i++) for (j = ; j < n; j++) distance[i][j] = cost[i][j]; for (k = ; k < n; k++) for (i = ; i < n; i++) for (j = ; j < n; j++) if (distance[i][k] + distance[k][j] < distance[i][j]) distance[i][j] = distance[i][k] + distance[k][j]; } Program. : All pairs, shortest paths function Chap : Graphs (Page ) A - A V V V A A Figure. : Matrix A k produced by allcosts for Figure.(a)
Chap : Graphs (Page ) Transitive Closure transitive closure matrix Denoted A +, of a directed graph, G, is a matrix such A + [i][j] = if there is a path of length > from i to j; otherwise, A + [i][j] =. reflexive transitive closure matrix Denoted A *, of a directed graph, G, is a matrix such that A * [i][j] = if there is a path of length from i to j; otherwise, A * [i][j] =. Program: Change allcosts distance[i][j] = distance[i][j] distance[i][k] && distance[k][j] Chap : Graphs (Page ) ex) transitive closure Digraph G Adjacent matrix A for G A + A *
Chap : Graphs (Page ). Activity Networks Activity Subproject (divide and conquer) The entire project is successfully completed when each of the activities is completed Two Networks Activity on Vertex (AOV) Networks Activity on Edge (AOE) Networks Chap : Graphs (Page 8) AOV Network Definitions: AOV Network: Digraph G (vertex = activities, edge = precedence relation) Predecessor (Successor): direct path from i to j Immediate Predecessor (Successor): <i, j> Partial Order: precedence relation that is both transitive and irreflexive Topological Order: linear ordering of vertices with precedence relation Problems How can we find out an algorithm that sorts the tasks into topological order? Topological Sort
Chap : Graphs (Page 9) Course number C C C C C C C C8 C9 C C C C C C Course name Programming I Discrete Mathematics Data Structures Calculus I Calculus II Linear Algebra Analysis of Algorithms Assembly Languages Operating Systems Programming Languages Compiler Design Artificial Intelligence Computational Theory Parallel Algorithms Numerical Analysis Prerequisites None None C, C None C C C, C C C, C8 C C C C C C (a) Courses needed for a computer science degree at a hypothetical university C C C C C C8 C C C C9 C C C (b) AOV network representing courses as vertices and edges as prerequisites Figure.8 : An AOV network C C Chap : Graphs (Page ) for ( i = ; i < n; i++) { if every vertex has a predecessor { fprintf (stderr, Network has a cycle.\n ); exit (); } pick a vertex v that has no predecessors; output v; delete v and all edges leading out of v from the network; } Program. : Topological sort
Chap : Graphs (Page ) V V V V V V V V V V V V V V V (a) initial (b) V (c) V V V V V V V (d) V (e) V (f) V (g) V Topological order generated : V, V, V, V, V, V Figure.9 : Simulation of Program. on an AOV network Chap : Graphs (Page ) Representation of AOV Network Determine if a vertex has any predecessors Keep a count of # of immediate predecessors for each vertex Delete a vertex and all of its incident edges Represent the network by its adjacency lists Declarations typedef struct node *node_pointer; typedef struct node { int vertex; node_pointer link; }; typedef struct { int count; node_pointer link; } hdnodes; hdnode graph[max_vertices]; v v v v v v count link Null Null Null Null Null Null
Chap : Graphs (Page ) void topsort(hdnodes graph[], int n) { int i, j, k, top; node_pointer ptr; /* PredecessorÈ Ž\ vertex stack ù */ top = -; for (i = ; i < n; i++) if (!graph[i].count) { graph[i].count = top; top = i; } for (i = ; i < n; i++) if (top == -) { fprintf(stderr, Network has a cycle.\n ); exit(); } else { j = top; /* Stack ä ä8 */ top = graph[top].count; printf( v%d,, j); for (ptr = graph[j].link; ptr; ptr = ptr->link) { /* Successor vertex count ØT */ k = ptr->vertex; graph[k].count--; if (!graph[k].count) { /* $ vertexd stack M */ graph[k].count = top; top = k; } } } } Chap : Graphs (Page ) Activity on Edge (AOE) Network Representation Edge: Activity Vertex: Event occurs when all incoming activities have been completed start v a = a = a = v v v a = v a = a = v a = 9 a = v v a 8 = a 9 = v 8 a = finish event v v v v v8 interpretation start of project completion of activity a completion of activity a and a completion of activity a and a8 completion of project (b) Interpretation of some of the events in the activity graph of (a) (a) AOE Network. Activity graph of a hypothetical project
Chap : Graphs (Page ) Critical Path of AOE Network Critical Path A path that has the longest length from start vertex to finish vertex Result in bottlenecks Earliest Time of an event v i Length of the longest path from start vertex v to v i (earliest() = ) earliest(v i ) Earliest start time for all outgoing activities of v i (early() = ) Latest Time, late(i), of activity a i Latest time the activity may start without increasing the project duration early() = & late() = 8, early() = & late() = Critical Activity Activity for which early(i) = late(i) Chap : Graphs (Page ) Calculation of Earliest Times Suppose activity a i is represented by edge <k, l> early(i) = earliest[k] late(i) = latest[l] - duration of activity a i Computing earliest[j] forward scan the network using topsort algorithm earliest[] = earliest[j] = max{earliest[i] + duration of <i, j>} for i P(j), where P(j) is the set of immediate predecessors of j Insert the following statement at the end of the else clause in topsort if (earliest[k] < earliest[j] + ptr->duration) earliest[k] = earliest[j] + ptr->duration
Chap : Graphs (Page ) count link vertex dur link V V V V V 9 V V 8 V 8 V 8 (a) Adjacency lists for Figure.(a) Chap : Graphs (Page 8) Earliest [] [] [] [] [] [] [] [] [8] Stack initial [] output V [,, ] output V [,, ] output V [, ] output V [] output V [] output V [, ] output V [] output V 8 [8] output V 8 (b) Computation of earliest Figure. : Computing earliest from topological sort
Chap : Graphs (Page 9) Calculation of Latest Times Computing latest[j] backward scan the network using topsort algorithm latest[n-] = earliest[n-] latest[j] = min{latest[i] - duration of <j, i>} for i S(j), where S(j) is the set of immediate successors of j Using inverse adjacence list Insert the following statement at the end of the else clause in topsort if (latest[k]> latest[j] - ptr->duration) latest[k] = latest[j] - ptr->duration Chap : Graphs (Page ) count link vertex dur link V V V V V V V 9 V V 8 (a) Inverted adjacency lists for AOE network of Figure.(a)
Chap : Graphs (Page ) Earliest [] [] [] [] [] [] [] [] [8] Stack initial 8 8 8 8 8 8 8 8 8 [8] output V 8 8 8 8 8 8 8 8 [, ] output V 8 8 8 8 8 [, ] output V 8 8 8 8 8 [, ] output V 8 8 8 8 [] output V 8 8 8 8 [] output V 8 8 [, ] output V 8 8 [] output V 8 8 [] (b) Computation of latest Figure. : Computing latest for AOE network of Figure.(a) Chap : Graphs (Page ) Calculation of Early(i) & Late(i) of a i Suppose activity a i is represented by edge <k, l> early(i) = earliest[k] late(i) = latest[l] - duration of activity a i Activity Early Late Late - Early Critical a a a a a a a a a 8 a 9 a 8 Yes No No Yes No No Yes Yes No Yes Yes