SlideShare a Scribd company logo
Greedy Algorithms
Md. Shafiuzzaman
Lecturer, Dept. of CSE, JUST
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
• 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
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
Ad

More Related Content

What's hot (19)

Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
abhinav108
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
Greedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. MohiteGreedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. Mohite
Zeal Education Society, Pune
 
Greedy method
Greedy method Greedy method
Greedy method
Dr Shashikant Athawale
 
Lec30
Lec30Lec30
Lec30
Nikhil Chilwant
 
Ms nikita greedy agorithm
Ms nikita greedy agorithmMs nikita greedy agorithm
Ms nikita greedy agorithm
Nikitagupta123
 
Lec37
Lec37Lec37
Lec37
Nikhil Chilwant
 
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
International Islamic University
 
greedy algorithm Fractional Knapsack
greedy algorithmFractional Knapsack greedy algorithmFractional Knapsack
greedy algorithm Fractional Knapsack
Md. Musfiqur Rahman Foysal
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
Krish_ver2
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
Abhishek Singh
 
test pre
test pretest pre
test pre
farazch
 
36 greedy
36 greedy36 greedy
36 greedy
Ikram Khan
 
Quick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili SaghafiQuick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili Saghafi
Professor Lili Saghafi
 
Artificial Neural Network
Artificial Neural Network Artificial Neural Network
Artificial Neural Network
Peter Zhou
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introduction
Low Ying Hao
 
Quick sort
Quick sortQuick sort
Quick sort
International Islamic University
 

Similar to Greedy algorithms (20)

Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
Ruchika Sinha
 
lec
leclec
lec
farazch
 
lect
lectlect
lect
farazch
 
Lecture34
Lecture34Lecture34
Lecture34
farazch
 
lect
lectlect
lect
farazch
 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
 
Design and Analysis of Algorithms (Greedy Algorithm)
Design and Analysis of Algorithms (Greedy Algorithm)Design and Analysis of Algorithms (Greedy Algorithm)
Design and Analysis of Algorithms (Greedy Algorithm)
SababAshfakFahim
 
greedy method.pdf
greedy method.pdfgreedy method.pdf
greedy method.pdf
deepakjoshi29912
 
Greedy1.ppt
Greedy1.pptGreedy1.ppt
Greedy1.ppt
PallaviDhade1
 
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjcppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
Greedy is Good
Greedy is GoodGreedy is Good
Greedy is Good
skku_npc
 
12 Greeddy Method
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
Andres Mendez-Vazquez
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
AnkitaVerma776806
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
CHANDAN KUMAR
 
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdfLec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
MAJDABDALLAH3
 
Greedy Method unit-2(Design and analysis of algorithms).pptx
Greedy Method unit-2(Design and analysis of algorithms).pptxGreedy Method unit-2(Design and analysis of algorithms).pptx
Greedy Method unit-2(Design and analysis of algorithms).pptx
shivani366010
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
sandeep54552
 
Greedy method class 11
Greedy method class 11Greedy method class 11
Greedy method class 11
Kumar
 
0 1 knapsack problem(greedy algorithm)
0 1 knapsack problem(greedy algorithm)0 1 knapsack problem(greedy algorithm)
0 1 knapsack problem(greedy algorithm)
SwatiRani13
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
Ruchika Sinha
 
Lecture34
Lecture34Lecture34
Lecture34
farazch
 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
 
Design and Analysis of Algorithms (Greedy Algorithm)
Design and Analysis of Algorithms (Greedy Algorithm)Design and Analysis of Algorithms (Greedy Algorithm)
Design and Analysis of Algorithms (Greedy Algorithm)
SababAshfakFahim
 
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjcppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
Greedy is Good
Greedy is GoodGreedy is Good
Greedy is Good
skku_npc
 
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdfLec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
MAJDABDALLAH3
 
Greedy Method unit-2(Design and analysis of algorithms).pptx
Greedy Method unit-2(Design and analysis of algorithms).pptxGreedy Method unit-2(Design and analysis of algorithms).pptx
Greedy Method unit-2(Design and analysis of algorithms).pptx
shivani366010
 
Greedy method class 11
Greedy method class 11Greedy method class 11
Greedy method class 11
Kumar
 
