SlideShare uma empresa Scribd logo
1 de 19
Analysis & Design of
Algorithms
www.advanced.edu.in 1
Greedy Algorithms
Greedy Algorithms
The General Method
Continuous Knapsack Problem
www.advanced.edu.in 2
1. Greedy Algorithms
Greedy Algorithm:
A greedy algorithm is an algorithm that follows the
problem solving heuristic of making the locally
optimal choice at each stage with the hope of finding
a global optimum.
www.advanced.edu.in 3
1.Greedy Algorithms
Methodology:
 Start with a solution to a small sub-problem
 Build up to the whole problem
 Make choices that look good in the short term but
not necessarily in the long term
www.advanced.edu.in 4
1.Greedy Algorithms
Advantages:
 When they work, they work fast
 Simple and easy to implement
Disadvantages:
 They do not always work.
 Short term choices may be disastrous on the long
term.
 Correctness is hard to prove
www.advanced.edu.in 5
1.Greedy Algorithms
Applications:
 Applications of greedy method are very board
Example:
 Sorting.
 Merging sorted list.
 Knapsack
 Minimum Spanning Tree (MST)
 Hoffman Encoding
www.advanced.edu.in 6
1.Greedy Algorithms
Characteristics and Features:
To construct the solution in an optimal way. Algorithm
maintains two sets. One contains chosen items and
the other contains rejected items.
The greedy algorithm consists of four (4) function.
A function that checks whether chosen set of items
provide a solution.
www.advanced.edu.in 7
1.Greedy Algorithms (Cont.)
Characteristics and Features:
A function that checks the feasibility of a set.
The selection function tells which of the candidates is
the most promising.
An objective function, which does not appear
explicitly, gives the value of a solution.
www.advanced.edu.in 8
2. The General Method
Let a[ ] be an array of elements that may contribute to
a solution. Let S be a solution,
Greedy (a[ ],n)
{
S = empty;
for each element (i) from a[ ], i = 1:n
{
x = Select (a,i);
if (Feasible(S,x)) S = Union(S,x);
}
return S;
}
www.advanced.edu.in 9
2. The General Method (Cont.)
 Select:
Selects an element from a[ ] and removes it. Selection is optimized
to satisfy an objective function.
 Feasible:
True if selected value can be included in the solution vector, False
otherwise.
 Union:
