 # MINIMUM SPANNING TREES

Information about MINIMUM SPANNING TREES

Published on March 6, 2009

Author: ankush85

Source: authorstream.com

MINIMUM SPANNING TREES : 1 MINIMUM SPANNING TREES Definition : Spanning tree : Let G = (V,E) be an un-directed connected graph. A sub-graph T = (V1, E1) of G is a spanning tree of G iff T is a tree. O O O O O O O O O O O O O O O (a) (b) (c) (d) MINIMUM SPANNING TREES (Contd..) : 2 MINIMUM SPANNING TREES (Contd..) Is a complete graph (b),(c),(d) are three of A’s spanning trees. (A minimal connected sub-graph of G which includes all the vertices of G is a spanning tree of G) APPLICATIONS OF SPANNING TREES : 3 APPLICATIONS OF SPANNING TREES Spanning trees can be used to obtain an independent set of circuit equations for an electrical network. Let B be the set of network edges not in the spanning tree. Adding an edge from B to the spanning tree creates a cycle. Different edges from B result in different cycles. MINIMUM SPANNING TREES (Contd..) : 4 MINIMUM SPANNING TREES (Contd..) Kirchoff’s second law is used on each cycle to obtain a circuit equation. The cycles obtained in this way are independent as each contains one edge from B which is not contained in any other cycle. Hence the circuit equations so obtained are also independent. MINIMUM SPANNING TREES (Contd..) : 5 MINIMUM SPANNING TREES (Contd..) (cities) arises from the property that a spanning tree is a minimal sub-graph G1 of G such that V(G) = V(G1) and G1 is connected. A connected graph with n vertices must have at least n-1 edges and all connected graphs with n-1 edges are trees. MINIMUM SPANNING TREES (Contd..) : 6 MINIMUM SPANNING TREES (Contd..) If the nodes of G represent cities and the edges represent possible communication links connecting two cities, the minimum number of links needed to connect the n cities is n-1. The spanning trees of G will represent all feasible choices. In practice, the edges will have weights associated with them. MINIMUM SPANNING TREES (Contd..) : 7 MINIMUM SPANNING TREES (Contd..) These might represent the cost of construction, the length of the link etc. We are interested in finding a spanning tree of G with minimum cost. Prim’s algorithm (Greedy method) to obtain a minimum cost spanning tree. If A is the set of edges selected so far, then A forms a tree. The next edges (u,v) to be included in A is a minimum cost edge not in A with the property that A U {u, v} is also a tree. Prim’s Algorithm- Example : 8 Prim’s Algorithm- Example Example : 2 10 50 1 40 3 45 35 5 30 25 15 55 4 6 20 Prim’s Algorithm- Example (Contd..) : 9 Prim’s Algorithm- Example (Contd..) Edge Cost Spanning tree (1,2) 10 10 1 2 (2,4) 25 10 1 2 6 25 (3,6) 15 10 1 2 25 6 15 3 Prim’s Algorithm- Example (Contd..) : 10 Prim’s Algorithm- Example (Contd..) Edge Cost Spanning tree (6,4) 20 10 1 2 25 4 20 6 15 3 (1,4) Reject (3,5) 35 10 1 2 5 25 4 20 6 15 3 35 PRIM’s algorithm to find minimum cost spanning tree : 11 PRIM’s algorithm to find minimum cost spanning tree The cost of spanning tree is 105. Procedure PRIM (E,cost, n, T, mincost) // E is the set of edges in G // // cost (n,n) is the adjacency matrix // // such at cost (i,j) is a +ve real number // // or cost (i,j) is 8 if no edge (i,j) exists // // A minimum cost spanning tree is // PRIM’s algorithm to find minimum cost spanning tree contd.. : 12 PRIM’s algorithm to find minimum cost spanning tree contd.. // computed and stored as a set // // of edges in the array T(1,n-1,2) // // (T(i,1), T(i,2) is an edge in minimum cost spanning tree // Real cost (n,n), mincost; Integer near(n),i,j,k,l,T(1:n-1,2) (k,l) ? edges with minimum cost 4 mincost ? cost (k,l) (T(1,1) T(1,2) )? (k,l) for i? 1 to n do // initialing near // PRIM’s algorithm to find minimum cost spanning tree contd.. : 13 PRIM’s algorithm to find minimum cost spanning tree contd.. if cost (i,L) < cost (I,k) then near (i) ? L else near (i) ? k endif repeat near(k) ? near (l) ? 0 // (K,L) is already in the tree // for i ? 2 to n-1 do //find n-2 additional edges for T // Let j be an index such that NEAR (J) ? 0 and COST (J,NEAR(J)) is minimum PRIM’s algorithm to find minimum cost spanning tree contd.. : 14 PRIM’s algorithm to find minimum cost spanning tree contd.. (T(i,1),T(i,2)) ? (j, NEAR (j)) Mincost ? mincost + cost (j, near (j)) NEAR (j) ? 0 for k ? 1 to n do // update near // if NEAR (k) ? 0 and COST(k near (k)) > cost(k,j) then NEAR (k) ? j endif 20 repeat Repeat if mincost >> 8 then print (‘no spanning tree’) endif END PRIM EXPLANATION OF PRISM’S ALGORITHM : 15 EXPLANATION OF PRISM’S ALGORITHM After including a minimum cost edge near (i) is computed for each I = 1,…n. Near (i) is the vertex (out of the two vertices in the included edge) which is near to i. Near (i) is computed so that after including an edge, we need to find the vertex with which we are trying to form the new /next edge in the spanning tree. EXPLANATION OF PRISM’S ALGORITHM (Contd..) : 16 EXPLANATION OF PRISM’S ALGORITHM (Contd..) To exclude already included edges make near (k) ?0 near (l) ? 0 if (k, l) is included. In the second for-loop we are finding I which forms an edge with near (i) and having minimum cost. In the third for-loop which is included in the second for-loop we check whether near (i) values are same or, are to be updated with respect to the newly added edge or w.r.t. to the newly included vertex j. EXPLANATION OF PRISM’S ALGORITHM (Contd..) : 17 EXPLANATION OF PRISM’S ALGORITHM (Contd..) Example: Minimum cost edge (1, 2) with cost 10 is included Near (3) ? 2 Near (4) ?1 Near (5) ?2 Near (6) ?2 Near (1) ? 0 Near (2)?0 Select out of 3,4,5,6 a vertex such that {Cost (3, 2) = 50 Cost (4, 1) = 30 Cost (5, 2) = 40