0 1 knapsack problem(greedy algorithm)
0 1 knapsack problem(greedy algorithm)0 1 knapsack problem(greedy algorithm)
0 1 knapsack problem(greedy algorithm)
SwatiRani13
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Ad

More from Md. Shafiuzzaman Hira (20)

Introduction to Web development
Introduction to Web developmentIntroduction to Web development
Introduction to Web development
Md. Shafiuzzaman Hira
 
Software measurement and estimation
Software measurement and estimationSoftware measurement and estimation
Software measurement and estimation
Md. Shafiuzzaman Hira
 
Why do we test software?
Why do we test software?Why do we test software?
Why do we test software?
Md. Shafiuzzaman Hira
 
Software Requirements engineering
Software Requirements engineeringSoftware Requirements engineering
Software Requirements engineering
Md. Shafiuzzaman Hira
 
Software architectural patterns
Software architectural patternsSoftware architectural patterns
Software architectural patterns
Md. Shafiuzzaman Hira
 
Class based modeling
Class based modelingClass based modeling
Class based modeling
Md. Shafiuzzaman Hira
 
Class diagram
Class diagramClass diagram
Class diagram
Md. Shafiuzzaman Hira
 
State diagram
State diagramState diagram
State diagram
Md. Shafiuzzaman Hira
 
Use case Modeling
Use case ModelingUse case Modeling
Use case Modeling
Md. Shafiuzzaman Hira
 
User stories
User storiesUser stories
User stories
Md. Shafiuzzaman Hira
 
Dev ops
Dev opsDev ops
Dev ops
Md. Shafiuzzaman Hira
 
Agile Methodology
Agile MethodologyAgile Methodology
Agile Methodology
Md. Shafiuzzaman Hira
 
Software Process Model
Software Process ModelSoftware Process Model
Software Process Model
Md. Shafiuzzaman Hira
 
Introduction to Software Engineering Course
Introduction to Software Engineering CourseIntroduction to Software Engineering Course
Introduction to Software Engineering Course
Md. Shafiuzzaman Hira
 
C files
C filesC files
C files
Md. Shafiuzzaman Hira
 
C pointers
C pointersC pointers
C pointers
Md. Shafiuzzaman Hira
 
C structures
C structuresC structures
C structures
Md. Shafiuzzaman Hira
 
How to Create Python scripts
How to Create Python scriptsHow to Create Python scripts
How to Create Python scripts
Md. Shafiuzzaman Hira
 
Regular expressions using Python
Regular expressions using PythonRegular expressions using Python
Regular expressions using Python
Md. Shafiuzzaman Hira
 
Password locker project
Password locker project Password locker project
Password locker project
Md. Shafiuzzaman Hira
 
Ad

Recently uploaded (20)

Collaborative Design for Social Impact Work by David Kelleher
Collaborative Design for Social Impact Work by David KelleherCollaborative Design for Social Impact Work by David Kelleher
Collaborative Design for Social Impact Work by David Kelleher
UXPA Boston
 
Outcome Over Output: How UXers Can Leverage an Outcome-Based Mindset by Malin...
Outcome Over Output: How UXers Can Leverage an Outcome-Based Mindset by Malin...Outcome Over Output: How UXers Can Leverage an Outcome-Based Mindset by Malin...
Outcome Over Output: How UXers Can Leverage an Outcome-Based Mindset by Malin...
UXPA Boston
 
Automating Call Centers with AI Agents_ Achieving Sub-700ms Latency.docx
Automating Call Centers with AI Agents_ Achieving Sub-700ms Latency.docxAutomating Call Centers with AI Agents_ Achieving Sub-700ms Latency.docx
Automating Call Centers with AI Agents_ Achieving Sub-700ms Latency.docx
Ihor Hamal
 
Assurance Best Practices: Unlocking Proactive Network Operations
Assurance Best Practices: Unlocking Proactive Network OperationsAssurance Best Practices: Unlocking Proactive Network Operations
Assurance Best Practices: Unlocking Proactive Network Operations
ThousandEyes
 
Secondary Storage for a microcontroller system
Secondary Storage for a microcontroller systemSecondary Storage for a microcontroller system
Secondary Storage for a microcontroller system
fizarcse
 
SQL Database Design For Developers at PhpTek 2025.pptx
SQL Database Design For Developers at PhpTek 2025.pptxSQL Database Design For Developers at PhpTek 2025.pptx
SQL Database Design For Developers at PhpTek 2025.pptx
Scott Keck-Warren
 
