Presentation is loading. Please wait.

Presentation is loading. Please wait.

Minimum-Cost Spanning Tree weighted connected undirected graph cost of spanning tree is sum of edge costs find spanning tree that has minimum cost Network.

Similar presentations


Presentation on theme: "Minimum-Cost Spanning Tree weighted connected undirected graph cost of spanning tree is sum of edge costs find spanning tree that has minimum cost Network."— Presentation transcript:

1 Minimum-Cost Spanning Tree weighted connected undirected graph cost of spanning tree is sum of edge costs find spanning tree that has minimum cost Network has 10 edges. Spanning tree has only n - 1 = 7 edges. Need to either select 7 edges or discard 3. 1357 2468 2 4 6 3 81014 127 9 Example

2 Edge Selection Greedy Strategies Start with an n vertex forest. Consider edges in ascending order of cost. Select edge if it does not form a cycle together with already selected edges.  Kruskal’s method Start with a 1 vertex tree and grow it into an n vertex tree by repeatedly adding a vertex and an edge. When there is a choice, add a least cost edge.  Prim’s method

3 Kruskal’s Method Start with a forest that has no edges. 1357 2468 2 4 6 3 81014 12 7 9 1357 2468 Consider edges in ascending order of cost. Edge (1,2) is considered first and added to the forest.

4 Kruskal’s Method Edge (7,8) is considered next and added. 1357 2468 2 4 6 3 81014 12 7 9 1357 2468 2 3 Edge (3,4) is considered next and added. 4 Edge (5,6) is considered next and added. 6 Edge (2,3) is considered next and added. 7 Edge (1,3) is considered next and rejected because it creates a cycle.

5 Kruskal’s Method Edge (2,4) is considered next and rejected because it creates a cycle. 1357 2468 2 4 6 3 81014 12 7 9 1357 2468 2 3 4 Edge (3,5) is considered next and added. 6 10 Edge (3,6) is considered next and rejected. 7 Edge (5,7) is considered next and added. 14

6 Kruskal’s Method n - 1 edges have been selected and no cycle formed. So we must have a spanning tree. Cost is 46. Min-cost spanning tree is unique when all edge costs are different. 1357 2468 2 4 6 3 81014 12 7 9 1357 2468 2 3 4 6 10 7 14

7 Prim’s Method Start with any single vertex tree. 1357 2468 2 4 6 3 81014 12 7 9 5 Get a 2 vertex tree by adding a cheapest edge. 6 6 Get a 3 vertex tree by adding a cheapest edge. 3 10 Grow the tree one edge at a time until the tree has n - 1 edges (and hence has all n vertices). 4 4 2 7 1 2 7 14 8 3

8 Another Example for Prim’s Method b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f D[.] = 1 0 5 1  

9 Another Example for Prim’s Method b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f D[.] = 1 0 5 1   a b c d e f new D[.] = 1 0 2 1 4 

10 Another Example for Prim’s Method b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f D[.] = 1 0 2 1 4  a b c d e f new D[.] = 1 0 2 1 4 2

11 Another Example for Prim’s Method b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f D[.] = 1 0 2 1 4 2 a b c d e f new D[.] = 1 0 2 1 1 2

12 Another Example for Prim’s Method b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f D[.] = 1 0 2 1 1 2 a b c d e f new D[.] = 1 0 2 1 1 2

13 Another Example for Prim’s Method b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f D[.] = 1 0 2 1 1 2 a b c d e f new D[.] = 1 0 2 1 1 2

14 Greedy Minimum-Cost Spanning Tree Methods Can prove that all result in a minimum-cost spanning tree. Prim’s method is fastest.  O(n 2 ) (worst case)  O((n+e) log n) if a Min Heap is used to keep track of distances of vertices to partially built tree. If e=O(n 2 ), MinHeap is not a good idea! Kruskal’s uses union-find trees to run in O(n + e log e) time.

15 P processors, n=|V| vertices Each processor is assigned n/p vertices (P i gets the set V i ) Each PE holds the n/p columns of A and n/p elements of d[] array... d[.] A P0P0P0P0 P1P1P1P1 PiPiPiPi P P p-1 | | | ….. n/p columns Parallel MST Algorithm (Prim’s)

