Link
Minimum Spanning Trees
Introducing and analyzing two algorithms for finding MSTs by repeatedly applying the cut property.
Kevin Lin, with thanks to many others.
1
Ask questions anonymously on Piazza. Look for the pinned Lecture Questions thread.

Feedback from the Reading Quiz
2

MSTs vs. SPTs
Is the MST for this graph also a shortest paths tree? 
If so, using which node as the starting node for this SPT?
3
B
C
A
2
4
3
1
D
Q

Is the MST for this graph also a shortest paths tree? 
If so, using which node as the starting node for this SPT?
4

MSTs vs. SPTs
Is the MST for this graph also a shortest paths tree? 
If so, using which node as the starting node for this SPT?
5
A
2
4
3
1
D
A
B
C
SPT from B == MST

MSTs vs. SPTs: A Key Difference
A shortest paths tree depends on the source vertex.
There is no source vertex for a minimum spanning tree.
The SPT for every other vertex is different from the MST!
6
A
2
4
3
1
D
B
C
SPT from A != MST

Repeated Application of Cut Property
Given a cut, the minimum-weight crossing edge must be in the minimum spanning tree.
But other crossing edges can also be in the minimum spanning tree.
7
A
2
4
3
1
D
B
C

Repeated Application of Cut Property
Given a cut, the minimum-weight crossing edge must be in the minimum spanning tree.
But other crossing edges can also be in the minimum spanning tree.
8
A
2
4
3
1
D
B
C

Repeated Application of Cut Property
Given a cut, the minimum-weight crossing edge must be in the minimum spanning tree.
But other crossing edges can also be in the minimum spanning tree.
9
A
2
4
3
1
D
B
C

Prim’s Algorithm
10

Conceptual Prim’s Algorithm
Idea. Iteratively apply cut property from a source vertex, expanding the fringe as we go.
11
Demo
Prim’s Algorithm, Dijkstra’s Algorithm (Kevin Wayne/Princeton)

Dijkstra’s Pseudocode
PQ.add(s, 0)
For all other vertices v, PQ.add(v, infinity)
While PQ is not empty:
p = PQ.removeSmallest()Relax all edges from p

Relaxing an edge (v, w) with weight:
If distTo[w] > distTo[v] + weight:
distTo[w] = distTo[v] + weightedgeTo[w] = vPQ.changePriority(w, distTo[w])
Invariants
edgeTo[v]: best known predecessor of v.
distTo[v]: best known distance of s to v.
PQ maintains vertices based on distTo.

Important properties
Always visits vertices in order of total distance from source. Relaxation always fails on edges to visited (white) vertices.
12

Prim’s Pseudocode
PQ.add(s, 0)
For all other vertices v, PQ.add(v, infinity)
While PQ is not empty:
p = PQ.removeSmallest()Relax all edges from p

Relaxing an edge (v, w) with weight:
If w is in PQ and distTo[w] > weight:
distTo[w] = weightedgeTo[w] = vPQ.changePriority(w, distTo[w])
Invariants
edgeTo[v]: best known predecessor of v.
distTo[v]: best known distance of s to v.
PQ maintains vertices based on distTo.
13
A
99
B
1
Extra check if w is in PQ!

Prim’s Runtime Analysis
PQ.add(s, 0)
For all other vertices v, PQ.add(v, infinity)
While PQ is not empty:
p = PQ.removeSmallest()Relax all edges from p

Relaxing an edge (v, w) with weight:
If w is in PQ and distTo[w] > weight:
distTo[w] = weightedgeTo[w] = vPQ.changePriority(w, distTo[w])
Same as Dijkstra’s.
ArrayHeapMinPQ implementation.
V adds, each O(log V) time.
V removals, each O(log V) time.
E contains, each O(log V) time.
E changePriority, each O(log V) time.

Simple:	O(V log V + E log V).
Assuming E > V, this is just O(E log V) for connected graphs.
14

Prim’s Algorithm as a Modification of Dijkstra’s
Prim’s Algorithm is almost the same as Dijkstra’s Algorithm.
Instead of measuring distance from the source, Prim’s considers distance from the tree.

