Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Size: px
Start display at page:

Download "Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees"

Transcription

1 Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo Copyright Networking Laboratory

2 Graphs Graph G = (V, E) V is set of vertices E is set (family) of edges (V V) edge or link or arc Types of graphs Undirected: edge (u, v) = (v, u) for all v, (v, v) E (No self loops) Directed: (u, v) is edge from u to v, denoted as u v self loops are allowed Weighted: each edge has an associated weight, given by a weight function w : E R Dense: E V 2 Sparse: E << V 2 E = O( V 2 ) Algorithms Networking Laboratory 2/122

3 Graphs If (u, v) E, then vertex v is adjacent to vertex u Adjacency relationship is: Symmetric if G is undirected Not necessarily so if G is directed If G is connected: There is a path between every pair of vertices E V 1 Furthermore, if E = V 1, then G is a tree Other definitions in Appendix B (B.4 and B.5) as needed On pages Algorithms Networking Laboratory 3/122

4 Eulerian and Hamiltonian Graphs A graph containing paths that include every edge exactly once and end at the initial vertex is called an Eulerian graph A graph containing paths that include every vertex exactly once and end at the initial vertex is called a Hamiltonian graph P Q R Hamiltonian but not Eulerian T S Algorithms Networking Laboratory 4/122

5 Trees and Planar Graphs A connected graph with only one path between each pair of vertices is called a tree A tree can also be defined as a connected graph containing no cycles (figure 1) A graph that can be redrawn without crossings is called a planar graph (figures 2 and 3) P Q P Q R R T S T S Fig. 1 Fig. 2 Fig. 3 Algorithms Networking Laboratory 5/122

6 Other Definitions Two digraphs are isomorphic if there is an isomorphism between their underlying graphs that preserves the ordering of the vertices in each arc See figures 4 and 5 : not isomorphic See figures 6 and 7 : isomorphic Fig. 4 Fig. 6 Fig. 5 Fig. 7 Algorithms Networking Laboratory 6/122

7 Practice Problems Which of the following pairs are isomorphic graphs: Algorithms Networking Laboratory 7/122

8 Connected and Strongly Connected A digraph D is connected if it cannot be expressed as the union of two digraphs This is equivalent to saying that the underlying graph of D is a connected graph D is strongly connected if, for any two vertices v and w of D, there is a path from v to w Every strongly connected digraph is connected, but not all connected digraphs are strongly connected See figure 4 Fig. 4 Algorithms Networking Laboratory 8/122

9 Connected and Strongly Connected We define a graph G to be orientable if each edge of G can be directed so that the resulting digraph is strongly connected See figures 8 and 9 Any Eulerian graph is orientable Fig. 8 Fig. 9 Algorithms Networking Laboratory 9/122

10 Adjacency Two vertices v and w are adjacent if there is an edge (v,w) joining them, and the vertices v and w are then incident with such an edge Two distinct edges e and f are adjacent if they have a vertex in common v w e f adjacent vertices adjacent edges Algorithms Networking Laboratory 10/122

11 Representation of Graphs Two standard ways Adjacency lists a b a b d c b a c c d c d d a a c b Adjacency matrix 1 2 a c b d Algorithms Networking Laboratory 11/122

12 Representation of Graphs Adjacency matrix A Incidence matrix M Algorithms Networking Laboratory 12/122

13 Algorithms Networking Laboratory 13/122 Adjacency Lists Consists of an array Adj of V lists One list per vertex For u V, Adj[u] consists of all vertices adjacent to u a d c b a b c d b c d d c a d c b a b c d b a d d c c a b a c If weighted, store weights also in adjacency lists

14 Degree The degree of a vertex v is the number of edges incident with v, and is written deg(v) A loop at v contributes 2 to the degree of v A vertex of degree 0 is an isolated vertex A vertex of degree 1 is an end-vertex Remember the handshaking lemma and its corollary u z v w i Algorithms Networking Laboratory 14/122

15 Handshaking Lemma Handshaking Lemma If several people shake hands, then the total number of hands shaken must be even In any graph the sum of all the vertex degrees is an even number Corollary in fact, twice the number of edges In any graph the number of vertices of odd degree is even Algorithms Networking Laboratory 15/122

16 Handshaking Dilemma The out-degree of a vertex v of G is the number of arcs of the form (v,w), and is denoted by outdeg(v) The in-degree of a vertex v of G is the number of arcs of the form (w,v), and is denoted by indeg(v) The sum of the out-degrees of all the vertices of G is equal to the sum of their in-degrees We call this result the handshaking dilemma A source of G is a vertex with in-degree 0 A sink of G is a vertex with out-degree 0 Algorithms Networking Laboratory 16/122

17 Storage Requirement For directed graphs Sum of lengths of all adj. lists is outdeg(v) = E v V Total storage: (V+E) For undirected graphs Sum of lengths of all adj. lists is deg(v) = 2 E v V Total storage: (V+E) Algorithms Networking Laboratory 17/122

18 Pros and Cons: Adj List Pros Space-efficient, when a graph is sparse Can be modified to support many graph variants Cons Determining if an edge (u,v) E is not efficient Have to search in u s adjacency list, (deg(u)) time (V) in the worst case Algorithms Networking Laboratory 18/122

19 Adjacency Matrix V V matrix A Number vertices from 1 to V in some arbitrary manner A is then given by: A[ i, j] a ij 1 0 if ( i, j) E otherwise 1 2 a c b d a c b d A = A T for undirected graphs Algorithms Networking Laboratory 19/122