RDM Training: Publish research data with the Research Data Repository
RDM Training: Publish research data with the Research Data RepositoryRDM Training: Publish research data with the Research Data Repository
RDM Training: Publish research data with the Research Data Repository
CSUC - Consorci de Serveis Universitaris de Catalunya
 
Building the plane as it flies through a black hole: revising five UX researc...
Building the plane as it flies through a black hole: revising five UX researc...Building the plane as it flies through a black hole: revising five UX researc...
Building the plane as it flies through a black hole: revising five UX researc...
UXPA Boston
 
GraphSummit Singapore Master Deck - May 20, 2025
GraphSummit Singapore Master Deck - May 20, 2025GraphSummit Singapore Master Deck - May 20, 2025
GraphSummit Singapore Master Deck - May 20, 2025
Neo4j
 
Bridging AI and Human Expertise: Designing for Trust and Adoption in Expert S...
Bridging AI and Human Expertise: Designing for Trust and Adoption in Expert S...Bridging AI and Human Expertise: Designing for Trust and Adoption in Expert S...
Bridging AI and Human Expertise: Designing for Trust and Adoption in Expert S...
UXPA Boston
 
Artificial Intelligence (Kecerdasan Buatan).pdf
Artificial Intelligence (Kecerdasan Buatan).pdfArtificial Intelligence (Kecerdasan Buatan).pdf
Artificial Intelligence (Kecerdasan Buatan).pdf
NufiEriKusumawati
 
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdfComputer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
fizarcse
 
Agentic AI, A Business Overview - May 2025
Agentic AI, A Business Overview - May 2025Agentic AI, A Business Overview - May 2025
Agentic AI, A Business Overview - May 2025
Peter Morgan
 
Stretching CloudStack over multiple datacenters
Stretching CloudStack over multiple datacentersStretching CloudStack over multiple datacenters
Stretching CloudStack over multiple datacenters
ShapeBlue
 
TAFs on WebDriver API - By - Pallavi Sharma.pdf
TAFs on WebDriver API - By - Pallavi Sharma.pdfTAFs on WebDriver API - By - Pallavi Sharma.pdf
TAFs on WebDriver API - By - Pallavi Sharma.pdf
Pallavi Sharma
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
Breaking it Down: Microservices Architecture for PHP Developers
Breaking it Down: Microservices Architecture for PHP DevelopersBreaking it Down: Microservices Architecture for PHP Developers
Breaking it Down: Microservices Architecture for PHP Developers
pmeth1
 
AI needs Hybrid Cloud - TEC conference 2025.pptx
AI needs Hybrid Cloud - TEC conference 2025.pptxAI needs Hybrid Cloud - TEC conference 2025.pptx
AI needs Hybrid Cloud - TEC conference 2025.pptx
Shikha Srivastava
 
John Carmack’s Slides From His Upper Bound 2025 Talk
John Carmack’s Slides From His Upper Bound 2025 TalkJohn Carmack’s Slides From His Upper Bound 2025 Talk
John Carmack’s Slides From His Upper Bound 2025 Talk
Razin Mustafiz
 
Apache CloudStack 101 - Introduction, What’s New and What’s Coming
Apache CloudStack 101 - Introduction, What’s New and What’s ComingApache CloudStack 101 - Introduction, What’s New and What’s Coming
Apache CloudStack 101 - Introduction, What’s New and What’s Coming
ShapeBlue
 
Collaborative Design for Social Impact Work by David Kelleher
Collaborative Design for Social Impact Work by David KelleherCollaborative Design for Social Impact Work by David Kelleher
Collaborative Design for Social Impact Work by David Kelleher
UXPA Boston
 
Outcome Over Output: How UXers Can Leverage an Outcome-Based Mindset by Malin...
Outcome Over Output: How UXers Can Leverage an Outcome-Based Mindset by Malin...Outcome Over Output: How UXers Can Leverage an Outcome-Based Mindset by Malin...
Outcome Over Output: How UXers Can Leverage an Outcome-Based Mindset by Malin...
UXPA Boston
 
