Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kruskal’s Minimum Spanning Tree Algorithm

Similar presentations


Presentation on theme: "Kruskal’s Minimum Spanning Tree Algorithm"— Presentation transcript:

1 Kruskal’s Minimum Spanning Tree Algorithm

2 Kruskal’s Algorithm Select edges in the order of smallest weights and accept an edge if it does not cause a cycle. Kruskal’s algorithm maintains a forest of trees. Initially each vertex is a tree with single node There are |V| trees. Then, adding an accepted edge merges two trees in the forest When algorithm terminates, there is a single tree with |V| vertices and it is a minimum spanning tree.

3 Initial Forest (all vertices are disjoint sets)
2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

4 Initial Forest Candidate edges are shown (edges that have low cost and edges that connect two trees) Step 1 v2 v5 v3 v6 v7 4 2 1 5 8 7 3 10 6 v1 v4

5 Initial Forest Step 2 Accept one of the candidate edges: (v1, v4)
(we can do random accept here). 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

6 Initial Forest Step 3 Merge the two trees connected by that edge.
Obtain a new tree in this way. 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

7 Repeat previous steps! Edge (v6-v7) is accepted.
2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

8 Merge the two trees connected by that edge!
2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

9 Accept edge (v1, v2) 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

10 Merge the two trees connected by that edge!
2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

11 Accept edge (v3, v4) 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

12 Merge the two trees connected by that edge!
2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

13 Accept edge (v4, v7) 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

14 Merge the two trees connected by that edge!
2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

15 Accept edge (v7, v5) 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

16 Merge the two trees connected by that edge!
2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7

17 The resulting MST is shown below!
Finished! The resulting MST is shown below! 2 v1 v2 1 2 v3 v4 v5 4 6 v6 1 v7

18 Kruskal’s Algorithm How is it different from Prim’s algorithm?
Prim’s algorithm grows one tree all the time Kruskal’s algorithm grows multiple trees (i.e., a forest) at the same time. Trees are merged together using safe edges Since an MST has exactly |V| - 1 edges, after |V| - 1 merges, we would have only one component u v tree1 tree2

19 Kruskal’s Algorithm Start with each vertex being its own component
d e h g f i 4 8 7 11 1 2 14 9 10 6 Start with each vertex being its own component Repeatedly merge two components into one by choosing the light edge that connects them Which components to consider at each iteration? Scan the set of edges in monotonically increasing order by weight We would add edge (c, f)

20 Example Add (h, g) Add (c, i) Add (g, f) Add (a, b) Add (c, f)
Ignore (i, g) Add (c, d) Ignore (i, h) Add (a, h) Ignore (b, c) Add (d, e) Ignore (e, f) Ignore (b, h) Ignore (d, f) {g, h}, {a}, {b}, {c}, {d}, {e}, {f}, {i} {g, h}, {c, i}, {a}, {b}, {d}, {e}, {f} {g, h, f}, {c, i}, {a}, {b}, {d}, {e} {g, h, f}, {c, i}, {a, b}, {d}, {e} {g, h, f, c, i}, {a, b}, {d}, {e} {g, h, f, c, i, d}, {a, b}, {e} {g, h, f, c, i, d, a, b}, {e} {g, h, f, c, i, d, a, b, e} a b c d e h g f i 4 8 7 11 1 2 14 9 10 6 {a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}

21 Implementation of Kruskal’s Algorithm
Uses a disjoint-set data structure to determine whether an edge connects vertices in different components

22 Operations on Disjoint Data Sets
MAKE-SET(u) – creates a new set whose only member is u FIND-SET(u) – returns a representative element from the set that contains u Any of the elements of the set that has a particular property E.g.: Su = {r, s, t, u}, the property is that the element be the first one alphabetically FIND-SET(u) = r FIND-SET(s) = r FIND-SET has to return the same value for a given set

23 Operations on Disjoint Data Sets
UNION(u, v) – unites the dynamic sets that contain u and v, say Su and Sv E.g.: Su = {r, s, t, u}, Sv = {v, x, y} UNION (u, v) = {r, s, t, u, v, x, y} Running time for FIND-SET and UNION depends on implementation.

24 KRUSKAL(V, E, w) (cont.) A ←  for each vertex v  V do MAKE-SET(v)
sort E into non-decreasing order by w for each (u, v) taken from the sorted list do if FIND-SET(u)  FIND-SET(v) then A ← A  {(u, v)} UNION(u, v) return A - Running time: O(V+ElgE+ElgV)=O(ElgE) - Since E=O(V2), we have lgE=O(2lgV)=O(lgV) O(V) O(ElgE) O(E) O(lgV) O(ElgV)

25 Kruskal’s Algorithm Kruskal’s algorithm is a “greedy” algorithm
Kruskal’s greedy strategy produces a globally optimum solution u v S V - S x y

26 Which one is more complex
O( |E| ) O( |O| ) Depends on the number of the edges


Download ppt "Kruskal’s Minimum Spanning Tree Algorithm"

Similar presentations


Ads by Google