Combines value with solution and updates objective function.
www.advanced.edu.in 10
3. Knapsack Problem
There are two versions of the problem:
1. 0-1 Knapsack Problem
2. Fractional Knapsack Problem
i. Bounded Knapsack Problem
ii. Unbounded Knapsack Problem
www.advanced.edu.in 11
3. Knapsack Problem
In a knapsack problem or rucksack problem,we are given a set of 𝑛items,
where each item 𝑖is specified by a size 𝑠𝑖 and a value 𝑣𝑖. We are also given
a size bound 𝑆, the size of our knapsack.
www.advanced.edu.in 12
Item i Size si Value vi
1 1 8
2 3 6
3 5 5
3. Knapsack Problem
Environment:
Object (i):
Total Weight wi
Total Profit pi
Fraction of object (i) is continuous (0 =< xi <= 1)
A Number of Objects
1 =< i <= n
A knapsack
Capacity m
www.advanced.edu.in 13
Objects
1 2
3 n
Capacity
M
3. Knapsack Problem
GreedyKnapsack ( p[ ] , w[ ] , m , n ,x[ ] )
{
insert indices (i) of items in a maximum heap on value vi = pi / wi ;
Zero the vector x; Rem = m ;
For k = 1..n
{ remove top of heap to get index (i);
if (w[i] > Rem) then break;
x[i] = 1.0 ; Rem = Rem – w[i] ;
}
if (k < = n ) x[i] = Rem / w[i] ;
}
// T(n) = O(n log n)
www.advanced.edu.in 14
3. Knapsack Problem
Example:
www.advanced.edu.in 15
Given:
𝑛 = 4 (# of elements)
𝑆 = 5 pounds (maximum size)
Elements (size, value) =
{ (1, 200), (3, 240), (2, 140), (5, 150) }
3. Knapsack Problem
Example:
www.advanced.edu.in 16
1. Calculate Vi =
vi
si
for 𝑖 = 1,2, … , 𝑛
2. Sort the items by decreasing Vi
3. Find j, such that
𝑠1 + 𝑠2 + ⋯ + 𝑠𝑗 ≤ 𝑆
< 𝑠1 + 𝑠2 + ⋯ + 𝑠𝑗+1
3. Knapsack Problem
Example:
www.advanced.edu.in 17
𝑉𝑖 =
𝑣𝑎𝑙𝑢𝑒𝑖
𝑠𝑖𝑧𝑒𝑖
=
𝑐𝑜𝑠𝑡𝑖
𝑤𝑒𝑖𝑔ℎ𝑡𝑖
COST WEIGHT VALUES
A 200 1 200
B 240 3 80
C 140 2 70
D 150 5 30
3. Knapsack Problem
Solution is:
www.advanced.edu.in 18
1 pounds A
3 pounds B
2 pounds C
4 pounds D
THANK YOU
www.advanced.edu.in 19
Contact Email Id: nikitaguptapalwal@gmail.com
Website: http://www.advanced.edu.in

Mais conteúdo relacionado

Mais procurados

daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
chidabdu
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 

Mais procurados (20)

Greedy
GreedyGreedy
Greedy
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategies
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
36 greedy
36 greedy36 greedy
36 greedy
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Greedymethod
GreedymethodGreedymethod
Greedymethod
 
Greedy algorithm activity selection fractional
Greedy algorithm activity selection fractionalGreedy algorithm activity selection fractional
Greedy algorithm activity selection fractional
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
 
Daa unit 3
Daa unit 3Daa unit 3
Daa unit 3
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
DP
DPDP
DP
 

Destaque (12)

05. greedy method
05. greedy method05. greedy method
05. greedy method
 
Lec 11 general greedy and fractional
Lec 11 general greedy and fractionalLec 11 general greedy and fractional
Lec 11 general greedy and fractional
 
12 Greeddy Method
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
 
Lecture#9
Lecture#9Lecture#9
Lecture#9
 
Greedy method class 11
Greedy method class 11Greedy method class 11
Greedy method class 11
 
Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5
 
Greedy Algoritham
Greedy AlgorithamGreedy Algoritham
Greedy Algoritham
 
Lec30
Lec30Lec30
Lec30
 
Application of greedy method
Application  of  greedy methodApplication  of  greedy method
Application of greedy method
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 

Semelhante a Ms nikita greedy agorithm

ADA Unit — 3 Dynamic Programming and Its Applications.pdf
ADA Unit — 3 Dynamic Programming and Its Applications.pdfADA Unit — 3 Dynamic Programming and Its Applications.pdf
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
RGPV De Bunkers
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
dakccse
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
22bcs058
 
An Introduction to the Finite Element Method
An Introduction to the Finite Element MethodAn Introduction to the Finite Element Method
An Introduction to the Finite Element Method
Mohammad Tawfik
 

Semelhante a Ms nikita greedy agorithm (20)

Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
Daa chapter4
Daa chapter4Daa chapter4
Daa chapter4
 
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
ADA Unit — 3 Dynamic Programming and Its Applications.pdfADA Unit — 3 Dynamic Programming and Its Applications.pdf
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptx
 
Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptx
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
 
Perform brute force
Perform brute forcePerform brute force
Perform brute force
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
nnml.ppt
nnml.pptnnml.ppt
nnml.ppt
 
N Queens problem
N Queens problemN Queens problem
N Queens problem
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
 
An Introduction to the Finite Element Method
An Introduction to the Finite Element MethodAn Introduction to the Finite Element Method
An Introduction to the Finite Element Method
 

Último

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 

Ms nikita greedy agorithm

  • 1. Analysis & Design of Algorithms www.advanced.edu.in 1
  • 2. Greedy Algorithms Greedy Algorithms The General Method Continuous Knapsack Problem www.advanced.edu.in 2
  • 3. 1. Greedy Algorithms Greedy Algorithm: A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. www.advanced.edu.in 3
  • 4. 1.Greedy Algorithms Methodology:  Start with a solution to a small sub-problem  Build up to the whole problem  Make choices that look good in the short term but not necessarily in the long term www.advanced.edu.in 4
  • 5. 1.Greedy Algorithms Advantages:  When they work, they work fast  Simple and easy to implement Disadvantages:  They do not always work.  Short term choices may be disastrous on the long term.  Correctness is hard to prove www.advanced.edu.in 5
  • 6. 1.Greedy Algorithms Applications:  Applications of greedy method are very board Example:  Sorting.  Merging sorted list.  Knapsack  Minimum Spanning Tree (MST)  Hoffman Encoding www.advanced.edu.in 6
  • 7. 1.Greedy Algorithms Characteristics and Features: To construct the solution in an optimal way. Algorithm maintains two sets. One contains chosen items and the other contains rejected items. The greedy algorithm consists of four (4) function. A function that checks whether chosen set of items provide a solution. www.advanced.edu.in 7
  • 8. 1.Greedy Algorithms (Cont.) Characteristics and Features: A function that checks the feasibility of a set. The selection function tells which of the candidates is the most promising. An objective function, which does not appear explicitly, gives the value of a solution. www.advanced.edu.in 8
  • 9. 2. The General Method Let a[ ] be an array of elements that may contribute to a solution. Let S be a solution, Greedy (a[ ],n) { S = empty; for each element (i) from a[ ], i = 1:n { x = Select (a,i); if (Feasible(S,x)) S = Union(S,x); } return S; } www.advanced.edu.in 9
  • 10. 2. The General Method (Cont.)  Select: Selects an element from a[ ] and removes it. Selection is optimized to satisfy an objective function.  Feasible: True if selected value can be included in the solution vector, False otherwise.  Union: Combines value with solution and updates objective function. www.advanced.edu.in 10
  • 11. 3. Knapsack Problem There are two versions of the problem: 1. 0-1 Knapsack Problem 2. Fractional Knapsack Problem i. Bounded Knapsack Problem ii. Unbounded Knapsack Problem www.advanced.edu.in 11
  • 12. 3. Knapsack Problem In a knapsack problem or rucksack problem,we are given a set of 𝑛items, where each item 𝑖is specified by a size 𝑠𝑖 and a value 𝑣𝑖. We are also given a size bound 𝑆, the size of our knapsack. www.advanced.edu.in 12 Item i Size si Value vi 1 1 8 2 3 6 3 5 5
  • 13. 3. Knapsack Problem Environment: Object (i): Total Weight wi Total Profit pi Fraction of object (i) is continuous (0 =< xi <= 1) A Number of Objects 1 =< i <= n A knapsack Capacity m www.advanced.edu.in 13 Objects 1 2 3 n Capacity M
  • 14. 3. Knapsack Problem GreedyKnapsack ( p[ ] , w[ ] , m , n ,x[ ] ) { insert indices (i) of items in a maximum heap on value vi = pi / wi ; Zero the vector x; Rem = m ; For k = 1..n { remove top of heap to get index (i); if (w[i] > Rem) then break; x[i] = 1.0 ; Rem = Rem – w[i] ; } if (k < = n ) x[i] = Rem / w[i] ; } // T(n) = O(n log n) www.advanced.edu.in 14
  • 15. 3. Knapsack Problem Example: www.advanced.edu.in 15 Given: 𝑛 = 4 (# of elements) 𝑆 = 5 pounds (maximum size) Elements (size, value) = { (1, 200), (3, 240), (2, 140), (5, 150) }
  • 16. 3. Knapsack Problem Example: www.advanced.edu.in 16 1. Calculate Vi = vi si for 𝑖 = 1,2, … , 𝑛 2. Sort the items by decreasing Vi 3. Find j, such that 𝑠1 + 𝑠2 + ⋯ + 𝑠𝑗 ≤ 𝑆 < 𝑠1 + 𝑠2 + ⋯ + 𝑠𝑗+1
  • 17. 3. Knapsack Problem Example: www.advanced.edu.in 17 𝑉𝑖 = 𝑣𝑎𝑙𝑢𝑒𝑖 𝑠𝑖𝑧𝑒𝑖 = 𝑐𝑜𝑠𝑡𝑖 𝑤𝑒𝑖𝑔ℎ𝑡𝑖 COST WEIGHT VALUES A 200 1 200 B 240 3 80 C 140 2 70 D 150 5 30
  • 18. 3. Knapsack Problem Solution is: www.advanced.edu.in 18 1 pounds A 3 pounds B 2 pounds C 4 pounds D
  • 19. THANK YOU www.advanced.edu.in 19 Contact Email Id: nikitaguptapalwal@gmail.com Website: http://www.advanced.edu.in