Announcements
Project 3 will be out this weekend.
Midterm 2 Friday. Lecture Friday will be review time. Bring questions. I’ll mostly focus on things that can be answered in ~5 minutes or so or less I think.
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: Θ(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 log* V) if we have path compression.
1
2
3
4
5
6
0
CS61B
Lecture 30: Graphs IV: 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://shoutkey.com/too
MST Applications
Network design (e.g. bicycle routes in Seattle (left))
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://shoutkey.com/were
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, http://shoutkey.com/were
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
MST vs. SPT, http://shoutkey.com/were
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://shoutkey.com/were
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 vertrex.�
Spanning Tree, http://shoutkey.com/degree
Give a valid MST for the tree 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 tree 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.
Cut Property in Action: http://shoutkey.com/went
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
Start from some arbitrary start node.
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 vs. Dijkstra’s
Prim’s and Dijkstra’s algorithms are exactly the same, except for the following:
Visit order:
Visitation:
Prim’s Implementation (Pseudocode, 1/2)
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)
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 (not shown in demo).
Vertex is closest, so add to MST.
Prim’s Runtime
What is the runtime of Prim’s algorithm?
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) |
Kruskal’s Algorithm
Initially mark all edges gray.
�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)
Kruskal’s Runtime
Kruskal’s algorithm is O(E log E).
Note: If edges are already sorted (e.g. stored in an ordered linked list), then we can skip Build-PQ and deletion is constant time, so overall runtime is O(E log* V).
Operation | Number of Times | Time per Operation | Total Time |
Build-PQ of edges | 1 | O(E) | O(E) |
Delete minimum | E | O(log E) | O(E log E) |
connect | V | O(log* V) | O(V log* V) |
isConnected | E | O(log* V) | O(E log*V) |
E log E if we insert repeatedly, E if we use “bottom up heapification” see HeapSort lecture.
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 | Pettie-Ramachandran |
??? | E ??? | ??? |
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