0% found this document useful (0 votes)
4 views6 pages

16th Question

A minimum spanning tree (MST) is a subgraph of a weighted connected graph that connects all vertices with the smallest total edge weight, existing only if the graph is connected. Prim's and Kruskal's algorithms are two methods to compute an MST, with Prim's using a greedy approach to iteratively add the lowest-cost edge and Kruskal's selecting edges in order of smallest weight while avoiding cycles. Applications of MSTs include optimizing wiring, minimizing travel costs, and reducing networking expenses.

Uploaded by

wwww.kakashi6
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)
4 views6 pages

16th Question

A minimum spanning tree (MST) is a subgraph of a weighted connected graph that connects all vertices with the smallest total edge weight, existing only if the graph is connected. Prim's and Kruskal's algorithms are two methods to compute an MST, with Prim's using a greedy approach to iteratively add the lowest-cost edge and Kruskal's selecting edges in order of smallest weight while avoiding cycles. Applications of MSTs include optimizing wiring, minimizing travel costs, and reducing networking expenses.

Uploaded by

wwww.kakashi6
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/ 6
10.7 MINIMUM SPANNING TREE > A tree is a connected graph with no cycles. A spanning tree is a subgraph ofGthathas the same set of vertices of G and is a tree. A minimum spanning tree of a weighted connected graph G is its spanning tree of the smallest weight, where the weight of a tree is defined as the sum of e weights on all its edges. The total number of edges in Minimum Spanning ‘Tree (MST) is |V1-1 where V is the number of vertices.A minimum spanning tree exists if and only if G is connected. Example: Consider the graph given in figure 10.37. The minimum spanning tree for figure 10.37 is shown in figure 10.38 with minimum cost = 5. Of) TN) Yet Fig 10.37 Minimum spanning tree Cost =7 Cost = 8 Cost = 8 Cost =9 @) 2 (D) () 2 (@) y L sisimm O2-@© O72 Spanning Tree Cost = 5 Cost = 5 Fig 10,38 Minimum spanning tree Spanning ‘Tree with minimum cost is considered as Minimum Spanning ‘Tree. 10.40 Data Structures and Algorithms Conditions for minimum spanning tree are: + Treo should not form any eyele. + Byery node must be visited ‘Types of Algorithm to find MST: 40.7.4 Prim's Algorithm “Prim's algorithm is one of the methods to compute a minimum spanning trve swhich uses a greedy technique. In each stage, one node is picked as the root, an edge with minimum cost will be added to form a tree. At each step, it finds a shortest edge (u,v) such that the cost of (uy, v) is the smallest among all edges Algorithm 1. One node is picked as a root nede(u) from the given connected graph. 2. At each stage choose a new vertex v from u, by considering an edge (u,v) with minimum east among all edges from u, where w is already in the tree and vis natin the tree, 1. The prisa's algorith table is constructed with three parameters. They 1. Known - Vervexis added in the troe or not. = Weight ofthe shortest arc erunecting v to a known vertex. Inst vertex which exases a change in d, sex %, update rale is spplied for each unknown W is dw = mic(de, C,,), ie, more than one path ; then dw ia updated vith minimam cost ROUTINE FOR PRIS ALGORITIDS void Prizs (Table 7) ' wercex V, W; /* Table initialization = 0;i.< Numvertex jit4) Graphs 10.81 Y for) 1 Let V be the start vortox with the smallest distance TW) dist = 0; TV ksown = Trost for ach W adjacent to IFT. Known) ‘TIW}.Dist = Min(TTW). Dist, Cy): ‘TWLpath = V; ‘The implementation of prim’s algorithm is virtuall The = rithm is virtually identieal to that of stra’ algorithm Prim’s algorithm runs on undirected graphs, So wl remember to put every edge in two adjacency lists. The runni ‘without heaps, which is optimal for dense graphs, ae graphs, and OIE! log IVI) Example: For the folowing graph construct MST using prim's algorithm, T Fig 10.29 Undirected graph ree vertex and proceed further Step 1: Consider V, as so Graphs 10.49 10.42 Data Structures and Algorithms 0g Initial state ‘Here ‘V4’ is marked as visited and then the distance of its adjacent ‘Vertex | Known | a, | _®, is updated. In step (2) distance for vertex V, is 4 from V,, now the distance for ° vertex V, is 2 from V,. Since 2 <4, update V, as 2. 2 Step 4: After V, is declared known Vertex | Known | a | P, VY aS Ve. |e Vy oie a) Ws Trea Step 2: ‘After V, is declared known v tH Vertex | Known V, CHD Ne oreeae Here ‘V2’ is marked as visited and then the distance of its adja is updated as follows: 4 | aye) = Min (2, 8) = 2. 2 | % \ aval = Min (7, 10] = 7 a 3 i After V, is declared known * z its: ar vane is Vertex | Known | 4, 7 Hore ‘Vy is marked as visited and then The astansy oF Leak voi [ae : i g=2. : : = a i 2 V, | are ot is dectar Tt ota yy, Here ‘Va is marked as visited and then the distance of its adjacent vertex ‘TLV, dist = Min (TTV,}.dist, C,, ,,] = Min (8, 5) =5 ————_ 40.44 Data Structures and Algorithms Graphs 10.45 Parl “After V, is declared known Here ‘Vs fs marked as visited and then the distance of its adjacent vertex Tae em TET ES] is updated, if necessary T The final MST is; ‘Minimum Spanning Tree Cost is 16 for the given Undirected graph. 10,72 kruskal’s Algorithm: 7 z Yan Kruskal’ algorithm, we select edges inorder of smallest Here ‘V7 is marked as visited and then the distance of {s updated as follows: | ‘TLV, ]dist = Min (TLV,]dist, Cyyqg) = Min (5, = 1 | 4. = Min (T1V,}dist, Cy) = Min (7, = 8 After V, is declared known Vertex | Known oat with Union/Find operation of sets, v Beer ae : a) The alge arn Dud inte shee arate and onc vet it ! A : , ue : ot Sade minim et et : \ Shinn spnsing weno a @ ‘ Ie returned eta are not seme then (ay) iv add ta the st 8 str ts marke sisted so nth anes ect tee | Speaton npr’, wth he entrain hat ang create a eycle in spanning tree set S. 1 fs updated, if necessary. t i i) & (i), unt : Ps After V, is declared known L iv) Repeat step (i) & (i), until @ spanning tree is constructed with Iv1-1 Step 8: Vertex | Known | a | P, | ion @ ens | Hoowe |. | Routine for Kruskal’s Algorithm : : ¥ riety void Kruskal(Graph G) [| ¥._ 7 C Vs i a | Vy i js wy i int BagosAccopted; Disjset S; vo 8 | i Wot i 1 Vv, [2 14 [% i i BagesAccepted = 0; ‘While( BagesAcceptod < NumVertex-1) hB=0M" “Analysis of the Kruskal's Algorithm: "The worst case running time ,d by heap opera ig. actually OIE! log ‘this algorithm is OIE! log BN), Notice that since 1! a, IVI), In practice, the algorithm is ‘Example: Fig Y0.40 Undirected graph tex isin its own set. No ge with minimum added to the minimum spanning tee. o ©® a @ ® © ©=>e 8 © e © eo © Stet nn ee ith mininomwgt and eek wei rm gn ae ps tn thn eg cna tee oh nim peng Q @ a © Se ®——_® ®———® (VV; ithe minimum weighed edge (V,,V,) isthe next minimum edge Step 3: ‘Repeat step 2 until all vertices are included in the minimum spanning O—-—_® age (V,V;) is the next minimum edge (V,V,) is the next minimum weighted edge 10.48 Data Structures and Algorithms Step 4: The final MST using kruskal’s algorithm is as follows: 2 Action of kruskal’s algorithm Bdge | Weight | Action | : (1, V4) 1 Accepted (v6, V7) 1 Accepted Wi, V2) 2 ‘Accepted (v3, V4) 2 Accepted (v2, V4) 3 Rejected, (V1, V3) 4 Rejected. (V4, V7) 4 Accepted (v3, V6) 5 Rejected (v5, VD 6 Accepted Applications of Minimum spanning trees: «Wiring a house with minimum of cables. « Cheapest cost tour of. traveling salesman. «Networking the PC’s with low cost. »

You might also like