Greedy method1

R
Chapter 5
Fundamental Algorithm
Design Techniques
The Greedy Method
A greedy algorithm is a mathematical process
that looks for simple, easy-to-implement
solutions to complex, multi-step problems by
deciding which next step will provide the most
obvious benefit.
The Greedy Method
• Such algorithms are called greedy because
while the optimal solution to each smaller
instance will provide an immediate output,
the algorithm doesn’t consider the larger
problem as a whole. Once a decision has been
made, it is never reconsidered.
• Greedy algorithms work by recursively
constructing a set of objects from the smallest
possible constituent parts.
06/29/15
A New Vending Machine?
• A new Coke machine in the
lounge behind the ulab?
• Being efficient computer
scientists, we want to return
the smallest possible number of
coins in change
The Greedy Method
The Greedy Method
• The greedy method is a general algorithm design
paradigm, built on the following elements:
– configurations: different choices, collections, or values to find
– objective function: a score assigned to configurations, which
we want to either maximize or minimize
• It works best when applied to problems with the
greedy-choice property:
– a globally-optimal solution can always be found by a series of
local improvements from a starting configuration.
Making Change
• Problem: A dollar amount to reach and a collection of coin
amounts to use to get there.
• Configuration: A dollar amount yet to return to a customer plus
the coins already returned
• Objective function: Minimize number of coins returned.
• Greedy solution: Always return the largest coin you can
• Example 1: Coins are valued $.32, $.08, $.01
– Has the greedy-choice property, since no amount over $.32 can be
made with a minimum number of coins by omitting a $.32 coin
(similarly for amounts over $.08, but under $.32).
• 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?)
How to rob a bank…
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 xidenote the amount we take of item i
– Objective: maximize
– Constraint:
∑∈Si
iii wxb )/(
∑∈
≤
Si
i Wx
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 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:
• 1 ml of 5
• 2 ml of 3
• 6 ml of 4
• 1 ml of 2
“knapsack”
The Fractional Knapsack
Algorithm
• Greedy choice: Keep taking item with
highest value (benefit to weight
ratio)
– Since
– Run time: O(n log n). Why?
• Correctness: Suppose there is a
better solution
– there is an item i with higher value
than a chosen item j, but xi<wi, xj>0
and vi<vj
– If we substitute some i with j, we get
a better solution
– How much of i: min{wi-xi, xj}
– Thus, there is no better solution than
the greedy one
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 w/ highest vi
xi ← min{wi , W - w}
w ← w + min{wi , W - w}
∑∑ ∈∈
=
Si
iii
Si
iii xwbwxb )/()/(
Task Scheduling
• Given: a set T of n tasks, each having:
– A start time, si
– A finish time, fi (where si < fi)
• Goal: Perform all the tasks using a minimum number of
“machines.”
1 98765432
Machine 1
Machine 3
Machine 2
Task Scheduling
Algorithm
• Greedy choice: consider tasks by their
start time and use as few machines as
possible with this order.
– Run time: O(n log n). Why?
• Correctness: Suppose there is a better
schedule.
– We can use k-1 machines
– The algorithm uses k
– Let i be first task scheduled on
machine k
– Machine i must conflict with k-1
other tasks
– But that means there is no non-
conflicting schedule using k-1
machines
Algorithm taskSchedule(T)
Input: set T of tasks w/ start time si
and finish time fi
Output: non-conflicting schedule
with minimum number of machines
m ← 0 {no. of machines}
while T is not empty
remove task i w/ smallest si
if there’s a machine j for i then
schedule i on machine j
else
m ← m + 1
schedule i on machine m
Example
• Given: a set T of n tasks, each having:
– A start time, si
– A finish time, fi (where si < fi)
– [1,4], [1,3], [2,5], [3,7], [4,7], [6,9], [7,8] (ordered by start)
• Goal: Perform all tasks on min. number of machines
1 98765432
Machine 1
Machine 3
Machine 2
1 de 14

Recomendados

Greedy method Greedy method
Greedy method Dr Shashikant Athawale
2K vistas10 diapositivas
Greedy algorithmsGreedy algorithms
Greedy algorithmssandeep54552
964 vistas12 diapositivas
Greedy AlgorithmGreedy Algorithm
Greedy AlgorithmWaqar Akram
4.4K vistas22 diapositivas
Greedy AlgorihmGreedy Algorihm
Greedy AlgorihmMuhammad Amjad Rana
16.2K vistas18 diapositivas

Más contenido relacionado

La actualidad más candente

5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
3.2K vistas23 diapositivas
lecture 26lecture 26
lecture 26sajinsc
1.7K vistas20 diapositivas
Branch and boundBranch and bound
Branch and boundDr Shashikant Athawale
10.2K vistas24 diapositivas
Asymptotic NotationAsymptotic Notation
Asymptotic NotationProtap Mondal
4.2K vistas23 diapositivas

La actualidad más candente(20)

Travelling SalesMan Problem(TSP)Travelling SalesMan Problem(TSP)
Travelling SalesMan Problem(TSP)
Akshay Kamble5.4K vistas
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
Krish_ver23.2K vistas
lecture 26lecture 26
lecture 26
sajinsc1.7K vistas
Branch and boundBranch and bound
Branch and bound
Dr Shashikant Athawale10.2K vistas
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal4.2K vistas
Greedy algorithmGreedy algorithm
Greedy algorithm
Caisar Oentoro20.9K vistas
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde8.4K vistas
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
Amrinder Arora15.2K vistas
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet2K vistas
A greedy algorithmsA greedy algorithms
A greedy algorithms
Amit Rathi514 vistas
Branch and boundBranch and bound
Branch and bound
Nv Thejaswini13.7K vistas
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala24.4K vistas
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale7.1K vistas
Lecture28 tspLecture28 tsp
Lecture28 tsp
Dr Sandeep Kumar Poonia12.7K vistas
Dynamic programming class 16Dynamic programming class 16
Dynamic programming class 16
Kumar 4.9K vistas
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman Problem
Shikha Gupta3.4K vistas

Destacado

Ms nikita greedy agorithmMs nikita greedy agorithm
Ms nikita greedy agorithmNikitagupta123
398 vistas19 diapositivas
05. greedy method05. greedy method
05. greedy methodOnkar Nath Sharma
566 vistas15 diapositivas
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
1.6K vistas18 diapositivas
12 Greeddy Method12 Greeddy Method
12 Greeddy MethodAndres Mendez-Vazquez
634 vistas110 diapositivas
Lecture#9Lecture#9
Lecture#9Ali Shah
305 vistas42 diapositivas

Destacado(16)

Ms nikita greedy agorithmMs nikita greedy agorithm
Ms nikita greedy agorithm
Nikitagupta123398 vistas
05. greedy method05. greedy method
05. greedy method
Onkar Nath Sharma566 vistas
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
chidabdu1.6K vistas
Lec 11 general greedy and fractionalLec 11 general greedy and fractional
Lec 11 general greedy and fractional
Ankita Karia317 vistas
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
Andres Mendez-Vazquez634 vistas
Lecture#9Lecture#9
Lecture#9
Ali Shah305 vistas
Algorithm Design and Complexity - Course 5Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5
Traian Rebedea3.9K vistas
Greedy method class 11Greedy method class 11
Greedy method class 11
Kumar 4.2K vistas
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
abhinav1082.6K vistas
Greedy AlgorithamGreedy Algoritham
Greedy Algoritham
RJ Mehul Gadhiya443 vistas
Lec30Lec30
Lec30
Nikhil Chilwant660 vistas
Application  of  greedy methodApplication  of  greedy method
Application of greedy method
Tech_MX13.9K vistas
GreedyGreedy
Greedy
koralverma1.9K vistas
GreedymethodGreedymethod
Greedymethod
Meenakshi Devi11.5K vistas
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Sahil Kumar26.4K vistas
Knapsack ProblemKnapsack Problem
Knapsack Problem
Jenny Galino104.5K vistas

Similar a Greedy method1

lectlect
lectfarazch
268 vistas20 diapositivas
leclec
lecfarazch
474 vistas20 diapositivas
Lecture34Lecture34
Lecture34guestc24b39
211 vistas20 diapositivas

Similar a Greedy method1(20)

Greedy method by Dr. B. J. MohiteGreedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. Mohite
Zeal Education Society, Pune5K vistas
Fractional knapsack problemFractional knapsack problem
Fractional knapsack problem
Learning Courses Online123 vistas
lectlect
lect
farazch268 vistas
leclec
lec
farazch474 vistas
Lecture34Lecture34
Lecture34
guestc24b39211 vistas
Lecture34Lecture34
Lecture34
farazch356 vistas
lectlect
lect
farazch478 vistas
Dynamic programmingDynamic programming
Dynamic programming
Melaku Bayih Demessie14.8K vistas
greedy method.pdfgreedy method.pdf
greedy method.pdf
deepakjoshi299126 vistas
Greedy algorithmsGreedy algorithms
Greedy algorithms
Md. Shafiuzzaman Hira153 vistas
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari2.1K vistas
module3_Greedymethod_2022.pdfmodule3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdf
Shiwani Gupta13 vistas
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar592 vistas
GreedyAlgorithms.pptGreedyAlgorithms.ppt
GreedyAlgorithms.ppt
MdSazzadHossain7411 vistas
GreedyAlgorithms.pptGreedyAlgorithms.ppt
GreedyAlgorithms.ppt
ssuser42264411 vistas
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam136 vistas
Greedy1.pptGreedy1.ppt
Greedy1.ppt
PallaviDhade19 vistas

Más de Rajendran

Red black treeRed black tree
Red black treeRajendran
3K vistas36 diapositivas
Hash tableHash table
Hash tableRajendran
3.6K vistas28 diapositivas
Medians and order statisticsMedians and order statistics
Medians and order statisticsRajendran
5.8K vistas18 diapositivas

Más de Rajendran (20)

Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower bounds
Rajendran 924 vistas
Red black treeRed black tree
Red black tree
Rajendran 3K vistas
Hash tableHash table
Hash table
Rajendran 3.6K vistas
Medians and order statisticsMedians and order statistics
Medians and order statistics
Rajendran 5.8K vistas
Proof master theoremProof master theorem
Proof master theorem
Rajendran 6.6K vistas
Recursion tree methodRecursion tree method
Recursion tree method
Rajendran 34.1K vistas
Recurrence theoremRecurrence theorem
Recurrence theorem
Rajendran 333 vistas
Master method Master method
Master method
Rajendran 2.9K vistas
Master method theoremMaster method theorem
Master method theorem
Rajendran 928 vistas
Hash tablesHash tables
Hash tables
Rajendran 1.1K vistas
Lower boundLower bound
Lower bound
Rajendran 9.1K vistas
Master method theoremMaster method theorem
Master method theorem
Rajendran 8.3K vistas
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of Quicksort
Rajendran 4.5K vistas
Np completenessNp completeness
Np completeness
Rajendran 3.5K vistas
computer languagescomputer languages
computer languages
Rajendran 1.2K vistas
proving non-computabilityproving non-computability
proving non-computability
Rajendran 281 vistas

Último(20)

BYSC infopack.pdfBYSC infopack.pdf
BYSC infopack.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego144 vistas
SIMPLE PRESENT TENSE_new.pptxSIMPLE PRESENT TENSE_new.pptx
SIMPLE PRESENT TENSE_new.pptx
nisrinamadani2146 vistas
Lecture: Open InnovationLecture: Open Innovation
Lecture: Open Innovation
Michal Hron82 vistas
Structure and Functions of Cell.pdfStructure and Functions of Cell.pdf
Structure and Functions of Cell.pdf
Nithya Murugan142 vistas
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
Svetlin Nakov57 vistas
Use of Probiotics in Aquaculture.pptxUse of Probiotics in Aquaculture.pptx
Use of Probiotics in Aquaculture.pptx
AKSHAY MANDAL69 vistas
Classification of crude drugs.pptxClassification of crude drugs.pptx
Classification of crude drugs.pptx
GayatriPatra1449 vistas
Women from Hackney’s History: Stoke Newington by Sue DoeWomen from Hackney’s History: Stoke Newington by Sue Doe
Women from Hackney’s History: Stoke Newington by Sue Doe
History of Stoke Newington103 vistas
ACTIVITY BOOK key water sports.pptxACTIVITY BOOK key water sports.pptx
ACTIVITY BOOK key water sports.pptx
Mar Caston Palacio132 vistas
Chemistry of sex hormones.pptxChemistry of sex hormones.pptx
Chemistry of sex hormones.pptx
RAJ K. MAURYA97 vistas
2022 CAPE Merit List 2023 2022 CAPE Merit List 2023
2022 CAPE Merit List 2023
Caribbean Examinations Council3K vistas
GSoC 2024GSoC 2024
GSoC 2024
DeveloperStudentClub1049 vistas
discussion post.pdfdiscussion post.pdf
discussion post.pdf
jessemercerail70 vistas
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxGopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Debapriya Chakraborty221 vistas
Material del tarjetero LEES Travesías.docxMaterial del tarjetero LEES Travesías.docx
Material del tarjetero LEES Travesías.docx
Norberto Millán Muñoz57 vistas

Greedy method1

  • 2. The Greedy Method A greedy algorithm is a mathematical process that looks for simple, easy-to-implement solutions to complex, multi-step problems by deciding which next step will provide the most obvious benefit.
  • 3. The Greedy Method • Such algorithms are called greedy because while the optimal solution to each smaller instance will provide an immediate output, the algorithm doesn’t consider the larger problem as a whole. Once a decision has been made, it is never reconsidered. • Greedy algorithms work by recursively constructing a set of objects from the smallest possible constituent parts. 06/29/15
  • 4. A New Vending Machine? • A new Coke machine in the lounge behind the ulab? • Being efficient computer scientists, we want to return the smallest possible number of coins in change
  • 6. The Greedy Method • The greedy method is a general algorithm design paradigm, built on the following elements: – configurations: different choices, collections, or values to find – objective function: a score assigned to configurations, which we want to either maximize or minimize • It works best when applied to problems with the greedy-choice property: – a globally-optimal solution can always be found by a series of local improvements from a starting configuration.
  • 7. Making Change • Problem: A dollar amount to reach and a collection of coin amounts to use to get there. • Configuration: A dollar amount yet to return to a customer plus the coins already returned • Objective function: Minimize number of coins returned. • Greedy solution: Always return the largest coin you can • Example 1: Coins are valued $.32, $.08, $.01 – Has the greedy-choice property, since no amount over $.32 can be made with a minimum number of coins by omitting a $.32 coin (similarly for amounts over $.08, but under $.32). • 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?)
  • 8. How to rob a bank…
  • 9. 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 xidenote the amount we take of item i – Objective: maximize – Constraint: ∑∈Si iii wxb )/( ∑∈ ≤ Si i Wx
  • 10. 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 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: • 1 ml of 5 • 2 ml of 3 • 6 ml of 4 • 1 ml of 2 “knapsack”
  • 11. The Fractional Knapsack Algorithm • Greedy choice: Keep taking item with highest value (benefit to weight ratio) – Since – Run time: O(n log n). Why? • Correctness: Suppose there is a better solution – there is an item i with higher value than a chosen item j, but xi<wi, xj>0 and vi<vj – If we substitute some i with j, we get a better solution – How much of i: min{wi-xi, xj} – Thus, there is no better solution than the greedy one 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 w/ highest vi xi ← min{wi , W - w} w ← w + min{wi , W - w} ∑∑ ∈∈ = Si iii Si iii xwbwxb )/()/(
  • 12. Task Scheduling • Given: a set T of n tasks, each having: – A start time, si – A finish time, fi (where si < fi) • Goal: Perform all the tasks using a minimum number of “machines.” 1 98765432 Machine 1 Machine 3 Machine 2
  • 13. Task Scheduling Algorithm • Greedy choice: consider tasks by their start time and use as few machines as possible with this order. – Run time: O(n log n). Why? • Correctness: Suppose there is a better schedule. – We can use k-1 machines – The algorithm uses k – Let i be first task scheduled on machine k – Machine i must conflict with k-1 other tasks – But that means there is no non- conflicting schedule using k-1 machines Algorithm taskSchedule(T) Input: set T of tasks w/ start time si and finish time fi Output: non-conflicting schedule with minimum number of machines m ← 0 {no. of machines} while T is not empty remove task i w/ smallest si if there’s a machine j for i then schedule i on machine j else m ← m + 1 schedule i on machine m
  • 14. Example • Given: a set T of n tasks, each having: – A start time, si – A finish time, fi (where si < fi) – [1,4], [1,3], [2,5], [3,7], [4,7], [6,9], [7,8] (ordered by start) • Goal: Perform all tasks on min. number of machines 1 98765432 Machine 1 Machine 3 Machine 2