Greedy Algorithms - Analysis of Algorithms - Lecture Slides, Slides of Design and Analysis of Algorithms

Main points of this lecture are: Greedy Algorithms, Dynamic Programming, Optimization Problems, Activity Selection, Set of Nonoverlapping, Optimal Substructure, Cut-And-Paste Argument, Recursive Solution, Making Greedy Choice, While Loop

Typology: Slides

2012/2013

Uploaded on 04/23/2013

asmita
asmita 🇮🇳

4.6

(34)

178 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Greedy Algorithms
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

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

  • S sorted by finish time:

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?
  1. Determine the optimal substructure.
  2. Develop a recursive solution.
  3. Show that if we make the greedy choice, only one subproblem remains.
  4. Prove that it’s always safe to make the greedy choice.
  5. Develop a recursive greedy algorithm.
  6. Convert it to an iterative algorithm.