20 Space and Time Space: (V 2 ) Not memory efficient for large graphs Time: to list all vertices adjacent to u (V) Time: to determine if (u, v) E (1) Can store weights instead of bits for weighted graph Algorithms Networking Laboratory 20/122

21 Practice Problems Draw the graph whose adjacency matrix is given as follows: Draw the graph whose incidence matrix is given as follows: Algorithms Networking Laboratory 21/122

22 Graph-Searching Algorithms Searching a graph Systematically follow the edges of a graph to visit the vertices of the graph Used to discover the structure of a graph Standard graph-searching algorithms Breadth-First Search (BFS) Depth-First Search (DFS) Algorithms Networking Laboratory 22/122

23 Breadth-First Search Input Graph G = (V, E), either directed or undirected, and source vertex s V Output d[v] = distance (smallest number of edges, or shortest path) from s to v, for all v V d[v] = if v is not reachable from s [v] = u such that (u, v) is last edge on shortest path s u is v s predecessor Builds Breadth-First Tree with root s that contains all reachable vertices v Algorithms Networking Laboratory 23/122

24 Breadth-First Search Expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier A vertex is discovered the first time it is encountered during the search A vertex is finished if all vertices adjacent to it have been discovered Colors the vertices to keep track of progress White Undiscovered Gray Discovered but not finished Black Finished Colors are required only to reason about the algorithm. Can be implemented without colors. Algorithms Networking Laboratory 24/122

25 Breadth-First Search S S S 3 3 Finished Discovered Undiscovered Algorithms Networking Laboratory 25/122

26 Pseudo Code BFS(G,s) 1. for each vertex u in V[G] {s} 2 do color[u] white 3 d[u] 4 [u] nil white: undiscovered gray: discovered black: finished 5 color[s] gray 6 d[s] 0 Q: a queue of discovered vertices 7 [s] nil color[v]: color of v 8 Q d[v]: distance from s to v 9 enqueue(q,s) [u]: predecessor of v 10 while Q 11 do u dequeue(q) 12 for each v in Adj[u] 13 do if color[v] = white 14 then color[v] gray 15 d[v] d[u] [v] u 17 enqueue(q,v) 18 color[u] black Algorithms Networking Laboratory 26/122

27 BFS Example r s t u 0 v w x y Q: s 0 Algorithms Networking Laboratory 27/122

28 BFS Example r s t u v w x y Q: w r 1 1 Algorithms Networking Laboratory 28/122

29 BFS Example r s t u v w x y Q: r t x Algorithms Networking Laboratory 29/122

30 BFS Example r s t u v w x y Q: t x v Algorithms Networking Laboratory 30/122

31 BFS Example r s t u v w x y Q: x v u Algorithms Networking Laboratory 31/122

32 BFS Example r s t u v w x y Q: v u y Algorithms Networking Laboratory 32/122

33 BFS Example r s t u v w x y Q: u y 3 3 Algorithms Networking Laboratory 33/122

34 BFS Example r s t u v w x y Q: y 3 Algorithms Networking Laboratory 34/122

35 BFS Example r s t u v w x y Q: Algorithms Networking Laboratory 35/122

36 BFS Example r s t u v w x y BF Tree Algorithms Networking Laboratory 36/122

37 Analysis of BFS Initialization takes O(V) Traversal Loop After initialization, each vertex is enqueued and dequeued at most once, and each operation takes O(1). So, total time for queuing is O(V) The adjacency list of each vertex is scanned at most once. The sum of lengths of all adjacency lists is (E) Summing up over all vertices total running time of BFS is O(V+E), linear in the size of the adjacency list representation of graph Algorithms Networking Laboratory 37/122

38 Breadth-First Trees For a graph G = (V, E) with source s, the predecessor subgraph of G is G = (V, E ) where V ={v V : [v] NIL} {s} E ={ ( [v],v) E : v V - {s} } The predecessor subgraph G is a breadth-first tree if: V consists of the vertices reachable from s and for all v V, there is a unique simple path from s to v in G that is also a shortest path from s to v in G. The edges in E are called tree edges E = V - 1 Algorithms Networking Laboratory 38/122

39 Depth-First Search Explore edges out of the most recently discovered vertex v When all edges of v have been explored, backtrack to explore other edges leaving the vertex from which v was discovered (its predecessor) Search as deep as possible first Continue until all vertices reachable from the original source are discovered If any undiscovered vertices remain, then one of them is chosen as a new source and search is repeated from that source Algorithms Networking Laboratory 39/122

40 Depth-First Search Input G = (V, E), directed or undirected. No source vertex given! Output 2 timestamps on each vertex. Integers between 1 and 2 V. d[v] = discovery time (v turns from white to gray) f [v] = finishing time (v turns from gray to black) [v] : predecessor of v = u, such that v was discovered during the scan of u s adjacency list. Uses the same coloring scheme for vertices as BFS White Undiscovered Gray Discovered but not finished Black Finished Algorithms Networking Laboratory 40/122

41 Pseudo Code DFS(G) 1. for each vertex u V[G] 2. do color[u] white 3. [u] NIL 4. time 0 5. for each vertex u V[G] 6. do if color[u] = white 7. then DFS-Visit(u) Uses a global timestamp time DFS-Visit(u) 1. color[u] GRAY White vertex u has been discovered 2. time time d[u] time 4. for each v Adj[u] 5. doif color[v] = WHITE 6. then [v] u 7. DFS-Visit(v) 8. color[u] BLACK Blacken u; it is finished. 9. f[u] time time + 1 white: undiscovered gray: discovered black: finished Algorithms Networking Laboratory 41/122

