Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

2/18/2009

1
CS 312: Algorithm Analysis
Lecture #19: Minimal Spanning
Trees; Prims Algorithm
Announcements
Homework #12 due now
Project #4 - Intelligent scissors
Howd the help session go? Howd the help session go?
Early: next Wednesday
Due: next Friday
Start early, finish early!
Objectives
More Greedy Algorithms
Implementation of Disjoint Sets
Analysis of Kruskals
Prims Algorithm for MST
Analysis of Prims
Kruskals Algorithm
Data Structure For Disjoint Sets
makeset(x): create a singleton set
containing just x
fi d( ) t hi h t d b l ? find(x): to which set does x belong?
union(x,y): merge the sets containing x
and y
Disjoint Sets as Directed-Trees
{B,E}
{A,C,D,F,G,H}
2/18/2009
2
Disjoint Set Operations Example Disjoint Set Ops
Example Disjoint Set Ops
Analyze: Disjoint Set Operations Kruskals Algorithm
Lemma: O(log|V|)=O(log|E|)
Proof:
2/18/2009
3
Prims Algorithm
Idea:
Intermediate set of edges X always forms a sub-tree
S is chosen to be the set of Xs vertices.
On each iteration, X grows by one edge
namely the lightest edge between a vertex in S and a vertex
outside S.
Greedy?
Greedy Schema for MST
Prims Algorithm
Choose arbitrary starting vertex
1 2 3
1 2
4
6
4
5
6
S ={5}
4 5 6
3 8
7
4
6
4
5
6
7
3 4
Prims Algorithm
Choose arbitrary starting vertex
1 2 3
1 2
4
6
4
5
6
S ={5} X :
4 5 6
3 8
7
4
6
4
5
6
7
3 4
Prims Algorithm
Pick shortest edge to leave S
1 2 3
1 2
4
6
4
5
6
S ={5}
{4,5}
X :
4 5 6
3 8
7
4
6
4
5
6
7
3 4
Prims Algorithm
Add connected vertex to S
1 2 3
1 2
4
6
4
5
6
S ={5}
{4,5} S ={4,5}
X :
4 5 6
3 8
7
4
6
4
5
6
7
3 4
2/18/2009
4
Prims Algorithm
Add connected vertex to S
1 2 3
1 2
4
6
4
5
6
S ={5}
{4,5} S ={4,5}
X :
4 5 6
3 8
7
4
6
4
5
6
7
3 4
Prims Algorithm
Repeat until S =V
1 2 3
1 2
4
6
4
5
6
S ={5}
{4,5} S ={4,5}
{14} S {145}
X :
4 5 6
3 8
7
4
6
4
5
6
7
3 4
{1,4} S ={1,4,5}
Prims Algorithm
Repeat until S =V
1 2 3
1 2
4
6
4
5
6
S ={5}
{4,5} S ={4,5}
{14} S {145}
X :
4 5 6
3 8
7
4
6
4
5
6
7
3 4
{1,4} S ={1,4,5}
{1,2} S ={1,2,4,5}
Prims Algorithm
Repeat until S =V
1 2 3
1 2
4
6
4
5
6
S ={5}
{4,5} S ={4,5}
{14} S {145}
X :
4 5 6
3 8
7
4
6
4
5
6
7
3 4
{1,4} S ={1,4,5}
{1,2} S ={1,2,4,5}
{2,3} S ={1,2,3,4,5}
Prims Algorithm
Repeat until S =V
1 2 3
1 2
4
6
4
5
6
S ={5}
{4,5} S ={4,5}
{14} S {145}
X :
4 5 6
3 8
7
4
6
4
5
6
7
3 4
{1,4} S ={1,4,5}
{1,2} S ={1,2,4,5}
{2,3} S ={1,2,3,4,5}
{4,7} S ={1,2,3,4,5,7}
Prims Algorithm
Repeat until S =V
1 2 3
1 2
4
6
4
5
6
S ={5}
{4,5} S ={4,5}
{14} S {145}
X :
4 5 6
3 8
7
4
6
4
5
6
7
3 4
{1,4} S ={1,4,5}
{1,2} S ={1,2,4,5}
{2,3} S ={1,2,3,4,5}
{4,7} S ={1,2,3,4,5,7}
{6,7} S ={1,2,3,4,5,6,7}
2/18/2009
5
3 Questions
Is it correct?
By the Cut Property
How long does it take?
Lik Dijk t ! Like Dijkstras!
Can we do better?
Prims Algorithm
Theorem: Prims Algorithm finds a minimum spanning tree
Basis: X =, and G is connected so a solution must exist
Prims Algorithm
Theorem: Prims Algorithm finds a minimum spanning tree
Induction Step: Assume X is part of an MST
1 2
S
X E
u
3 8
v
4
6
4
5
6
7
3 4
S
No edge in X leaves S
e ={u,v}is shortest
edge that leaves S
Cut Property holds
Thus, X U {e}is part of
an MST
Prims Algorithm
Identify:
- Elements of a
Greedy algorithm
Implications for Prims Algorithm
Dense Graph: |E| =O(|V|
2
)
Array:
Binary heap:
Fibonacci heap: Fibonacci heap:
Sparse Graph: |E| =O(|V|)
Array:
Binary heap:
Fibonacci heap:
Bottom Line
Dense Graph: |E| =O(|V|
2
)
Kruskals: (|E| log |V|) =(|V|
2
log |V|)
Prims (w/ Array PQ): (|V|
2
)
Prims (w/ Binary Heap PQ): (|V|
2
log |V|)
Prims (w/ Fibonacci Heap PQ): (|V|
2
)
Sparse Graph: |E| =O(|V|)
K k l (|E| l |V|) (|V| l |V|) Kruskals: (|E| log |V|) =(|V| log |V|)
Prims (w/ Array PQ): (|V|
2
)
Prims (w/ Binary Heap PQ): (|V| log |V|)
Prims (w/ Fibonacci Heap PQ): (|V| log |V|)
Punch-lines?
2/18/2009
6
Bottom Line
Dense Graph: |E| =O(|V|
2
)
Kruskals: (|E| log |V|) =(|V|
2
log |V|)
Prims (w/ Array PQ): (|V|
2
)
Prims (w/ Binary Heap PQ): (|E| log |V|) =(|V|
2
log |V|)
Prims (w/ Fibonacci Heap PQ): (|V|
2
)
Sparse Graph: |E| =O(|V|)
K k l (|E| l |V|) (|V| l |V|) Kruskals: (|E| log |V|) =(|V| log |V|)
Prims (w/ Array PQ): (|V|
2
)
Prims (w/ Binary Heap PQ): (|E| log |V|) =(|V| log |V|)
Prims (w/ Fibonacci Heap PQ): (|E| log |V|) =(|V| log |V|)
Punch-lines?
Prefer Prims with Array or Fib. Heap PQ for a dense graph
Prefer Kruskals or Prims with Bin. Heap or Fib. Heap PQ for a sparse
graph
Prims Example
Assignment
HW #13: 5.2(a)
Reading: Sec. 5.2
Optional: Sec. 5.3 & 5.4

You might also like