1 of 48

Lecture 7

Greedy graph algorithms (minimum spanning trees)

CSE 421 Autumn 2026

1

2 of 48

Previously…

2

3 of 48

Greedy algorithms

3

What’s a greedy algorithm?

An algorithm that builds a solution by:

  • Considering objects one at a time, using a simple rule to decide on each object
  • Never going back and changing its mind.

4 of 48

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.

  • When designing greedy algorithms, there are often multiple equally intuitive options for what it means to be “greedy”.
  • May not be easy to prove correctness (typically need to find a key structural property of the problem)

Other considerations:

5 of 48

Interval scheduling

  • Input: start and end times for for “jobs”
  • Output: A maximal set of non-overlapping jobs

5

6 of 48

Greedy algorithm for interval scheduling

Greedy algorithm: Pick the next job to include in the set based on a simple rule.

6

Possible rules?

  • Earliest start time
  • Latest start time
  • Earliest end time
  • Latest end time
  • Shortest duration

These were the winners

7 of 48

Some takeaways

7

You’ll probably have a few ideas for “simple rules” for greedy algorithms! Check what happens on some simple examples to rule out some of them.

Proofs of correctness may not be easy: generally, try to find a reason why the greedy algorithm is staying ahead of all other algorithms at every step.

8 of 48

Today

8

  • Greedy graph algorithms (minimum spanning tree: Prim’s and Kruskal’s)

9 of 48

Minimum spanning trees

9

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.

10 of 48

Minimum spanning trees

Input: A connected graph , edge weights

Output: A tree such that every vertex is included and� is minimized. This is called a minimum spanning tree.

10

11 of 48

Minimum spanning forests

Input: A (not necessarily connected) graph , edge weights

Output: consisting of a minimum spanning tree for each connected component. This is called a minimum spanning forest.

11

12 of 48

Greedy MST algorithms

12

Build up the set of edges according to a simple rule.

Never go back and change.

Possible rules?

  • Starting from a source vertex , add the edge of minimum weight that connects to an unvisited vertex.
  • Simply add the edge of minimum weight that doesn’t create a cycle. Without requiring that the edge connects to the previous vertex set.

Prim’s algorithm

Kruskal’s algorithm

13 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

13

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

14 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

14

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

15 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

15

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

16 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

16

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

17 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

17

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

18 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

18

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

19 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

19

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

20 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

20

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

21 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

21

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

22 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

22

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

23 of 48

Prim’s algorithm

  • Pick a starting vertex . Let .
  • While doesn’t equal :
    • Find the edge of minimal weight .
    • Set .

23

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

24 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

24

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

25 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

25

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

26 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

26

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

27 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

27

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

28 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

28

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

29 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

29

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

30 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

30

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

31 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

31

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

32 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

32

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

33 of 48

Kruskal’s algorithm

  • Start with .
  • While there exists edges such that contains no cycles, add such edge of minimal weight to

33

2

-7

1

6

4

2

3

8

-4

-1

4

3

1

5

34 of 48

A unified argument for proving correctness

Of both Prim’s and Kruskal’s algorithm

  • A cut is a partition of the set of vertices into two pieces and .
  • The cut is denoted as .
  • An edge crosses the cut if and .
  • A subset of edges and a cut are compatible if no edge of crosses the cut.

34

35 of 48

A unified argument for proving correctness

Of both Prim’s and Kruskal’s algorithm

  • A cut is a partition of the set of vertices into two pieces and .
  • The cut is denoted as .
  • An edge crosses the cut if and .
  • A subset of edges and a cut are compatible if no edge of crosses the cut.

35

, the subset of non-dotted edges, is compatible with the cut

36 of 48

Proving correctness of greedy MST algorithms

Definition: An edge is safe for some subset of edges if there is some cut compatible with such that is the minimum weight edge crossing the cut.

36

37 of 48

Proving correctness of greedy MST algorithms

Definition: An edge is safe for some subset of edges if there is some cut compatible with such that is the minimum weight edge crossing the cut.

37

38 of 48

Proving correctness of greedy MST algorithms

Definition: An edge is safe for some subset of edges if there is some cut compatible with such that is the minimum weight edge crossing the cut.

