Greedy Algorithm - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Greedy Algorithm

Description:

A greedy algorithm always makes the choice that looks best at the moment ... Dynamic programming can be overkill. Greedy algorithms tend to be easier to code. 3 ... – PowerPoint PPT presentation

Number of Views:314
Avg rating:3.0/5.0
Slides: 20
Provided by: yaotin
Category:

less

Transcript and Presenter's Notes

Title: Greedy Algorithm


1
Greedy Algorithm
Instructor Yao-Ting Huang
Bioinformatics Laboratory, Department of Computer
Science Information Engineering, National Chung
Cheng University.
2
Greedy Algorithms
  • A greedy algorithm always makes the choice that
    looks best at the moment
  • The hope a locally optimal choice will lead to a
    globally optimal solution.
  • For some problems, it works.
  • Dynamic programming can be overkill
  • Greedy algorithms tend to be easier to code

3
The Knapsack Problem
  • The 0-1 knapsack problem
  • The thief must choose among n items, where the
    ith item worth vi dollars and weighs wi pounds
  • Carrying at most W pounds, maximize value
  • Note assume vi, wi, and W are all integers
  • 0-1 b/c each item must be taken or left in
    entirety
  • A variation, the fractional knapsack problem
  • Thief can take fractions of items
  • Think of items in 0-1 problem as gold ingots, in
    fractional problem as buckets of gold dust

4
Solving The Knapsack Problem
  • The optimal solution to the fractional knapsack
    problem can be found with a greedy algorithm
  • How?
  • The optimal solution to the 0-1 problem cannot be
    found with greedy strategy.
  • Greedy strategy take in order of dollars/pound
  • Example 3 items weighing 10, 20, and 30 pounds,
    knapsack can hold 50 pounds

5
The Greedy Strategy
6
The Knapsack Problem Greedy Vs. Dynamic
  • The fractional problem can be solved greedily.
  • The 0-1 problem cannot be solved with a greedy
    approach.
  • It can be solved with dynamic programming.

7

Activity-Selection
  • The activity-selection problem is to select a
    maximum-size subset of mutually compatible
    activities.
  • Example

8
Activity-Selection
  • Formally
  • Given a set S of n activities
  • si start time of activity i
  • fi finish time of activity i
  • Find max-size subset A of compatible activities
  • Assume that f1 ? f2 ? ? fn

9
Activity SelectionRepeated Subproblems
  • Consider a recursive algorithm that tries all
    possible compatible subsets to find a maximal
    set, and notice repeated subproblems

S1?A?
yes
no
S2?A?
S-12?A?
no
no
yes
yes
S-1,2
S
S-2
S
10
The optimal substructure of the
activity-selection problem
  • Sijak?S fi ? sk lt fk ? sj,
  • Sij is the subset of activities in S that can
    start after activity ai finishes and finish
    before activity aj starts.
  • f00 and sn1 ?. Then S S0,n1, and the
    ranges for i and j are given by 0 ? i, j ? n1.
  • Let Aik and Akj be optimal solutions
  • Aij could be Aik ?ak?Akj

11
A recursive solution
Let ci, j size of maximum-size subset of
mutually compatible activities in Sij
  • Dynamic programming?

12
Converting a dynamic-programming solution to a
greedy solution
  • Consider any nonempty subproblem Sij, and let am
    be the activity in Sij with the earliest finish
    time
  • We will show
  • 1. Activity am is used in some maximum-size
    subset of mutually compatible activities of Sij.
  • 2. The subproblem Sim is empty, so that choosing
    am leaves the subproblem Smj as the only one that
    may be nonempty.

13
Converting a dynamic-programming solution to a
greedy solution
  • Let am be the activity in Sij with the earliest
    finish time
  • We wanna prove the subproblem Sim is empty.
  • Suppose Sim is nonempty.
  • There is some activity ak between i and m.
  • ak is earlier than am.
  • Contradiction.

14
Converting a dynamic-programming solution to a
greedy solution
  • Let am be the activity in Sij with the earliest
    finish time
  • Activity am is used in some maximum-size subset
    of mutually compatible activities of Sij.
  • Let Aij be a maximum-size subset of activities in
    Sij
  • Let ak is the first activity in Aij
  • Suppose ak ? am
  • Consider Aij - ak ? am
  • fm lt fk
  • This new set has the same size as Aij

15
Converting a dynamic-programming solution to a
greedy solution
  • Consider any nonempty subproblem Sij, and let am
    be the activity in Sij with the earliest finish
    time
  • 1. Activity am is used in some maximum-size
    subset of mutually compatible activities of Sij.
  • 2. The subproblem Sim is empty, so that choosing
    am leaves the subproblem Smj as the only one that
    may be nonempty.

16
Activity SelectionA Greedy Algorithm
  • So actual algorithm is simple
  • Sort the activities by finish time
  • Schedule the first activity (i.e., earliest
    finish time)
  • Then schedule the next activity in sorted list
    which starts after previous activity finishes
  • Repeat until no more activities
  • Intuition is even more simple
  • Always pick the shortest activity available at
    the time

17
An iterative greedy algorithm
  • GREEDY-ACTIVITY-SELECTOR(s, f)
  • 1 n ? lengths
  • 2 a ? a1
  • 3 i ? 1
  • 4 for m ? 2 to n
  • 5 do if sm ? fi
  • 6 then A ? A ? am
  • 7 i ? m
  • 8 return A

18
Example on 11 Activities
19
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com