Seyed Masoud Hosseini · Overview · Study log · Weekly summaries · Ideas · Search · Transcript · RSS feed

Design & Analysis of Algorithms · Lecture 16 of 34 · 1:22:09

12. Greedy Algorithms: Minimum Spanning Tree

12. Greedy Algorithms: Minimum Spanning Tree on YouTube

Study guide

What this lecture covers

The lecture answers how to find a minimum-weight spanning tree of a weighted, undirected, connected graph, and why a greedy strategy (rather than full dynamic programming) can solve it efficiently. It follows the all-pairs shortest paths lecture and continues the course's study of greedy algorithms, first introduced in lecture one.

By the end, you can state the minimum spanning tree (MST) problem, explain why brute-force search over spanning trees is exponential, prove the optimal-substructure and greedy-choice properties for MST using edge contraction and cut-and-paste arguments, and implement and justify both Prim's algorithm and Kruskal's algorithm.

Key ideas

  • Spanning tree: a subset of a graph's edges that is acyclic, connected, and touches every vertex; a minimum spanning tree (MST) minimizes the sum of edge weights among all spanning trees.
  • Brute force is exponential: the number of spanning trees can be exponential in the number of vertices (shown with a complete bipartite graph), so trying all of them is not viable.
  • Optimal substructure via edge contraction: if an edge e is known to be in some MST, contracting e (merging its endpoints, keeping the lighter of any duplicate edges) and solving the smaller problem, then adding e back, yields an MST of the original graph.
  • Greedy choice property (cut lemma): for any partition of the vertices into a set S and its complement, the minimum-weight edge crossing that cut is guaranteed to belong to some MST; this is proved with a cut-and-paste argument.
  • Cut-and-paste proofs: the standard technique for proving a greedy algorithm correct — assume an optimal solution lacking the desired edge, swap in that edge for another crossing edge, and show the result is still optimal.
  • Prim's algorithm: grows a single set S one vertex at a time, always adding the minimum-weight edge leaving S, using a priority queue keyed by the minimum crossing edge weight; runtime matches Dijkstra's, V log V + E with a Fibonacci heap.
  • Kruskal's algorithm: sorts all edges by weight and adds each edge if its endpoints are in different components, tracked with a union-find (disjoint-set) data structure; runtime is dominated by the sort, E log V, or linear with integer weights via radix sort.
  • Union-find: the disjoint-set data structure (make-set, union, find-set) that Kruskal's algorithm uses to test whether adding an edge would create a cycle.

Walkthrough

Greedy algorithms recap and the MST problem (0:00)

The lecture reintroduces greedy algorithms as strategies that make locally best choices, contrasted with dynamic programming's guess-and-recurse approach. It then defines a spanning tree (a connected, acyclic subgraph touching every vertex) and the minimum spanning tree problem: find the spanning tree of minimum total edge weight in a weighted, undirected, connected graph.

Why brute force fails, and setting up edge contraction (4:08)

Trying every spanning tree is shown to be exponential using a complete bipartite graph example with 2^n spanning trees. The lecture then sketches a dynamic-programming-style approach: guess an edge known to be in the MST, and simplify the graph around it.

Edge contraction and optimal substructure (15:39)

Contracting an edge e = (u,v) means merging u and v into a single vertex, removing e, and keeping only the lighter of any resulting duplicate edges. The lecture proves that if e belongs to some MST of the original graph G, then an MST of the contracted graph G/e, plus e added back, is an MST of G. Turning this into an algorithm that guesses which edge to contract at each step is still exponential, since there's no structure to exploit in the guess — but it sets up the idea of choosing the edge correctly instead of guessing.

The cut lemma and greedy choice property (29:13)

For any cut of the vertices into S and its complement, the lecture proves that the minimum-weight edge crossing the cut is guaranteed to be in some MST. The proof is a cut-and-paste argument: starting from an arbitrary MST that doesn't contain the target edge e, it identifies the edge e' that crosses the same cut on the unique path between e's endpoints, swaps e' for e, and shows the result is still a spanning tree with weight no greater than the original — hence also minimum. This lemma is the tool that lets both algorithms choose edges without guessing.

Prim's algorithm (42:36)

Prim's algorithm starts with a single vertex as the set S and repeatedly adds the minimum-weight edge leaving S, growing S by one vertex at a time. It's implemented with a priority queue where each vertex outside S is keyed by the minimum weight of an edge connecting it to S, updated via decrease-key operations exactly as in Dijkstra's algorithm. The lecture works a full numeric example on a small graph, then proves correctness by showing the tree built within S is always contained in some MST, using induction and the greedy choice property. Running time matches Dijkstra's: V log V + E with a Fibonacci heap.

Kruskal's algorithm (1:06:41)

Kruskal's algorithm sorts all edges by increasing weight and processes them in order, adding an edge to the growing forest only if its endpoints lie in different connected components — checked and updated using a union-find data structure (make-set, find-set, union). The lecture notes union-find was invented specifically to make this algorithm fast, and that with near-constant amortized union-find operations, the running time is dominated by the sort: E log V in general, or linear time if weights are small integers and radix sort applies. Correctness again follows from the greedy choice property, choosing the cut to be one endpoint's connected component versus everything else, and arguing by induction that every edge added is a safe edge.

