Presetatio for use with the textbook, lgorithm esig ad pplicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 0 Miimum Spaig Trees 0 Goodrich ad Tamassia Miimum Spaig Trees
pplicatio: oectig a Network Suppose the remote moutai coutry of Vectoria has bee give a major grat to istall a large Wi-i the ceter of each of its moutai villages. ommuicatio cables ca ru from the mai Iteret access poit to a village tower ad cables ca also ru betwee pairs of towers. The challege is to itercoect all the towers ad the Iteret access poit as cheaply as possible. 0 Goodrich ad Tamassia Miimum Spaig Trees
pplicatio: oectig a Network We ca model this problem usig a graph, G, where each vertex i G is the locatio of a Wi-i the Iteret access poit, ad a edge i G is a possible cable we could ru betwee two such vertices. ach edge i G could the be give a weight that is eual to the cost of ruig the cable that that edge represets. Thus, we are iterested i fidig a coected acyclic subgraph of G that icludes all the vertices of G ad has miimum total cost. Usig the laguage of graph theory, we are iterested i fidig a miimum spaig tree (MST) of G. 0 Goodrich ad Tamassia Miimum Spaig Trees
Miimum Spaig Trees Spaig subgraph Subgraph of a graph G cotaiig all the vertices of G Spaig tree OR 0 PIT Spaig subgraph that is itself a (free) tree Miimum spaig tree (MST) Spaig tree of a weighted graph with miimum total edge weight N STL pplicatios ommuicatios etworks Trasportatio etworks W TL 0 Goodrich ad Tamassia Miimum Spaig Trees
ycle Property ycle Property: Let T be a miimum spaig tree of a weighted graph G Let e be a edge of G that is ot i T ad let be the cycle formed by e with T or every edge f of, weight(f) weight(e) Proof: y cotradictio If weight(f) > weight(e) we ca get a spaig tree of smaller weight by replacig e with f 0 Goodrich ad Tamassia Miimum Spaig Trees f f e Replacig f with e yields a better spaig tree e
Partitio Property Partitio Property: osider a partitio of the vertices of G ito subsets U ad V Let e be a edge of miimum weight across the partitio There is a miimum spaig tree of G cotaiig edge e Proof: Let T be a MST of G If T does ot cotai e, cosider the cycle formed by e with T ad let f be a edge of across the partitio y the cycle property, weight(f) weight(e) Thus, weight(f) = weight(e) We obtai aother MST by replacig f with e 0 Goodrich ad Tamassia Miimum Spaig Trees U U f e f e V Replacig f with e yields aother MST V
Prim-Jarik s lgorithm Similar to ijkstra s algorithm We pick a arbitrary vertex s ad we grow the MST as a cloud of vertices, startig from s We store with each vertex v label d(v) represetig the smallest weight of a edge coectig v to a vertex i the cloud t each step: We add to the cloud the vertex u outside the cloud with the smallest distace label We update the labels of the vertices adjacet to u 0 Goodrich ad Tamassia Miimum Spaig Trees
Prim-Jarik Pseudo-code 0 Goodrich ad Tamassia Miimum Spaig Trees
0 Goodrich ad Tamassia Miimum Spaig Trees xample 0 0 0 0
0 Goodrich ad Tamassia Miimum Spaig Trees 0 xample (cotd.) 0 0
alysis Graph operatios We cycle through the icidet edges oce for each vertex Label operatios We set/get the distace, paret ad locator labels of vertex z O(deg(z)) times Settig/gettig a label takes O() time Priority ueue operatios ach vertex is iserted oce ito ad removed oce from the priority ueue, where each isertio or removal takes O(log ) time The key of a vertex w i the priority ueue is modified at most deg(w) times, where each key chage takes O(log ) time Prim-Jarik s algorithm rus i O(( + m) log ) time provided the graph is represeted by the adjacecy list structure Recall that Σ v deg(v) = m The ruig time is O(m log ) sice the graph is coected 0 Goodrich ad Tamassia Miimum Spaig Trees
Kruskal s pproach Maitai a partitio of the vertices ito clusters Iitially, sigle-vertex clusters Keep a MST for each cluster Merge closest clusters ad their MSTs priority ueue stores the edges outside clusters (or you could eve sort the edges) Key: weight lemet: edge t the ed of the algorithm Oe cluster ad oe MST 0 Goodrich ad Tamassia Miimum Spaig Trees
Kruskal s lgorithm 0 Goodrich ad Tamassia Miimum Spaig Trees
0 Goodrich ad Tamassia ampus Tour xample of Kruskal s lgorithm G 0 G 0 G 0 G 0 H H H H
0 Goodrich ad Tamassia ampus Tour xample (cotd.) five steps G 0 G 0 G 0 G 0 H H H H
ata Structure for Kruskal s lgorithm The algorithm maitais a forest of trees priority ueue extracts the edges by icreasig weight edge is accepted it if coects distict trees We eed a data structure that maitais a partitio, i.e., a collectio of disjoit sets, with operatios: makeset(u): create a set cosistig of u fid(u): retur the set storig u uio(, ): replace sets ad with their uio 0 Goodrich ad Tamassia Miimum Spaig Trees
List-based Partitio ach set is stored i a seuece ach elemet has a referece back to the set operatio fid(u) takes O() time, ad returs the set of which u is a member. i operatio uio(,), we move the elemets of the smaller set to the seuece of the larger set ad update their refereces the time for operatio uio(,) is mi(, ) Wheever a elemet is processed, it goes ito a set of size at least double, hece each elemet is processed at most log times 0 Goodrich ad Tamassia Miimum Spaig Trees
Partitio-ased Implemetatio Partitio-based versio of Kruskal s lgorithm luster merges as uios luster locatios as fids Ruig time O(( + m) log ) Priority Queue operatios: O(m log ) Uio-id operatios: O( log ) 0 Goodrich ad Tamassia Miimum Spaig Trees
lterative Implemetatio 0 Goodrich ad Tamassia Miimum Spaig Trees
aruvka s lgorithm Like Kruskal s lgorithm, aruvka s algorithm grows may clusters at oce ad maitais a forest T ach iteratio of the while loop halves the umber of coected compoets i forest T The ruig time is O(m log ) lgorithm aruvkamst(g) T V {just the vertices of G} while T has fewer tha - edges do for each coected compoet i T do Let edge e be the smallest-weight edge from to aother compoet i T if e is ot already i T the dd edge e to T retur T 0 Goodrich ad Tamassia Miimum Spaig Trees 0
0 Goodrich ad Tamassia xample of aruvka s lgorithm (aimated) Miimum Spaig Trees Slide by Matt Stallma icluded with permissio.