### EXPLANATION OF PRISM’S ALGORITHM (Contd..) :

18 EXPLANATION OF PRISM’S ALGORITHM (Contd..) Cost (6, 2) = 25} is minimum cleary it is 6 ?j?6 so the edge j, near (j) i.e. (6, 2) is included. Now let us update near (k) values k =1…6 Near (1) = near (2) = near (6) = 0 K=3 cost (3, near (3) = 2) = 50 > cost (3, 6) =15 ? near (3) is 6 near (4) ? 6 ? cost (4,1) = 30 > cost (4,6) = 20 near (5) ?2 ? cost (5,2) = 40 < cost (5,6) = 55 KRUSKAL’S ALGORITHM FOR MINIMUM COST SPANNING TREE : 19 KRUSKAL’S ALGORITHM FOR MINIMUM COST SPANNING TREE The edges are considered in the non decreasing order. To get the minimum cost spanning tree, the set of edges so far considered may not be a tree. T will be a forest since the set of edges T can be completed into a tree iff there are no cycles in T. KRUSKAL’S ALGORITHM - Example : 20 KRUSKAL’S ALGORITHM - Example Example: 10 1 2 25 50 30 45 40 35 3 4 5 55 15 20 6 KRUSKAL’S ALGORITHM – Example (Contd..) : 21 KRUSKAL’S ALGORITHM – Example (Contd..) Edge Cost Spanning forest (1,2) 10 1 2 3 4 5 6 (3,6) 15 1 2 3 4 5 6 (4,6) 20 1 2 3 5 4 6 KRUSKAL’S ALGORITHM – Example (Contd..) : 22 KRUSKAL’S ALGORITHM – Example (Contd..) Edge Cost Spanning forst (2,6) 25 1 2 5 3 6 4 (1,4) 30 Reject ( T will form a cycle) (3,5) 35 1 2 5 3 6 4 Stages in Kruskal’s algorithm. High level Kruskal’s algorithm For minimum cost spanning tree : 23 High level Kruskal’s algorithm For minimum cost spanning tree T ? f While T contains fewer than (n-1) edges and E ? 0 do Choose an edge (v, w) from G of lower cost. Delete (v, w) from E If (v, w) does not create a cycle in T, then add (v, w) to T else discard (v, w). Implementation issues in Kruskal’s algorithm for minimum cost spanning tree : 24 Implementation issues in Kruskal’s algorithm for minimum cost spanning tree A tree with (n) vertices has (n: 1) edges Minheap is used to delete minimum cost edge. To make sure that the added edge (v, w) does not form a cycle in T, (to add to T), we use UNION and Find algorithms of sets. The vertices are grouped in such a way that all the vertices in a connected component are placed into a set. Two vertices are connected in T iff they are in the same set. Detailed Kruskal’s algorithm for minimum cost spanning tree : 25 Detailed Kruskal’s algorithm for minimum cost spanning tree Procedure Kruskal (E, cost, n, T, mincost) // E is the set of edges in G. G has n vertices // // cost (u, v) is the cost of edge (u , v) T is the set of edges in the minimum cost spanning tree and mincost is the cost // real mincost, cost (1: n, 1:n) integer parent (1: n), T(1:n-1, 2), n Parent?-1 //Each vertex is in the different set // i? mincost? 0 While (i < n -1) and (heap not empty) do delete a minimum cost edge (u,v) from the heap and reheapify using ADJUST Detailed Kruskal’s algorithm for minimum cost spanning tree contd.. : 26 Detailed Kruskal’s algorithm for minimum cost spanning tree contd.. J?FIND (u); k?FIND (v) If j ? k then i?i+1 T(i, 1) ? u; T (i,2) ?v Mincost ? mincost + cost(u,v) Call union (j, k) endif Repeat if i ? n-1 // heap is empty but i ? n-1// then “no spanning tree” endif return end KRUSKAL Detailed Kruskal’s algorithm for minimum cost spanning tree- Example : 27 Detailed Kruskal’s algorithm for minimum cost spanning tree- Example Initially each vertex is in a different set {1}{2}{3}{4}{5}{6} Consider(1, 2) j?1= Find(1); k = 2 = Find(2) 1 ?2 so i?1 (1,2) is included and union (1,2) = {1,2} Consider (3,6) Test if (3,6) is forming a cycle with (1,2) 3? Find (1) 6? Find (6) 3 ? 6 so i?2 (3,6) is included; UNION (3,6) = (3,6) Consider (4,6); 4? Find(4) ; Find(6) is 3 3? 4 so i? 3 and (4, 6) is added and union (3, 4) = (3, 4, 6) Consider (2,6) Find(2) ?1 [ union (1,3) is Find(6) ?3 union {1,2,3,4,6} ] COMPLEXITY OF KRUSKAL’S ALGORITHM : 28 COMPLEXITY OF KRUSKAL’S ALGORITHM The edges are maintained as a min heap. The next edge can be obtained / deleted in O(log e) time if G has e edges. Reconstruction of heap using ADJUST takes O(e) time. ?Kruskal’s algorithm takes O(e loge) time . Theroem: Kruskal’s algorithm generates a minimum cost spanning tree. COMPLEXITY OF KRUSKAL’S ALGORITHM (Contd..) : 29 COMPLEXITY OF KRUSKAL’S ALGORITHM (Contd..) Proof: Let G be any undirected connected graph. Let T be the spanning tree for G generated by Kruskal’s algorithm. Let T1 be a minimum cost spanning tree for G. We show that T and T1 have the same cost. Let E(T) denote the set of edges in T Let E(T1) denote the set of edges in T1 If n is the number of vertices in G, T and T’ have n-1 edges. If E(T) = E(T1) then T is of minimum cost and theorem is prooved. COMPLEXITY OF KRUSKAL’S ALGORITHM (Contd..) : 30 COMPLEXITY OF KRUSKAL’S ALGORITHM (Contd..) Assume E(T) ? E(T1).Then let e be a minimum cost edge. Such that e?E(T) and e?E(T1) (not necessarily smallest cost edge in E(T)) Since T1 is a minimum cost spanning tree, the inclusion of e in T1 creates a unique cycle. Let e1, e2, e3…. e4 be this cycle. At least one of the eis 1= I = k does not belong to E(T); otherwise T will also contain this cycle. Let ej is such that ej?E(T). We claim c(ej) ? c(e) (c( ) is the cost). COMPLEXITY OF KRUSKAL’S ALGORITHM (Contd..) : 31 COMPLEXITY OF KRUSKAL’S ALGORITHM (Contd..) If c(ej) < c(e) it would have been included into T. Also, all the edges in E(T) of cost less than the cost of e are also in E(T1) and do not form a cycle with ej. (e is an edge in E(T) and e?E(T1); also E(T) and (ET1) have the same number of edges and hence they will have edges less cost than e Now consider the graph with edge set E(T1)U{e}. COMPLEXITY OF KRUSKAL’S ALGORITHM (Contd..) : 32 COMPLEXITY OF KRUSKAL’S ALGORITHM (Contd..) Removal of any edge on the cycle e1,e2,….ek will leave behind a tree T11. In particular, if we remove the edge e then the resulting tree T11 will have a cost no more than the cost of T1. Hence T11 is also a minimum cost tree. By repeatedly using the transformation described above, T1 can be transformed into the spanning tree T without increase in cost. Hence T is a minimum cost spanning tree. SINGLE SOURCE SHORTEST PATH PROBLEM : 33 SINGLE SOURCE SHORTEST PATH PROBLEM Vertices in a graph may represent cities and edges may represent sections of highway. The weights of edges may be distance between two cities or the average time to drive from one city to the other. The shortest path problem is as below : Given a directed graph = (V, E) a weighted function c(e) for edges e of G and a source vertex V0. The problem is to determine the shortest path from vo to all the remaining vertices of G. All weights are supposed to be positive. SINGLE SOURCE SHORTEST PATH PROBLEM (Contd..) : 34 SINGLE SOURCE SHORTEST PATH PROBLEM (Contd..) Example: 45 Path Length V0 50 V1 10 V4 1) V0V2 10 35 2) V0V2V3 25 10 20 20 30 3) V0V2V3V1 45 V2 V3 V5 4) V0V4 45 15 3 There is no path from V0 to V5 GREEDY ALGORITHM TO GENERATE SHORTEST PATH : 35 GREEDY ALGORITHM TO GENERATE SHORTEST PATH The paths are generated in non decreasing order of path length. First a shortest path to the nearest vertex is generated. Then a shortest path to the second nearest vertex is generated and so on. We need to determine (i) the next vertex to which a shortest path must be generated and (ii) a shortest path to this vertex. GREEDY ALGORITHM TO GENERATE SHORTEST PATH (Contd..) : 36 GREEDY ALGORITHM TO GENERATE SHORTEST PATH (Contd..) Let S denote the set of vertices (including vo) to which the shortest paths have already been generated. For w ? S, let Dist(w) be the length of the shortest path starting from vo going through only those vertices which are in S and ending at w. Observations in Greedy Algorithm (Contd..) : 37 Observations in Greedy Algorithm (Contd..) The following points need to be observed : 1. If the next shortest path is to vertex u, then the path begins at vo ends at u and goes through only those vertices which are in S. To prove this, assume there is a vertex w on this path from Vo to u that is not in S. Thus the vo to u path also contains a path from vo to w which is of length less than vo to u path. Observations in Greedy Algorithm (Contd..) : 38 Observations in Greedy Algorithm (Contd..) By assumption the shortest paths are being generated in non decreasing order of path length and so the shortest path vo to w must already have been generated. Hence there can be no intermediate vertex which is not in S. (ii)The destination of the next path generated must be the vertex which has the minimum distance Dist (w) among all vertices not in S. Observations in Greedy Algorithm (Contd..) : 39 Observations in Greedy Algorithm (Contd..) (iii)Having selected a vertex u as in (ii) and generated the shortest vo to u path, vertex u becomes a member of S. - The value of Dist (w) may change. - If it changes, the length of this path is Dist (u) + c (u, w). DIJKSTRA’S SHORTEST PATH ALGORITHM : 40 DIJKSTRA’S SHORTEST PATH ALGORITHM Procedure SHORT-PATHS (v, cost, Dist, n) // Dist (j) is the length of the shortest path from v to j in the //graph G with n vertices; Dist (v) = 0 // Boolean S(1:n); real cost (1:n,1:n), Dist (1:n); integer u, v, n, num, i, w // S(i) = 0 if i is not in S and s(i) =1 if it is in S// // cost (i, j) = +? if edge (i, j) is not there// // cost (i,j) = 0 if i = j; cost (i, j) = weight of < i, j > // for i?1 to do // initialize S to empty // S(i) ?0; Dist (i)? cost(v, i) repeat DIJKSTRA’S SHORTEST PATH ALGORITHM (Contd..) : 41 DIJKSTRA’S SHORTEST PATH ALGORITHM (Contd..) // initially for no vertex shortest path is available// S (v)?1; dist(v)?0// Put v in set S // for num?2 to n-1 do // determine n-1 paths from// //vertex v // choose u such that Dist (u)=min{dist(w)} and S(w)=0 S(u)?1 // Put vertex u in S // Dist(w)?min (dist(w),Dist(u) + cost (u,w)) Repeat repeat end SHORT - PATHS Dijkstra’s shortest path algorithm -example : 42 Dijkstra’s shortest path algorithm -example Explanation of algorithm with example 45 S(1) = 0; S(2) = 0; …, S(5) = 0 Dist(1) ?cost (V0,V1) = 50 V0 50 V1 10 V4 Dist(2) ?cost (V0,V2) ? 10 15 Dist(3) ?cost (V0,V3)? + ? 35 Dist(4) ? 45; Dist(5) ? ? 20 10 20 Let S(V0)? 1 Dist (V0) ? 0; 30 For num ? 2 to 4 do V2 V3 V5 Dist(2) = min{Dist(1)…Dist(5)} 15 3 = 10 As vertex 2 is included, ?U ? 2 ? S(2) ? 1 Dijkstra’s shortest path algorithm –example (Contd..) : 43 Dijkstra’s shortest path algorithm –example (Contd..) Some of Dist(3), … Dist(5) may change new Dist(3) ? min (old Dist(3) = ?, Dist(2)+cost(2,3)) ? min(?,10+15) = 25 new Dist(4) ? min (old Dist(4) = 45, Dist(2)+cost(2,4)) ? min(45,10+?) = 45 new Dist(5) ? min (old Dist(5) = ?, Dist(2)+cost(2,5)) ? min(?,10+ ?) = ? Now choose u such that dist (u)= min{Dist (3), Dist (4), Dist (5)))} i.e min { 25, 45, ? } = 25 ?U ? 3 S(3) ? 1 The process is continued. The time taken by the algorithm is 0(n2). Dijkstra’s shortest path algorithm –example (Contd..) : 44 Dijkstra’s shortest path algorithm –example (Contd..) The edges on the shortest paths from a vertex v to all remaining vertices in a connected undirected graph G form a spanning tree of G called shortest path spanning tree. Shortest path spanning tree for the above graph is : Dijkstra’s shortest path algorithm –example (Contd..) : 45 Dijkstra’s shortest path algorithm –example (Contd..) 55 1 4 1 2 25 45 25 55 45 4 30 30 25 3 8 3 4 8 2 3 4 8 5 40 20 50 40 5 30 5 15 7 2 20 5 7 35 10 5 5 7 15 6 16 10 Graph G 10 6 6 minimum cost spanning tree G Shortest path spanning tree

