Lecture 7
Greedy graph algorithms (minimum spanning trees)
CSE 421 Autumn 2025
1
Previously…
2
Greedy algorithms
3
What’s a greedy algorithm?
An algorithm that builds a solution by:
In a nutshell: greedily do what appears best for you right here, right now.
Greedy algorithms
4
Pros
Cons
Simple
Rarely works
When it works, it’s typically very efficient
When it doesn’t work, often gives good approximations
greedily do what appears best for you right here, right now.
Other considerations:
Interval scheduling
5
Today
6
Minimum spanning trees
7
An electric company needs to choose where to build wires to connect all the cities below to their plant.
The company knows how much it would cost to lay electric wires between each pair of cities, and wants to find the cheapest way to ensure electricity flows from the plant to every city.
Minimum spanning trees
8
Minimum spanning forests
9
Greedy MST algorithms
10
Build up the set of edges according to a simple rule.
Never go back and change.
Possible rules?
Prim’s algorithm
Kruskal’s algorithm
Prim’s algorithm
11
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
12
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
13
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
14
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
15
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
16
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
17
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
18
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
19
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
20
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Prim’s algorithm
21
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
22
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
23
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
24
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
25
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
26
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
27
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
28
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
29
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
30
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
Kruskal’s algorithm
31
2
-7
1
6
4
2
3
8
-4
-1
4
3
1
5
A unified argument for proving correctness
Of both Prim’s and Kruskal’s algorithm
32
A unified argument for proving correctness
Of both Prim’s and Kruskal’s algorithm
33
Proving correctness of greedy MST algorithms
34
This theorem is quite powerful! Once we prove it, we’ll just have to show that both Prim and Kruskal select safe edges at each step.
Proving correctness of greedy MST algorithms
35
Proving correctness of greedy MST algorithms
36
Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.
Proving correctness of greedy MST algorithms
37
Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.
Proving correctness of greedy MST algorithms
38
Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.
Why?
Proving correctness of greedy MST algorithms
39
Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.
Now that we have this powerful hammer.. we can use it to prove correctness of Prim’s and Kruskal’s algorithms.
All that we need to do is argue that they always add an edge that is safe for the current edge set.
Proving correctness of greedy MST algorithms
Prim’s algorithm
40
Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.
Proving correctness of greedy MST algorithms
Kruskal’s algorithm
41
Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.
Greedy algorithm proof strategies
42
What we did to prove correctness of greedy algorithm that adds safe edges.