minimum spanning trees. a b d f g e c a b d f g e c

Post on 22-Dec-2015

222 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Minimum Spanning Trees

a

b

d

f g

e

c

a

b

d

f g

e

c

a

b

d

f g

e

c

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

Minimum spanning tree weight = 0

Systematic Approach:

-Grow a spanning tree A

-Always add a safe edge to A that we know belongs to some minimal spanning tree

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

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?

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

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 edgeWhat about the two 5 edges? The red is possible, but they both are !!

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’’

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’’’

Kruskal’s Algorithm

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.

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

0

5

-1

8

0

R’’’

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

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?

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)

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))

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))

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(E*lg(E))

O(V+E*α(V)) =O(V+E*lg(V)) = O(V+E*lg(E)) =O(E*lg(E))

a

b

d

f g

e

c

0

2

-3

-1

5

5

7

1

5

-1

8

0

R

Obviously tree. Which edge should we choose?

top related