03. 04. 2009
0 views

06. 03. 2009
0 views

27. 03. 2009
0 views

20. 11. 2009
0 views

22. 11. 2009
0 views

14. 07. 2009
0 views

18. 03. 2009
0 views

08. 04. 2009
0 views

06. 03. 2009
0 views

20. 04. 2009
0 views

03. 06. 2013
0 views

03. 06. 2013
0 views

16. 10. 2012
0 views

25. 09. 2012
0 views

25. 09. 2012
0 views

03. 01. 2012
0 views

24. 11. 2009
0 views

22. 11. 2009
0 views

22. 11. 2009
0 views

16. 05. 2009
0 views

19. 04. 2009
0 views

16. 05. 2009
0 views

10. 07. 2009
0 views

01. 07. 2009
0 views

21. 07. 2009
0 views

21. 07. 2009
0 views

01. 07. 2009
0 views

05. 03. 2009
0 views

13. 06. 2009
0 views

11. 05. 2009
0 views

05. 03. 2009
0 views

06. 03. 2009
0 views

25. 08. 2009
0 views

19. 06. 2009
0 views

24. 06. 2009
0 views

02. 10. 2008
0 views

25. 04. 2009
0 views

28. 04. 2009
0 views

17. 06. 2009
0 views

25. 04. 2009
0 views

