SlideShare a Scribd company logo
1 of 15
Greedy is Good
  Greedy Algorithm
Introduction
• Greedy algorithm always makes the choice that
  looks best at the moment, with hoping that a locally
  optimal choice will lead to a global optimum
• Similar to Dynamic Programming, it applies to
  Optimization Problem
• It is usually easy to think up and implement
• Most problems for which they work well have two
  properties
   – Greedy choice property
   – Optimal substructure
Greedy choice property
• Greedy algorithm never reconsiders its choices
• This is main difference from dynamic programming
Optimal substructure
• Optimal solution to the problem contains optimal
  solution to the sub-problems
Activity Selection problem
• Activity Selection problem is to select the maximum
  number of activities that can be performed by a
  single person or machine within a time frame , given
  a set of activities each marked by start time and
  finish time
• Formal definition
   –   number of activity: n
   –   start time of activity i is si
   –   finish time of activity i is fi
   –   non-conflicting activities i and j: si≥fj or sj≥fi
   –   Find the maximum set (S) of non-conflicting activities
Early Finish Greedy
• Activity Selection problem has optimal substructure
   – Assume that activities are sorted by monotonically
     increasing finish time
   – Aij = Aik ∪ {ak} ∪ Akj
• Select the activity with the earliest finish
• Eliminate the activities that are in conflict
• Repeat until there is no remains
Early Finish Greedy
Early Finish Greedy
Early Finish Greedy
Early Finish Greedy
Early Finish Greedy
Early Finish Greedy
Sort the set of activities by finishing time (f[i])
S=1
f = f[1]
for i=1 to n
   if s[i] ≥ f
        S=SUi
        f = f[i]
end for
Cases of failure
• Greedy algorithms don’t always yields on optimal
  solution
• Ex) How can a given amount of money be made with
  the least number of coins of given denominations?
  –   Target amount: 6
  –   Denominations: 1, 3, 4
  –   Greedy solution: (4, 1, 1)
  –   Optimal solution: (3, 3)
Conclusion
• Greedy algorithms are usually easy to think of, easy
  to implement and run fast,
• but it may fail to produce the optimal solution
• Mathematical concepts may give you a recipe for
  proving that a problem can be solved with greedy,
  but it ultimately comes down to the experience of
  the programmer.
References
• http://en.wikipedia.org/wiki/Greedy_algorithm
• http://www.topcoder.com/tc?module=Static&d1=tu
  torials&d2=greedyAlg
• http://security.re.kr/~sjkim/LectureNotes/SKKU/201
  0/CSE3002/Lec13(Alg).pdf

More Related Content

Similar to Greedy is Good

Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptxShaistaRiaz4
 
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.pptRuchika Sinha
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of AlgorithmsBulbul Agrawal
 
Dynamic programming, Branch and bound algorithm & Greedy algorithms
Dynamic programming, Branch and bound algorithm & Greedy algorithms Dynamic programming, Branch and bound algorithm & Greedy algorithms
Dynamic programming, Branch and bound algorithm & Greedy algorithms SURBHI SAROHA
 
DynamicProgramming.pptx
DynamicProgramming.pptxDynamicProgramming.pptx
DynamicProgramming.pptxSaimaShaheen14
 
Computational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxComputational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxssuser1a5f25
 
GreedyAlgorithms.ppt
GreedyAlgorithms.pptGreedyAlgorithms.ppt
GreedyAlgorithms.pptssuser422644
 
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer methodModule 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer methodJyoReddy9
 
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.pdfMAJDABDALLAH3
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity CalculationAkhil Kaushik
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
DAA 1 ppt.pptx
DAA 1 ppt.pptxDAA 1 ppt.pptx
DAA 1 ppt.pptxRAJESH S
 

Similar to Greedy is Good (20)

greedy method.pdf
greedy method.pdfgreedy method.pdf
greedy method.pdf
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptx
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
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
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
Dynamic programming, Branch and bound algorithm & Greedy algorithms
Dynamic programming, Branch and bound algorithm & Greedy algorithms Dynamic programming, Branch and bound algorithm & Greedy algorithms
Dynamic programming, Branch and bound algorithm & Greedy algorithms
 
Unit V.pdf
Unit V.pdfUnit V.pdf
Unit V.pdf
 
Greedy method
Greedy methodGreedy method
Greedy method
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
DynamicProgramming.pptx
DynamicProgramming.pptxDynamicProgramming.pptx
DynamicProgramming.pptx
 
Computational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxComputational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptx
 
GreedyAlgorithms.ppt
GreedyAlgorithms.pptGreedyAlgorithms.ppt
GreedyAlgorithms.ppt
 
GreedyAlgorithms.ppt
GreedyAlgorithms.pptGreedyAlgorithms.ppt
GreedyAlgorithms.ppt
 
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer methodModule 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
 
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
 
Lecture01.ppt
Lecture01.pptLecture01.ppt
Lecture01.ppt
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of Algorithms
 