16 1. Initialize: Vt := {r}; d[k] =  for all k (except d[r] = 0) 2. P 0 broadcasts selectedV = r using one-to-all broadcast. 3. The PE responsible for "selectedV" marks it as belonging to set Vt. 4. For v = 2 to n=|V| do 5. Each P i updates d[k] = Min[d[k], w(selectedV, k)] for all k  Vi 6. Each P i computes MIN-di = (minimum d[] value among its unselected elements) 7. PEs perform a "global minimum" using MIN-di values and store the result in P 0. Call the winning vertex, selectedV. 8. P 0 broadcasts "selectedV" using one-to-all broadcast. 9. The PE responsible for "selectedV" marks it as belonging to set Vt. 10. EndFor

17 TIME COMPLEXITY ANALYSIS : In the worst-case, T seq = n 2 (Hypercube) T par = n*(n/p) + n*logp computation + communication (Mesh) T par = n*(n/p) + n * Sqrt(p) The algorithm is cost-optimal on a hypercube if plogp/n = O(1) Parallel MST Algorithm (Prim’s)

18 Dijkstra’s SSSP Algorithm b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f L [.] = 1 0 5 1  S new L[i] = Min{ L[i], L[k] + W[k, i] } where k is the newly-selected intermediate node and W[.] is the distance between k and i

19 b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f L [.] = 1 0 5 1   a b c d e f new L [.] = 1 0 3 1 5  Dijkstra’s SSSP Algorithm new L[i] = Min{ L[i], L[k] + W[k, i] } where k is the newly-selected intermediate node and W[.] is the distance between k and i

20 b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f L [.] = 1 0 3 1 5  a b c d e f new L [.] = 1 0 3 1 5 3 Dijkstra’s SSSP Algorithm new L[i] = Min{ L[i], L[k] + W[k, i] } where k is the newly-selected intermediate node and W[.] is the distance between k and i

21 b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f L [.] = 1 0 3 1 5 3 a b c d e f new L [.] = 1 0 3 1 4 3 Dijkstra’s SSSP Algorithm new L[i] = Min{ L[i], L[k] + W[k, i] } where k is the newly-selected intermediate node and W[.] is the distance between k and i

22 b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f L [.] = 1 0 3 1 4 3 a b c d e f new L [.] = 1 0 3 1 4 3 Dijkstra’s SSSP Algorithm new L[i] = Min{ L[i], L[k] + W[k, i] } where k is the newly-selected intermediate node and W[.] is the distance between k and i

23 b a d c f e 1 1 1 5 5 3 2 4 2 a b c d e f a 0 1 3   2 b 1 0 5 1   c 3 5 0 2 1  d  1 2 0 4  e   1 4 0 5 f 2    5 0 a b c d e f L [.] = 1 0 3 1 4 3 a b c d e f new L [.] = 1 0 3 1 4 3 Dijkstra’s SSSP Algorithm new L[i] = Min{ L[i], L[k] + W[k, i] } where k is the newly-selected intermediate node and W[.] is the distance between k and i

24 Task Partitioning for Parallel SSSP Algorithm P processors, n=|V| vertices Each processor is assigned n/p vertices (P i gets the set V i ) Each PE holds the n/p columns of A and n/p elements of L[] array as shown below... L[.] A P0P0P0P0 P1P1P1P1 PiPiPiPi P P p-1 | | | ….. n/p columns

25 1. Initialize: Vt := {r}; L[k] =  for all k except L[r] = 0; 2. P 0 broadcasts selectedV = r using one-to-all broadcast. 3. The PE responsible for "selectedV" marks it as belonging to set Vt. 4. For v = 2 to n=|V| do 5. Each P i updates L[k] = Min[ L[k], L(selectedV)+W(selectedV, k) ] for  k  Vi 6. Each P i computes MIN-Li = (minimum L[.] value among its unselected elements) 7. PEs perform a "global minimum" using MIN-Li values and result is stored in P 0. Call the winning vertex, selectedV. 8. P 0 broadcasts "selectedV" and L[selectedV] using one-to-all broadcast. 9. The PE responsible for "selectedV" marks it as belonging to set Vt. 10. EndFor Parallel SSSP Algorithm (Dijkstra’s)

26 TIME COMPLEXITY ANALYSIS : In the worst-case, T seq = n 2 (Hypercube) T par = n*(n/p) + n*logp computation + communication (Mesh) T par = n*(n/p) + n * Sqrt(p) The algorithm is cost-optimal on a hypercube if plogp/n = O(1) Parallel SSSP Algorithm (Dijkstra’s)


Download ppt "Minimum-Cost Spanning Tree weighted connected undirected graph cost of spanning tree is sum of edge costs find spanning tree that has minimum cost Network."

Similar presentations


Ads by Google