Professional Documents
Culture Documents
Adc 9
Adc 9
Course 9
Overview
Minimum Spanning Trees
Generic Algorithm
Kruskal’s Algorithm
Disjoint Sets
Prim’s Algorithm
Fibonacci Heaps
Spanning Trees
G(V, E) undirected, connected and weighted graph
Optimization problem
Does it have optimal substructure?
Are the sub-solutions optimal as well?
Maybe greedy or dynamic programming
If there are two edges with the same weight => probably
there are more MSTs
A graph that has the same weight for all the edges => all
the spanning trees have the same cost
Example 1st MST I
3 5
A J
Two MST of the graph 2
G
2
The dotted edges are not
9 8 6 4 8
K
B C
I D E L
2
3 5 9
J F
A
2
G
2
9 8 I
6 4 8
K 2nd MST 5
B C 3 J
5 A
2
H 7 G
8 1 2
9 8 6 4 8
D E L K
2 B C
9 5
F
H 7
8 1
D E L
2
9
F
MST – Applications
Computer networks
Road infrastructure
Other networks
Optimal sub-structure!
MST – Generic Algorithm
Follows directly from the presented solution
The loop invariant is respected
However, it does not provide a way to select the safe
edges => the algorithm is not fully specified
Need to extend it in order to determine how to find the
safe edges
GENERIC-MST(G, w)
A=
WHILE (|A| < |V| – 1)
find an edge (u, v) that is safe for A
A = A U {(u, v)}
RETURN A
Finding Safe Edges
If A =
The edge with the lowest cost in G is safe for A =
If A !=
Let S V the set of vertices covered by the edges in A
V \ S is not empty
The edge (c, f), cS, fV \ S, that has the minimum cost from all
the edges that have one endpoint in S and the other one in V \S
9
G
2
AI-3 9
G
2
8 8
B
6 4 8
K GH-4 B
6 4 8
K
5 5
C BC-5 C
H H
8 1
7
IJ-5 8 1
7
D
E
L
AH-6 D
E
L
9 2
F KL-7 9 2
F
BG-8
CD-8
IL-8
AB-9
Exemplu (III)
CE -1
EF -2
I
3
I
5
AG-2 3 5
J J
A JK-2 A
2 2
G
2
AI-3 9
G
2
9 8 8
6 4 8
K
GH-4 B
6 4 8
K
B 5 5
C BC-5 C
H H
8 1
7
IJ-5 8 1
7
D
E
L
AH-6 D
E
L
9 2
F
KL-7 9 2
F
BG-8
CD-8
IL-8
AB-9
Exemplu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-2
I
3
I
5
AG-2 3 5
J J
A JK-2 A
2
2
G
2
AI-3 9
G
2
9 8 8
6 4 8
K
GH-4 B
6 4 8
K
B 5 5
C BC-5 C
H H
8 1
7
IJ-5 8 1
7
D
E
L
AH-6 D
E
L
9 2
F
KL-7 9 2
F
BG-8
CD-8
IL-8
AB-9
Comparison Prim - Kruskal
I I
3 5 5
J 3 J
A A
2 2
G G
9 8 2 2
6 8 9 8
4 6 4 8
B K K
5 B 5
C C
H 7 H
8 1 7
8 1
E E
D L D
2 L
9 9 2
F F
Disjoint Sets
http://en.wikipedia.org/wiki/Disjoint-set_data_structure
We want to partition the vertices of the graph into a number
of separate and non-overlapping sets
To remember the connected components in the partial MST tree
Operations:
MAKE-SET(u): creates a set with a single element u
FIND-SET(u): finds the set that u is part of (usually returns the
representative element of that set, e.g. an ID of each set)
UNION(u, v): merges two distinct sets into a single one (need to
move all the elements of a set into the other one, in the end all the
elements in the new set must have the same representative)
Alternatives for Disjoint Sets
Can be implemented using lists, arrays, forest of trees and
forest of trees + heuristics
A B C D E F G H I J K L
0 1 1 1 1 1 1 1 0 0 0 0
I
3 5
J
A
2
G
9 8 2
6 4 8
B K
5
C
H 7
8 1
D
E L
9 2 F
Arrays as Disjoint Sets
Complexity?
MAKE-SET(u): (1)
FIND-SET(u): (1)
Just return set[u]
UNION(u, v): (n)
Have to walk through all the elements of the smallest disjoint set and
change their representative to the one of the highest disjoint set!
Kruskal complexity?
(m*logm + m + n2) = (m*logm + n2)
Want better!
Forest of Trees as Disjoint Sets
Use a forest of trees
One tree for each disjoint set
The representative of the disjoint set is the root element of
each tree
Complexity?
MAKE-SET(u): (1)
FIND-SET(u): (max_height)
Need to return the root element
Start from u and walk up to the root
UNION(u, v): (max_height)
Need to append all the elements in one tree to the other tree
Just make the root of the first tree point to an element in the second
tree (the root of the second tree or even to v)
But for this we need to find the root of the first tree
Forest of Trees as Disjoint Sets (2)
But, in the worst case
When unions are not made very wisely
max_height of a tree is O(n)
Therefore, the complexity of the two operations is O(n)
How?
Make all the elements on the path from u up to the root
of the tree point directly to the root
Thus, when we call FIND-SET for these elements, we can
return the root in (1)
I I
A J
A J
K K L
L
Forests with Both Heuristics
When using forests with union-by-rank and path-compression,
the average time of any operation on the disjoint set structure
(FIND-SET, UNION) is:
(α(n)) = (1) even for n – very large
α(n) = Ack-1(n, n)
Kruskal complexity?
(m*logm + m + n) = (m*logm + n) = (m*logn) WHY?
Prim’s Algorithm
Instead of building the partial MST in different connected
components
Build the partial MST in a single connected component S
Always consider the cut (S, V \ S) and choose the light
edge for this cut
Easier to implement?
Easier to understand?
I
3 5
J
A
2
G
2
Q: G(2), J(5), H(6),
9 8
6 4 8
K
L(8), B(9), C(∞), D(∞),
B 5
C
E(∞), F(∞), K(∞) G
H 7
8 1
E
D L
9 2
F
Exemplu (III)
I
3 5
J
A
2
G
2
Q: J(5), L(8), B(8),
9 8
6 4 8
K
C(∞), D(∞), E(∞), F(∞),
B 5
C
K(∞) J
H 7
8 1
E
D L
9 2
F
Exemplu (V)
I
3 5
J
A
2
G
2
Q: K(2), L(8), B(8),
9 8
6 4 8
K
C(∞), D(∞), E(∞), F(∞)
B 5
C
K
H 7
8 1
E
D L
9 2
F
Exemplu (VI)
I
3 5
J
A
2
G
Q: B(8), C(∞), D(∞),
9 8
6 8
2 E(∞), F(∞) B
4
B K
5
C
H 7
8 1
E
D L
9 2
F
Exemplu (VIII)
I
3 5
J
A
2
G
Q: C(5), D(∞), E(∞),
9 8
6 8
2 F(∞) C
4
B K
5
C
H 7
8 1
E
D L
9 2
F
Exemplu (IX)
I
3 5
J
A
2
G
Q: E(1), D(8), F(∞)
9 8
6 8
2 E
4
B K
5
C
H 7
8 1
E
D L
9 2
F
Exemplu (X)
I
3 5
J
A
2
G
Q: F(2), D(8) F
9 8 2
6 4 8
B K
5
C
H 7
8 1
E
D L
9 2
F
Exemplu (XI)
I
3 5
J
A
2
G
Q: D(8) D
9 8 2
6 4 8
B K
5
C
H 7
8 1
E
D L
9 2
F
Exemplu (XII)
I
3 5
J
A
2
G
Q: Ø
9 8 2
6 4 8
B K
5
C
H 7
8 1
E
D L
9 2
F
References
CLRS – Chapter 24