SlideShare a Scribd company logo
Greedy Algorithms
Greedy algorithms
• A greedy algorithm always makes the choice that
looks best at the moment
– My everyday examples:
• Driving in Los Angeles, NY, or Boston for that matter
• Playing cards
• Invest on stocks
• Choose a university
– The hope: a locally optimal choice will lead to a
globally optimal solution
– For some problems, it works
• greedy algorithms tend to be easier to code
An Activity Selection Problem
(Conference Scheduling Problem)
• Input: A set of activities S = {a1,…, an}
• 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
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}
Interval Representation
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Early Finish Greedy
• Select the activity with the earliest finish
• Eliminate the activities that could not be
scheduled
• Repeat!
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Assuming activities are sorted by
finish time
Why it is Greedy?
• Greedy in the sense that it leaves as much
opportunity as possible for the remaining
activities to be scheduled
• The greedy choice is the one that maximizes
the amount of unscheduled time remaining
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
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
Optimal Substructures
– Once the greedy choice of activity 1 is made, the problem
reduces to finding an optimal solution for the activity-selection
problem over those activities in S that are compatible with
activity 1
• Optimal Substructure
• If A is optimal to S, then A’ = A – {1} is optimal to S’={i S: si  f1}
• Why?
– If we could find a solution B’ to S’ with more activities than A’, adding
activity 1 to B’ would yield a solution B to S with more activities than A
 contradicting the optimality of A
– After each greedy choice is made, we are left with an
optimization problem of the same form as the original problem
• By induction on the number of choices made, making the greedy
choice at every step produces an optimal solution
Elements of Greedy Strategy
• An greedy algorithm makes a sequence of choices, each
of the choices that seems best at the moment is chosen
– NOT always produce an optimal solution
• Two ingredients that are exhibited by most problems
that lend themselves to a greedy strategy
– Greedy-choice property
– Optimal substructure
Greedy-Choice Property
• A globally optimal solution can be arrived at by
making a locally optimal (greedy) choice
– Make whatever choice seems best at the moment and
then solve the sub-problem arising after the choice is
made
– The choice made by a greedy algorithm may depend on
choices so far, but it cannot depend on any future
choices or on the solutions to sub-problems
• Of course, we must prove that a greedy choice at
each step yields a globally optimal solution
Optimal Substructures
• A problem exhibits optimal substructure if an
optimal solution to the problem contains
within it optimal solutions to sub-problems
– If an optimal solution A to S begins with activity 1,
then A’ = A – {1} is optimal to S’={i S: si  f1}
Knapsack Problem
• One wants to pack n items in a luggage
– The ith item is worth vi dollars and weighs wi pounds
– Maximize the value but cannot exceed W pounds
– vi , wi, W are integers
• 0-1 knapsack  each item is taken or not taken
• Fractional knapsack  fractions of items can be taken
• Both exhibit the optimal-substructure property
– 0-1: If item j is removed from an optimal packing, the remaining
packing is an optimal packing with weight at most W-wj
– Fractional: If w pounds of item j is removed from an optimal
packing, the remaining packing is an optimal packing with weight
at most W-w that can be taken from other n-1 items plus wj – w of
item j
Greedy Algorithm for Fractional
Knapsack problem
• Fractional knapsack can be solvable by the greedy
strategy
– Compute the value per pound vi/wi for each item
– Obeying a greedy strategy, take as much as possible of the
item with the greatest value per pound.
– If the supply of that item is exhausted and there is still more
room, take as much as possible of the item with the next value
per pound, and so forth until there is no more room
– O(n lg n) (we need to sort the items by value per pound)
– Greedy Algorithm?
– Correctness?
O-1 knapsack is harder!
• 0-1 knapsack cannot be solved by the greedy
strategy
– Unable to fill the knapsack to capacity, and the empty
space lowers the effective value per pound of the
packing
– We must compare the solution to the sub-problem in
which the item is included with the solution to the sub-
problem in which the item is excluded before we can
make the choice
– Dynamic Programming
Greedy Algorithms WITH Activity Selection Problem.ppt
Optimal substructures
• Define the following subset of activities which are activities that
can start after ai finishes and finish before aj starts
• Sort the activities according to finish time
• We now define the the maximal set of activities from i to j as
• Let c[i,j] be the maximal number of activities
• We can solve this using dynamic programming, but a simpler
approach exists
• Our recurrence relation for finding c[i, j] becomes