42 DFS Example 1/ u v w x y z Algorithms Networking Laboratory 42/122

43 DFS Example u v w 1/ 2/ x y z Algorithms Networking Laboratory 43/122

44 DFS Example u v w 1/ 2/ 3/ x y z Algorithms Networking Laboratory 44/122

45 DFS Example 1/ u v w 2/ 4/ 3/ x y z Algorithms Networking Laboratory 45/122

46 DFS Example u v w 1/ 2/ B 4/ 3/ x y z Algorithms Networking Laboratory 46/122

47 DFS Example u v w 1/ 2/ B 4/5 3/ x y z Algorithms Networking Laboratory 47/122

48 DFS Example u v w 1/ 2/ B 4/5 3/6 x y z Algorithms Networking Laboratory 48/122

49 DFS Example u v w 1/ 2/7 B 4/5 3/6 x y z Algorithms Networking Laboratory 49/122

50 DFS Example 1/ u v w 2/7 F B 4/5 3/6 x y z Algorithms Networking Laboratory 50/122

51 DFS Example u v w 1/8 2/7 F B 4/5 3/6 x y z Algorithms Networking Laboratory 51/122

52 DFS Example u v w 1/8 2/7 9/ F B 4/5 3/6 x y z Algorithms Networking Laboratory 52/122

53 DFS Example u v w 1/8 2/7 9/ F B C 4/5 3/6 x y z Algorithms Networking Laboratory 53/122

54 DFS Example u v w 1/8 2/7 9/ F B C 4/5 3/6 10/ x y z Algorithms Networking Laboratory 54/122

55 DFS Example u v w 1/8 2/7 9/ F B C 4/5 3/6 10/ x y z B Algorithms Networking Laboratory 55/122

56 DFS Example u v w 1/8 2/7 9/ F B C 4/5 3/6 10/11 x y z B Algorithms Networking Laboratory 56/122

57 DFS Example u v w 1/8 2/7 9/12 F B C 4/5 3/6 10/11 x y z B Algorithms Networking Laboratory 57/122

58 Analysis of DFS Loops on lines 1-2 & 5-7 take (V) time, excluding time to execute DFS-Visit DFS-Visit is called once for each white vertex v V when it s painted gray the first time. Lines 3-6 of DFS-Visit is executed Adj[v] times. The total cost of executing DFS-Visit is v V Adj[v] = (E) Total running time of DFS is (V+E) Algorithms Networking Laboratory 58/122

59 Depth-First Trees Predecessor subgraph defined slightly different from that of BFS The predecessor subgraph of DFS is G = (V, E ) where E ={( [v],v) : v V and [v] NIL}. How does it differ from that of BFS? The predecessor subgraph G forms a depth-first forest composed of several depth-first trees. The edges in E are called tree edges. Algorithms Networking Laboratory 59/122

60 Classification of Edges Tree edge: in the depth-first forest. Found by exploring (u, v) Back edge: (u, v), where u is a descendant of v (in the depth-first tree) Forward edge: (u, v), where v is a descendant of u, but not a tree edge Cross edge: any other edge Can go between vertices in same depth-first tree or in different depthfirst trees Algorithms Networking Laboratory 60/122

61 Practice Problems Perform a depth-first search on the following graph starting at A. Break all ties by picking the vertices in alphabetical order (i.e A before Z). Algorithms Networking Laboratory 61/122

62 Topological Sort A topological sort of a Directed Acyclic Graph (DAG) is a linear order of all its vertices s.t. if G contains an edg e (u, v), then u appears before v in the ordering If the graph is not acyclic, then no linear ordering is possible. A topological sort can be viewed as an ordering of its vertices along a horizontal line so that all directed edges go from left to right (V+E) Algorithms Networking Laboratory 62/122

63 Topological Sort Algorithms Networking Laboratory 63/122

64 Minimum Spanning Tree A town has a set of houses and a set of roads A road connects 2 and only 2 houses A road connecting houses u and v has a repair cost w(u, v) Goal: Repair enough (and no more) roads such that everyone stays connected: can reach every house from all other houses, and total repair cost is minimum Algorithms Networking Laboratory 64/122

65 Minimum Spanning Tree Model as a graph: Undirected graph G = (V, E) Weight w(u, v) on each edge (u, v) E Find T E such that T connects all vertices (T is a spanning tree) w(t ) = ( u, v) T w( u, v) is minimized Algorithms Networking Laboratory 65/122

66 Minimum Spanning Tree A spanning tree whose weight is minimum over all spanning trees is called a Minimum Spanning Tree, or MST Example: In this example, there is more than one MST Replace edge (b,c) by (a,h) Get a different spanning tree with the same weight Algorithms Networking Laboratory 66/122

67 Minimum Spanning Tree Which edges form the Minimum Spanning Tree (MST) of the below graph? 14 A H B C G E D 3 F 8 2 Algorithms Networking Laboratory 67/122

68 Minimum Spanning Tree MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees Let T be an MST of G with an edge (u,v) in the middle Removing (u,v) partitions T into two trees T 1 and T 2 Claim: T 1 is an MST of G 1 = (V 1,E 1 ), and T 2 is an MST of G 2 = (V 2,E 2 ) ( Do V 1 and V 2 share vertices? Why? ) Proof: w(t) = w(u,v) + w(t 1 ) + w(t 2 ) (There can t be a better tree than T 1 or T 2, or T would be suboptimal) Algorithms Networking Laboratory 68/122