Before you watch

  • Be comfortable with graph terminology: trees, spanning subgraphs, connected components, and cuts.
  • Review Dijkstra's algorithm and its use of a priority queue with decrease-key, since Prim's algorithm follows the same pattern.
  • Familiarity with the union-find (disjoint-set) data structure is useful, though the lecture reviews the operations it needs.

Check your understanding

  1. Why does contracting an edge known to be in the MST reduce the problem correctly, and why does turning this into an algorithm by guessing the edge still take exponential time?
  2. State the cut lemma (greedy choice property) for minimum spanning trees, and sketch how the cut-and-paste argument proves it.
  3. In Prim's algorithm, what invariant is maintained about the tree built so far, and how does the induction step use the greedy choice property to extend it?
  4. Why does Kruskal's algorithm need union-find rather than simply checking each edge against the tree built so far, and what determines whether its running time is E log V or linear?

Vocabulary

greedy algorithm (noun)
An algorithm that repeatedly makes the choice that looks best right now.
A greedy algorithm can find the minimum spanning tree efficiently.
spanning tree (noun)
A set of edges connecting every point in a graph without forming any loops.
A spanning tree touches all vertices using the fewest possible edges.
minimum spanning tree (MST) (noun)
The spanning tree whose total edge weight is as small as possible.
Finding the minimum spanning tree connects a network at lowest cost.
acyclic (adjective)
Containing no loops or cycles.
A spanning tree must always be acyclic.
edge contraction (noun)
Merging the two endpoints of an edge into a single point, removing that edge.
Edge contraction simplifies the graph before recursing.
cut (noun)
A way of splitting a graph's vertices into two separate groups.
The cut lemma looks at the cheapest edge crossing a cut.
cut-and-paste (noun)
A proof technique that swaps one solution's part for another to show it stays optimal.
A cut-and-paste argument proves the greedy choice is always safe.
priority queue (noun)
A data structure that always gives quick access to the smallest or largest item.
Prim's algorithm uses a priority queue to pick the next cheapest edge.
decrease-key (noun)
An operation that lowers the priority value of an existing item in a queue.
Prim's algorithm updates vertex priorities using decrease-key operations.
union-find (noun)
A data structure that tracks groups of connected items and can merge groups quickly.
Kruskal's algorithm uses union-find to detect when adding an edge would form a cycle.
connected component (noun)
A group of points all reachable from each other within a graph.
Two vertices in the same connected component are already linked.
radix sort (noun)
A sorting method that processes numbers digit by digit rather than by comparison.
Radix sort can make Kruskal's algorithm run in linear time for small integer weights.
brute force (noun)
A method that checks every possible option, without any clever shortcut.
Brute force over all spanning trees is exponential in the number of vertices.
bipartite graph (noun)
A graph whose vertices split into two groups with edges only between the groups.
A complete bipartite graph is used to show how many spanning trees exist.
duplicate (edge) (adjective)
Repeated, appearing more than once between the same two points.
Contraction keeps only the lighter of any duplicate edges.
guarantee (to belong) (verb)
To ensure something is definitely true or included.
The lemma guarantees the minimum crossing edge belongs to some MST.
invariant (noun)
A property that stays true throughout an algorithm's execution.
Prim's algorithm maintains an invariant about the tree built so far.
forest (graph) (noun)
A collection of separate trees with no cycles.
Kruskal's algorithm grows a forest of edges as it proceeds.
safe edge (phrase)
An edge proven to belong in some optimal solution without risk.
Every edge added by Kruskal's algorithm is proven a safe edge.
dominated (by) (adjective)
Controlled mainly by one factor that outweighs the others.
The running time is dominated by the initial sort.
invented (specifically for) (phrase)
Created for one particular purpose.
Union-find was invented specifically to make this algorithm fast.
sketch (a proof) (verb)
To outline the main steps of an argument without full detail.
The lecture sketches an approach before giving the full proof.
viable (adjective)
Practical and capable of actually working.
Trying every spanning tree is not a viable strategy.
amortized (adjective)
Measured as an average cost spread across a whole sequence of operations.
Union-find operations are nearly constant time when amortized.
sub-linear (adjective)
Growing slower than a straight proportional increase with input size.
Near-constant amortized cost is much better than a sub-linear bound would need.

From the YouTube description

MIT 6.046J Design and Analysis of Algorithms, Spring 2015
View the complete course: http://ocw.mit.edu/6-046JS15
Instructor: Erik Demaine

In this lecture, Professor Demaine introduces greedy algorithms, which make locally-best choices without regards to the future.

License: Creative Commons BY-NC-SA
More information at http://ocw.mit.edu/terms
More courses at http://ocw.mit.edu

← 11. Dynamic Programming: All-Pairs Shortest Paths · R6. Greedy Algorithms →