Greedy algorithms
For a greedy approach to be viable, the problem must have
- optimal substructure, that is, a solution of a subproblem is contained in the solution of the parent problem.
- the greedy choice property: a globally optimal solution can be arrived at by locally optimal choices.
[!Example] Minimum spanning tree
Recall Kruskal’s algorithm for MSTs.
Optimal substructure: Let be a MST of , and . If is a MST of , then is a MST of .
Proof: Let be a MST of that contains . is a spanning tree of . . Thus, .
This gives us a generic algorithm:
- Find select a “safe edge” .
- Contract the edge .
- Recurse on .
- Expand and add .
Greedy choice property: Given any , is a cut of .
Claim: For any cut in a graph any least weight crossing edge is in some spanning tree.
Prim’s algorithm