Download Greedy Algorithms - Analysis of Algorithms - Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!
Greedy Algorithms
Greedy Algorithms
- Similar to dynamic programming.
- Used for optimization problems.
- Idea
- When we have a choice to make, make the one that looks best right now. Make a locally optimal choice in hope of getting a globally optimal solution.
- Greedy algorithms don’t always yield an optimal solution. But sometimes they do. We’ll see a problem for which they do. Then we’ll look at some general characteristics of when greedy algorithms give optimal solutions.
Activity selection
- Note
- Could have many other objectives:
- Schedule room for longest time.
- Maximize income rental fees.
- Assume that activities are sorted by finish
time: f 1 ≤ f 2 ≤ f 3 ≤... ≤ fn-1 ≤ fn.
Example
Optimal substructure of activity
selection
- Let Aij be a maximum-size set of mutually
compatible activities in S ij.
- Let ak ∈ Aij be some activity in Aij. Then we
have two subproblems:
- Find mutually compatible activities in Sik (activities that start after a (^) i finishes and that finish before a (^) k starts).
- Find mutually compatible activities in Skj (activities that start after a (^) k finishes and that finish before a (^) j starts).
Optimal substructure of activity
selection
- Let
- A (^) ik = A (^) ij ∩ Sik = activities in A (^) ij that finish before a (^) k starts ;
- A (^) kj = A (^) ij ∩ Skj = activities in A (^) ij that start afer a (^) k finishes :
- Then A (^) ij = A (^) ik ∪{a (^) k } ∪A (^) kj
- ⇒ ⎮A (^) ij⎮= ⎮A (^) ik⎮ + ⎮A (^) kj⎮+ 1.
- Claim
- Optimal solution A (^) ij must include optimal solutions for the two subproblems for S (^) ik and S (^) kj.
One recursive solution
- Since optimal solution A (^) ij must include optimal solutions to the subproblems for S (^) ik and S (^) kj, could solve by dynamic programming.
- Let c[i, j] = size of optimal solution for Sij. Then
- c[i, j] = c[i, k] + c[k, j] + 1:
- But we don’t know which activity a (^) k to choose, so we have to try them all:
- Could then develop a recursive algorithm and memoize it. Or could develop a bottom-up algorithm and fill in table entries.
- Instead, we will look at a greedy approach.
Making the greedy choice
- Choose an activity to add to optimal solution before solving subproblems. For activity-selection problem, we can get away with considering only the greedy choice: the activity that leaves the resource available for as many other activities as possible.
- Question: Which activity leaves the resource available for the most other activities?
- Answer: The first activity to finish. (If more than one activity has earliest finish time, can choose any such activity.)
- Since activities are sorted by finish time, just choose activity a 1.
Making the greedy choice
- Making greedy choice of a 1 ⇒ S 1 remains as
only subproblem to solve.
- By optimal substructure, if a 1 is in an optimal
solution, then an optimal solution to the
original problem consists of a 1 plus all
activities in an optimal solution to S 1.
- But need to prove that a 1 is always part of
some optimal solution.
Theorem
- If S (^) k is nonempty and a (^) m has the earliest finish time in S (^) k, then a (^) m is included in some optimal solution.
- Proof Let Ak be an optimal solution to S (^) k, and let a (^) j have the earliest finish time of any activity in Ak. If a (^) j = a (^) m , done. Otherwise, let A’k = Ak – {a (^) j} ∪ {a (^) m} but with a (^) m substituted for a (^) j.
- Claim
- Activities in A’k are disjoint.
- Proof Activities in Ak are disjoint, a (^) j is first activity in Ak to finish, and f (^) m ≤ f (^) j. (proves claim)
- Since |A’k| = |Ak|, conclude that A’k is an optimal solution to S (^) k, and it includes a (^) m. (proves theorem)
Recursive greedy algorithm
- Start and finish times are represented by arrays s
and f , where f is assumed to be already sorted in monotonically increasing order.
- To start, add fictitious activity a 0 with f 0 = 0, so
that S 0 = S, the entire set of activities.
- Procedure REC-ACTIVITY-SELECTOR takes as
parameters the arrays s and f, index k of current subproblem, and number n of activities in the original problem.
Recursive greedy algorithm
Iterative greedy algorithm
- Can convert the recursive algorithm to an iterative
one. It’s already almost tail recursive.
Greedy strategy
- The choice that seems best at the moment is the one we go with.
- What did we do for activity selection?
- Determine the optimal substructure.
- Develop a recursive solution.
- Show that if we make the greedy choice, only one subproblem remains.
- Prove that it’s always safe to make the greedy choice.
- Develop a recursive greedy algorithm.
- Convert it to an iterative algorithm.