CS180 S25 L09 MSTs Part2 Annotated-1
CS180 S25 L09 MSTs Part2 Annotated-1
Administrative Information
https://app.sli.do/event/aNNu7dWGCTGkJXtSWRx8
tu
3
Objectives for Today Single-pair shortest path problem
5
4 1
source s 0 2 3
8
7 2 9
7
9 6 1
11
5
5
4 13
5 4 20 6
destination t
length of path = 9 + 4 + 1 + 11 = 25 6
Problem. Given a digraph G = (V, E), edge lengths ℓe ≥ 0, source s ∈ V, ・Map routing.
find a shortest directed path from s to every node. ・Robot navigation.
・Urban traffic planning.
・Routing of telecommunications messages.
・Network routing protocols
1 3
・Optimal truck routing through given traffic congestion pattern.
1
5
5
4 1
source s 0 2 3
8
7 2 9
7
9 6 1
11
5
5
4 13
shortest-paths tree 7 9
Dijkstra′s algorithm (for single-source shortest paths problem) Dijkstra′s algorithm (for single-source shortest paths problem)
Greedy approach. Maintain a set of explored nodes S for which Greedy approach. Maintain a set of explored nodes S for which
algorithm has determined d[u] = length of a shortest s↝u path. algorithm has determined d[u] = length of a shortest s↝u path.
・Initialize S ← { s }, d[s] ← 0. ・Initialize S ← { s }, d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes ・Repeatedly choose unexplored node v ∉ S which minimizes
the length of a shortest path from s the length of a shortest path from s
to some node u in explored part S, to some node u in explored part S,
followed by a single edge e = (u, v) followed by a single edge e = (u, v)
add v to S, and set d[v] ← π(v).
・To recover path, set pred[v] ← e that achieves min.
d[v]
ℓe v ℓe v
d[u] d[u]
u u
S S
s s
10 11
12
Dijkstra′s algorithm: proof of correctness
P′ e y
x
s P
S u
v
16
Previous lecture code solutions
19
1. Dijkstra’s Algorithm
Def. A cycle is a path with no repeated nodes or edges other than the
2. Minimum Spanning Trees starting and ending nodes.
3. Prim’s Algorithm
2 3
4. Kruskal’s Algorithm
1 6 5 4
7 8
path P = { (1, 2), (2, 3), (3, 4), (4, 5), (5, 6) }
20
cycle C = { (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1) }
21
Cuts
Def. A cut is a partition of the nodes into two nonempty subsets S and V – S.
Def. The cutset of a cut S is the set of edges with exactly one endpoint in S.
2 3
1 6 5 4
7 8
cut S = { 4, 5, 8 }
cutset D = { (3, 4), (3, 5), (5, 6), (5, 7), (8, 7) }
22
Spanning Trees
In an undirected connected graph G, a spanning tree is a subgraph that is a tree and that
contains all vertices of G.
DFS and BFS on connected graphs both produce spanning trees of the graphs.
9
7
8
5 4 6
Spanning tree definition
graph G = (V, E)
spanning tree H = (V, T)
26
graph G = (V, E)
spanning tree H = (V, T) 28
Minimum Spanning Trees Minimum spanning tree (MST)
Def. Given a connected, undirected graph G = (V, E) with edge costs ce,
a minimum spanning tree (V, T ) is a spanning tree of G such that the sum
Given a connected undirected graph, G= (V,E) with positive edge weights, find the spanning tree
with the minimum sum of the edge weights. This is the minimum spanning tree (MST). of the edge costs in T is minimized.
The sum of the edge weights is the cost of the MST.
24
4
Let’s see how greedy algorithms can work for this.
6 23 9
18
10
9 16 5
11
7 7 Example for a shipping company: 8
7
8 Let vertices can be the factories, 10 14
17
11 edges connect factories if they are on a shipping route,
21
12 and the edge weight is the distance between the factories.
How can the shipping company visit all the factories, but in
the least distance traveled?
5 4 6 MST cost = 50 = 4 + 6 + 8 + 5 + 11 + 9 + 7
5 4
31
Applications
・Cluster analysis.
・Max bottleneck paths.
・Real-time face verification.
・Find road networks in satellite and aerial imagery.
・Reducing data storage in sequencing amino acids in a protein.
・Autoconfig protocol for Ethernet bridging to avoid cycles in a network.
・Approximation algorithms for NP-hard problems (e.g., TSP, Steiner tree).
・Network design (communication, electrical, hydraulic, computer, road).
32
Cut Property of MST Proof Exercise: Cycle Property of MST Proof
The cut property of MST: Let S be any subset of vertices S that is non-empty and not equal to V. If The cycle property of MST: For any cycle in the graph, G, if e is the maximum cost edge in the
e is the minimum cost edge between S and V-S, then every MST contains e. cycle, then e is not in any MST of G.
36 37
Objectives for Today
Exercise: Cycle Property of MST Proof
The cycle property of MST: For any cycle in the graph, G, if e is the maximum cost edge in the
cycle, then e is not in any MST of G.
1. Dijkstra’s Algorithm
Proof by contradiction:
2. Minimum Spanning Trees
3. Prim’s Algorithm
4. Kruskal’s Algorithm
38
39
Prim’s Algorithm
1
B C
4 2
3 40
6
A E
7
10
5
D
F
9
Prim′s algorithm Prim′s proof of correctness
S S
42 43
Loop invariant: T is a MST of the vertices in S. Loop invariant: T is a MST of the vertices in S.
Inductive step:
What happens on completion of the algorithm?
S S
44 45
Prim′s algorithm: implementation Objectives for Today
Theorem. Prim’s algorithm can be implemented to run in O(m log n) time.
Pf. Implementation almost identical to Dijkstra’s algorithm.
1. Dijkstra’s Algorithm
2. Minimum Spanning Trees
PRIM (V, E, c)
3. Prim’s Algorithm
__
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_
S ← , T ←
s ← any node in V. 4. Kruskal’s Algorithm
FOREACH v ≠ s : π [v] ← ∞, pred[v] ← null; π [s] ← 0.
Create an empty priority queue pq.
FOREACH v ∈ V : INSERT(pq, v, π[v]).
WHILE (IS-NOT-EMPTY(pq)) π[v] = cost of cheapest
u ← DEL-MIN(pq). known edge between v and S
S ← S ∪ { u }, T ← T ∪ { pred[u] }.
FOREACH edge e = (u, v) ∈ E with v ∉ S :
IF (ce < π [v])
47
DECREASE-KEY(pq, v, ce).
π [v] ← ce; pred[v] ← e.
46
Kruskal’s Algorithm
1
B C
4 2
3 48
6
A E
7
10
5
D
F
9
Kruskal′s algorithm Kruskal′s algorithm
Consider edges in ascending order of cost: Consider edges in ascending order of cost:
・Add to tree unless it would create a cycle. ・Add to tree unless it would create a cycle.
Theorem. Kruskal’s algorithm computes an MST. Theorem. Kruskal’s algorithm computes an MST.
Pf. One each iteration Kruskal’s consider adding some edge (u,v) to T Pf. One each iteration Kruskal’s consider adding some edge (u,v) to T
・Case 1: If u and v are already part of T ・Case 2: v or u (or both) are not in T yet
・Consider S the set of nodes with path to v
50 51
To create union sets, set the pointer to the element for larger set: Union is O(1)
To find set of element, traverse pointers. Since set doubles in size on union and cannot
have size more than n: Find-Set is O(log n)
52 53
Kruskal′s algorithm: implementation MINIMUM BOTTLENECK SPANNING TREE
Theorem. Kruskal’s algorithm can be implemented to run in O(m log m) time.
・Sort edges by cost. Problem. Given a connected graph G with positive edge costs, find a
・Use union–find data structure to dynamically maintain connected spanning tree that minimizes the most expensive edge.
components.
Goal. O(m log m) time or better.
KRUSKAL (V, E, c)
__
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
FOREACH v ∈ V : MAKE-SET(v).
FOR i = 1 TO m 4 10 11 9
(u, v) ← ei.
6 5 9
IF (FIND-SET(u) ≠ FIND-SET(v)) are u and v in
same component?
T ← T ∪ { ei }. 3 8 7 14 7
・Let e ∈ T* have cost strictly larger than min bottleneck cost. ・Define E = { e ∈ E : c ≤ x } and G = (V, E ).
x e x x
・Consider cut formed by deleting e from T*. ・Use binary search to find smallest value of x for which G is connected. x
1. Dijkstra’s Algorithm
2. Minimum Spanning Trees
3. Prim’s Algorithm
4. Kruskal’s Algorithm
58