Chapter 23 Minimum Spanning Trees

advertisement
Chapter 23
Minimum Spanning Trees
Minimum Spanning Trees
• Find a minimum-cost set of edges that connect all
vertices of a graph
• Applications
– Connect “nodes” with a minimum of “wire”
• Networking
• Circuit design
2
Minimum Spanning Trees
• Find a minimum-cost set of edges that connect all
vertices of a graph
• Applications
– Collect nearby nodes
• Clustering, taxonomy construction
3
Minimum Spanning Trees
• Find a minimum-cost set of edges that connect all
vertices of a graph
• Applications
– Approximating graphs
4
Minimum Spanning Trees
• A tree is an acyclic, undirected, connected graph
• A spanning tree of a graph is a tree containing all
vertices from the graph
• A minimum spanning tree is a spanning tree,
where the sum of the weights on the tree’s edges
are minimal
5
Minimum Spanning Trees
• A minimum spanning tree is a spanning tree,
where the sum of the weights on the tree’s edges
are minimal
6
Minimum Spanning Trees
• Problem formulation
– Given an undirected, weighted graph
with
weights
for each edge
– Find an acyclic subset
that connects all of the
vertices and minimizes the total weight:
– The minimum spanning tree is
• Minimum spanning tree may be not unique (can be more than
one)
7
Minimum Spanning Trees
• Both Kruskal’s and Prim’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
8
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
– Work with edges
– Two steps:
• Sort edges by increasing edge weight
• Select the first |V| - 1 edges that do not generate a cycle
– Walk through:
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
9
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Sort the edges by increasing edge weight
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv
edge
dv
(D,E)
1
(B,E)
4
(D,G)
2
(B,F)
4
(E,G)
3
(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
10
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv
(B,E)
4
2
(B,F)
4
(E,G)
3
(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)

11
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4

(B,F)
4
3
(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
2
(E,G)
12
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
Accepting edge (E,G) would create a cycle
13
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
14
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
15
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
16
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
17
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)

18
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
19
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4

(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
20
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
3
F
10
C
4
A
3
4
8
6
5
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4

(C,D)
3

(A,H)
5

(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
21
Minimum Spanning Trees
• Solution 1: Kruskal’s algorithm
Select first |V|–1 edges which do not
generate a cycle
F
C
3
A
4
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4

(C,D)
3

(A,H)
5

(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
5
B
H
2
D
1
3
G
E
}
not
considered
3
Done
Total Cost = 21
22
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
– Work with nodes (instead of edges)
– Two steps
• Select node with minimum distance
• Update distances of adjacent, unselected nodes
– Walk through:
2
3
F
10
C
7
A
3
4
8
18
4
B
9
D
10
H
25
2
3
G
7
E
23
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
2
Initialize array
3
F
10
7
A
3
4
8
18
4
B
9
D
10
H
25
2
3
G
K
dv
pv
A
F


B
F


C
F


D
F


E
F


F
F


G
F


H
F


C
7
E
K : whether in the tree
dv : distance to the tree
pv : closest node that is in the tree
24
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
2
Start with any node, say D
3
F
10
C
7
A
4
B
9
D
10
H
25
2
pv
T
0

B
18
4
dv
A
3
8
K
C
D
E
3
G
7
E
F
G
H
25
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Update distances of
adjacent, unselected nodes
2
3
F
10
K
7
A
4
3
D
0

E
25
D
F
18
D
G
2
D
B
18
4
B
D
10
H
25
2
3
G
pv
A
3
8
9
dv
C
7
E
C
D
T
H
26
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Select node with
minimum distance
2
3
F
10
K
7
A
4
3
D
0

E
25
D
F
18
D
2
D
B
18
4
B
D
10
H
25
2
3
G
pv
A
3
8
9
dv
C
7
E
C
D
G
T
T
H
27
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Update distances of
adjacent, unselected nodes
2
3
F
10
K
7
A
4
3
D
0

E
7
G
F
18
D
2
D
3
G
B
18
4
B
D
10
H
25
2
3
G
pv
A
3
8
9
dv
C
7
E
C
D
G
H
T
T
28
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Select node with
minimum distance
2
3
F
10
7
A
4
pv
C
T
3
D
D
T
0

E
7
G
F
18
D
2
D
3
G
B
18
4
B
D
10
H
25
2
3
G
dv
A
3
8
9
K
C
7
E
G
H
T
29
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Update distances of
adjacent, unselected nodes
2
3
F
10
K
C
7
A
B
18
4
B
9
D
10
H
25
2
3
G
7
E
pv
4
C
A
3
4
8
dv
C
T
3
D
D
T
0

E
7
G
F
3
C
2
D
3
G
G
H
T
30
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Select node with
minimum distance
2
3
F
10
K
C
7
A
4
B
18
4
B
9
D
10
H
25
2
G
7
E
4
C
C
T
3
D
D
T
0

7
G
E
3
pv
A
3
8
dv
F
T
3
C
G
T
2
D
3
G
H
31
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Update distances of
adjacent, unselected nodes
2
3
F
10
K
7
A
3
4
8
18
4
B
9
dv
pv
A
10
F
B
4
C
C
D
10
H
25
2
C
T
3
D
D
T
0

2
F
E
3
G
7
E
F
T
3
C
G
T
2
D
3
G
H
32
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Select node with
minimum distance
2
3
F
10
K
7
A
3
4
8
18
4
B
9
D
10
H
25
2
3
G
dv
pv
A
10
F
B
4
C
C
7
E
C
T
3
D
D
T
0

E
T
2
F
F
T
3
C
G
T
2
D
3
G
H
33
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Update distances of
adjacent, unselected nodes
2
3
F
10
K
7
A
3
4
8
18
4
B
9
D
10
H
25
2
3
G
dv
pv
A
10
F
B
4
C
C
7
E
C
T
3
D
D
T
0

E
T
2
F
F
T
3
C
G
T
2
D
3
G
H
Table entries unchanged
34
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Select node with
minimum distance
2
3
F
10
K
7
A
3
4
8
18
4
B
9
D
10
H
25
2
3
G
dv
pv
A
10
F
B
4
C
C
7
E
C
T
3
D
D
T
0

E
T
2
F
F
T
3
C
G
T
2
D
H
T
3
G
35
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Update distances of
adjacent, unselected nodes
2
3
F
10
K
7
A
3
4
8
18
4
B
9
D
10
H
25
2
3
G
dv
pv
A
4
H
B
4
C
C
7
E
C
T
3
D
D
T
0

E
T
2
F
F
T
3
C
G
T
2
D
H
T
3
G
36
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Select node with
minimum distance
2
3
F
10
C
7
A
A
3
4
8
B
9
D
10
H
25
2
3
G
7
dv
pv
T
4
H
4
C
B
18
4
K
E
C
T
3
D
D
T
0

E
T
2
F
F
T
3
C
G
T
2
D
H
T
3
G
37
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Update distances of
adjacent, unselected nodes
2
3
F
10
C
7
A
A
3
4
8
B
9
D
10
H
25
2
3
G
7
dv
pv
T
4
H
4
C
B
18
4
K
E
C
T
3
D
D
T
0

E
T
2
F
F
T
3
C
G
T
2
D
H
T
3
G
Table entries unchanged
38
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Select node with
minimum distance
2
3
F
10
7
A
3
4
8
18
4
B
9
D
10
H
25
2
3
G
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
C
7
E
39
Minimum Spanning Trees
• Solution 2: Prim’s algorithm
Cost of Minimum
Spanning Tree = 21
2
3
F
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
C
3
A
4
4
B
D
H
2
3
G
E
Done
40
Minimum Spanning Trees
• Runtime
– When using binary heaps, the runtime of the Kruskal’s
algorithm is
– When using binary heaps, the runtime of the Prim’s
algorithm is
When using the Fibonacci heaps, the runtime of the
Prim’s algorithm becomes:
– So, when an undirected graph is dense (i.e.,
is
much small than
), the Prim’s algorithm is more
efficient
41
Download