08 CS316 Graph 2 MST
08 CS316 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
MINIMUM SPANNING TREE (MST)
c 3
d
c 3
d
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 9
• Find a light edge crossing (VA, V - VA) 2
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[h]=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
8
b c
7
d 0
4 9
2 Q = {a, b, c, d, e, f, g, h,
a 11 e
i 4 14 i}
7 6
8 10
VA =
h g 2
f
1
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 =
h g 2
f
1
{a}
8
= {a, #, #, #, #,#, a,#}
Extract-MIN(Q) b
EXAMPLE
4 8
key [c] = 8 [c] = b
8 7
b c d key [h] = 8 [h] = a
4 9
2 unchanged
a 11 i 4 14 e 8 8
7 6
8 10 Q = {c, d, e, f, g, h, i} VA =
h g f
1
2
{a, b}
8
4 8
7 = {b, #, #, #,#, a,#}
8 7 key [d] = 7
Extract-MIN(Q) [d]
c =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 =
8
4 {a, b, c}
= { c, #, c ,#, a, c}
EXAMPLE key [h] = 7 [h] = i
4 8
8
7 key [g] = 6 [g] = i
7
b c d 7 46 8
4 9
2 2
11
Q = {d, e, f, g, h} VA = {a,
a i 4 14 e
7 6 b, c, i}
8 10
h g f = { c, #, c ,i, i}
1 2
87
6 4 Extract-MIN(Q) f
4 8 7
key [g] = 2 [g] = f
b
8
c
7
d
key [d] = 7 [d] = c
4 9 10 unchanged
2 2
11
key [e] = 10 [e] = f
a i 4 14 e
8
7 6 7 10 2 8
10
h g 2
f Q = {d, e, g, h} VA = {a, b,
1
7 6
2 4 c, i, f}
= { c, f ,f, i}
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,
a 11 i 14 e
6
4 i, f, g}
7
8 10
= { c, f ,g}
h g 2
f
1
2 4
Extract-MIN(Q) h
7
1
4 8 7 7 10
8 7
b c d Q = {d, e} VA = {a, b, c, i,
4 9
2 2 10 f, g, h}
a 11 i 14 e
4 = { c, f}
7 6
8 10 Extract-MIN(Q) d
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 2 10
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,
1 2
1 2 4 h, d}
= { d}
Extract-MIN(Q) e
Q = VA = {a, b, c, i, f, g, h,
d, e}
PRIM(V, E, W, R) For the
Preferred to be
graph
implemented as a
1. Q ← binary heap
2. for each u V
A binary heap is a complete
3. do key[u]
binary tree which ← satisfies
∞
4. π[u] ← NIL
the heap ordering property.
In Q change the key
of r to be 0
5. INSERT(Q, u)
Thekey[r]
6. min-heap
← 0 ►property: the
DECREASE-KEY(Q, r, 0)
value of each node is greater
7. while Q
than or equal to the value of
8.
its parent.do u ← node
Root EXTRACT-MIN(Q)
hold
9.
the smallest for
keyeach v Adj[u]
element.
10. do if v Q and w(u, v)<key[v]
A heap with N nodes then
11. alwaysπ[v] ← u
has O(log N) height.
12. DECREASE-KEY(Q, v,
w(u, v))
RUNNING TIME ANALYSIS
Extract_Mi Decrease_K
n ey
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)
Min-heap
7. while Q Executed |V| times
operations:
8. do u ← EXTRACT-MIN(Q) Takes O(lgV) O(VlgV)
9. for each v Adj[u] Executed O(E) times total
10. do if v Q and w(u, v)<key[v] Constant O(ElgV)
12. DECREASE-KEY(Q, v,
w(u, v))
PRIM’S ALGORITHM
Prim’s algorithm is a “greedy” algorithm
u y
v
V-S
REFERENCES