SlideShare a Scribd company logo
Design and Analysis of Algorithms
Greedy Algorithms
2
Greedy Algorithm
โ€ข Algorithms for optimization problems typically go
through a sequence of steps, with a set of choices at
each step.
โ€ข Greedy algorithms make the choice that looks best at
the moment.
โ€“ That is, it makes such a decision in the hope that this will
lead to a globally optimal solution
โ€ข This locally optimal choice may lead to a globally
optimal solution (i.e., an optimal solution to the
entire problem).
3
When can we use Greedy algorithms?
We can use a greedy algorithm when the following are true:
1) The greedy choice property: A A(greedy) choice.
2) The optimal substructure property: The optimal solution
contains within its optimal solutions to subproblems.
4
An Activity Selection Problem
(Conference Scheduling Problem)
โ€ข Input: A set of activities S = {a1,โ€ฆ, an}
โ€ข We have n proposed activities that wish to use a
resource, such as a lecture hall, which can serve only
one activity at a time.
โ€ข Each activity has start time and a finish time
โ€“ai=(si, fi)
โ€ข Two activities are compatible if and only if their interval
does not overlap
โ€ข Output: a maximum-size subset of mutually
compatible activities
5
The Activity Selection Problem
โ€ข Here are a set of start and finish times
โ€ข What is the maximum number of activities that can be
completed?
โ€ข {a3, a9, a11} can be completed
โ€ข But so can {a1, a4, a8โ€™ a11} which is a larger set
โ€ข But it is not unique, consider {a2, a4, a9โ€™ a11}
6
Input: list of time-intervals L
Output: a non-overlapping subset S of the intervals
Objective: maximize |S| 3,7
2,4
5,8
6,9
1,11
10,12
0,3
The Activity Selection Problem
7
Input: list of time-intervals L
Output: a non-overlapping subset S of the
intervals
Objective: maximize |S|
3,7
2,4
5,8
6,9
1,11
10,12
0,3
Answer = 3
The Activity Selection Problem
8
Algorithm 1:
1. sort the activities by the starting time
2. pick the first activity โ€œaโ€
3. remove all activities conflicting with โ€œaโ€
4. repeat
The Activity Selection Problem
9
Algorithm 1:
1. sort the activities by the starting time
2. pick the first activity โ€œaโ€
3. remove all activities conflicting with โ€œaโ€
4. repeat
The Activity Selection Problem
10
Algorithm 1:
1. sort the activities by the starting time
2. pick the first activity โ€œaโ€
3. remove all activities conflicting with โ€œaโ€
4. repeat
The Activity Selection Problem
11
Algorithm 2:
1. sort the activities by length
2. pick the shortest activity โ€œaโ€
3. remove all activities conflicting with โ€œaโ€
4. repeat
The Activity Selection Problem
12
Algorithm 2:
1. sort the activities by length
2. pick the shortest activity โ€œaโ€
3. remove all activities conflicting with โ€œaโ€
4. repeat
The Activity Selection Problem
13
Algorithm 2:
1. sort the activities by length
2. pick the shortest activity โ€œaโ€
3. remove all activities conflicting with โ€œaโ€
4. repeat
The Activity Selection Problem
14
Algorithm 3:
1. sort the activities by ending time
2. pick the activity which ends first
3. remove all activities conflicting with a
4. repeat
The Activity Selection Problem
15
The Activity Selection Problem
Algorithm 3:
1. sort the activities by ending time
2. pick the activity which ends first
3. remove all activities conflicting with a
4. repeat
16
The Activity Selection Problem
Algorithm 3:
1. sort the activities by ending time
2. pick the activity which ends first
3. remove all activities conflicting with a
4. repeat
17
The Activity Selection Problem
Algorithm 3:
1. sort the activities by ending time
2. pick the activity which ends first
3. remove all activities conflicting with a
4. repeat
18
Algorithm 3:
1. sort the activities by ending time
2. pick the activity โ€œaโ€ which ends first
3. remove all activities conflicting with โ€œaโ€
4. repeat
Theorem:
Algorithm 3 gives an optimal solution to
the activity selection problem.
The Activity Selection Problem
19
Activity Selection Algorithm
Idea: At each step, select the activity with the smallest finish time
that is compatible with the activities already chosen.
Greedy-Activity-Selector(s, f)
n <- length[s]
A <- {1} {Automatically select first
activity}
j <- 1 {Last activity selected so far}
for i <- 2 to n do
if si >= fj then
A <- A U {i} {Add activity i to the set}
j <- i {record last activity added}
return A
The idea is to always select the activity with the earliest finishing
time, as it will free up the most time for other activities.
20
The Activity Selection Problem
โ€ข Here are a set of start and finish times
โ€ข What is the maximum number of activities that can be
completed?
โ€ข {a3, a9, a11} can be completed
โ€ข But so can {a1, a4, a8โ€™ a11} which is a larger set
โ€ข But it is not unique, consider {a2, a4, a9โ€™ a11}
21
Interval Representation
Added in optimal Solution
Not Observed yet
Removed from the list
22
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
23
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
24
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
25
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
26
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
27
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
28
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
29
Why this Algorithm is Optimal?
โ€ข We will show that this algorithm uses the
following properties
โ€ข The problem has the optimal substructure
property
โ€ข The algorithm satisfies the greedy-choice
property
โ€ข Thus, it is Optimal
30
Optimal Substructure Property
โ€ข Base Case: For the smallest subproblem of size 1 (only one
activity), the optimal solution is trivially the activity itself.
โ€ข Inductive Hypothesis: Assume that we have already proven that
the optimal solution can be constructed for any subset of activities
with size k, where 1 โ‰ค k โ‰ค n - 1.
31
Optimal Substructure Property
Inductive Step: Now we want to prove that the optimal solution can
be constructed for a subset of activities with size k + 1.
Let's consider the set of activities {A1, A2, ..., Ak+1}. Since the activities
are sorted by finishing times,
the last activity in this set, Ak+1, will have the maximum finish time
among all activities.
We have two cases:
First case: Activity Ak+1 is included in the optimal solution.
โ€ข In this case, we need to find an optimal solution for the remaining
activities {A1, A2, ..., Ak} that are non-overlapping with Ak+1.
โ€ข By our inductive hypothesis, we know that an optimal solution can be
constructed for these k activities.
โ€ข Combining Ak+1 with this optimal solution gives us an optimal solution for
the entire set {A1, A2, ..., Ak+1}.
32
Optimal Substructure Property
First case: Activity Ak+1 is included in the optimal solution.
โ€ข In this case, we need to find an optimal solution for the remaining activities {A1, A2, ..., Ak}
that are non-overlapping with Ak+1.
โ€ข By our inductive hypothesis, we know that an optimal solution can be constructed for
these k activities.
โ€ข Combining Ak+1 with this optimal solution gives us an optimal solution for the entire set
{A1, A2, ..., Ak+1}
Second Case: Activity Ak+1 is not included in the optimal solution.
โ€ข In this case, we simply need to find an optimal solution for the activities
{A1, A2, ..., Ak}, which we have already assumed possible by our inductive
hypothesis.
Since we've covered both cases, we can conclude that the optimal solution
for the set {A1, A2, ..., Ak+1} can be constructed from the optimal solutions
of the smaller subproblems {A , A , ..., A },
33
Greedy-Choice Property
โ€ข Show there is an optimal solution that begins with a greedy
choice (with activity 1, which as the earliest finish time)
โ€ข Suppose A ๏ƒ S in an optimal solution
โ€“ Order the activities in A by finish time. The first activity in A is k
โ€ข If k = 1, the schedule A begins with a greedy choice
โ€ข If k ๏‚น 1, show that there is an optimal solution B to S that begins with the
greedy choice, activity 1
โ€“ Let B = A โ€“ {k} ๏ƒˆ {1}
โ€ข f1 ๏‚ฃ fk ๏ƒ  activities in B are disjoint (compatible)
โ€ข B has the same number of activities as A
โ€ข Thus, B is optimal
Example of Greedy Algorithm
โ€ข Fractional Knapsack
โ€ข Huffman Coding
โ€ข Minimum Spanning Tree โ€“ Prims and Kruskalโ€™s
โ€ข Activity Selection Problem
โ€ข Dijkstraโ€™s Shortest Path Algorithm
โ€ข Network Routing
โ€ข Job sequencing with deadlines
โ€ข Coin change problems
โ€ข Graph Coloring: Greedy algorithms can be used to color
a graph (though not necessarily optimally) by assigning
the next available color to a vertex.
34
35
Designing Greedy Algorithms
1. Cast the optimization problem as one for which:
โ€ข we make a choice and are left with only one subproblem
to solve
2. Prove the GREEDY CHOICE
โ€ข that there is always an optimal solution to the original
problem that makes the greedy choice
3. Prove the OPTIMAL SUBSTRUCTURE:
โ€ข the greedy choice + an optimal solution to the resulting
subproblem leads to an optimal solution
36
Example: Making Change
โ€ข Instance: amount (in cents) to return to customer
โ€ข Problem: do this using fewest number of coins
โ€ข Example:
โ€“ Assume that we have an unlimited number of coins of
various denominations:
โ€“ 1c (pennies), 5c (nickels), 10c (dimes), 25c (quarters), 1$
(loonies)
โ€“ Objective: Pay out a given sum $5.64 with the
smallest number of coins possible.
37
The Coin Changing Problem
โ€ข Assume that we have an unlimited number of coins of various
values:
โ€ข 1c (pennies), 5c (nickels), 10c (dimes), 25c (quarters), 1$ (loonies)
โ€ข Objective: Pay out a given sum S with the smallest number of
coins possible.
โ€ข The greedy coin changing algorithm:
โ€ข This is a ๏‘(m) algorithm where m = number of values.
while S > 0 do
c := value of the largest coin no larger than S;
num := S / c;
pay out num coins of value c;
S := S - num*c;
38
Example: Making Change
โ€ข E.g.:
$5.64 = $2 +$2 + $1 +
.25 + .25 + .10 +
.01 + .01 + .01 +.01
39
Making Change โ€“ A big problem
โ€ข Example 2: Coins are valued $.30, $.20, $.05,
$.01
โ€“ Does not have greedy-choice property, since $.40 is
best made with two $.20โ€™s, but the greedy solution will
pick three coins (which ones?)
40
The Fractional Knapsack Problem
โ€ข Given: A set S of n items, with each item i having
โ€“ bi - a positive benefit
โ€“ wi - a positive weight
โ€ข Goal: Choose items with maximum total benefit but with weight at
most W.
โ€ข If we are allowed to take fractional amounts, then this is the fractional
knapsack problem.
โ€“ In this case, we let xi denote the amount we take of item i
โ€“ Objective: maximize
โ€“ Constraint:
๏ƒฅ
๏ƒŽS
i
i
i
i w
x
b )
/
(
i
i
S
i
i w
x
W
x ๏‚ฃ
๏‚ฃ
๏‚ฃ
๏ƒฅ
๏ƒŽ
0
,
41
Example
โ€ข Given: A set S of n items, with each item i having
โ€“ bi - a positive benefit
โ€“ wi - a positive weight
โ€ข Goal: Choose items with maximum total benefit but with total weight at
most W.
Weight:
Benefit:
1 2 3 4 5
4 ml 8 ml 2 ml 6 ml 1 ml
$12 $32 $40 $30 $50
Items:
Value: 3
($ per ml)
4 20 5 50
10 ml
Solution: P
โ€ข 1 ml of 5
50$
โ€ข 2 ml of 3
40$
โ€ข 6 ml of 4
30$
โ€ข 1 ml of 2
4$
โ€œknapsackโ€
42
The Fractional Knapsack Algorithm
โ€ข Greedy choice: Keep taking item with highest value (benefit to
weight ratio)
โ€“ Since
Algorithm fractionalKnapsack(S, W)
Input: set S of items w/ benefit bi and weight wi; max. weight W
Output: amount xi of each item i to maximize benefit w/ weight at most W
for each item i in S
xi ๏‚ฌ 0
vi ๏‚ฌ bi / wi {value}
w ๏‚ฌ 0 {total weight}
while w < W
remove item i with highest vi
xi ๏‚ฌ min{wi , W - w}
w ๏‚ฌ w + min{wi , W - w}
๏ƒฅ
๏ƒฅ ๏ƒŽ
๏ƒŽ
๏€ฝ
S
i
i
i
i
S
i
i
i
i x
w
b
w
x
b )
/
(
)
/
(
43
The Fractional Knapsack Algorithm
โ€ข Running time: Given a collection S of n items, such that each item i
has a benefit bi and weight wi, we can construct a maximum-benefit
subset of S, allowing for fractional amounts, that has a total weight W in
O(nlogn) time.
โ€“ Use heap-based priority queue to store S
โ€“ Removing the item with the highest value takes O(logn) time
โ€“ In the worst case, need to remove all items

More Related Content

Similar to Design and Analysis of Algorithms (Greedy Algorithm) (20)

PPTX
Design and analysis of algorithms
PSG College of Technology
ย 
PPT
Chapter 17
ashish bansal
ย 
PPT
lec
farazch
ย 
PPT
lect
farazch
ย 
PPT
Lecture34
farazch
ย 
PPT
lect
farazch
ย 
PPT
lecture 26
sajinsc
ย 
PPT
Lecture34
guestc24b39
ย 
PDF
Sec16 greedy algorithm no1
Keisuke OTAKI
ย 
PPTX
Algorithm Design and Complexity - Course 5
Traian Rebedea
ย 
PPTX
Greedy aproach towards problem solution
Rashid Ansari
ย 
PDF
Unit V.pdf
KPRevathiAsstprofITD
ย 
PPT
GreedyAlgorithms.ppt
MdSazzadHossain74
ย 
PPT
GreedyAlgorithms.ppt
ssuser422644
ย 
PPT
Greedy method
Dr Shashikant Athawale
ย 
PPTX
Design and Analysis of Algorithm-Lecture.pptx
bani30122004
ย 
PPTX
daa-unit-3-greedy method
hodcsencet
ย 
PDF
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
MAJDABDALLAH3
ย 
PPTX
Chapter 5.pptx
Tekle12
ย 
PPT
test pre
farazch
ย 
Design and analysis of algorithms
PSG College of Technology
ย 
Chapter 17
ashish bansal
ย 
lec
farazch
ย 
lect
farazch
ย 
Lecture34
farazch
ย 
lect
farazch
ย 
lecture 26
sajinsc
ย 
Lecture34
guestc24b39
ย 
Sec16 greedy algorithm no1
Keisuke OTAKI
ย 
Algorithm Design and Complexity - Course 5
Traian Rebedea
ย 
Greedy aproach towards problem solution
Rashid Ansari
ย 
Unit V.pdf
KPRevathiAsstprofITD
ย 
GreedyAlgorithms.ppt
MdSazzadHossain74
ย 
GreedyAlgorithms.ppt
ssuser422644
ย 
Greedy method
Dr Shashikant Athawale
ย 
Design and Analysis of Algorithm-Lecture.pptx
bani30122004
ย 
daa-unit-3-greedy method
hodcsencet
ย 
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
MAJDABDALLAH3
ย 
Chapter 5.pptx
Tekle12
ย 
test pre
farazch
ย 

Recently uploaded (20)

PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
PDF
Laboratory Workflows Digitalized and live in 90 days with Scifeonยดs SAPPA P...
info969686
ย 
PPTX
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
ย 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 
PPTX
For my supp to finally picking supp that work
necas19388
ย 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
ย 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
ย 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
ย 
PDF
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
ย 
PPTX
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
ย 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
PPTX
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
ย 
PDF
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
ย 
PPTX
declaration of Variables and constants.pptx
meemee7378
ย 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
Laboratory Workflows Digitalized and live in 90 days with Scifeonยดs SAPPA P...
info969686
ย 
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
ย 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 
For my supp to finally picking supp that work
necas19388
ย 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
ย 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
ย 
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
ย 
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
ย 
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
ย 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
ย 
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
ย 
declaration of Variables and constants.pptx
meemee7378
ย 
Ad

Design and Analysis of Algorithms (Greedy Algorithm)

  • 1. Design and Analysis of Algorithms Greedy Algorithms
  • 2. 2 Greedy Algorithm โ€ข Algorithms for optimization problems typically go through a sequence of steps, with a set of choices at each step. โ€ข Greedy algorithms make the choice that looks best at the moment. โ€“ That is, it makes such a decision in the hope that this will lead to a globally optimal solution โ€ข This locally optimal choice may lead to a globally optimal solution (i.e., an optimal solution to the entire problem).
  • 3. 3 When can we use Greedy algorithms? We can use a greedy algorithm when the following are true: 1) The greedy choice property: A A(greedy) choice. 2) The optimal substructure property: The optimal solution contains within its optimal solutions to subproblems.
  • 4. 4 An Activity Selection Problem (Conference Scheduling Problem) โ€ข Input: A set of activities S = {a1,โ€ฆ, an} โ€ข We have n proposed activities that wish to use a resource, such as a lecture hall, which can serve only one activity at a time. โ€ข Each activity has start time and a finish time โ€“ai=(si, fi) โ€ข Two activities are compatible if and only if their interval does not overlap โ€ข Output: a maximum-size subset of mutually compatible activities
  • 5. 5 The Activity Selection Problem โ€ข Here are a set of start and finish times โ€ข What is the maximum number of activities that can be completed? โ€ข {a3, a9, a11} can be completed โ€ข But so can {a1, a4, a8โ€™ a11} which is a larger set โ€ข But it is not unique, consider {a2, a4, a9โ€™ a11}
  • 6. 6 Input: list of time-intervals L Output: a non-overlapping subset S of the intervals Objective: maximize |S| 3,7 2,4 5,8 6,9 1,11 10,12 0,3 The Activity Selection Problem
  • 7. 7 Input: list of time-intervals L Output: a non-overlapping subset S of the intervals Objective: maximize |S| 3,7 2,4 5,8 6,9 1,11 10,12 0,3 Answer = 3 The Activity Selection Problem
  • 8. 8 Algorithm 1: 1. sort the activities by the starting time 2. pick the first activity โ€œaโ€ 3. remove all activities conflicting with โ€œaโ€ 4. repeat The Activity Selection Problem
  • 9. 9 Algorithm 1: 1. sort the activities by the starting time 2. pick the first activity โ€œaโ€ 3. remove all activities conflicting with โ€œaโ€ 4. repeat The Activity Selection Problem
  • 10. 10 Algorithm 1: 1. sort the activities by the starting time 2. pick the first activity โ€œaโ€ 3. remove all activities conflicting with โ€œaโ€ 4. repeat The Activity Selection Problem
  • 11. 11 Algorithm 2: 1. sort the activities by length 2. pick the shortest activity โ€œaโ€ 3. remove all activities conflicting with โ€œaโ€ 4. repeat The Activity Selection Problem
  • 12. 12 Algorithm 2: 1. sort the activities by length 2. pick the shortest activity โ€œaโ€ 3. remove all activities conflicting with โ€œaโ€ 4. repeat The Activity Selection Problem
  • 13. 13 Algorithm 2: 1. sort the activities by length 2. pick the shortest activity โ€œaโ€ 3. remove all activities conflicting with โ€œaโ€ 4. repeat The Activity Selection Problem
  • 14. 14 Algorithm 3: 1. sort the activities by ending time 2. pick the activity which ends first 3. remove all activities conflicting with a 4. repeat The Activity Selection Problem
  • 15. 15 The Activity Selection Problem Algorithm 3: 1. sort the activities by ending time 2. pick the activity which ends first 3. remove all activities conflicting with a 4. repeat
  • 16. 16 The Activity Selection Problem Algorithm 3: 1. sort the activities by ending time 2. pick the activity which ends first 3. remove all activities conflicting with a 4. repeat
  • 17. 17 The Activity Selection Problem Algorithm 3: 1. sort the activities by ending time 2. pick the activity which ends first 3. remove all activities conflicting with a 4. repeat
  • 18. 18 Algorithm 3: 1. sort the activities by ending time 2. pick the activity โ€œaโ€ which ends first 3. remove all activities conflicting with โ€œaโ€ 4. repeat Theorem: Algorithm 3 gives an optimal solution to the activity selection problem. The Activity Selection Problem
  • 19. 19 Activity Selection Algorithm Idea: At each step, select the activity with the smallest finish time that is compatible with the activities already chosen. Greedy-Activity-Selector(s, f) n <- length[s] A <- {1} {Automatically select first activity} j <- 1 {Last activity selected so far} for i <- 2 to n do if si >= fj then A <- A U {i} {Add activity i to the set} j <- i {record last activity added} return A The idea is to always select the activity with the earliest finishing time, as it will free up the most time for other activities.
  • 20. 20 The Activity Selection Problem โ€ข Here are a set of start and finish times โ€ข What is the maximum number of activities that can be completed? โ€ข {a3, a9, a11} can be completed โ€ข But so can {a1, a4, a8โ€™ a11} which is a larger set โ€ข But it is not unique, consider {a2, a4, a9โ€™ a11}
  • 21. 21 Interval Representation Added in optimal Solution Not Observed yet Removed from the list
  • 22. 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 23. 23 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 24. 24 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 25. 25 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 26. 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 27. 27 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 28. 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 29. 29 Why this Algorithm is Optimal? โ€ข We will show that this algorithm uses the following properties โ€ข The problem has the optimal substructure property โ€ข The algorithm satisfies the greedy-choice property โ€ข Thus, it is Optimal
  • 30. 30 Optimal Substructure Property โ€ข Base Case: For the smallest subproblem of size 1 (only one activity), the optimal solution is trivially the activity itself. โ€ข Inductive Hypothesis: Assume that we have already proven that the optimal solution can be constructed for any subset of activities with size k, where 1 โ‰ค k โ‰ค n - 1.
  • 31. 31 Optimal Substructure Property Inductive Step: Now we want to prove that the optimal solution can be constructed for a subset of activities with size k + 1. Let's consider the set of activities {A1, A2, ..., Ak+1}. Since the activities are sorted by finishing times, the last activity in this set, Ak+1, will have the maximum finish time among all activities. We have two cases: First case: Activity Ak+1 is included in the optimal solution. โ€ข In this case, we need to find an optimal solution for the remaining activities {A1, A2, ..., Ak} that are non-overlapping with Ak+1. โ€ข By our inductive hypothesis, we know that an optimal solution can be constructed for these k activities. โ€ข Combining Ak+1 with this optimal solution gives us an optimal solution for the entire set {A1, A2, ..., Ak+1}.
  • 32. 32 Optimal Substructure Property First case: Activity Ak+1 is included in the optimal solution. โ€ข In this case, we need to find an optimal solution for the remaining activities {A1, A2, ..., Ak} that are non-overlapping with Ak+1. โ€ข By our inductive hypothesis, we know that an optimal solution can be constructed for these k activities. โ€ข Combining Ak+1 with this optimal solution gives us an optimal solution for the entire set {A1, A2, ..., Ak+1} Second Case: Activity Ak+1 is not included in the optimal solution. โ€ข In this case, we simply need to find an optimal solution for the activities {A1, A2, ..., Ak}, which we have already assumed possible by our inductive hypothesis. Since we've covered both cases, we can conclude that the optimal solution for the set {A1, A2, ..., Ak+1} can be constructed from the optimal solutions of the smaller subproblems {A , A , ..., A },
  • 33. 33 Greedy-Choice Property โ€ข Show there is an optimal solution that begins with a greedy choice (with activity 1, which as the earliest finish time) โ€ข Suppose A ๏ƒ S in an optimal solution โ€“ Order the activities in A by finish time. The first activity in A is k โ€ข If k = 1, the schedule A begins with a greedy choice โ€ข If k ๏‚น 1, show that there is an optimal solution B to S that begins with the greedy choice, activity 1 โ€“ Let B = A โ€“ {k} ๏ƒˆ {1} โ€ข f1 ๏‚ฃ fk ๏ƒ  activities in B are disjoint (compatible) โ€ข B has the same number of activities as A โ€ข Thus, B is optimal
  • 34. Example of Greedy Algorithm โ€ข Fractional Knapsack โ€ข Huffman Coding โ€ข Minimum Spanning Tree โ€“ Prims and Kruskalโ€™s โ€ข Activity Selection Problem โ€ข Dijkstraโ€™s Shortest Path Algorithm โ€ข Network Routing โ€ข Job sequencing with deadlines โ€ข Coin change problems โ€ข Graph Coloring: Greedy algorithms can be used to color a graph (though not necessarily optimally) by assigning the next available color to a vertex. 34
  • 35. 35 Designing Greedy Algorithms 1. Cast the optimization problem as one for which: โ€ข we make a choice and are left with only one subproblem to solve 2. Prove the GREEDY CHOICE โ€ข that there is always an optimal solution to the original problem that makes the greedy choice 3. Prove the OPTIMAL SUBSTRUCTURE: โ€ข the greedy choice + an optimal solution to the resulting subproblem leads to an optimal solution
  • 36. 36 Example: Making Change โ€ข Instance: amount (in cents) to return to customer โ€ข Problem: do this using fewest number of coins โ€ข Example: โ€“ Assume that we have an unlimited number of coins of various denominations: โ€“ 1c (pennies), 5c (nickels), 10c (dimes), 25c (quarters), 1$ (loonies) โ€“ Objective: Pay out a given sum $5.64 with the smallest number of coins possible.
  • 37. 37 The Coin Changing Problem โ€ข Assume that we have an unlimited number of coins of various values: โ€ข 1c (pennies), 5c (nickels), 10c (dimes), 25c (quarters), 1$ (loonies) โ€ข Objective: Pay out a given sum S with the smallest number of coins possible. โ€ข The greedy coin changing algorithm: โ€ข This is a ๏‘(m) algorithm where m = number of values. while S > 0 do c := value of the largest coin no larger than S; num := S / c; pay out num coins of value c; S := S - num*c;
  • 38. 38 Example: Making Change โ€ข E.g.: $5.64 = $2 +$2 + $1 + .25 + .25 + .10 + .01 + .01 + .01 +.01
  • 39. 39 Making Change โ€“ A big problem โ€ข Example 2: Coins are valued $.30, $.20, $.05, $.01 โ€“ Does not have greedy-choice property, since $.40 is best made with two $.20โ€™s, but the greedy solution will pick three coins (which ones?)
  • 40. 40 The Fractional Knapsack Problem โ€ข Given: A set S of n items, with each item i having โ€“ bi - a positive benefit โ€“ wi - a positive weight โ€ข Goal: Choose items with maximum total benefit but with weight at most W. โ€ข If we are allowed to take fractional amounts, then this is the fractional knapsack problem. โ€“ In this case, we let xi denote the amount we take of item i โ€“ Objective: maximize โ€“ Constraint: ๏ƒฅ ๏ƒŽS i i i i w x b ) / ( i i S i i w x W x ๏‚ฃ ๏‚ฃ ๏‚ฃ ๏ƒฅ ๏ƒŽ 0 ,
  • 41. 41 Example โ€ข Given: A set S of n items, with each item i having โ€“ bi - a positive benefit โ€“ wi - a positive weight โ€ข Goal: Choose items with maximum total benefit but with total weight at most W. Weight: Benefit: 1 2 3 4 5 4 ml 8 ml 2 ml 6 ml 1 ml $12 $32 $40 $30 $50 Items: Value: 3 ($ per ml) 4 20 5 50 10 ml Solution: P โ€ข 1 ml of 5 50$ โ€ข 2 ml of 3 40$ โ€ข 6 ml of 4 30$ โ€ข 1 ml of 2 4$ โ€œknapsackโ€
  • 42. 42 The Fractional Knapsack Algorithm โ€ข Greedy choice: Keep taking item with highest value (benefit to weight ratio) โ€“ Since Algorithm fractionalKnapsack(S, W) Input: set S of items w/ benefit bi and weight wi; max. weight W Output: amount xi of each item i to maximize benefit w/ weight at most W for each item i in S xi ๏‚ฌ 0 vi ๏‚ฌ bi / wi {value} w ๏‚ฌ 0 {total weight} while w < W remove item i with highest vi xi ๏‚ฌ min{wi , W - w} w ๏‚ฌ w + min{wi , W - w} ๏ƒฅ ๏ƒฅ ๏ƒŽ ๏ƒŽ ๏€ฝ S i i i i S i i i i x w b w x b ) / ( ) / (
  • 43. 43 The Fractional Knapsack Algorithm โ€ข Running time: Given a collection S of n items, such that each item i has a benefit bi and weight wi, we can construct a maximum-benefit subset of S, allowing for fractional amounts, that has a total weight W in O(nlogn) time. โ€“ Use heap-based priority queue to store S โ€“ Removing the item with the highest value takes O(logn) time โ€“ In the worst case, need to remove all items