Greedy Algorithms
Optimization problems involve profit maximization (or cost minimization) under resource constraints p Greedy Algorithms build solutions step-bystep: each step uses locally optimal (short-sighted?) rules to optimize the objective function
p
Greed
p
Greed is good. Greed is right. Greed works. -- Gordon Gekko (Michael Douglas) in the movie Wall Street
Knapsack Problem
Interval scheduling
p
Set of jobs {1, 2, , n} p Job i starts at time si and ends at time fi p Two jobs compatible if they do not overlap p Goal: find the maximum subset of mutually compatible jobs
Interval scheduling
Greedy approach
n n n
Consider jobs in a specific order Choose the current best job Delete all other jobs that are not compatible with this job
Analysis
Running time = O(n log n) p Let A be the solution of the greedy algorithm
p
n
Analysis
Running time = O(n log n) p Let A be the solution of the greedy algorithm (earliest finish time algo.)
p
n n
OPT = j1,j2,,jm
Both A and OPT are arranged in the natural interval order p Need to show that k m
p
Base case: r = 1. Greedy picks the earliest finish time Assume f(ir-1) f(jr-1) Can the greedy algorithms rth job really finish later than OPTs?
Base case: r = 1. Greedy picks the earliest finish time Assume f(ir-1) f(jr-1)
Base case: r = 1. Greedy picks the earliest finish time Hypothesis: Let r > 1; assume f(ir-1) f(jr-1) Induction step
p p
f(ir-1) f(jr-1) s(jr) Job jr is available after jr-1 finishes for greedy algo; greedy goes for one with least finish time Hence f(ir) f(jr)
Greedy is optimal
p
Proof by contradiction: Assume A is not optimal; lets see what happens Let |A| = k and |OPT| = m > k f(ik) f(jk) s(jk+1) After finishing job jk, job jk+1 is available for greedy contradicts termination condition for greedy