Warm-up Problem
Given a undirected graph, determine if it contains any cycles.
1
2
3
4
5
6
0
Warm-up Problem
Given a undirected graph, determine if it contains any cycles.
Approach 1: Do DFS from 0 (arbitrary vertex).
Worst case runtime: O(V + E) -- do study guide problems to reinforce this.
1
2
3
4
5
6
0
Warm-up Problem
Given a undirected graph, determine if it contains any cycles.
Approach 2: Use a WeightedQuickUnionUF object.
Worst case runtime: O(V + E α(V)) if we have path compression.
1
2
3
4
5
6
0
CS61B, 2019
Lecture 26: Minimum Spanning Trees
Spanning Trees
Given an undirected graph, a spanning tree T is a subgraph of G, where T:
Example:
A minimum spanning tree is a spanning tree of minimum total weight.
These two properties make it a tree.
This makes it spanning.
Spanning Trees
How Many are Spanning Trees? http://yellkey.com/now
MST Applications
Old school handwriting recognition (left (link))
Medical imaging (e.g. arrangement of nuclei in cancer cells (right))
For more, see: http://www.ics.uci.edu/~eppstein/gina/mst.html
MST
Find the MST for the graph.
B
C
A
s
2
3
2
2
D
MST
Find the MST for the graph.
B
C
A
s
2
3
2
2
D
MST vs. SPT, http://yellkey.com/sit
Is the MST for this graph also a shortest paths tree? If so, using which node as the starting node for this SPT?
B
C
A
s
2
3
2
2
D
MST vs. SPT
Is the MST for this graph also a shortest paths tree? If so, using which node as the starting node for this SPT?
B
C
A
2
3
2
2
D
s = D
Doesn’t work for s = D!
MST vs. SPT, http://yellkey.com/approach
Is the MST for this graph also a shortest paths tree? If so, using which node as the starting node for this SPT?
B
C
A
s
2
3
2
2
D
SPT from A
B
C
A
2
3
2
2
D
SPT from B,
MST
B
C
A
s
2
3
2
2
D
SPT from C
SPT from D
s
s
s
MST vs. SPT, http://yellkey.com/approach
A shortest paths tree depends on the start vertex:
There is no source for a MST.
Nonetheless, the MST sometimes happens to be an SPT for a specific vertex.�
Spanning Tree, http://yellkey.com/both
Give a valid MST for the graph below.
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
0
0
Spanning Tree
Give a valid MST for the graph below.
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
0
0
Spanning Tree
Give a valid MST for the graph below.
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
0
0
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
0
0
Example SPT from bottom left vertex:
A Useful Tool for Finding the MST: Cut Property
Cut property: Given any cut, minimum weight crossing edge is in the MST.
Prim’s Runtime
Exactly like Dijkstra’s runtime:
Overall runtime, assuming E > V, we have O(E log V) runtime.
Operation | Number of Times | Time per Operation | Total Time |
Insert | V | O(log V) | O(V log V) |
Delete minimum | V | O(log V) | O(V log V) |
Decrease priority | E | O(log V) | O(E log V) |
Cut Property in Action: http://yellkey.com/drive
Which edge is the minimum weight edge crossing the cut {2, 3, 5, 6}?
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
Cut Property in Action
Which edge is the minimum weight edge crossing the cut {2, 3, 5, 6}?
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
Cut Property Proof
Suppose that the minimum crossing edge e were not in the MST.
Generic MST Finding Algorithm
Start with no edges in the MST.
This should work, but we need some way of finding a cut with no crossing edges!
Prim’s Algorithm
Prim’s Algorithm
Start from some arbitrary start node.
Conceptual Prim’s Algorithm Demo (Link)
Why does Prim’s work? Special case of generic algorithm.
Prim’s vs. Dijkstra’s (visual)
Prim’s Algorithm
Dijkstra’s Algorithm
Demos courtesy of Kevin Wayne, Princeton University
Prim’s Algorithm Implementation
The natural implementation of the conceptual version of Prim’s algorithm is highly inefficient.
Can use some cleverness and a PQ to speed things up.
Realistic Implementation Demo (Link)
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
3
1
1
4
1
Prim’s vs. Dijkstra’s
Prim’s and Dijkstra’s algorithms are exactly the same, except Dijkstra’s considers “distance from the source”, and Prim’s considers “distance from the tree.”
Visit order:
Relaxation:
Prim’s Implementation (Pseudocode, 1/2)
public class PrimMST {
public PrimMST(EdgeWeightedGraph G) {
edgeTo = new Edge[G.V()];
distTo = new double[G.V()];
marked = new boolean[G.V()];
fringe = new SpecialPQ<Double>(G.V());
distTo[s] = 0.0;
setDistancesToInfinityExceptS(s);
insertAllVertices(fringe);
/* Get vertices in order of distance from tree. */
while (!fringe.isEmpty()) {
int v = fringe.delMin();
scan(G, v);
}
}
...
Fringe is ordered by distTo tree. Must be a specialPQ like Dijkstra’s.
Get vertex closest to tree that is unvisited.
Scan means to consider all of a vertices outgoing edges.
Prim’s Implementation (Pseudocode, 2/2)
private void scan(EdgeWeightedGraph G, int v) {
marked[v] = true;
for (Edge e : G.adj(v)) {
int w = e.other(v);
if (marked[w]) { continue; }
if (e.weight() < distTo[w]) {
distTo[w] = e.weight();
edgeTo[w] = e;
pq.decreasePriority(w, distTo[w]);
}
}
}
Important invariant, fringe must be ordered by current best known distance from tree.
Already in MST, so go to next edge.
Better path to a particular vertex found, so update current best known for that vertex.
Vertex is closest, so add to MST.
while (!fringe.isEmpty()) {
int v = fringe.delMin();
scan(G, v);
}
Prim’s Runtime
What is the runtime of Prim’s algorithm?
private void scan(EdgeWeightedGraph G, int v) {
marked[v] = true;
for (Edge e : G.adj(v)) {
int w = e.other(v);
if (marked[w]) { continue; }
if (e.weight() < distTo[w]) {
distTo[w] = e.weight();
edgeTo[w] = e;
pq.decreasePriority(w, distTo[w]);
}
}
}
while (!fringe.isEmpty()) {
int v = fringe.delMin();
scan(G, v);
}
Prim’s Algorithm Runtime
Priority Queue operation count, assuming binary heap based PQ:
Overall runtime: O(V*log(V) + V*log(V) + E*logV).
| # Operations | Cost per operation | Total cost |
PQ add | V | O(log V) | O(V log V) |
PQ delMin | V | O(log V) | O(V log V) |
PQ decreasePriority | O(E) | O(log V) | O(E log V) |
Kruskal’s Algorithm
Kruskal’s Algorithm
Initially mark all edges gray.
Conceptual Kruskal’s Algorithm Demo (Link)
Realistic Kruskal’s Algorithm Implementation Demo (Link)
�Why does Kruskal’s work? Special case of generic MST algorithm.
Prim’s vs. Kruskal’s
Prim’s Algorithm
Demos courtesy of Kevin Wayne, Princeton University
Kruskal’s Algorithm
Kruskal’s Implementation (Pseudocode)
public class KruskalMST {
private List<Edge> mst = new ArrayList<Edge>();
public KruskalMST(EdgeWeightedGraph G) {
MinPQ<Edge> pq = new MinPQ<Edge>();
for (Edge e : G.edges()) {
pq.insert(e);
}
WeightedQuickUnionPC uf =
new WeightedQuickUnionPC(G.V());
while (!pq.isEmpty() && mst.size() < G.V() - 1) {
Edge e = pq.delMin();
int v = e.from();
int w = e.to();
if (!uf.connected(v, w)) {
uf.union(v, w);
mst.add(e);
} } } }
What is the runtime of Kruskal’s algorithm?
Kruskal’s Runtime
Kruskal’s algorithm on previous slide is O(E log E).
Note: If we use a pre-sorted list of edges (instead of a PQ), then we can simply iterate through the list in O(E) time, so overall runtime is O(E + V log*V + E log* V) = O(E log*V).
Operation | Number of Times | Time per Operation | Total Time |
Insert | E | O(log E) | O(E log E) |
Delete minimum | O(E) | O(log E) | O(E log E) |
union | O(V) | O(log* V) | O(V log* V) |
isConnected | O(E) | O(log* V) | O(E log*V) |
Fun fact: In HeapSort lecture, we discuss how do this step in O(E) time using “bottom-up heapification”.
Shortest Paths and MST Algorithms Summary
Question: Can we do better than O(E log* V)?
Problem | Algorithm | Runtime (if E > V) | Notes |
Shortest Paths | Dijkstra’s | O(E log V) | Fails for negative weight edges. |
MST | Prim’s | O(E log V) | Analogous to Dijkstra’s. |
MST | Kruskal’s | O(E log E) | Uses WQUPC. |
MST | Kruskal’s with pre-sorted edges | O(E log* V) | Uses WQUPC. |
170 Spoiler: State of the Art Compare-Based MST Algorithms
year | worst case | discovered by |
1975 | E log log V | Yao |
1984 | E log* V | Fredman-Tarjan |
1986 | E log (log* V) | Gabow-Galil-Spencer-Tarjan |
1997 | E α(V) log α(V) | Chazelle |
2000 | E α(V) | Chazelle |
2002 | optimal (link) | Pettie-Ramachandran |
??? | E ??? | ??? |
(Slide Courtesy of Kevin Wayne, Princeton University)
Citations
Tree fire: http://www.miamidade.gov/fire/library/hotlines/2011-december_files/tree-fire.jpg
Bicycle routes in Seattle: https://www.flickr.com/photos/ewedistrict/21980840
Cancer MST: http://www.bccrc.ca/ci/ta01_archlevel.html