69 Growing An MST Some properties of an MST: It has V -1 edges It has no cycles It might not be unique Building up the solution We will build a set A of edges Initially, A has no edges As we add edges to A, maintain a loop invariant: Loop invariant: A is a subset of some MST Add only edges that maintain the invariant If A is a subset of some MST, an edge (u, v) is safe for A if and only if A {(u, v)} is also a subset of some MST So we will add only safe edges Algorithms Networking Laboratory 69/122

70 Growing An MST Generic MST algorithm Use the loop invariant to show that this generic algorithm works Initialization The empty set trivially satisfies the loop invariant Maintenance Since we add only safe edges, A remains a subset of some MST Termination All edges added to A are in an MST, so when we stop, A is a spanning tree that is also an MST Algorithms Networking Laboratory 70/122

71 Growing An MST Finding a safe edge Intuitiveness: Let S V be any set of vertices that includes h but not g (so that g is in V - S). In any MST, there has to be one edge (at least) that connects S with V - S. Why not choose the edge with minimum weight? Some definitions: Let S V and A E A cut (S, V - S) is a partition of vertices into disjoint sets S and V - S Edge (u, v) E crosses cut (S, V - S) if one endpoint is in S and the other is in V - S A cut respects A if and only if no edge in A crosses the cut An edge is a light edge crossing a cut if and only if its weight is minimum over all edges crossing the cut Algorithms Networking Laboratory 71/122

72 Growing An MST cut, crosses, respects, light edge Algorithms Networking Laboratory 72/122

73 Growing An MST Theorem Let T be MST of G, and let A T be subtree of T Let (u,v) be min-weight edge connecting A to V-A Then (u,v) T Algorithms Networking Laboratory 73/122

74 Disjoint-Set Union Problem Want a data structure to support disjoint sets Collection of disjoint sets S = {S i }, S i S j = Need to support following operations: MakeSet(x): S = S U {{x}} Union(S i, S j ): S = S - {S i, S j } U {S i U S j } FindSet(x): return S i S such that x S i Before discussing implementation details, we look at example application: MSTs Algorithms Networking Laboratory 74/122

75 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 75/122

76 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 76/122

77 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Algorithms Networking Laboratory 77/122

78 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 78/122

79 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2? 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 79/122

80 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 80/122

81 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Algorithms Networking Laboratory 81/122

82 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 82/122

83 Kruskal s Algorithm Kruskal() { T = ; 8? for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 83/122

84 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 84/122

85 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? 1 Algorithms Networking Laboratory 85/122

86 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 86/122

87 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? 9 1 Algorithms Networking Laboratory 87/122

88 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 88/122

89 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 14? Algorithms Networking Laboratory 89/122

90 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 90/122

91 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 14 17? Algorithms Networking Laboratory 91/122

92 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19? sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 92/122

93 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: ? 13 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 9 1 Algorithms Networking Laboratory 93/122

94 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Algorithms Networking Laboratory 94/122

95 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: 2 19 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Algorithms Networking Laboratory 95/122

96 Kruskal s Algorithm Kruskal() { T = ; 8 for each v V MakeSet(v); Run the algorithm: sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 9 1 Algorithms Networking Laboratory 96/122

97 Kruskal s Algorithm Algorithms Networking Laboratory 97/122

98 Kruskal s Algorithm Algorithms Networking Laboratory 98/122

99 Kruskal s Algorithm Algorithms Networking Laboratory 99/122

100 Kruskal s Algorithm Kruskal() { } T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; What will affect the running time? 1 Sort O(V) MakeSet() calls O(E) FindSet() calls O(V) Union() calls (Exactly how many Union()s?) Union(FindSet(u), FindSet(v)); Algorithms Networking Laboratory 100/122

101 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; 14 key[v] = w(u,v); Run on example graph Algorithms Networking Laboratory 101/122

102 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Run on example graph Algorithms Networking Laboratory 102/122

103 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) r u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Pick a start vertex r 9 15 Algorithms Networking Laboratory 103/122

104 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Red vertices have been removed from Q Algorithms Networking Laboratory 104/122

105 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Blue arrows indicate parent pointers Algorithms Networking Laboratory 105/122

106 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Algorithms Networking Laboratory 106/122

107 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u Algorithms Networking Laboratory 107/122

108 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u Algorithms Networking Laboratory 108/122

109 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u Algorithms Networking Laboratory 109/122

110 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 9 15 Algorithms Networking Laboratory 110/122

111 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 9 15 Algorithms Networking Laboratory 111/122

112 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 9 15 Algorithms Networking Laboratory 112/122

113 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 9 15 Algorithms Networking Laboratory 113/122

114 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 9 15 Algorithms Networking Laboratory 114/122

115 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 9 15 Algorithms Networking Laboratory 115/122

116 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 9 15 Algorithms Networking Laboratory 116/122

117 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 9 15 Algorithms Networking Laboratory 117/122

118 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] u if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Algorithms Networking Laboratory 118/122

119 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u Algorithms Networking Laboratory 119/122

120 Prim s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u Algorithms Networking Laboratory 120/122

121 Prim s Algorithm Algorithms Networking Laboratory 121/122

122 Prim s Algorithm Algorithms Networking Laboratory 122/122

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V) Graph Algorithms Graphs Graph G = (V, E) V = set of vertices E = set of edges (V V) Types of graphs Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self loops.) Directed: (u, v) is edge from

More information

Elementary Graph Algorithms

Elementary Graph Algorithms Elementary Graph Algorithms Graphs Graph G = (V, E)» V = set of vertices» E = set of edges (V V) Types of graphs» Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self loops.)» Directed: (u, v)

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms CSE 5311 Lecture 18 Graph Algorithm Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Graphs Graph G = (V,

More information

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Next: Graph Algorithms Graphs Ch 22 Graph representations adjacency list adjacency matrix Minimum Spanning Trees Ch 23 Traversing graphs Breadth-First Search Depth-First Search 11/30/17 CSE 3101 1 Graphs

More information

Graph Representation

Graph Representation Graph Representation Adjacency list representation of G = (V, E) An array of V lists, one for each vertex in V Each list Adj[u] contains all the vertices v such that there is an edge between u and v Adj[u]

More information

Representations of Graphs

Representations of Graphs ELEMENTARY GRAPH ALGORITHMS -- CS-5321 Presentation -- I am Nishit Kapadia Representations of Graphs There are two standard ways: A collection of adjacency lists - they provide a compact way to represent

More information

Unit 2: Algorithmic Graph Theory

Unit 2: Algorithmic Graph Theory Unit 2: Algorithmic Graph Theory Course contents: Introduction to graph theory Basic graph algorithms Reading Chapter 3 Reference: Cormen, Leiserson, and Rivest, Introduction to Algorithms, 2 nd Ed., McGraw

More information

Chapter 22. Elementary Graph Algorithms

Chapter 22. Elementary Graph Algorithms Graph Algorithms - Spring 2011 Set 7. Lecturer: Huilan Chang Reference: (1) Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 2nd Edition, The MIT Press. (2) Lecture notes from C. Y. Chen

More information

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths Part VI Graph algorithms Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths 1 Chapter 22 Elementary Graph Algorithms Representations of graphs

More information

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2 Graphs Graph G = (V, E)» V = set of vertices» E = set of edges (V V) V) Types of graphs» Undirected:

More information

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Graph Algorithms: Chapters Part 1: Introductory graph concepts UMass Lowell Computer Science 91.503 Algorithms Dr. Haim Levkowitz Fall, 2007 Graph Algorithms: Chapters 22-25 Part 1: Introductory graph concepts 1 91.404 Graph Review Elementary Graph Algorithms Minimum

More information

Review: Graph Theory and Representation

Review: Graph Theory and Representation Review: Graph Theory and Representation Graph Algorithms Graphs and Theorems about Graphs Graph implementation Graph Algorithms Shortest paths Minimum spanning tree What can graphs model? Cost of wiring

More information

Graph representation

Graph representation Graph Algorithms 1 Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent for algorithms: 1. Adjacency lists. 2. Adjacency matrix. When expressing

More information

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree CS3 Lecture 0 Jana Kosecka Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree Many slides here are based on E. Demaine, D. Luebke, Kleinberg-Tardos slides Graph Algs.

More information

COT 6405 Introduction to Theory of Algorithms

COT 6405 Introduction to Theory of Algorithms COT 6405 Introduction to Theory of Algorithms Topic 14. Graph Algorithms 11/7/2016 1 Elementary Graph Algorithms How to represent a graph? Adjacency lists Adjacency matrix How to search a graph? Breadth-first

More information

DFS & STRONGLY CONNECTED COMPONENTS

DFS & STRONGLY CONNECTED COMPONENTS DFS & STRONGLY CONNECTED COMPONENTS CS 4407 Search Tree Breadth-First Search (BFS) Depth-First Search (DFS) Depth-First Search (DFS) u d[u]: when u is discovered f[u]: when searching adj of u is finished

More information

Graph: representation and traversal

Graph: representation and traversal Graph: representation and traversal CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang! Acknowledgement The set of slides have use materials from the following resources Slides for textbook

More information

Graph Search. Adnan Aziz

Graph Search. Adnan Aziz Graph Search Adnan Aziz Based on CLRS, Ch 22. Recall encountered graphs several weeks ago (CLRS B.4) restricted our attention to definitions, terminology, properties Now we ll see how to perform basic

More information

Introduction to Algorithms. Lecture 11

Introduction to Algorithms. Lecture 11 Introduction to Algorithms Lecture 11 Last Time Optimization Problems Greedy Algorithms Graph Representation & Algorithms Minimum Spanning Tree Prim s Algorithm Kruskal s Algorithm 2 Today s Topics Shortest

