Lecture15
Lecture15
• Agenda:
1. What is a Minimum Spanning Tree?
2. Short break to introduce some graph theory tools
3. Prim’s algorithm
4. Kruskal’s algorithm
Minimum Spanning Tree
Say we have an undirected weighted graph
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
A tree is a
1 2 connected graph
H G F
with no cycles!
7 6
8 10
A tree is a
This tree 1 2 connected graph
has cost 67 H G F
with no cycles!
7 6
8 10
A tree is a
1 2 connected graph
H G F
with no cycles!
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
7 6
8 10
1 2
H G F
Figure 2: Fully parsimonious minimal spanning tree of 933 SNPs for 282 isolates of Y. pestis colored by location.
Morelli et al. Nature genetics 2010
How to find an MST?
• Today we’ll see two greedy algorithms.
• In order to prove that these greedy algorithms work, we’ll
need to show something like:
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Brief aside
for a discussion of cuts in graphs!
Cuts in graphs
• A cut is a partition of the vertices into two parts:
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
8
B C D
7
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
7 6
8 10
1 2
H G F
B 8 C D
7
4 9
2
11 4
A I 14 E
8 7 6
10
H 1 G 2 F
x y
u a
v b
Proof of Lemma
• Assume that we have:
• a cut that respects A
• A is part of some MST T.
• Say that (u,v) is light.
x y
• lowest cost crossing the cut
u a
v b
Claim: Adding any additional edge to
a spanning tree will create a cycle.
v b
Claim: Adding any additional edge to
a spanning tree will create a cycle.
x y
u a
v b
Proof of Lemma ctd.
• Consider swapping (u,v) for (x,y) in T.
• Call the resulting tree T’.
• Claim: T’ is still an MST.
• It is still a tree:
x y
• we deleted (x,y)
• It has cost at most that of T
• because (u,v) was light.
• T had minimal cost.
• So T’ does too. u a
• So T’ is an MST
containing (u,v).
• This is what we wanted. v b
Lemma
• Let A be a set of edges, and consider a cut that respects A.
• Suppose there is an MST containing A.
• Let (u,v) be a light edge.
• Then there is an MST containing A ∪ {(u,v)}
B 8 C D
7
4 9
2
11 4
A I 14 E
8 7 6
10
H 1 G 2 F
• The strategy:
• Make a series of choices, adding edges to the tree.
• Show that each edge we add is safe to add:
• we do not rule out the possibility of success
• we will choose light edges crossing cuts and use the Lemma.
• Keep going until we have an MST.
Idea 1
Start growing a tree, greedily add the shortest edge
we can to grow the tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Idea 1
Start growing a tree, greedily add the shortest edge
we can to grow the tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Idea 1
Start growing a tree, greedily add the shortest edge
we can to grow the tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Idea 1
Start growing a tree, greedily add the shortest edge
we can to grow the tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Idea 1
Start growing a tree, greedily add the shortest edge
we can to grow the tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Idea 1
Start growing a tree, greedily add the shortest edge
we can to grow the tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Idea 1
Start growing a tree, greedily add the shortest edge
we can to grow the tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Idea 1
Start growing a tree, greedily add the shortest edge
we can to grow the tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Idea 1
Start growing a tree, greedily add the shortest edge
we can to grow the tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
We’ve discovered Prim’s algorithm!
• slowPrim( G = (V,E), starting vertex s ):
• Let (s,u) be the lightest edge coming out of s.
• MST = { (s,u) }
n iterations of this
• verticesVisited = { s, u } while loop.
• while |verticesVisited| < |V|:
• find the lightest edge (x,v) in E so that: Maybe take time
• x is in verticesVisited m to go through all
• v is not in verticesVisited the edges and find
• add (x,v) to MST the lightest.
• add v to verticesVisited
• return MST
Naively, the running time is O(nm):
• For each of n-1 iterations of the while loop:
• Maybe go through all the edges.
Two questions
1. Does it work?
• That is, does it actually return a MST?
B 8 C D
7
4 9
2
11 4
A I 14 E
8 7 6
10
H 1 G 2 F
A is the set of
edges selected so far.
8 7
B C D
4 9
11 2
4
A I 14 E
8 7 6
2 10
H G F
1
Suppose we are partway through Prim
• Assume that our choices A so far are safe.
• they don’t rule out success
• Consider the cut {visited, unvisited}
• A respects this cut.
• The edge we add next is a light edge.
• Least weight of any edge crossing the cut.
A is the set of
• By the Lemma, edges selected so far.
8 7 6
2 10
add this one next H G F
1
Hooray!
• Our greedy choices don’t rule out success.
I’m 7 away.
C is the closest.
B
8 C
7 D
4 9
2
11 4
A I 14 E
8 7 6
10 I can’t get to the
H
1 G 2 F tree in one edge
How do we actually implement this?
• Each vertex keeps:
• the distance from itself to the growing spanning tree
if you can get there in one edge.
• how to get there.
• Choose the closest vertex, add it.
I’m 7 away.
C is the closest.
B
8 C
7 D
4 9
2
11 4
A I 14 E
8 7 6
10 I can’t get to the
H
1 G 2 F tree in one edge
How do we actually implement this?
• Each vertex keeps:
• the distance from itself to the growing spanning tree
if you can get there in one edge.
• how to get there.
• Choose the closest vertex, add it.
I’m 7 away.
C is the closest.
B
8 C
7 D
4 9
2
11 4
A I 14 E
8 7 6
10 I can’t get to the
H
1 G 2 F tree in one edge
How do we actually implement this?
• Each vertex keeps:
• the distance from itself to the growing spanning tree
if you can get there in one edge.
• how to get there.
• Choose the closest vertex, add it.
• Update the stored info. I’m 7 away.
C is the closest.
B
8 C
7 D
4 9
2
11 4
A I 14 E
8 7 6
10 I’m 10 away.
H
1 G 2 F F is the closest.
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
k[x] is the distance of x
k[x] from the growing tree
∞ ∞ ∞
8 7
B C D
4 9
2
4 ∞
11
A I ∞
14 E
0
7 6
8 10
1 2
H G F
∞ ∞ ∞
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
∞ ∞ ∞
8 7
B C D
4 9
2
4 ∞
11
A I ∞
14 E
0
7 6
8 10
1 2
H G F
∞ ∞ ∞
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
k[b] comes from.
4 ∞ ∞
8 7
B C D
4 9
2
4 ∞
11
A I ∞
14 E
0
7 6
8 10
1 2
H G F
8 ∞ ∞
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 ∞ ∞
8 7
B C D
4 9
2
4 ∞
11
A I ∞
14 E
0
7 6
8 10
1 2
H G F
8 ∞ ∞
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 ∞ ∞
8 7
B C D
4 9
2
4 ∞
11
A I ∞
14 E
0
7 6
8 10
1 2
H G F
8 ∞ ∞
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 ∞
8 7
B C D
4 9
2
4 ∞
11
A I ∞
14 E
0
7 6
8 10
1 2
H G F
8 ∞ ∞
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 ∞
8 7
B C D
4 9
2
4 ∞
11
A I ∞
14 E
0
7 6
8 10
1 2
H G F
8 ∞ ∞
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 ∞
8 7
B C D
4 9
2
4 ∞
11
A I ∞
14 E
0
7 6
8 10
1 2
H G F
8 ∞ ∞
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 7
8 7
B C D
4 9
2
4 ∞
11
A I 2
14 E
0
7 6
8 10
1 2
H G F
8 ∞ 4
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 7
8 7
B C D
4 9
2
4 ∞
11
A I 2
14 E
0
7 6
8 10
1 2
H G F
8 ∞ 4
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 7
8 7
B C D
4 9
2
4 ∞
11
A I 2
14 E
0
7 6
8 10
1 2
H G F
8 ∞ 4
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 7
8 7
B C D
4 9
2
4 ∞
11
A I 2
14 E
0
7 6
8 10
1 2
H G F
7 6 4
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 7
8 7
B C D
4 9
2
4 ∞
11
A I 2
14 E
0
7 6
8 10
1 2
H G F
7 6 4
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 7
8 7
B C D
4 9
2
4 ∞
11
A I 2
14 E
0
7 6
8 10
1 2
H G F
7 6 4
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 7
8 7
B C D
4 9
2
4 10
11
A I 2
14 E
0
7 6
8 10
1 2
H G F
7 2 4
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 7
8 7
B C D
4 9
2
4 10
11
A I 2
14 E
0
7 6
8 10
1 2
H G F
7 2 4
Efficient implementation x
x
Can’t reach x yet
x is “active”
Every vertex has a key and a parent x Can reach x
Until all the vertices are reached:
• Activate the unreached vertex u with the smallest key. k[x] is the distance of x
k[x] from the growing tree
• for each of u’s neighbors v:
• k[v] = min( k[v], weight(u,v) ) p[b] = a, meaning that
a b
• if k[v] updated, p[v] = u a was the vertex that
• Mark u as reached, and add (p[u],u) to MST. k[b] comes from.
4 8 7
8 7
B C D
4 9
2
4 10
11
A I 2
14 E
0
7 6
8 10
7
H etc.1
G
2
2 F
4
This should look pretty familiar
• Very similar to Dijkstra’s algorithm!
• Differences:
1. Keep track of p[v] in order to return a tree at the end
• But Dijkstra’s can do that too, that’s not a big difference.
*See CS166
Two questions
1. Does it work?
• That is, does it actually return a MST?
• Yes!
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
That won’t
cause a cycle
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
That won’t
cause a cycle
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
That won’t
cause a cycle
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
That won’t
cause a cycle
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
That’s not the only greedy algorithm
what if we just always take the cheapest edge?
whether or not it’s connected to what we have so far?
That won’t
cause a cycle
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
We’ve discovered Kruskal’s algorithm!
• slowKruskal(G = (V,E)):
• Sort the edges in E by non-decreasing weight.
• MST = {}
m iterations through this loop
• for e in E (in sorted order):
• if adding e to MST won’t cause a cycle:
• add e to MST. How do we check this?
• return MST
Let’s do this
2. How do we actually implement this? one first
• the pseudocode above says “slowKruskal”…
At each step of Kruskal’s, A forest is a
collection of
we are maintaining a forest. disjoint trees
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
At each step of Kruskal’s, A forest is a
collection of
we are maintaining a forest. disjoint trees
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
At each step of Kruskal’s, A forest is a
collection of
we are maintaining a forest. disjoint trees
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
At each step of Kruskal’s, A forest is a
collection of
we are maintaining a forest. disjoint trees
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
At each step of Kruskal’s, A forest is a
collection of
we are maintaining a forest. disjoint trees
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
We never add an edge within a tree since that would create a cycle.
Keep the trees in a special data structure
“treehouse”?
Union-find data structure
also called disjoint-set data structure
• Used for storing collections of sets
• Supports:
• makeSet(u): create a set {u}
• find(u): return the set that u is in
• union(u,v): merge the set that u is in with the set that v is in.
makeSet(x) x
makeSet(y) y
makeSet(z)
union(x,y)
z
Union-find data structure
also called disjoint-set data structure
• Used for storing collections of sets
• Supports:
• makeSet(u): create a set {u}
• find(u): return the set that u is in
• union(u,v): merge the set that u is in with the set that v is in.
makeSet(x) x y
makeSet(y)
makeSet(z)
union(x,y)
z
Union-find data structure
also called disjoint-set data structure
• Used for storing collections of sets
• Supports:
• makeSet(u): create a set {u}
• find(u): return the set that u is in
• union(u,v): merge the set that u is in with the set that v is in.
makeSet(x) x y
makeSet(y)
makeSet(z)
union(x,y)
find(x)
z
Kruskal pseudo-code
• kruskal(G = (V,E)):
• Sort E by weight in non-decreasing order
• MST = {} // initialize an empty tree
• for v in V:
• makeSet(v) // put each vertex in its own tree in the forest
• for (u,v) in E: // go through the edges in sorted order
• if find(u) != find(v): // if u and v are not in the same tree
• add (u,v) to MST
• union(u,v) // merge u’s tree with v’s tree
• return MST
Once more…
To start, every vertex is in it’s own tree.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Once more…
Then start merging.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Once more…
Then start merging.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Once more…
Then start merging.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Once more…
Then start merging.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Once more…
Then start merging.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Once more…
Then start merging.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Once more…
Then start merging.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Stop when we have one big tree!
Once more…
Then start merging.
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Running time
• Sorting the edges takes O(m log(n))
• In practice, if the weights are integers we can use
radixSort and take time O(m)
• For the rest: In practice, each of
• n calls to makeSet makeSet, find, and union
• put each vertex in its own set run in constant time*
• 2m calls to find
• for each edge, find its endpoints
• n calls to union
• we will never add more than n-1 edges to the tree,
• so we will never call union more than n-1 times.
• Total running time:
• Worst-case O(mlog(n)), just like Prim.
• Closer to O(m) if you can do radixSort
*technically, they run in amortized time O(𝛼(𝑛)), where 𝛼(𝑛) is the inverse Ackerman function.
𝛼 𝑛 ≤ 4 provided that n is smaller than the number of atoms in the universe.
Two questions
1. Does it work?
Now that we
• That is, does it actually return a MST? understand this
“tree-merging” view,
let’s do this one.
B 8 C D
7
4 9
2
11 4
A I 14 E
8 7 6
10
H 1 G 2 F
B 8 C 7 D
4 9
2
11 4
A I 14 E
8 7 6 10
A is the set of
H 1 G 2 F
edges selected so far.
Suppose we are partway through Kruskal
• Assume that our choices A so far are safe.
• they don’t rule out success
• The next edge we add will merge two trees, T1, T2
• Consider the cut {T1, V – T1}.
• A respects this cut. This is the
• Our new edge is light for the cut next edge
T1
B 8 C 7 D
4 9
2
11 4
A I 14 E
8 7 6 10
A is the set of
H 1 G 2 F
edges selected so far. T2
Suppose we are partway through Kruskal
• Assume that our choices A so far are safe.
• they don’t rule out success
• The next edge we add will merge two trees, T1, T2
• Consider the cut {T1, V – T1}.
• A respects this cut. This is the
• Our new edge is light for the cut next edge
T1
• By the Lemma, 8 7
B C D
this edge is safe.
• it also doesn’t 4 9
2
rule out 11 4
A I 14 E
success.
8 7 6 10
A is the set of
H 1 G 2 F
edges selected so far. T2
Hooray!
• Our greedy choices don’t rule out success.
B 8 C D
7
4 9
2
11 4
A I 14 E
8 7 6
10
H 1 G 2 F
• Karger-Klein-Tarjan 1995:
• O(m) time randomized algorithm
• Chazelle 2000:
• O(m⋅ 𝛼(𝑛)) time deterministic algorithm
• Pettie-Ramachandran 2002:
The optimal number of comparisons
• O N*(n,m) you need to solve the time deterministic algorithm
problem, whatever that is…
Recap
• Two algorithms for Minimum Spanning Tree
• Prim’s algorithm
• Kruskal’s algorithm