38

39 of 48

Proving correctness of greedy MST algorithms

Definition: An edge is safe for some subset of edges if there is some cut compatible with such that is the minimum weight edge crossing the cut.

39

40 of 48

Proving correctness of greedy MST algorithms

Definition: An edge is safe for some subset of edges if there is some cut compatible with such that is the minimum weight edge crossing the cut.

Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.

40

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.

41 of 48

Proving correctness of greedy MST algorithms

Definition: An edge is safe for some subset of edges if there is some cut compatible with such that is the minimum weight edge crossing the cut.

Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.

Proof: Consider a greedy algorithm as above. Denote by the sequence of selected edges. We will show that each is always contained in an MST. In particular, if this is true for , then must be an MST.

41

42 of 48

Proving correctness of greedy MST algorithms

42

Definition: An edge is safe for some subset of edges if there is some compatible cut such that is the minimum weight edge crossing the cut.

Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.

Proof: Consider a greedy algorithm as above. Denote by the sequence of selected edges. We will show that each is always contained in an MST.

43 of 48

Proving correctness of greedy MST algorithms

43

Definition: An edge is safe for some subset of edges if there is some compatible cut such that is the minimum weight edge crossing the cut.

Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.

Proof: Consider a greedy algorithm as above. Denote by the sequence of selected edges. We will show that each is always contained in an MST. Suppose not.

Let be the first (safe) edge added by the algorithm such that is contained in an MST , but is not.

Black edges: �Red edges: other edges of the MST containing �Dotted edges: other edges in

44 of 48

Proving correctness of greedy MST algorithms

44

Definition: An edge is safe for some subset of edges if there is some compatible cut such that is the minimum weight edge crossing the cut.

Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.

Proof: Consider a greedy algorithm as above. Denote by the sequence of selected edges. We will show that each is always contained in an MST. Suppose not.

Let be the first (safe) edge added by the algorithm such that is contained in an MST , but is not. Then, contains exactly one cycle (since we added one edge to a sp. tree).

This cycle must contain some other edge that connects and .

Black edges: �Red edges: other edges of the MST containing �Dotted edges: other edges in

Why?

Final observation: removing from , removes the only cycle, and thus leaves a spanning tree!

Crucially, by our greedy rule, .

The weight of this tree is less than that of , so it’s also an MST! Contradiction.

45 of 48

Proving correctness of greedy MST algorithms

45

Definition: An edge is safe for some subset of edges if there is some compatible cut such that is the minimum weight edge crossing the cut.

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.

46 of 48

Proving correctness of greedy MST algorithms

Prim’s algorithm

46

Definition: An edge is safe for some subset of edges if there is some compatible cut such that is the minimum weight edge crossing the cut.

Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.

Adds the minimum-weight edge from the current tree to the rest.

Is this edge safe for ?

Yes, take to be the vertices spanned by . Then is trivially compatible with . Moreover, by definition, Prim picks to be the minimum-weight edge from to the rest, i.e. from to .

47 of 48

Proving correctness of greedy MST algorithms

Kruskal’s algorithm

47

Definition: An edge is safe for some subset of edges if there is some compatible cut such that is the minimum weight edge crossing the cut.

Theorem: Greedy algorithms that always choose edges that are safe for the current set of selected edges correctly compute an MST.

Adds the minimum-weight edge that does not create a cycle.

So, take to be the vertex set of the connected component containing . Then, by construction no edges of cross the cut .

Is this edge safe for the current edge set ?

By definition of Kruskal, does not create a cycle, so and must lie in different connected components of the subgraph .

Moreover, any edge that crosses the cut does not create a cycle in , and is the minimum-weight such edge.

48 of 48

Greedy algorithm proof strategies

  • “Greedy algorithm stays ahead”: Show that after each step of the greedy algorithm, its solution is at least as good as any other algorithms
  • Structural: Show the greedy algorithm produces a solution with a certain useful property. And any solution with this property must be optimal.
  • Exchange argument: We can gradually transform any solution into the one found by the greedy algorithm with each transformation only improving or maintaining the value of the current solution.

48

What we did to prove correctness of greedy algorithm that adds safe edges.