Automating Call Centers with AI Agents_ Achieving Sub-700ms Latency.docx
Automating Call Centers with AI Agents_ Achieving Sub-700ms Latency.docxAutomating Call Centers with AI Agents_ Achieving Sub-700ms Latency.docx
Automating Call Centers with AI Agents_ Achieving Sub-700ms Latency.docx
Ihor Hamal
 
Assurance Best Practices: Unlocking Proactive Network Operations
Assurance Best Practices: Unlocking Proactive Network OperationsAssurance Best Practices: Unlocking Proactive Network Operations
Assurance Best Practices: Unlocking Proactive Network Operations
ThousandEyes
 
Secondary Storage for a microcontroller system
Secondary Storage for a microcontroller systemSecondary Storage for a microcontroller system
Secondary Storage for a microcontroller system
fizarcse
 
SQL Database Design For Developers at PhpTek 2025.pptx
SQL Database Design For Developers at PhpTek 2025.pptxSQL Database Design For Developers at PhpTek 2025.pptx
SQL Database Design For Developers at PhpTek 2025.pptx
Scott Keck-Warren
 
Building the plane as it flies through a black hole: revising five UX researc...
Building the plane as it flies through a black hole: revising five UX researc...Building the plane as it flies through a black hole: revising five UX researc...
Building the plane as it flies through a black hole: revising five UX researc...
UXPA Boston
 
GraphSummit Singapore Master Deck - May 20, 2025
GraphSummit Singapore Master Deck - May 20, 2025GraphSummit Singapore Master Deck - May 20, 2025
GraphSummit Singapore Master Deck - May 20, 2025
Neo4j
 
Bridging AI and Human Expertise: Designing for Trust and Adoption in Expert S...
Bridging AI and Human Expertise: Designing for Trust and Adoption in Expert S...Bridging AI and Human Expertise: Designing for Trust and Adoption in Expert S...
Bridging AI and Human Expertise: Designing for Trust and Adoption in Expert S...
UXPA Boston
 
Artificial Intelligence (Kecerdasan Buatan).pdf
Artificial Intelligence (Kecerdasan Buatan).pdfArtificial Intelligence (Kecerdasan Buatan).pdf
Artificial Intelligence (Kecerdasan Buatan).pdf
NufiEriKusumawati
 
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdfComputer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
fizarcse
 
Agentic AI, A Business Overview - May 2025
Agentic AI, A Business Overview - May 2025Agentic AI, A Business Overview - May 2025
Agentic AI, A Business Overview - May 2025
Peter Morgan
 
Stretching CloudStack over multiple datacenters
Stretching CloudStack over multiple datacentersStretching CloudStack over multiple datacenters
Stretching CloudStack over multiple datacenters
ShapeBlue
 
TAFs on WebDriver API - By - Pallavi Sharma.pdf
TAFs on WebDriver API - By - Pallavi Sharma.pdfTAFs on WebDriver API - By - Pallavi Sharma.pdf
TAFs on WebDriver API - By - Pallavi Sharma.pdf
Pallavi Sharma
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
Breaking it Down: Microservices Architecture for PHP Developers
Breaking it Down: Microservices Architecture for PHP DevelopersBreaking it Down: Microservices Architecture for PHP Developers
Breaking it Down: Microservices Architecture for PHP Developers
pmeth1
 
AI needs Hybrid Cloud - TEC conference 2025.pptx
AI needs Hybrid Cloud - TEC conference 2025.pptxAI needs Hybrid Cloud - TEC conference 2025.pptx
AI needs Hybrid Cloud - TEC conference 2025.pptx
Shikha Srivastava
 
John Carmack’s Slides From His Upper Bound 2025 Talk
John Carmack’s Slides From His Upper Bound 2025 TalkJohn Carmack’s Slides From His Upper Bound 2025 Talk
John Carmack’s Slides From His Upper Bound 2025 Talk
Razin Mustafiz
 
Apache CloudStack 101 - Introduction, What’s New and What’s Coming
Apache CloudStack 101 - Introduction, What’s New and What’s ComingApache CloudStack 101 - Introduction, What’s New and What’s Coming
Apache CloudStack 101 - Introduction, What’s New and What’s Coming
ShapeBlue
 

Greedy algorithms

  • 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 • 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. 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
  • 21. 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
  • 22. 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}
  • 23. 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
  • 24. 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?
  • 25. 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
  翻译: