Introdction to lgorithms oncrete example Imagine: Yo wish to connect all the compters in an office bilding sing the least amont of cable - ach vertex in a graph G represents a compter - ach edge represents the amont of cable needed to connect all compters hapter : Minimm Spanning Trees Spanning Tree spanning tree of G is a sbgraph which is tree (acyclic) and connect all the vertices in V. Minimm Spanning Tree Inpt: Undirected connected graph G = (V, ) and weight fnction w : R, Otpt: Minimm i spanning tree T : tree that t connects all the vertices and minimizes wt ( ) = wv (, ) ( v, ) T Spanning tree has only V - edges. Greedy lgorithms Krskal s algorithm Prim s algorithm
xample: MST xample Krskal's s lgorithm dge based algorithm Greedy strategy: rom the remaining edges, select a least-cost edge that does not reslt in a cycle when added to the set of already selected edges Repeat V - times Krskal's s lgorithm INPUT: edge-weighted graph G = (V, ), with V = n OUTPUT: a spanning tree of G toches all vertices, has n- edges of minimm i cost ( = total t edge weight) lgorithm: Start with empty, dd the edges one at a time, in increasing weight order n edge is accepted if it connects vertices of distinct trees (if the edge does not form a cycle in ) ntil contains n- edges
Krskal's s lgorithm MST-Krskal(G,w) for each vertex v V[G] do Make-Set(v) //creates set containing v (for initialization) sort the edges of for each (,v) do if ind-set() ) ind-set(v) ) then // different component 7 {(,v)} Union(Set(),Set(v)) // merge retrn Data Strctres or Krskal s lgorithm Does the addition of an edge (, v) to T reslt in a cycle? ach component of T is a tree. When and v are in the same component, the addition of the edge (, v) creates a cycle. 7 different components, the addition of the edge (, v) does not create a cycle. 7 Data Strctres or Krskal s lgorithm ach component of T is defined by the vertices in the component. Represent each component as a set of vertices. {,,, }, {, }, {7, } Two vertices are in the same component iff they are in the same set of vertices. 7 7 Data Strctres or Krskal s lgorithm When an edge (, v) is added to T, the two components that have vertices and v combine to become a single component In or set representation of components, the set that has vertex and the set that has vertex v are nited. {,,, } + {, } {,,,,, } 7 7
Krskal s s lgorithm Krskal s s lgorithm????? Krskal s s lgorithm Krskal s s lgorithm??????
D D D D
D D cycle!! D D
Krskal's s lgorithm minimm- spanning tree D MST-Krskal(G,w) for each vertex v V[G] do // takes O(V) Make-Set(v) ( ) sort the edges of // takes O( lg ) // takes O() for each (,v), in nondecreasing of weight do if ind-set() ind-set(v) then 7 {(,v)} Union(Set(),Set(v)) Set(v)) retrn Rnning Time of Krskal ss lgorithm Krskal s lgorithm consists of two stages. Initializing the set in line takes O() time. Sorting the edges by weight in line. takes O( lg ) Performing V MakeSet() operations for loop in lines -. indset(), for loop in lines -. V - Union(), for loop in lines -. which takes O(V + ) The total rnning time is O( lg ) We have lg = O(lg V) becase # of = V- So total rnning time becomes O( lg V). Prim s lgorithm The tree starts from an arbitrary root vertex r and grows ntil the tree spans all the vertices in V. t each step, dds only edges that are safe for. When algorithm terminates, edges in form MST. Vertex based algorithm. Grows one tree T, one vertex at a time
Prim s lgorithm MST-Prim(G,w,r) //G: graph with weight w and a root vertex r for each V[G]{ key[] p[] NULL // parent of } key[r] 0 Q = ildminheap(v,key); // Q vertices ot of T while Q do 7 xtractmin(q) // making part of T for each v dj[] do if v Q and w(,v) < key[v] then pdating p[v] key[v] w(,v) keys or each vertex v, key [v] is min weight of any edge connecting v to a vertex in tree. key [v]= if there is no edge and p [v] names parent of v in tree. When algorithm terminates the min-priority i i qee Q is empty. Prim s lgorithm Lines - set the key of each vertex to (except root r, whose key is set to 0 first vertex processed). lso, set parent of each vertex to NULL, and initialize min-priority qee Q to contain all vertices. Line 7 identifies a vertex є Q Removing from set Q adds it to set Q-V of vertices in tree, ths adding (, p[ ]) to. The for loop of lines - pdate key and p fields of every vertex v adjacent to bt not in tree. Rn on example graph Rn on example graph key[] =
Rn on example graph Rn on example graph r 0 Pick a start vertex r 0 Red vertices have been removed from Q Rn on example graph Rn on example graph 0 Red arrows indicate parent pointers 0
Rn on example graph Rn on example graph 0 0 xtract_min from Q Rn on example graph Rn on example graph 0 0
Rn on example graph Rn on example graph 0 0 Rn on example graph 0 Rn on example graph 0
Rn on example graph 0 Rn on example graph 0 Rn on example graph 0 Rn on example graph 0
Rn on example graph 0 Rn on example graph 0