Visit order:
Dijkstra’s	visits vertices in order of distance from the source.
Prim’s	visits vertices in order of distance from the MST-under-construction.
Relaxation:
Dijkstra’s	considers an edge better based on distance to source.
Prim’s	considers an edge better based on distance to tree.
15
Demo

Dijkstra’s Algorithm Correctness
Dijkstra’s algorithm. Visit vertices in order of distance from source.
On visit, relax every edge from the visited vertex.

Dijkstra’s can fail if the graph has negative weight edges. Give an example graph.
Hide the real shortest path behind a later-explored path.
16
A
33
34
-99
-90
9
1
9
Violates distTo invariant!

Does Prim’s algorithm work on graphs with negative edge weights?
17

Prim’s Algorithm Correctness
Dijkstra’s algorithm. Visit vertices in order of distance from source.
Dijkstra’s can fail if the graph has negative weight edges.
Hide the real shortest path behind a later-explored path.

Prim’s algorithm. Visit vertices in order of distance from MST-under-construction.
In Prim’s, the concept of a “later-explored path” is just a single edge weight.
Can’t hide the “real shortest path” behind a single edge weight.
18
A

Kruskal’s Algorithm
19

Repeated Application of Cut Property
Given a cut, the minimum-weight crossing edge must be in the minimum spanning tree.
But other crossing edges can also be in the minimum spanning tree.
20
A
2
4
3
1
D
B
C

Conceptual Kruskal’s Algorithm
Idea. Consider edges by increasing weight. Add edge to MST (mark black) unless doing so creates a cycle. Repeat until V-1 edges.
21
Demo
Prim’s Algorithm, Kruskal’s Algorithm (Kevin Wayne/Princeton)

Kruskal’s Runtime Analysis
Sort all edges by weight
While number of edges in MST < V - 1:
e = next lightest edgeIf adding e doesn’t cause a cycle:	Add e to the MST
Simple graph: E < V2.
Sorting: O(E log E) = O(E log V).
E cycle-checks.
V - 1 edges added to the MST.

Cycle-finding runtime?
Cycle-finding?
22

Finding Cycles
Given a undirected graph, determine if it contains any cycles.
Use any data structure or algorithm from the course.
23
Q
1
2
3
4
5
6
0

Finding Cycles: Depth-First Search
Given a undirected graph, determine if it contains any cycles.
Use any data structure or algorithm from the course.

Depth-first search from 0 (or any vertex).
Keep going until you see an already-marked vertex.
Potential danger: 1 looks back at 0 and sees that it’s marked.Solution: Just don’t count the node you came from.
Worst case runtime: O(V + E)
Kruskal’s needs to check for cycles up to E times!
24
A
1
2
3
4
5
6
0

Finding Cycles: Connected Components
For each vertex v, its connected component is the set of all vertices that are connected to v.
Model connectedness in terms of sets of vertices. Keep track of the component (set) for v.
25
A
2
4
3
1
D
B
C
{A, B}
{C, D}
isConnected(B, D)
    connect(B, D)

Finding Cycles: Connected Components
For each vertex v, its connected component is the set of all vertices that are connected to v.
Model connectedness in terms of sets of vertices. Keep track of the component (set) for v.
26
A
2
4
3
1
D
B
C
{A, B, C, D}
isConnected(B, D)
    connect(B, D)

Disjoint Sets ADT
V vertices in the disjoint sets ADT.
Kruskal’s loops up to E times.
Calls isConnected each time.
But only up to V - 1 calls to connect.

Goal: O(log V) implementation for both connect and isConnected.
27
public interface DisjointSets {
  /** Connects two items P, Q. */
  void connect(int p, int q);

  /** True if P, Q are connected. */
  boolean isConnected(int p, int q);
}

Summary
Minimum Spanning Tree. A spanning tree of minimum weight.
Cut Property. Given a cut, the minimum-weight crossing edge must be in the MST.

Prim’s Algorithm. Dijkstra’s Algorithm, except focused on distance from the tree.
Only take into account the edge weight!

Kruskal’s Algorithm. Sort edges in increasing order of weight, use connected components.
Disjoint sets ADT to efficiently find cycles by modeling them with connected components.
28

What is your burning question from today’s lecture?
29