
Lecture 7
Greedy Algorithms
Making Change, Minimum Spanning Trees, Knapsack Problem
Basic Technique
Optimization problem
Many āfeasible solutionsā
Some solutions are better than others by some criteria
Objective function
Choices
We can break the problem down into a sequence of ālocalā choices
Selection function
Greedy Technique
Make a ālocalā choice that is ālocally optimalā
Proof Techniques
Alternation between the following two strategies
1) Prove the algorithm is NOT optimal by find a counter-example
2) Prove the algorithm IS optimal by observing specific structures
in the problem
Making Change
Objective function: number of coins
Finding a counter-example
Problem 6.2 (1, 3, 6, 12, 24, 30)
Problem 6.4 (1, 10, 25, 50, 100)
Finding a counter-example or proving the greedy algorithm is optimal
Problem 6.3 (1, 2.5, 5, 10, 20, 25, 50)
Problem 6.5 (1, 2, 4, 8, 16, 32, ā¦, 2n)
Is there a coin we could eliminate from Problem 6.2 and then make greedy optimal?
Which coin and why?
Minimum Spanning Tree
Problem Specification
Input: Weighted, connected graph G = (V,E)
Weight (length) function w on each edge e in E
Task: Compute a spanning tree of G of minimum total weight
Spanning tree: If there are n nodes in G, a spanning tree consists of n-1 edges
such that no cycles are formed
Kruskalās algorithm
- Selection algorithm: Add minimum weight edge that does not form a cycle
Implementation
- sort edges by increasing lengths
- Or use heap to organize edges
- creating heap can be done in ļ(|E|) timeE|E|) time) time
- finding element with minimum value may take ļ(log |E|) timeE|E|) time) time
- Initialize set of connected components to be each individual node
- use disjoint set structure from section 5.9
- Find(v): determine which CC a node v belongs to
- Merge(cc1, cc2): merge two connected components
Running time analysis
ļ(|E|) timeE|E|) time log |E|) timeE|E|) time) time dominates for sorting edges (or using heap implementation)