[go: up one dir, main page]

0% found this document useful (0 votes)
97 views16 pages

Minimum Spanning Tree

Uploaded by

Aryan Neelam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
97 views16 pages

Minimum Spanning Tree

Uploaded by

Aryan Neelam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 16
22.1. INTRODUCTION et The sta seation of minimum spanning tree is to solve a problem like phone network Deiat never bosinses with several offices; we want to lease phone lines to cone them up with each other; and the phone company charges jiffere: icttoad tine. ey connect different pairs of cities. We want a set of lines that connects ofc with coe rmum total cost, It should be a minimum spanning tree, since if a network is't a tree we can always remove some edges and save money. We can model this phone network problem with a connected, undirected graph G = (V, B), where Vis the set of cities, and is the set of possible interconnections between pairs of cities, and for each edge, we have a weight w specifying cost or the amount of wire needed to connect two cities, We then wish, to find an acyclic subset T < E that connects alll of the vertices and whose total weight is minimized. w(T) = w(u,v) ee 22.2. SPANNING TREES A spanning tree of a graph is any tree that includes every vertex in the graph. More formally, a spanning tree of a graph G is a subgraph of G that is a tree and contains all the vertices of G. We construct spanning tree whenever we want to find a simple, cheap and efficient way to connect a set of terminals (computers, cities, factories, etc). Spanning trees are important because of following reasons. * Spanning trees construct a sparse sub graph that tells a lot about the original graph. * Spanning trees are very important in designing efficient routing algorithms. . Some hard problems (e.g., Steiner tree problem and travelling salesman problem) ca” ¢ solved approximately by using spanning trees, oles pace a : ‘nning trees have wide applications in many areas, such as network design, ete: imum s] i et OM) sone ; ini spanning tree aa ae free (MST) is a spanning tree of minimum weight, The minimu® nique, but it is true if all the edge weights are distinct. } 438 linimum Spanning Tree she following is an example of spanning and minimum spanning tr ee, — ‘Agraph G ‘Thee (ofthe many possible) spanning {trees from graph (Gy 2 2 ; XI oie ay O 7 O 1 ‘Aveighted graph (G) The minimum spanning ‘tree from weighted graph G Fig. 22.1. There are two algorithms: The Kruskal’s algorithm and the prim’s algorithm to find the ninimum spanning tree of an undirected weighted graph. These algorithms use similar approaches, but works in very different ways. 22.3. KRUSKAL’S ALGORITHM FOR MST Kruskal’s algorithm is a greedy algorithm. A greedy algorithm chooses some local optimum (ie, picking an edge with the least weight in a MST). It builds the MST:in forest. Initially, each vertex is in its own tree in forest. Then, algorithm considers each edge in turn, ordered by increasing weight. Ifan edge (wu, v) connécts two different trees then (u, v) is added to the set of edges of the MST, and two trees connected by an edge (u, v) are merged into a single tree on the other hand, if an edge (w, v) connects two vertices in the same tree, then edge (u, »)is discarded. ‘Therefore is Start with an empty set A, and select at every stage the smallest edge that has not been chosen or rejected, regardless of where this edge is situated in graph. Kruskal’s algorithm uses a disjoint-set data structure to maintain several disjoint sets of elements, Digjoint-Sets Data Structure: * Make-Set (x): Create a new set whose only member is pointed to by v. Note that for this operation v must already be in a set. * FIND-SET; Returns a pointer to the set containing v. * UNION (uw, v): Units the dynamic sets that contain u and v into anew set that is union of these two sets, | MST-KRUSKAL (6, w) ph Aco = Design and Analysis of Algorithms ™ x mo \_— for each vertex V & vicl do MAKE-SET(V) f sort the edges of E into nol : for each edge (Ur v) € E, taken in nondecreaing 2. i n decreasing order by weight w 4, 5. 6, dof FIND-SET(u) # FIND-SET (v) 7. 8 9. order by weight then Ac AU {(u, VT : UNION(U, v) . return A Analysis: Let V be the number of vertices and E be the number of edges and we assume that E> V-1 It takes © (E log £) time to sort the edges. The for loop iterates E number of times, and each iteration involves a constant number of accessed to the Union-Find disjoint set data structure on the collection of V items. Thus each access takes @(V) time, which gives a total of @ (E log V) times. ‘Thus the total running time for the algo is @((V + E) log V). Since V is not asympto' expressed as © (E log V). rithm is evaluated by adding up all these, which tically larger than E, the running time can be Problems With Kruskal: + Kruskal’s algorithm grows a series of trees which are disjoint. © Growing a set of disjoint trees may be a problem in some applications. * Kruskal also continues checking edges even when all nodes are inserted. * Checking the edges can be inefficient in that a graph can have up V? edges. * Many of these edges will be creating cycles. EXAMPLE 22.1. Using Kruskal’s algorithm find the minimum spanning tree of the following given graph. = Fig. 22.2. ‘olution: Step 1: Initialize the set A to empty set Az=6 Minimum Spanning Tree step 2: Create |V| trees one for each verted by using MAKE-SET - procedure {€1}, (2, {3}, (4), 5), (8 Step 3: Sort the edges of E into non-decreasing order by weight (4,3) =2 (4,6) =3 (2,5) =4 (3,6) =5 (1,4) =6 (2,8) =6 (3, 4) =6 (3,5) =7 (5,6) =7 (1,2) =7 Step 4: Select at every stage the smallest edge that has been chosen or rejected, regardless of where this edge is situated in graph. Now we choose (1, 3) ® | o ) © {(1, 3), (2), {4}, (5), (6) Now we choose (4, 6) ‘= Design and Analysis of Algorithms = © a 3 © © {(1, 3}. (2), (4, 6) (5 pag {1,3}, (2, 5}, 4, OF Now we choose (2, 5) Now we choose (3, 6) {(1 3, 4, 6} {2, 5} Now we choose (2, 3) {(1, 2, 3, 4, 5, 6} therefore, weight of MST is 243 + 4+5+6=20 = Minimum Spanning Tree = EXAMPLE 22.2. Find the MST of the following given graph using Kruskals algorithm. Fig. 22.3. Solution: Step 1 : Initialize the set A to empty set A = Step 2: Create |V| trees one for each vertex by using MAKE-SET procedure QO” © © © © {{a}, {b}, {c}, {a}, fe}, {9 Step 3 : Sort the edges of E into non-decreasing order by weight. (a,d) = ff) = (e, e) (d, e) (c, d) (df) = (a, c) (a,b) = 12 (b,c) = 14 ae 4: Select at every stage the smallest edge that has not been choosen or rejectes ss of where this edge is situated in graph. " " ae ROD me = Design and Analysis of Algorithms = We choose (a, d) 1 © ete © {{a, dh, {b}, {} {2} (9) Now we choose (e, f) O-+-O {fa,d}, {b}, {c} {e, }} Next we choose (c, e) Next we choose (e, d) Minimum Spanning Tree 445 Front we enoose (aD) 2 © fore weight of MST is 12+1+2+8+1=19. fa.b,c,d,e,f} ‘There! GORITHM FOR MST ‘m is another greedy algorithm for minimum spanning trees. It differs from Kruskal’s algorithm only in how it selects the next safe edge to add at each step. The main jdea of Prim’s algorithm is similar to that of Dijkstra’s algorithm for finding shortest path jnagiven graph. Prim's algorithm has the property that the edges in the set that always form a single tree. We begin with some vertex u in a given graph G = (V, EB), defining the al set of vertices A. Then, it each iteration, we choose a minimum weight edge (u, v), init A to the vertex v outside of set A. Then vertex v is brought tonnecting a vertex u in the set jatoA. This process is repeated ‘until a spanning tree is formed. Like Kruskal’s algorithm, the important fact about MST is that we always choose the smallest-weight edge joining a vertex inside set A to the one outside the set A. Prim’s is slightly more efficient than Kruskal. m4. PRIM'S: jn’s algorith MST-PRIM (G, W, T) 1, foreach ue VIG] 2. dokey [uJ = 3. fu] — NIL 4. key [r] 0 5. Qe VIG] 6. whileQ+o 7. dou EXTRACT-MIN (Q) 8. for each v « Adj[u] 8 do if v ¢ Qand w(u, v) < key Lv] 10. then x[v] cu LM keyv] < w(u, v) How it works: oe the tree * The prim’s algorithm starts with an arbitrary root vertex rand grows Spans all the vertices in V. * Initialize the root vertex with key 0 and other vertices queue s with key . Pe ee key. The initial step is to put all vertices onto a priority Cae * The source vertex has 0 as its key, 60 it is choosen first and its adjace for the smallest weight edge which is added to the tree. list explored as \ Ms = Design and Analysis of Algorithms = * Atall times the smallest weight edge between included in the tree is added. * This process continues until all vertices are removed from the priority quoug, “he included vertices and the ver, 4 ieee | Tree Growing: prim’s algorithm creates the tree by adding one edge at a time to the current ty, | process can start with a root vertex r (it can be any vertex). At any time, the subset ea s, A forms a single tree whereas Kruskal’s algorithm formed a forest. We add a single, “tt as a leaf to the tree. The process is illustrated in the following figure, Vertex Fig. 22.4, Analysis: It takes O(log V) to extract each vertex from the priority queue. For each incident ed, OAog v) time is spent in decreasing the key of the neighbouring vertex. Thus the total tie is O(log V + deg (u) log V). The other steps for updation tak. i BOton ¥ = de pi e constant time. So the total TW, A= > (log V + degree(u)logV) ueV = > (1+degree(u))log¥ ueV = log V_>\ (1+degree(u)) ueV We know that, Dd degree(u) = a7 eV TW, E) = (log V) (V + 28) OV + £) log V) Vis not ically greate ing ti ich i aa Kruskal’ glgoithes sreater than E, so the running time is @(E log V) which is sa 0 EXAMPLE 22.3. Using Prim’s aleom it given graph Ti"é Prim’s algorithm find the minimum spanning tree of the followité = Minimum Spanning Tree TD NANA Fig. 22.5. ve NY AN S= @ SS Q= {b,c,d,e,f,g} A= Thos edge = {a,b} “I ve aS be Step 2. ge, YY OTN stay :D}} / lightest edge = {b,d}, {a,c} NEA NS A 3 Solution: Step 1. 5 Ne cA NES sziehe, Q= {ce,f.g} 10 eo 6 A= { {a,b}, {b.d}} ae a lightest edge = {d,c} XL ne 43 Step 4, oe teenie f9) © a (a,b), {b,d}, {c,d} } Ne 10 lightest edge = {c,f} x SY. Peles 6.4.) (e.g) {a,b}, {b,d}, {c,d}, {c.f} } lightest edge = {f.g} fe} oO A= { {a,b}, {0,4}, {c.d}, {c.f}, {f.9}} \, 3 Ne ° © lightest edge = {fe} WZ 2 2 Step 7. cdefg} RO WY o. Maa @ Aillon.e9.001060,00.40) \ ‘0 os NN x aS A oe cA SOLVED EXAMPLES EXAMPLE 22.4, Let (u, v) be a minimum weight edge in a graph G. Show that (u, ) belongs to some minimum spanning tree of G. Solution: Let us consider that A is a subset of some MST T with edge (u, v) € A. Light edges of some cut with u and v on separate sides. That's why (u, v) is minimum weight edge in directed graph for some cut. EXAMPLE 22.5. Define spanning tree. Write an algorithm for finding the spanning tree using Kruskal’s method and analyze it, (UPTU, MCA, 200102) Solution: Refer Section 21.2, 21.3, Solution: Refer Section 21.4. a = Minimum Spanning Tree « 449 EXAMPLE 22.7. Find out the minimum spanning tree of the following graph. (UPTU, MCA, 2003-04) Fig. 22.6, Solution: Step 1. ® © © © © (43. {8), (©). (0). FD Step 2, We choose (E, D) © © © ' ® @ {(A), (B}, {C). (0, } (FH Step 3. We choose (F, B) 1A). (8, FD. (C) (E. OF Final MST Fig. 22.7. ae Minimum Spanning Treo 454 ‘otal minimum spanning Tree cost is 6 +4 434142 = 16. EXAMPLE 22.8. Indicate the order in which Kruskal’s algorithm would include edge Preonstructing a minimum spanning tree for the following graph. (UPTU, MCA, 2004-05) Fig. 22.8. © © Solution: Step 1. {(B), (CE), (F}, (G}, (HY) Step 2. We choose (E, G) (8), {(C), {E, G), {F}, (HY © © © Step 3. We choose (C, F*) ©——® {8}, (C.F), (E, Gh {HY} O+—® ® Step 4. Now we choose (F, H) ©——® {(B), (C. FH}. (E, GI) ©O+—-O)——-® . Design and Analysis of Algorithms Step 5. We choose (E, F) (8). {CE FG Hyp ‘Step 6. We choose (B, C) z—©) © (B,C, E76 Hy th are as ‘etwork given below as highway map and the number recorded next to ead the maximum elevation encountered in 1 to 12 on this highway. a node deen the are. A traveller plans to drive from dislikes high altit his traveller Path connecti, minimize the max altitude, ing node 1 to 12 that using a mini : the tudes and so would like to find mum spanning tree. er - Find the best path for the travel = Minimum Spanning Tree w Fig. 22.10. 4, Discuss Prim's algorithm to find MST with example and also find its analysis. 5. | What are spanning tree. Illustrate with examples. Also write down their various application. ¢. Find the minimum spanning tree for the following graph using prims algorithm and Kruskal’s algorithm. Fig. 22.11.

You might also like