More Related Content

What's hot (20)

PPT
Branch & bound
kannanchirayath
 
PPTX
Heap Sort in Design and Analysis of algorithms
samairaakram
 
PPTX
Bloom filters
Devesh Maru
 
PPT
Dinive conquer algorithm
Mohd Arif
 
PPTX
Dynamic programming class 16
Kumar
 
PPTX
String matching algorithms
Ashikapokiya12345
 
PPT
Master method theorem
Rajendran
 
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
PPTX
Greedy method
Anusha sivakumar
 
PPTX
Analysis of algorithm
Rajendra Dangwal
 
PPTX
Greedy Algorithms
Amrinder Arora
 
PPT
Heapsort ppt
Mariam Saeed
 
PPT
Minimum spanning tree
Hinal Lunagariya
 
PPTX
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
PDF
08 Hash Tables
Andres Mendez-Vazquez
 
PPT
Greedy algorithms
Rajendran
 
PPTX
Webinar : P, NP, NP-Hard , NP - Complete problems
Ziyauddin Shaik
 
PPTX
Kruskal & Prim's Algorithm
Ifad Rahman
 
PPT
Heap sort
Mohd Arif
 
PPTX
Solving recurrences
Megha V
 
Branch & bound
kannanchirayath
 
Heap Sort in Design and Analysis of algorithms
samairaakram
 
Bloom filters
Devesh Maru
 
Dinive conquer algorithm
Mohd Arif
 
Dynamic programming class 16
Kumar
 
String matching algorithms
Ashikapokiya12345
 
Master method theorem
Rajendran
 
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
Greedy method
Anusha sivakumar
 
Analysis of algorithm
Rajendra Dangwal
 
Greedy Algorithms
Amrinder Arora
 
Heapsort ppt
Mariam Saeed
 
Minimum spanning tree
Hinal Lunagariya
 
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
08 Hash Tables
Andres Mendez-Vazquez
 
Greedy algorithms
Rajendran
 
Webinar : P, NP, NP-Hard , NP - Complete problems
Ziyauddin Shaik
 
Kruskal & Prim's Algorithm
Ifad Rahman
 
Heap sort
Mohd Arif
 
Solving recurrences
Megha V
 

Similar to Greedy Algorithms WITH Activity Selection Problem.ppt (20)

PPT
Greedy algorithms
Md. Shafiuzzaman Hira
 
PPT
lec
farazch
 
PPT
lect
farazch
 
PPT
Lecture34
farazch
 
PPT
lect
farazch
 
PPT
lecture 26
sajinsc
 
PPT
Lecture34
guestc24b39
 
PPSX
Design and Analysis of Algorithms (Greedy Algorithm)
SababAshfakFahim
 
PDF
Greedy algorithm activity selection fractional
Amit Kumar Rathi
 
PPT
CSS 332 : Algorithms - greedy Algorithms
SohamSaha49
 
PPT
Greedy1.ppt
PallaviDhade1
 
PDF
greedy method.pdf
deepakjoshi29912
 
PDF
Greedy is Good
skku_npc
 
PPT
Greedy method by Dr. B. J. Mohite
Zeal Education Society, Pune
 
PPT
5.1 greedyyy 02
Krish_ver2
 
PPTX
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
PDF
12 Greeddy Method
Andres Mendez-Vazquez
 
PPTX
Module 3_DAA (2).pptx
AnkitaVerma776806
 
PDF
Sec16 greedy algorithm no1
Keisuke OTAKI
 
PPT
GreedyAlgorithms.ppt
MdSazzadHossain74
 
Greedy algorithms
Md. Shafiuzzaman Hira
 
lec
farazch
 
lect
farazch
 
