ENع
←Prim's MST
Lesson 38 · Shortest paths and spanning trees

Minimum spanning trees with Prim

14 min11 steps5 questionsAdvanced
About this lesson

Join every vertex of a weighted graph as cheaply as possible. Prim's algorithm grows one tree from a start vertex and always adds the cheapest edge leaving it. Watch offers flow from the edges into a min-heap and come out smallest first on a 5-vertex, 7-edge graph, skip the stale ones that would close a cycle, and finish with a tree of weight 11. Then prove the greedy choice is safe with the cut property and count the cost. It uses the heaps from "Binary heaps: sift up and sift down" and "Priority queues and heapsort", and the exchange argument from "Greedy choices: the exchange argument and when greedy fails". "Minimum spanning trees with Kruskal" solves the same problem a different way.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 38 · Interactive
01 / 11

The cheapest network

Five places and seven possible cables, each with a cost: its weight. A spanning tree joins every vertex using V − 1 = 4 edges and no cycle. The minimum spanning tree (MST) is the one with the smallest total weight.

an undirected weighted graph, V = 5 vertices, E = 7 edges
  A – B   weight 4
  A – C   weight 1
  B – C   weight 2
  B – D   weight 5
  C – D   weight 7
  C – E   weight 6
  D – E   weight 3

spanning tree: V − 1 edges, reaches every vertex, no cycle
all weights differ, so exactly one MST exists
Loading figure…
or ← →
Read the lesson step by step
  1. The cheapest network

    Five places and seven possible cables, each with a cost: its weight. A spanning tree joins every vertex using V − 1 = 4 edges and no cycle. The minimum spanning tree (MST) is the one with the smallest total weight.

    an undirected weighted graph, V = 5 vertices, E = 7 edges
      A – B   weight 4
      A – C   weight 1
      B – C   weight 2
      B – D   weight 5
      C – D   weight 7
      C – E   weight 6
      D – E   weight 3
    
    spanning tree: V − 1 edges, reaches every vertex, no cycle
    all weights differ, so exactly one MST exists
  2. One tree, one queue

    Prim grows a single tree from a start vertex, here A. A priority queue (a min-heap) holds offers (weight, from, to) and always hands out the smallest. It starts with (0, A, A): reach A for free.

    seen = set of vertices in the tree
    start  push (0, A, A)
    
    tree  none   weight 0
    seen  {}
    queue (0, A, A)
    heappop always returns the smallest tuple
  3. A joins, offers its edges

    Pop (0, A, A). A is not in seen, so it joins the tree. Then every edge from A to a vertex outside the tree becomes an offer: (4, A, B) and (1, A, C) go into the queue.

    pop (0, A, A)  A joins the tree (no edge)
      push (4, A, B)
      push (1, A, C)
    
    tree  none   weight 0
    seen  {A}
    queue (1, A, C) (4, A, B)
  4. Take the cheapest edge out

    The smallest offer is (1, A, C). C is new, so edge A–C joins the tree: weight 1. C then offers its edges to B, D and E. Edges back to A are not offered: A is already in.

    pop (1, A, C)  C joins, edge A–C (1)
      push (2, C, B)
      push (7, C, D)
      push (6, C, E)
    
    tree  A–C   weight 1
    seen  {A, C}
    queue (2, C, B) (4, A, B) (6, C, E) (7, C, D)
  5. B joins through C

    Next comes (2, C, B), cheaper than the older (4, A, B). B joins by edge C–B, and the total is 3. B offers its edge to D, (5, B, D). The offer (4, A, B) is still waiting in the queue.

    pop (2, C, B)  B joins, edge C–B (2)
      push (5, B, D)
    
    tree  A–C B–C   weight 3
    seen  {A, C, B}
    queue (4, A, B) (5, B, D) (6, C, E) (7, C, D)
  6. Skip an offer that loops

    Out comes (4, A, B). Both A and B are already in the tree, so taking A–B would close the cycle A, C, B. Line 5 skips it. Leaving old offers in the queue and skipping them later is the lazy version of Prim.

    pop (4, A, B)  B is already in the tree: skip
    
    tree  A–C B–C   weight 3
    seen  {A, C, B}
    queue (5, B, D) (6, C, E) (7, C, D)
    a stale offer: both ends are already in seen
  7. D joins for 5

    The smallest offer is now (5, B, D), so D joins by edge B–D and the total is 8. D offers (3, D, E). E already had an offer of 6 through C, but the new one is cheaper.

    pop (5, B, D)  D joins, edge B–D (5)
      push (3, D, E)
    
    tree  A–C B–C B–D   weight 8
    seen  {A, C, B, D}
    queue (3, D, E) (6, C, E) (7, C, D)
  8. E joins for 3, not 6

    Pop (3, D, E): E joins by edge D–E and the total is 11. Notice that 3 came after 5. Prim takes the cheapest edge leaving the tree it has so far, not the cheapest edge in the whole graph.

    pop (3, D, E)  E joins, edge D–E (3)
    
    tree  A–C B–C B–D D–E   weight 11
    seen  {A, C, B, D, E}
    queue (6, C, E) (7, C, D)
    edges taken in the order 1, 2, 5, 3
  9. Empty queue: the MST

    The last offers, (6, C, E) and (7, C, D), are both stale and skipped. The queue is empty, so the loop ends. The tree has 4 edges and total weight 11: the minimum spanning tree.

    pop (6, C, E)  E is already in the tree: skip
    pop (7, C, D)  D is already in the tree: skip
    
    tree  A–C B–C B–D D–E   weight 11
    seen  {A, C, B, D, E}
    queue empty
    
    tree = [('A', 'C', 1), ('C', 'B', 2), ('B', 'D', 5), ('D', 'E', 3)]
    sum of weights = 11
  10. Why the cheapest is safe

    Go back to the tree {A, C}. Split the vertices into the tree and the rest: a cut. Its crossing edges cost 2, 4, 6 and 7. Any spanning tree must cross it; swapping in the cheapest crossing edge never costs more.

    cut property: the cheapest edge crossing a cut is in the MST
    tree side {A, C}, other side {B, D, E}
      crossing: C–B 2, A–B 4, C–E 6, C–D 7  (the queue holds exactly these)
    proof by exchange (the greedy argument):
      suppose an MST T leaves out C–B
      T + C–B has a cycle, which crosses the cut a second time at some e′
      T − e′ + C–B is a spanning tree that weighs less, since 2 < w(e′)
      contradiction, so C–B is in every MST
  11. How much it costs

    Each edge is pushed at most once, so the queue holds at most E + 1 offers. Here: 8 pushes, 8 pops, 3 stale. Each heap operation costs O(log E), so the total is O(E log E) = O(E log V).

    this run: V = 5, E = 7
      pushes  8 (the start + one per edge)
      pops    8 (5 join, 3 stale)
    
    E ≤ V², so log E ≤ 2 log V: O(E log E) = O(E log V)
    lazy (this code): space O(E)   eager, indexed heap: space O(V)
    Fibonacci heap: O(E + V log V)  (CLRS, MIT 6.046J)

Quiz Unlocks after the last step.