More information

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Graph G(V, E): V set of nodes (vertices); E set of edges. Notation: n = V and m = E. (Vertices are numbered

More information

Graph Algorithms. Definition

Graph Algorithms. Definition Graph Algorithms Many problems in CS can be modeled as graph problems. Algorithms for solving graph problems are fundamental to the field of algorithm design. Definition A graph G = (V, E) consists of

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Overview Problem A town has a set of houses and a set of roads. A road connects and only houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and

More information

Outlines: Graphs Part-2

Outlines: Graphs Part-2 Elementary Graph Algorithms PART-2 1 Outlines: Graphs Part-2 Graph Search Methods Breadth-First Search (BFS): BFS Algorithm BFS Example BFS Time Complexity Output of BFS: Shortest Path Breath-First Tree

More information

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS This chapter presents methods for representing a graph and for searching a graph. Searching a graph means systematically following the edges of the graph so as to

More information

CSI 604 Elementary Graph Algorithms

CSI 604 Elementary Graph Algorithms CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms s of s Computer Science & Engineering 423/823 Design and Analysis of Lecture 03 (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 29 s of s s are abstract data types that are applicable

More information

Basic Graph Algorithms

Basic Graph Algorithms Basic Graph Algorithms 1 Representations of Graphs There are two standard ways to represent a graph G(V, E) where V is the set of vertices and E is the set of edges. adjacency list representation adjacency

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 04 Elementary Graph Algorithms (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) sscott@cse.unl.edu Introduction

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees 1 Minimum- Spanning Trees 1. Concrete example: computer connection. Definition of a Minimum- Spanning Tree Concrete example Imagine: You wish to connect all the computers in an office

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and no

More information

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms CS43-09 Elementary Graph Algorithms Outline Representation of Graphs Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 1:30pm - 2:30pm or by appointments lifei@cs.gmu.edu with subject: CS43

More information

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms CS483-09 Elementary Graph Algorithms Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 1:30pm - 2:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/ lifei/teaching/cs483_fall07/

More information

Graph implementations :

Graph implementations : Graphs Graph implementations : The two standard ways of representing a graph G = (V, E) are adjacency-matrices and collections of adjacencylists. The adjacency-lists are ideal for sparse trees those where

More information

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u]. Tutorial Question 1 A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first tree can also be used to classify the edges reachable from the source

More information

Tree. number of vertices. Connected Graph. CSE 680 Prof. Roger Crawfis

Tree. number of vertices. Connected Graph. CSE 680 Prof. Roger Crawfis Tree Introduction to lgorithms Spanning Trees CSE Prof. Roger Crawfis We call an undirected graph a tree if the graph is connected and contains no cycles. Trees: Not Trees: Not connected Has a cycle Number

More information

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019 CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction

More information

Basic Graph Algorithms (CLRS B.4-B.5, )

Basic Graph Algorithms (CLRS B.4-B.5, ) Basic Graph Algorithms (CLRS B.-B.,.-.) Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices V and a finite set of edges E. Directed graphs: E is a set of ordered pairs of vertices

More information

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1 Undirected Graphs Terminology. Free Trees. Representations. Minimum Spanning Trees (algorithms: Prim, Kruskal). Graph Traversals (dfs, bfs). Articulation points & Biconnected Components. Graph Matching

More information

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS Taking Stock IE170: Algorithms in Systems Engineering: Lecture 16 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University February 28, 2007 Last Time The Wonderful World of This

More information

Lecture Notes for Chapter 23: Minimum Spanning Trees

Lecture Notes for Chapter 23: Minimum Spanning Trees Lecture Notes for Chapter 23: Minimum Spanning Trees Chapter 23 overview Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has

More information

Graph: representation and traversal

Graph: representation and traversal Graph: representation and traversal CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Acknowledgement The set of slides have use materials from the following resources Slides for textbook

More information

Minimum Spanning Trees My T. UF

Minimum Spanning Trees My T. UF Introduction to Algorithms Minimum Spanning Trees @ UF Problem Find a low cost network connecting a set of locations Any pair of locations are connected There is no cycle Some applications: Communication

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 9 Minimum Spanning Trees Thanks to the text authors who contributed to these slides Why Minimum Spanning Trees (MST)? Example 1 A

More information

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 Graphs A graph, G = (V, E), consists of two sets: V is a finite non-empty set of vertices. E is a set of pairs of vertices, called

More information

Minimum Spanning Trees

Minimum Spanning Trees Chapter 23 Minimum Spanning Trees Let G(V, E, ω) be a weighted connected graph. Find out another weighted connected graph T(V, E, ω), E E, such that T has the minimum weight among all such T s. An important

More information

Week 11: Minimum Spanning trees

Week 11: Minimum Spanning trees Week 11: Minimum Spanning trees Agenda: Minimum Spanning Trees Prim s Algorithm Reading: Textbook : 61-7 1 Week 11: Minimum Spanning trees Minimum spanning tree (MST) problem: Input: edge-weighted (simple,

More information

Partha Sarathi Manal

Partha Sarathi Manal MA 515: Introduction to Algorithms & MA5 : Design and Analysis of Algorithms [-0-0-6] Lecture 20 & 21 http://www.iitg.ernet.in/psm/indexing_ma5/y09/index.html Partha Sarathi Manal psm@iitg.ernet.in Dept.

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms 6.046J/18.401J LECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy choice Prim s greedy MST algorithm Prof. Charles

More information

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Greedy Algorithms. At each step in the algorithm, one of several choices can be made. Greedy Algorithms At each step in the algorithm, one of several choices can be made. Greedy Strategy: make the choice that is the best at the moment. After making a choice, we are left with one subproblem

More information

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship Graph Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance or cost) path Graph Theory Many problems are mapped

More information

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search 1 Graph Searching: Breadth-First Search Graph G =(V, E), directed or undirected with adjacency list repres. GOAL: Systematically explores

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms 6.046J/18.401J LECTURE 13 Graph algorithms Graph representation Minimum spanning trees Greedy algorithms Optimal substructure Greedy choice Prim s greedy MST algorithm Prof.

More information

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1 Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms

More information

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search Taking Stock IE170: Algorithms in Systems Engineering: Lecture 17 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University March 2, 2007 Last Time Depth-First Search This Time:

More information

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution Taking Stock IE170: Algorithms in Systems Engineering: Lecture 26 Jeff Linderoth Last Time Department of Industrial and Systems Engineering Lehigh University April 2, 2007 This Time Review! Jeff Linderoth

More information

Announcements Problem Set 5 is out (today)!

Announcements Problem Set 5 is out (today)! CSC263 Week 10 Announcements Problem Set is out (today)! Due Tuesday (Dec 1) Minimum Spanning Trees The Graph of interest today A connected undirected weighted graph G = (V, E) with weights w(e) for each

More information

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington Graphs CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington 1 Representation Adjacency matrix??adjacency lists?? Review Graphs (from CSE 2315)

More information

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya CS60020: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Graphs (review) Definition. A directed graph (digraph) G = (V, E) is an ordered pair consisting of a set V of vertices

More information

Week 12: Minimum Spanning trees and Shortest Paths

Week 12: Minimum Spanning trees and Shortest Paths Agenda: Week 12: Minimum Spanning trees and Shortest Paths Kruskal s Algorithm Single-source shortest paths Dijkstra s algorithm for non-negatively weighted case Reading: Textbook : 61-7, 80-87, 9-601

More information

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms. 1 2 Week 5 3 4 5 General remarks We finish, by analysing it. Then we consider the second main graph- algorithm, depth-first (). And we consider one application of, of graphs. Reading from CLRS for week

More information

CS 561, Lecture 9. Jared Saia University of New Mexico

CS 561, Lecture 9. Jared Saia University of New Mexico CS 561, Lecture 9 Jared Saia University of New Mexico Today s Outline Minimum Spanning Trees Safe Edge Theorem Kruskal and Prim s algorithms Graph Representation 1 Graph Definition A graph is a pair of

More information

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006 Trees and Graphs Basic Definitions Tree: Any connected, acyclic graph G = (V,E) E = V -1 n-ary Tree: Tree s/t all vertices of degree n+1 A root has degree n Binary Search Tree: A binary tree such that

More information

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department CS473-Algorithms I Lecture 3-A Graphs Graphs A directed graph (or digraph) G is a pair (V, E), where V is a finite set, and E is a binary relation on V The set V: Vertex set of G The set E: Edge set of

More information

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Practical Session No. 12 Graphs, BFS, DFS, Topological sort Practical Session No. 12 Graphs, BFS, DFS, Topological sort Graphs and BFS Graph G = (V, E) Graph Representations (V G ) v1 v n V(G) = V - Set of all vertices in G E(G) = E - Set of all edges (u,v) in

More information

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10 CS 220: Discrete Structures and their Applications graphs zybooks chapter 10 directed graphs A collection of vertices and directed edges What can this represent? undirected graphs A collection of vertices

More information

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge Taking Stock IE170: in Systems Engineering: Lecture 19 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University March 16, 2007 Last Time Minimum This Time More Strongly Connected

More information

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Net: Graph Algorithms Graphs Ch 22 Graph representations adjacenc list adjacenc matri Minimum Spanning Trees Ch 23 Traersing graphs Breadth-First Search Depth-First Search 7/16/2013 CSE 3101 1 Graphs Definition

More information

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting. Week 5 General remarks and 2 We consider the simplest graph- algorithm, breadth-first (). We apply to compute shortest paths. Then we consider the second main graph- algorithm, depth-first (). And we consider

More information

W4231: Analysis of Algorithms

W4231: Analysis of Algorithms W4231: Analysis of Algorithms 10/21/1999 Definitions for graphs Breadth First Search and Depth First Search Topological Sort. Graphs AgraphG is given by a set of vertices V and a set of edges E. Normally

More information

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem Minimum Spanning Trees (Forests) Given an undirected graph G=(V,E) with each edge e having a weight w(e) : Find a subgraph T of G of minimum total weight s.t. every pair of vertices connected in G are

More information

Data Structures and Algorithms. Werner Nutt

Data Structures and Algorithms. Werner Nutt Data Structures and Algorithms Werner Nutt nutt@inf.unibz.it http://www.inf.unibz/it/~nutt Part 9 Academic Year 2011-2012 1 Acknowledgements & Copyright Notice These slides are built on top of slides developed

More information

Minimum Spanning Trees and Prim s Algorithm

Minimum Spanning Trees and Prim s Algorithm Minimum Spanning Trees and Prim s Algorithm Version of October 3, 014 Version of October 3, 014 Minimum Spanning Trees and Prim s Algorithm 1 / 3 Outline Spanning trees and minimum spanning trees (MST).

More information

Data Structures and Algorithms. Chapter 7. Graphs

Data Structures and Algorithms. Chapter 7. Graphs 1 Data Structures and Algorithms Chapter 7 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting. General remarks Week 5 2 We finish, by analysing it. Then we consider the second main graph- algorithm, depth-first (). And we consider one application of, of graphs. Reading from CLRS for week 5 Chapter

More information

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s CSCE 750, Fall 2002 Notes 6 Page 1 10 Graph Algorithms (These notes follow the development in Cormen, Leiserson, and Rivest.) 10.1 Definitions ffl graph, directed graph (digraph), nodes, edges, subgraph

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms s of s Computer Science & Engineering 423/823 Design and Analysis of Lecture 03 (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 29 Spring 2010 s of s s are abstract data types that

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms, Lecture 1 /1/200 Introduction to Algorithms.04J/1.401J LECTURE 11 Graphs, MST, Greedy, Prim Graph representation Minimum spanning trees Greedy algorithms hallmarks. Greedy choice

More information

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u). Parenthesis property of DFS discovery and finishing times (Thm 23.6 of CLR): For any two nodes u,v of a directed graph, if d[u] < d[v] (i.e. u discovered before v), either f[v] < f[u] (i.e. visit time

More information

Unit 5F: Layout Compaction

Unit 5F: Layout Compaction Course contents Unit 5F: Layout Compaction Design rules Symbolic layout Constraint-graph compaction Readings: Chapter 6 Unit 5F 1 Design rules: restrictions on the mask patterns to increase the probability

More information

Unit 3: Layout Compaction

Unit 3: Layout Compaction Unit 3: Layout Compaction Course contents Design rules Symbolic layout Constraint-graph compaction Readings: Chapter 6 Unit 3 1 Design rules: restrictions on the mask patterns to increase the probability

More information

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search 6.082 Fall 2006 Shortest Path Routing, Slide 1 Routing in an

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

Homework Assignment #3 Graph

Homework Assignment #3 Graph CISC 4080 Computer Algorithms Spring, 2019 Homework Assignment #3 Graph Some of the problems are adapted from problems in the book Introduction to Algorithms by Cormen, Leiserson and Rivest, and some are

More information

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl Graph Theory ICT Theory Excerpt from various sources by Robert Pergl What can graphs model? Cost of wiring electronic components together. Shortest route between two cities. Finding the shortest distance

More information

Solutions to relevant spring 2000 exam problems

Solutions to relevant spring 2000 exam problems Problem 2, exam Here s Prim s algorithm, modified slightly to use C syntax. MSTPrim (G, w, r): Q = V[G]; for (each u Q) { key[u] = ; key[r] = 0; π[r] = 0; while (Q not empty) { u = ExtractMin (Q); for

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

Outline. Graphs. Divide and Conquer.

Outline. Graphs. Divide and Conquer. GRAPHS COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges books. Outline Graphs.

More information

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides Jana Kosecka Red-Black Trees Graph Algorithms Many slides here are based on E. Demaine, D. Luebke slides Binary Search Trees (BSTs) are an important data structure for dynamic sets In addition to satellite

More information

Topic 12: Elementary Graph Algorithms

Topic 12: Elementary Graph Algorithms Topic 12: Elementary Graph Algorithms Pierre Flener Lars-enrik Eriksson (version of 2013-02-10) Different kinds of graphs List Tree A B C A B C Directed Acyclic Graph (DAG) D A B C D E Directed Graph (Digraph)

More information

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch] Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] 1 Directed Graphs Let V be a finite set and E a binary relation on V, that is, E VxV. Then the pair G=(V,E) is called a directed graph.

More information

22.1 Representations of graphs

22.1 Representations of graphs 22.1 Representations of graphs There are two standard ways to represent a (directed or undirected) graph G = (V,E), where V is the set of vertices (or nodes) and E is the set of edges (or links). Adjacency

More information

Elementary Graph Algorithms

Elementary Graph Algorithms Elementary Graph Algorithms Representations Breadth-First Search Depth-First Search Topological Sort Strongly Connected Components CS 5633 Analysis of Algorithms Chapter 22: Slide 1 Graph Representations

More information

Sample Solutions to Homework #4

Sample Solutions to Homework #4 National Taiwan University Handout #25 Department of Electrical Engineering January 02, 207 Algorithms, Fall 206 TA: Zhi-Wen Lin and Yen-Chun Liu Sample Solutions to Homework #4. (0) (a) Both of the answers

More information

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search IS 320 Introduction to lgorithms all 2014 Lecture 15 epth irst Search 1 Traversing raphs Systematic search of every edge and vertex of graph (directed or undirected) fficiency: each edge is visited no

More information

Suggested Study Strategy

Suggested Study Strategy Final Exam Thursday, 7 August 2014,19:00 22:00 Closed Book Will cover whole course, with emphasis on material after midterm (hash tables, binary search trees, sorting, graphs) Suggested Study Strategy

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 4 Graphs Definitions Traversals Adam Smith 9/8/10 Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a

More information

2 A Template for Minimum Spanning Tree Algorithms

2 A Template for Minimum Spanning Tree Algorithms CS, Lecture 5 Minimum Spanning Trees Scribe: Logan Short (05), William Chen (0), Mary Wootters (0) Date: May, 0 Introduction Today we will continue our discussion of greedy algorithms, specifically in

More information

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Practical Session No. 12 Graphs, BFS, DFS Topological Sort Practical Session No. 12 Graphs, BFS, DFS Topological Sort Graphs and BFS Graph G = (V, E) Graph Representations (VG ) v1 v n V(G) = V - Set of all vertices in G E(G) = E - Set of all edges (u,v) in G,

More information

Announcements. HW3 is graded. Average is 81%

Announcements. HW3 is graded. Average is 81% CSC263 Week 9 Announcements HW3 is graded. Average is 81% Announcements Problem Set 4 is due this Tuesday! Due Tuesday (Nov 17) Recap The Graph ADT definition and data structures BFS gives us single-source

More information

Minimum Spanning Tree

Minimum Spanning Tree Minimum Spanning Tree 1 Minimum Spanning Tree G=(V,E) is an undirected graph, where V is a set of nodes and E is a set of possible interconnections between pairs of nodes. For each edge (u,v) in E, we

More information