25. 06. 2009
0 views

19. 06. 2009
0 views

18. 03. 2009
0 views

27. 04. 2009
0 views

23. 03. 2009
0 views

23. 05. 2009
0 views

23. 05. 2009
0 views

24. 06. 2009
0 views

24. 05. 2009
0 views

03. 04. 2009
0 views

08. 04. 2009
0 views

23. 03. 2009
0 views

06. 03. 2009
0 views

06. 03. 2009
0 views

29. 04. 2009
0 views

24. 05. 2009
0 views

06. 03. 2009
0 views

11. 04. 2009
0 views

08. 03. 2009
0 views

13. 03. 2009
0 views

04. 09. 2009
0 views

20. 11. 2009
0 views

22. 11. 2009
0 views

06. 03. 2009
0 views

06. 03. 2009
0 views

21. 04. 2009
0 views

25. 05. 2009
0 views

05. 03. 2009
0 views

14. 07. 2009
0 views

14. 07. 2009
0 views

05. 03. 2009
0 views

09. 05. 2009
0 views

05. 03. 2009
0 views

05. 03. 2009
0 views

05. 03. 2009
0 views

06. 03. 2009
0 views

06. 03. 2009
0 views

06. 03. 2009
0 views

06. 03. 2009
0 views

06. 03. 2009
0 views

