Title: Greedy Algorithm
1Greedy Algorithm
Instructor Yao-Ting Huang
Bioinformatics Laboratory, Department of Computer
Science Information Engineering, National Chung
Cheng University.
2Greedy 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
3The 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
4Solving 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
5The Greedy Strategy
6The 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
8Activity-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
9Activity 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
10The 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
11A recursive solution
Let ci, j size of maximum-size subset of
mutually compatible activities in Sij
12Converting 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.
13Converting 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.
14Converting 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
15Converting 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.
16Activity 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
17An 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
18Example on 11 Activities
19(No Transcript)