Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
Tiny os
Next
Download to read offline and view in fullscreen.

2

Share

Prim Algorithm and kruskal algorithm

Download to read offline

Design and analysis of algorithms

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Prim Algorithm and kruskal algorithm

  1. 1. Minimum Spanning Trees Text • Read Weiss, §9.5 Prim’s Algorithm • Weiss §9.5.1 • Similar to Dijkstra’s Algorithm Kruskal’s Algorithm • Weiss §9.5.2 • Focuses on edges, rather than nodes
  2. 2. Definition • A Minimum Spanning Tree (MST) is a subgraph of an undirected graph such that the subgraph spans (includes) all nodes, is connected, is acyclic, and has minimum total edge weight
  3. 3. Algorithm Characteristics • Both Prim’s and Kruskal’s Algorithms work with undirected graphs • Both work with weighted and unweighted graphs but are more interesting when edges are weighted • Both are greedy algorithms that produce optimal solutions
  4. 4. Prim’s Algorithm • Similar to Dijkstra’s Algorithm except that dv records edge weights, not path lengths
  5. 5. Walk-Through Initialize array K dv pv A F ∞ − B F ∞ − C F ∞ − D F ∞ − E F ∞ − F F ∞ − G F ∞ − H F ∞ − 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 2
  6. 6. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Start with any node, say D K dv pv A B C D T 0 − E F G H 2
  7. 7. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B C 3 D D T 0 − E 25 D F 18 D G 2 D H 2
  8. 8. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B C 3 D D T 0 − E 25 D F 18 D G T 2 D H 2
  9. 9. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B C 3 D D T 0 − E 7 G F 18 D G T 2 D H 3 G 2
  10. 10. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B C T 3 D D T 0 − E 7 G F 18 D G T 2 D H 3 G 2
  11. 11. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B 4 C C T 3 D D T 0 − E 7 G F 3 C G T 2 D H 3 G 2
  12. 12. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B 4 C C T 3 D D T 0 − E 7 G F T 3 C G T 2 D H 3 G 2
  13. 13. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 10 F B 4 C C T 3 D D T 0 − E 2 F F T 3 C G T 2 D H 3 G 2
  14. 14. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A 10 F B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H 3 G 2
  15. 15. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 10 F B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H 3 G 2 Table entries unchanged
  16. 16. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A 10 F B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2
  17. 17. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 4 H B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2
  18. 18. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A T 4 H B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2
  19. 19. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A T 4 H B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2 Table entries unchanged
  20. 20. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A T 4 H B T 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2
  21. 21. 4 A H B F E D C G 2 3 4 3 3 Cost of Minimum Spanning Tree = Σ dv = 21 K dv pv A T 4 H B T 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2 Done
  22. 22. Kruskal’s Algorithm • Work with edges, rather than nodes • Two steps: – Sort edges by increasing edge weight – Select the first |V| – 1 edges that do not generate a cycle
  23. 23. Walk-Through Consider an undirected, weight graph 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10
  24. 24. Sort the edges by increasing edge weight edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  25. 25. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  26. 26. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  27. 27. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Accepting edge (E,G) would create a cycle
  28. 28. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  29. 29. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  30. 30. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  31. 31. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  32. 32. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 χ (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  33. 33. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 χ (B,F) 4 χ (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  34. 34. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 χ (B,F) 4 χ (B,H) 4 χ (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  35. 35. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 χ (B,F) 4 χ (B,H) 4 χ (A,H) 5 √ (D,F) 6 (A,B) 8 (A,F) 10
  36. 36. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 2 3 3 3 edge dv (B,E) 4 χ (B,F) 4 χ (B,H) 4 χ (A,H) 5 √ (D,F) 6 (A,B) 8 (A,F) 10 Done Total Cost = Σ dv = 21 4 }not considered
  • LourdumarieSophie

    Nov. 18, 2017
  • GuntupalliNagamalli

    Jul. 17, 2017

Design and analysis of algorithms

Views

Total views

888

On Slideshare

0

From embeds

0

Number of embeds

3

Actions

Downloads

42

Shares

0

Comments

0

Likes

2

×