Lecture 1
Greedy Algorithms
- Builds up a solution by making a series of small decisions that optimize some objective.
- Make one irrevocable choice at a time, creating smaller and smaller sub-problems of the same kind as the original problem.
- There are many potential greedy strategies and picking the right one can be challenging.
A Scheduling Problem
You manage a giant space telescope.
- There are research projects that want to use it to make observations.
- Only one project can use the telescope at a time.
- Project needs the telescope starting at time and running for a length of time .
- Goal: schedule as many as possible
Formally
Input:
- Given a set of projects,
- Each request occupies interval , where
Goal: Choose a subset such that
- No two projects in have overlapping intervals.
- The number of selected projects is maximized.
Shortest Interval
Counter-example: [1,10],[9,12],[11,20]
Earliest start time
Counter-example: [1,10],[2,3],[4,5]
Fewest Conflicts
Counter-example: [1,2],[1,4],[1,4],[3,6],[7,8],[5,8],[5,8]
Earliest finish time
Correct... but why
Theorem of Greedy Strategy (Earliest Finishing Time)
Say this greedy strategy (Earliest Finishing Time) picks a set of intervals, some other strategy picks a set of intervals.
Assume sorted by finishing time
We want to show that
Lemma: For all
We proceed the proof by induction.
-
Base Case, when r=1. is the earliest finish time, and cannot pick a interval with earlier finish time, so
-
Inductive step, when r>1. Since is the earliest finish time, so for any set in , , for any inserted to , it can also be inserted to . So cannot pick an interval with earlier finish time than since it will also be picked by definition if is the optimal solution .
Problem of “Greedy Stays Ahead” Proof
- Every problem has very different theorem.
- It can be challenging to even write down the correct statement that you must prove.
- We want a systematic approach to prove the correctness of greedy algorithms.
Road Map to Prove Greedy Algorithm
1. Make a Choice
Pick an interval based on greedy choice, say
Proof: Greedy Choice Property: Show that using our first choice is not "fatal" – at least one optimal solution makes this choice.
Techniques: Exchange Argument: "If an optimal solution does not choose , we can turn it into an equally good solution that does."
Let be any optimal solution for project set .
- If , we are done.
- Otherwise, let be the optimal solution from that does not pick . We create another solution that replace with , and prove that the is as optimal as
2. Create a smaller instance of the original problem
has the same optimization criteria.
Proof: Inductive Structure: Show that after making the first choice, we're left with a smaller version of the same problem, whose solution we can safely combine with the first choice.
Let be the subproblem left after making first choice in problem and let be an optimal solution to . Then is an optimal solution to .
projects conflicting with
3. Solution: Union of choices that we made
Union of choices that we made.
Proof: Optimal Substructure: Show that if we solve the subproblem optimally, adding our first choice creates an optimal solution to the whole problem.
Let be the first choice, be the subproblem left after making in problem , be an optimal solution to . We claim that is an optimal solution to .
We proceed the proof by contradiction.
Assume that is not optimal.
By Greedy choice property . we already know that an optimal solution for problem that contains . If is not optimal, . Then since is also a feasible solution to . which leads to contradiction that is an optimal solution to .
4. Put 1-3 together to write an inductive proof of the Theorem
This is independent of problem, same for every problem.
Use scheduling problem as an example:
Theorem: given a scheduling problem , if we repeatedly choose the remaining feasible project with the earliest finishing time, we will construct an optimal feasible solution to .
Proof: We proceed by induction on . (based on the size of problem ).
- Base case: .
- Inductive step.
- Inductive hypothesis: For all problems of size , earliest finishing time (EFT) gives us an optimal solution.
- EFT is optimal for problem of size .
- Proof: Once we pick q, because of greedy choice. interval that conflict with . , By Inductive hypothesis, EFT gives us an optimal solution to , but by inductive substructure, and optimal substructure. (optimal solution to ), we have optimal solution to .
this step always holds as long as the previous three properties hold, and we don't usually write the whole proof.
# Algorithm construction for Interval scheduling problem
def schedule(p):
# sorting takes O(n)=nlogn
p=sorted(p,key=lambda x:x[1])
res=[P[0]]
# O(n)=n
for i in p[1:]:
if res[-1][-1]<i[0]:
res.append(i)
return res
Extra Examples:
File compression problem
You have files of different sizes .
You want to merge them to create a single file. takes time and creates a file of size .
Goal: Find the order of merges such that the total time to merge is minimized.
Thinking process: The merge process is a binary tree and each of the file is the leaf of the tree.
The total time required =, where is the depth of the file in the compression tree.
So compressing the smaller file first may yield a faster run time.
Proof:
Greedy Choice Property
Construct part of the solution by making a locally good decision.
Lemma: some optimal solution that merges the two smallest file first, lets say
Proof: Exchange argument
- Case 1: Optimal choice already merges , done. Time order does not matter in this problem at some point.
- eg: [2,2,3], merge 2,3 and 2,2 first don't change the total cost
- Case 2: Optimal choice does not merges and .
- Suppose the optimal solution merges as the deepest merge.
- Then . Exchanging with would yield a strictly less greater solution since already smallest.
Inductive Structure
- We can combine feasible solution to the subproblem with the greedy choice to get a feasible solution to
- After making greedy choice , we are left with a strictly smaller subproblem with the same optimality criteria of the original problem
Proof: Optimal Substructure: Show that if we solve the subproblem optimally, adding our first choice creates an optimal solution to the whole problem.
Let be the first choice, be the subproblem left after making in problem , be an optimal solution to . We claim that is an optimal solution to .
We proceed the proof by contradiction.
Assume that is not optimal.
By Greedy choice property . we already know that is optimal solution that contains . Then is also feasible solution to . which is an optimal solution to which leads to contradiction.
Proof: Smaller problem size
After merging the smallest two files into one, we have strictly less files waiting to merge.
Optimal Substructure
- We can combine optimal solution to the subproblem with the greedy choice to get a optimal solution to
Step 4 ignored, same for all greedy problems.
Conclusion: Greedy Algorithm
- Algorithm
- Runtime Complexity
- Proof
- Greedy Choice Property
- Construct part of the solution by making a locally good decision.
- Inductive Structure
- We can combine feasible solution to the subproblem with the greedy choice to get a feasible solution to
- After making greedy choice , we are left with a strictly smaller subproblem with the same optimality criteria of the original problem
- Optimal Substructure
- We can combine optimal solution to the subproblem with the greedy choice to get a optimal solution to
- Greedy Choice Property
- Standard Contradiction Argument simplifies it
Review:
Essence of master method
Let and be constants, let be a function, and let be defined on the nonnegative integers by the recurrence
where we interpret to mean either ceiling or floor of . Then has to following asymptotic bounds.
-
Case I: if ( "dominates" ) where , then
-
Case II: if , ( have no dominate) then
Extension for
-
if
-
if
-
if
-
-
Case III: if ( "dominates" ) for some constant , and if a for some constant then for all sufficiently large ,