08 CS316 - Algorithms - Graph 2 - MST
08 CS316 - Algorithms - Graph 2 - MST
Central office
WIRING: NAÏVE APPROACH
Central office
Expensive!
WIRING: BETTER APPROACH
Central office
8 7 6
10
h g f
1 2
A town has a set of houses and a set of roads
A road connects 2 and only 2 houses
A road connecting houses u and v has a repair cost w(u, v)
PROBLEM 7
8
b c d
4 9
2
a 11 i 4 14 e
8 7 6
10
h g f
1 2
Goal: Repair enough (and no more) roads such that:
1. Everyone stays connected
i.e., can reach every house from all other houses
2. Total repair cost is minimum
MINIMUM SPANNING TREES
A connected, undirected graph:
c 3
d
1 1
a b a b
3 3
4 2 4 2
c 3
d c 3
d
Minimum spanning tree is not unique
Number of edges in a MST: |V| - 1
APPLICATIONS OF MST
Any time you want to visit all vertices in a graph at
minimum cost
At each step: 8 7
b c d
4
• Find a light edge crossing (VA, V - VA) 2
9
a 11 i 4 14 e
• Add this edge to A 7 6
8 10
• Repeat until the tree spans all vertices h
1
g 2
f
HOW TO FIND LIGHT EDGES QUICKLY?
Use a priority queue Q contains vertices not yet included in the
tree, i.e., (V – VA)
• EX: VA = {a, b}, Q = {c, d, e, f, g, h, i}
We associate a key with each vertex v:
key[v] = minimum weight of any edge (u, v) connecting v to VA
8 7
b c d
4 9
2
a i 4 14 e
11
7 6
8 10
Key[a]=min(8, 11) h g f
1 2
HOW TO FIND LIGHT EDGES QUICKLY?
Initially Key of v is if v is not adjacent to any vertices
in VA
8 7
b c d
4 9
2
a i 4 14 e
11
7 6
8 10
h g f
1 2
EXAMPLE
b
8
c
7
d 0
4 9
2 Q = {a, b, c, d, e, f, g, h, i}
a 11 i 4 14 e
7 6 VA =
8 10
h
1
g 2
f Extract-MIN(Q) a
4
key [b] = 4 [b] = a
8 7
b c d key [h] = 8 [h] = a
4 9
2
a 11 i 14 e
4 4 8
7 6
8 10 Q = {b, c, d, e, f, g, h, i} VA = {a}
h g 2
f
1
8 = {a, #, #, #, #,#, a,#}
Extract-MIN(Q) b
EXAMPLE
4 8
key [c] = 8 [c] = b
8 7
b c d
9
key [h] = 8 [h] = a
4
2 unchanged
a 11 i 4 14 e 8 8
7 6
8 10 Q = {c, d, e, f, g, h, i} VA = {a, b}
h g f
8
1
2
= {b, #, #, #,#, a,#}
4 8
7 Extract-MIN(Q) c
8 7 key [d] = 7 [d] = c
b c d
4 2 2 9 key [f] = 4 [f] = c
a 11 i 4 14 e key [i] = 2 [i] = c
8
7 6
10 7 4 8 2
h
1
g 2
f Q = {d, e, f, g, h, i} VA = {a, b, c}
8
4 = { c, #, c ,#, a, c}
Extract-MIN(Q) i
EXAMPLE key [h] = 7 [h] = i
8
4
8
7 key [g] = 6 [g] = i
7
b c d 7 46 8
4 9
2 2 Q = {d, e, f, g, h} VA = {a, b, c, i}
a 11 i 4 14 e
8
7 6 = { c, #, c ,i, i}
10
h g 2
f Extract-MIN(Q) f
1
87
6 4
4 8 7
key [g] = 2 [g] = f
b
8
c
7
d
key [d] = 7 [d] = c
4 9 10 unchanged
2 2
key [e] = 10 [e] = f
a 11 i 4 14 e
8
7 6 7 10 2 8
10
h g 2
f Q = {d, e, g, h} VA = {a, b, c, i, f}
1
7 6
2 4 = { c, f ,f, i}
Extract-MIN(Q) g
EXAMPLE
4 8 7 key [h] = 1 [h] = g
8 7
b c d 7 10 1
4 9
2 2 10 Q = {d, e, h} VA = {a, b, c, i, f, g}
a 11 i 4 14 e
7 6 = { c, f ,g}
8 10
h g f
Extract-MIN(Q) h
1 2
7
1 2 4
4 8 7 7 10
8 7
b c d Q = {d, e} VA = {a, b, c, i, f, g, h}
4 9
2 2 10 = { c, f}
a 11 i 14 e
7 6
4
Extract-MIN(Q) d
8 10
h g 2
f
1
1 2 4
8 7
EXAMPLE 4
b c d
9
2
a 11 i 4 14 e
7 6
8 10
4 8 7 h g 2
f
1
8 7
b c d
4 9 9
2 10
2
key [e] = 9 [e] = d
a 11 i 4 14 e
7 6 9
8 10
h g f Q = {e} VA = {a, b, c, i, f, g, h, d}
1 2
1 2 4 = { d}
Extract-MIN(Q) e
Q = VA = {a, b, c, i, f, g, h, d, e}
PRIM(V, E, W, R) In Q change the
1. Q← key of r to be 0
2. for each u V
3. do key[u] ← ∞
4. π[u] ← NIL
5. INSERT(Q, u)
6. key[r] ← 0 ► DECREASE-KEY(Q, r, 0)
7. while Q
8. do u ← EXTRACT-MIN(Q)
9. for each v Adj[u]
10. do if v Q and w(u, v)<key[v]
11. then π[v] ← u
12. DECREASE-KEY(Q, v, w(u, v))
RUNNING TIME ANALYSIS
Extract_Min Decrease_Key
Binary heap O(lg V) O(lg V)
PRIM(V, E, W, R)
1. Q←
2. for each u V Total time: O(VlgV + ElgV) = O(ElgV)
5. INSERT(Q, u)
6. key[r] ← 0 ► DECREASE-KEY(Q, r, 0) O(lgV)
8
Initially we have 9 set of vertices:
b c 7 d
4 9 {a}, {b}, {c}, {d}, {e}, {f}, {g}, {h},
2 {i}
a 11 i 4 14 e
7 6 Edges arranged in increasing order
8 10
h g f according to their weights:
1 2
1: (h, g) 2: (c, i), (g, f)
1: (h, g) 8: (a, h), (b, c)
2: (c, i), (g, f) 9: (d, e) 4: (a, b), (c, f) 6: (i, g)
4: (a, b), (c, f) 10: (e, f) 7: (c, d), (i, h) 8: (a, h), (b, c)
6: (i, g) 11: (b, h) 9: (d, e) 10: (e, f)
7: (c, d), (i, h) 14: (d, f) 11: (b, h) 14: (d, f)
{a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}
IMPLEMENTATION OF KRUSKAL’S ALGORITHM
• Uses a disjoint-set data structure to determine
whether an edge connects vertices in different
components.
• Disjoint-set: is a data structure that tracks a set of
elements partitioned into a number of disjoint
(non-overlapping) subsets.
• A disjoint-set forest consists of a number of
elements each of which stores a parent pointer.
IMPLEMENTATION OF KRUSKAL’S ALGORITHM
• If an element's parent pointer points to no other
element, then the element is the root of a tree
and is the representative member of its set.
• Each element in the set that has a particular
property representative member.
OPERATIONS ON DISJOINT DATA SETS
MAKE-SET(u) – creates a new set whose only member is u
FIND-SET(u) – returns a representative element from the set
that contains u
• E.g.: Su = {r, s, t, u}
FIND-SET(u) = r FIND-SET(s) = r
• FIND-SET has to return the same value for the elements of
the same set
OPERATIONS ON DISJOINT DATA SETS
UNION(u, v) – unites the dynamic sets that contain u and v,
say Su and Sv
u y
v
V-S
REFERENCES