06. 03. 2009
0 views

06. 03. 2009
0 views

06. 03. 2009
0 views

09. 03. 2009
0 views

09. 03. 2009
0 views

13. 03. 2009
0 views

19. 03. 2009
0 views

25. 03. 2009
0 views

08. 04. 2009
0 views

10. 04. 2009
0 views

10. 04. 2009
0 views

13. 04. 2009
0 views

15. 04. 2009
0 views

17. 04. 2009
0 views

23. 04. 2009
0 views

21. 04. 2009
0 views

21. 04. 2009
0 views

21. 04. 2009
0 views

23. 04. 2009
0 views

25. 04. 2009
0 views

25. 04. 2009
0 views

29. 04. 2009
0 views

29. 04. 2009
0 views

26. 04. 2009
0 views

26. 04. 2009
0 views

27. 04. 2009
0 views

05. 05. 2009
0 views

14. 05. 2009
0 views

16. 05. 2009
0 views

15. 05. 2009
0 views

15. 05. 2009
0 views

23. 04. 2009
0 views

23. 05. 2009
0 views

23. 05. 2009
0 views

24. 05. 2009
0 views

28. 05. 2009
0 views

04. 06. 2009
0 views

19. 06. 2009
0 views

19. 06. 2009
0 views

07. 06. 2009
0 views

13. 06. 2009
0 views

13. 06. 2009
0 views

13. 06. 2009
0 views

13. 06. 2009
0 views

13. 06. 2009
0 views

13. 06. 2009
0 views

13. 06. 2009
0 views

13. 06. 2009
0 views

13. 06. 2009
0 views

15. 06. 2009
0 views

26. 06. 2009
0 views

27. 06. 2009
0 views

01. 07. 2009
0 views

06. 07. 2009
0 views

05. 07. 2009
0 views

05. 07. 2009
0 views

10. 07. 2009
0 views

01. 08. 2009
0 views

26. 04. 2009
0 views

21. 08. 2009
0 views

25. 08. 2009
0 views

27. 08. 2009
0 views

27. 08. 2009
0 views

27. 08. 2009
0 views

04. 09. 2009
0 views

04. 09. 2009
0 views

27. 06. 2009
0 views