6.046 greedy algorithms
minimum spanning tree¶
Notions+
- tree, connected acyclic graph
- spanning, it contains all the vertices
- spanning tree of graph G is the subset of edges of G that form a tree & hit all vertices of G
- E, undirected edges
- The fastest MST is a randomized algorithm with O(V+E) proposed by KKT in 1993
MST: given graph G=(V, E) & edge weights w for E , find a spanning tree T of minimum weight
Goal: exponential # spanning tree -> polynomial (geedy) -> near linear time
greedy properties¶
- optimal substructure: optimal solution to subproblems -> optimal solution to a problem
- greedy choice property: locally optimal choiced lead to a globally optimal solution
optimal substructure for MST¶
If e = {u, v} is an edge of some MST:
-
remove edge e: still connected by red lines, not working
-
contract e: merge u,v, contraction preserves connectivity.
1. If T’ is a MST of G/e, then T’ {e} is an MST of G. Proof. Say e
MST T* & G
- => T*/e is a spanning tree of G’ = G/{e}, by connectivity.
- T’ is an MST of G’ =>
- =>
- DP algs: Lemma 1 prove the correctness of this algorithm
- guess edge e in a MST
- contract e
- recurse
- decontract e
- add e to MST
- analysis: reduce exponential time by taking a good guess
greedy choice property for MST¶
- cut and paste argument for greedy proof
- cut (S, V-S), a crossing edge is the edge that crosses the cut
- only modify edges crossing cut
- Lemma2 provides good guess
2. Consider any cut (S, V-S), S V. Say e is a least-weight edge crossing cut, e = {u, v}, u S, v V-S, then e some MST. Proof. Let T* be a MST of G. Say e
T* (modify T* to include e).
- => there must be e’
T* crossing the cut. is spanning tree (graph theory) - greedy part:
- =>
is MST
Prim’s Algorithm¶
- Idea: choose a cut - a single vertex, an arbitrary start vertex s. And find the minimum weight edge sequentially to connect all vertices. Return our MST.
- Design: maintain priority queue Q on V-S where v.key = min{w(u, v) | u
S}; Choose arbitrary start vertex s ∈ V, s.key = 0 - Proof for correctness
- Analysis:
- O(E) results from the sum of # of adjacent vertices in G, which is 2|E| by the handshaking lemma
Invariants of Prim’s Algorithm are:
- v
S => v.key = min{w(u, v) | u S} - Tree
within S MST of G Proof of 2.
Kruskal’s Algorithm¶
- Idea: take the globally lowest-weight edge and contract it.
- Design: Maintain connected components that have been added to the MST so far T, in a Union-Find structure
- Proof for correctness
- Analysis:
- Union-Find data structures
- If w in [0, E^O(1)], using counting sort or similar can make Kruskal’s beat Prim’s
1 | Initialize T = 0 |
. The tree-so-far T MST T*
OLC¶
- exponential number of edges to guess that form the MST.
- handshaking lemma is sum of in-degree vertices and out-degree vertices are both |E|