DAA 1 ppt.pptx
DAA 1 ppt.pptxDAA 1 ppt.pptx
DAA 1 ppt.pptx
 

More from skku_npc

Maximum Flow
Maximum FlowMaximum Flow
Maximum Flowskku_npc
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexityskku_npc
 
String Searching Algorithms
String Searching AlgorithmsString Searching Algorithms
String Searching Algorithmsskku_npc
 
disjoint-set data structures
disjoint-set data structuresdisjoint-set data structures
disjoint-set data structuresskku_npc
 
Line sweep algorithms
Line sweep algorithmsLine sweep algorithms
Line sweep algorithmsskku_npc
 
Data Structures
Data StructuresData Structures
Data Structuresskku_npc
 
Prime numbers, factorization
Prime numbers, factorizationPrime numbers, factorization
Prime numbers, factorizationskku_npc
 
Mathematics
MathematicsMathematics
Mathematicsskku_npc
 
Binary Search
Binary SearchBinary Search
Binary Searchskku_npc
 
How to find a solution
How to find a solutionHow to find a solution
How to find a solutionskku_npc
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingskku_npc
 
An introduction to recursion
An introduction to recursionAn introduction to recursion
An introduction to recursionskku_npc
 
Algorithm Games
Algorithm GamesAlgorithm Games
Algorithm Gamesskku_npc
 
Introduction to Graphs
Introduction to GraphsIntroduction to Graphs
Introduction to Graphsskku_npc
 

More from skku_npc (14)

Maximum Flow
Maximum FlowMaximum Flow
Maximum Flow
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 
String Searching Algorithms
String Searching AlgorithmsString Searching Algorithms
String Searching Algorithms
 
disjoint-set data structures
disjoint-set data structuresdisjoint-set data structures
disjoint-set data structures
 
Line sweep algorithms
Line sweep algorithmsLine sweep algorithms
Line sweep algorithms
 
Data Structures
Data StructuresData Structures
Data Structures
 
Prime numbers, factorization
Prime numbers, factorizationPrime numbers, factorization
Prime numbers, factorization
 
Mathematics
MathematicsMathematics
Mathematics
 
Binary Search
Binary SearchBinary Search
Binary Search
 
How to find a solution
How to find a solutionHow to find a solution
How to find a solution
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
An introduction to recursion
An introduction to recursionAn introduction to recursion
An introduction to recursion
 
Algorithm Games
Algorithm GamesAlgorithm Games
Algorithm Games
 
Introduction to Graphs
Introduction to GraphsIntroduction to Graphs
Introduction to Graphs
 

Recently uploaded

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Recently uploaded (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Greedy is Good

  • 1. Greedy is Good Greedy Algorithm
  • 2. Introduction • Greedy algorithm always makes the choice that looks best at the moment, with hoping that a locally optimal choice will lead to a global optimum • Similar to Dynamic Programming, it applies to Optimization Problem • It is usually easy to think up and implement • Most problems for which they work well have two properties – Greedy choice property – Optimal substructure
  • 3. Greedy choice property • Greedy algorithm never reconsiders its choices • This is main difference from dynamic programming
  • 4. Optimal substructure • Optimal solution to the problem contains optimal solution to the sub-problems
  • 5. Activity Selection problem • Activity Selection problem is to select the maximum number of activities that can be performed by a single person or machine within a time frame , given a set of activities each marked by start time and finish time • Formal definition – number of activity: n – start time of activity i is si – finish time of activity i is fi – non-conflicting activities i and j: si≥fj or sj≥fi – Find the maximum set (S) of non-conflicting activities
  • 6. Early Finish Greedy • Activity Selection problem has optimal substructure – Assume that activities are sorted by monotonically increasing finish time – Aij = Aik ∪ {ak} ∪ Akj • Select the activity with the earliest finish • Eliminate the activities that are in conflict • Repeat until there is no remains
  • 12. Early Finish Greedy Sort the set of activities by finishing time (f[i]) S=1 f = f[1] for i=1 to n if s[i] ≥ f S=SUi f = f[i] end for
  • 13. Cases of failure • Greedy algorithms don’t always yields on optimal solution • Ex) How can a given amount of money be made with the least number of coins of given denominations? – Target amount: 6 – Denominations: 1, 3, 4 – Greedy solution: (4, 1, 1) – Optimal solution: (3, 3)
  • 14. Conclusion • Greedy algorithms are usually easy to think of, easy to implement and run fast, • but it may fail to produce the optimal solution • Mathematical concepts may give you a recipe for proving that a problem can be solved with greedy, but it ultimately comes down to the experience of the programmer.
  • 15. References • http://en.wikipedia.org/wiki/Greedy_algorithm • http://www.topcoder.com/tc?module=Static&d1=tu torials&d2=greedyAlg • http://security.re.kr/~sjkim/LectureNotes/SKKU/201 0/CSE3002/Lec13(Alg).pdf