390 likes | 459 Views
Minimum Spanning Trees. c. b. a. d. e. g. f. c. b. a. d. e. g. f. c. b. a. d. e. g. f. c. 5. b. -1. 2. 7. 5. a. d. 0. e. 8. 0. 5. -3. 0. -1. g. f. c. 5. b. -1. 2. 7. 5. a. d. 0. e. 8. 0. 5. -3. 0. -1. g. f.
E N D
c b a d e g f
c b a d e g f
c b a d e g f
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f Minimum spanning tree weight = 0
Systematic Approach: -Grow a spanning tree A -Always add a safe edgeto A that we know belongs to some minimal spanning tree
c 5 R b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f 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?
c 5 R b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f 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 edgeWhat about the two 5 edges? The red is possible, but they both are !!
c 5 R’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’’’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
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.
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
c 5 R’’’ b -1 2 7 5 a d 0 e 8 0 5 -3 0 -1 g f
MSTkruskal(G,w) • 1 AØ • for each vertex vV[G] • do MakeSet(v) • sort the edges of E into nondecreasing order by weight w • for each edge (u,v) E, taken in nondecreasing order by weight • doif FindSet(u) FindSet(v) • thenAA {(u,v)} • Union(u,v) • returnA
MSTkruskal(G,w) • 1 AØ • for each vertex vV[G] • do MakeSet(v) • sort the edges of E into nondecreasing order by weight w • for each edge (u,v) E, taken in nondecreasing order by weight • doif FindSet(u) FindSet(v) • thenAA {(u,v)} • Union(u,v) • returnA Time?
MSTkruskal(G,w) • 1 AØ • for each vertex vV[G] • doMakeSet(v) • sort the edges of E into nondecreasing order by weight w • for each edge (u,v) E, taken in nondecreasing order by weight • doifFindSet(u) FindSet(v) • thenAA {(u,v)} • Union(u,v) • returnA O(E*lgE) Time?
MSTkruskal(G,w) • 1 AØ • for each vertex vV[G] • doMakeSet(v) • sort the edges of E into nondecreasing order by weight w • for each edge (u,v) E, taken in nondecreasing order by weight • doifFindSet(u) FindSet(v) • thenAA {(u,v)} • Union(u,v) • returnA O(E*lgE) Theorem 21.13: m FindSet, Union, and MakeSet operations of which n are MakeSet operations takes O(m*α(n)) Time?
MSTkruskal(G,w) • 1 AØ • for each vertex vV[G] • doMakeSet(v) • sort the edges of E into nondecreasing order by weight w • for each edge (u,v) E, taken in nondecreasing order by weight • doifFindSet(u) FindSet(v) • thenAA {(u,v)} • Union(u,v) • returnA O(E*lg(E)) O(V+E*α(V)) =O(V+E*lg(V)) = O(V+E*lg(E)) =O(E*lg(E)) Time?
MSTkruskal(G,w) • 1 AØ • for each vertex vV[G] • doMakeSet(v) • sort the edges of E into nondecreasing order by weight w • for each edge (u,v) E, taken in nondecreasing order by weight • doifFindSet(u) FindSet(v) • thenAA {(u,v)} • Union(u,v) • returnA O(E*lg(E)) O(V+E*α(V)) =O(V+E*lg(V)) = O(V+E*lg(E)) =O(E*lg(E)) O(E*lg(E))
c 5 R b -1 2 7 5 a d 1 e 8 0 5 -3 0 -1 g f Obviously tree. Which edge should we choose?