Lecture34
farazch
 
lect
farazch
 
lecture 26
sajinsc
 
Lecture34
guestc24b39
 
Design and Analysis of Algorithms (Greedy Algorithm)
SababAshfakFahim
 
Greedy algorithm activity selection fractional
Amit Kumar Rathi
 
CSS 332 : Algorithms - greedy Algorithms
SohamSaha49
 
Greedy1.ppt
PallaviDhade1
 
greedy method.pdf
deepakjoshi29912
 
Greedy is Good
skku_npc
 
Greedy method by Dr. B. J. Mohite
Zeal Education Society, Pune
 
5.1 greedyyy 02
Krish_ver2
 
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
12 Greeddy Method
Andres Mendez-Vazquez
 
Module 3_DAA (2).pptx
AnkitaVerma776806
 
Sec16 greedy algorithm no1
Keisuke OTAKI
 
GreedyAlgorithms.ppt
MdSazzadHossain74
 
Ad

More from Ruchika Sinha (20)

PPT
Python Programming Introduction - Loops & Boolean
Ruchika Sinha
 
PPTX
Difference Between Normal & Smart/Automated Home
Ruchika Sinha
 
PPT
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
PPT
Greedy Algorithms Huffman Coding.ppt
Ruchika Sinha
 
PPT
DynProg_Knapsack.ppt
Ruchika Sinha
 
PPT
Dijkstra.ppt
Ruchika Sinha
 
PPT
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
PPT
Clipping
Ruchika Sinha
 
PPT
Lec22 intel
Ruchika Sinha
 
PPTX
Pc assembly
Ruchika Sinha
 
PPT
Casing
Ruchika Sinha
 
PPTX
Installation of motherboard
Ruchika Sinha
 
PPT
Shortest path
Ruchika Sinha
 
PPT
Bellman ford algorithm
Ruchika Sinha
 
PPTX
Python material
Ruchika Sinha
 
PPT
Python3
Ruchika Sinha
 
PPT
Optimization problems
Ruchika Sinha
 
PPT
Regular Grammar
Ruchika Sinha
 
PPTX
Software Teting
Ruchika Sinha
 
PPTX
Backtrack search-algorithm
Ruchika Sinha
 
Python Programming Introduction - Loops & Boolean
Ruchika Sinha
 
Difference Between Normal & Smart/Automated Home
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Greedy Algorithms Huffman Coding.ppt
Ruchika Sinha
 
DynProg_Knapsack.ppt
Ruchika Sinha
 
Dijkstra.ppt
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Clipping
Ruchika Sinha
 
Lec22 intel
Ruchika Sinha
 
Pc assembly
Ruchika Sinha
 
Installation of motherboard
Ruchika Sinha
 
Shortest path
Ruchika Sinha
 
Bellman ford algorithm
Ruchika Sinha
 
Python material
Ruchika Sinha
 
Python3
Ruchika Sinha
 
Optimization problems
Ruchika Sinha
 
Regular Grammar
Ruchika Sinha
 
Software Teting
Ruchika Sinha
 
Backtrack search-algorithm
Ruchika Sinha
 
Ad

Recently uploaded (20)

PDF
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
PDF
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
PPTX
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPT
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
PDF
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PDF
01-introduction to the ProcessDesign.pdf
StiveBrack
 
PDF
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PPTX
ASBC application presentation template (ENG)_v3 (1).pptx
HassanMohammed730118
 
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
 
PDF
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
PDF
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
PDF
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
Functions in Python Programming Language
BeulahS2
 
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
01-introduction to the ProcessDesign.pdf
StiveBrack
 
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
ASBC application presentation template (ENG)_v3 (1).pptx
HassanMohammed730118
 
PRIZ Academy - Process functional modelling
PRIZ Guru
 
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 

