Presentation is loading. Please wait.

Presentation is loading. Please wait.

Minimum Spanning Trees. a b d f g e c a b d f g e c.

Similar presentations


Presentation on theme: "Minimum Spanning Trees. a b d f g e c a b d f g e c."— Presentation transcript:

1 Minimum Spanning Trees

2 a b d f g e c

3 a b d f g e c

4 a b d f g e c

5 a b d f g e c 0 2 -3 5 5 7 0 5 8 0

6 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 Minimum spanning tree weight = 0

7 Systematic Approach: -Grow a spanning tree A -Always add a safe edge to A that we know belongs to some minimal spanning tree

8 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R

9 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R There must go exactly one edge from b to the minimum spanning tree. Which edge should we choose? Are all three a possibility (maybe suboptimal)? Could the 7 edge be optimal to choose? What about the two 5 edges?

10 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R There must go exactly one edge from b to the spanning tree. Which edge should we choose? Are all three a possibility? Yes, every vertex will connect to the tree Could the 7 edge be optimal to choose? No, we must connect with a light edge What about the two 5 edges? The red is possible, but they both are !!

11 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’

12 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’

13 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’

14 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’

15 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’

16 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’

17 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’

18 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’

19 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’’

20 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’’

21 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’’’

22 Kruskal’s Algorithm

23 Idea: Rethink the previous algorithm from vertices to edges -Initially each vertex forms a singleton tree -In each iteration add the lightest edge connecting two trees in the forest.

24 a b d f g e c 0 2 -3 5 5 7 0 5 8 0

25 a b d f g e c 0 2 -3 5 5 7 0 5 8 0

26 a b d f g e c 0 2 -3 5 5 7 0 5 8 0

27 a b d f g e c 0 2 -3 5 5 7 0 5 8 0

28 a b d f g e c 0 2 -3 5 5 7 0 5 8 0

29 a b d f g e c 0 2 -3 5 5 7 0 5 8 0

30 a b d f g e c 0 2 -3 5 5 7 0 5 8 0

31 a b d f g e c 0 2 -3 5 5 7 0 5 8 0

32 a b d f g e c 0 2 -3 5 5 7 0 5 8 0 R’’’

33 MSTkruskal(G,w) 1 A Ø 2 for each vertex v V[G] 3 do MakeSet(v) 4 sort the edges of E into nondecreasing order by weight w 5 for each edge (u,v) E, taken in nondecreasing order by weight 6 do if FindSet(u) FindSet(v) 7 then A A {(u,v)} 8 Union(u,v) 9 return A

34 MSTkruskal(G,w) 1 A Ø 2 for each vertex v V[G] 3 do MakeSet(v) 4 sort the edges of E into nondecreasing order by weight w 5 for each edge (u,v) E, taken in nondecreasing order by weight 6 do if FindSet(u) FindSet(v) 7 then A A {(u,v)} 8 Union(u,v) 9 return A Time?

35 MSTkruskal(G,w) 1 A Ø 2 for each vertex v V[G] 3 do MakeSet(v) 4 sort the edges of E into nondecreasing order by weight w 5 for each edge (u,v) E, taken in nondecreasing order by weight 6 do if FindSet(u) FindSet(v) 7 then A A {(u,v)} 8 Union(u,v) 9 return A Time? O(E*lgE)

36 MSTkruskal(G,w) 1 A Ø 2 for each vertex v V[G] 3 do MakeSet(v) 4 sort the edges of E into nondecreasing order by weight w 5 for each edge (u,v) E, taken in nondecreasing order by weight 6 do if FindSet(u) FindSet(v) 7 then A A {(u,v)} 8 Union(u,v) 9 return A Time? O(E*lgE) Theorem 21.13: m FindSet, Union, and MakeSet operations of which n are MakeSet operations takes O(m*α(n))

37 MSTkruskal(G,w) 1 A Ø 2 for each vertex v V[G] 3 do MakeSet(v) 4 sort the edges of E into nondecreasing order by weight w 5 for each edge (u,v) E, taken in nondecreasing order by weight 6 do if FindSet(u) FindSet(v) 7 then A A {(u,v)} 8 Union(u,v) 9 return A Time? O(E*lg(E)) O(V+E*α(V)) = O(V+E*lg(V)) = O(V+E*lg(E)) = O(E*lg(E))

38 MSTkruskal(G,w) 1 A Ø 2 for each vertex v V[G] 3 do MakeSet(v) 4 sort the edges of E into nondecreasing order by weight w 5 for each edge (u,v) E, taken in nondecreasing order by weight 6 do if FindSet(u) FindSet(v) 7 then A A {(u,v)} 8 Union(u,v) 9 return A O(E*lg(E)) O(V+E*α(V)) = O(V+E*lg(V)) = O(V+E*lg(E)) = O(E*lg(E))

39 a b d f g e c 0 2 -3 5 5 7 1 5 8 0 R Obviously tree. Which edge should we choose?


Download ppt "Minimum Spanning Trees. a b d f g e c a b d f g e c."

Similar presentations


Ads by Google