Greedy Algorithms WITH Activity Selection Problem.ppt

  • 2. Greedy algorithms • A greedy algorithm always makes the choice that looks best at the moment – My everyday examples: • Driving in Los Angeles, NY, or Boston for that matter • Playing cards • Invest on stocks • Choose a university – The hope: a locally optimal choice will lead to a globally optimal solution – For some problems, it works • greedy algorithms tend to be easier to code
  • 3. An Activity Selection Problem (Conference Scheduling Problem) • Input: A set of activities S = {a1,…, an} • 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
  • 4. 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. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 7. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 8. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 9. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 10. Early Finish Greedy • Select the activity with the earliest finish • Eliminate the activities that could not be scheduled • Repeat!
  • 11. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 12. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 13. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 14. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 15. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 16. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 17. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 18. Assuming activities are sorted by finish time
  • 19. Why it is Greedy? • Greedy in the sense that it leaves as much opportunity as possible for the remaining activities to be scheduled • The greedy choice is the one that maximizes the amount of unscheduled time remaining
  • 20. 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
  • 21. 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
  • 22. Optimal Substructures – Once the greedy choice of activity 1 is made, the problem reduces to finding an optimal solution for the activity-selection problem over those activities in S that are compatible with activity 1 • Optimal Substructure • If A is optimal to S, then A’ = A – {1} is optimal to S’={i S: si  f1} • Why? – If we could find a solution B’ to S’ with more activities than A’, adding activity 1 to B’ would yield a solution B to S with more activities than A  contradicting the optimality of A – After each greedy choice is made, we are left with an optimization problem of the same form as the original problem • By induction on the number of choices made, making the greedy choice at every step produces an optimal solution
  • 23. Elements of Greedy Strategy • An greedy algorithm makes a sequence of choices, each of the choices that seems best at the moment is chosen – NOT always produce an optimal solution • Two ingredients that are exhibited by most problems that lend themselves to a greedy strategy – Greedy-choice property – Optimal substructure
  • 24. Greedy-Choice Property • A globally optimal solution can be arrived at by making a locally optimal (greedy) choice – Make whatever choice seems best at the moment and then solve the sub-problem arising after the choice is made – The choice made by a greedy algorithm may depend on choices so far, but it cannot depend on any future choices or on the solutions to sub-problems • Of course, we must prove that a greedy choice at each step yields a globally optimal solution
  • 25. Optimal Substructures • A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to sub-problems – If an optimal solution A to S begins with activity 1, then A’ = A – {1} is optimal to S’={i S: si  f1}
  • 26. Knapsack Problem • One wants to pack n items in a luggage – The ith item is worth vi dollars and weighs wi pounds – Maximize the value but cannot exceed W pounds – vi , wi, W are integers • 0-1 knapsack  each item is taken or not taken • Fractional knapsack  fractions of items can be taken • Both exhibit the optimal-substructure property – 0-1: If item j is removed from an optimal packing, the remaining packing is an optimal packing with weight at most W-wj – Fractional: If w pounds of item j is removed from an optimal packing, the remaining packing is an optimal packing with weight at most W-w that can be taken from other n-1 items plus wj – w of item j
  • 27. Greedy Algorithm for Fractional Knapsack problem • Fractional knapsack can be solvable by the greedy strategy – Compute the value per pound vi/wi for each item – Obeying a greedy strategy, take as much as possible of the item with the greatest value per pound. – If the supply of that item is exhausted and there is still more room, take as much as possible of the item with the next value per pound, and so forth until there is no more room – O(n lg n) (we need to sort the items by value per pound) – Greedy Algorithm? – Correctness?
  • 28. O-1 knapsack is harder! • 0-1 knapsack cannot be solved by the greedy strategy – Unable to fill the knapsack to capacity, and the empty space lowers the effective value per pound of the packing – We must compare the solution to the sub-problem in which the item is included with the solution to the sub- problem in which the item is excluded before we can make the choice – Dynamic Programming
  • 30. Optimal substructures • Define the following subset of activities which are activities that can start after ai finishes and finish before aj starts • Sort the activities according to finish time • We now define the the maximal set of activities from i to j as • Let c[i,j] be the maximal number of activities • We can solve this using dynamic programming, but a simpler approach exists • Our recurrence relation for finding c[i, j] becomes