SlideShare une entreprise Scribd logo
1  sur  14
Dynamic Programming
Kasun Ranga Wijeweera
(Email: krw19870829@gmail.com)
Introduction
• In divide and conquer principle, large problem is solved by
breaking it up into smaller problems which can be solved
independently
• In dynamic programming, this principle is carried to an
extreme
• When we do not know exactly which smaller problems to
solve, we simply solve them all, then store the answers away
to be used later in solving larger problems
Two Difficulties
• It may not always be possible to combine the solutions of
smaller problems to form the solution of a larger one
• The number of small problems to solve may be unacceptably
large
Knapsack Problem
• A thief robbing a safe finds it filled with N types of items of
varying size and value
• But has only a knapsack of capacity M to carry the goods
• The knapsack problem is to find the combination of items
which the thief should choose for his knapsack in order to
maximize the total value of all the items he takes
Example
• Capacity of the knapsack = 17
• Then the thief can take five A’s for a total take of 20, or he can
fill up his knapsack with a D and an E for a total take of 24
• He can try many other combinations
• But which will maximize his total take?
Size 3 4 7 8 9
Value 4 5 10 11 13
Name A B C D E
Dynamic Programming Solution
• We calculate the best combination for all knapsack sizes up to
M
• It turns out that we can perform this calculation very
efficiently by doing things in an appropriate order
The program
for (j = 1; j <= N; j++)
{
for (i = 1; i <= M; i++)
if( i >= size[j] )
if( cost[i] < cost[i – size[j]) + val[j] )
{
cost[i] = cost[i – size[j]] + val[j];
best[i] = j;
}
}
Details
• cost[i] is the highest value that can be achieved with a
knapsack of capacity i
• best[i] is the last item that was added to achieve that maximum
• cost[0] = 0
i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
cost[i] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
best[i]
Contents of the Optimal Knapsack
• The actual contents of the optimal knapsack can be computed
with the aid of the best array
• By definition, best[M] is included
• The remaining contents are the same as for the optimal
knapsack of size M – size[best[M]]
• Therefore best[M – size[best[M]]] is included, and so forth
For Our Example
• First, best[17] = C
• Then we find another type-C item at size 10
• Then a type-A item at size 3
Conclusions
• The dynamic programming solution to the knapsack problem
takes time proportional to N * M
• The knapsack problem is easily solved if M is not large
• But the running time can become unacceptable for large
capabilities
• This method does not work at all if M and the sizes or values
are, for example, real numbers instead of integers
Reference
Any Questions?
Thank You!

Contenu connexe

En vedette (20)

Queuing theory
Queuing theoryQueuing theory
Queuing theory
 
Replacement theory
Replacement theoryReplacement theory
Replacement theory
 
Replacement theory
Replacement theoryReplacement theory
Replacement theory
 
Operational research
Operational researchOperational research
Operational research
 
Simulation
SimulationSimulation
Simulation
 
Unit 7 dynamic programming
Unit 7   dynamic programmingUnit 7   dynamic programming
Unit 7 dynamic programming
 
Monte carlo simulation
Monte carlo simulationMonte carlo simulation
Monte carlo simulation
 
Game theory
Game theoryGame theory
Game theory
 
Game theory
Game theoryGame theory
Game theory
 
Decision making under uncertaionity
Decision making under uncertaionityDecision making under uncertaionity
Decision making under uncertaionity
 
Queuing model
Queuing model Queuing model
Queuing model
 
Transportation model and assignment model
Transportation model and assignment modelTransportation model and assignment model
Transportation model and assignment model
 
Operations research
Operations researchOperations research
Operations research
 
Transportation model powerpoint
Transportation model powerpointTransportation model powerpoint
Transportation model powerpoint
 
Queueing theory
Queueing theoryQueueing theory
Queueing theory
 
Applications of simulation in Business with Example
Applications of simulation in Business with ExampleApplications of simulation in Business with Example
Applications of simulation in Business with Example
 
Replacement theory
Replacement theoryReplacement theory
Replacement theory
 
unbalanced transportation problem
unbalanced transportation problemunbalanced transportation problem
unbalanced transportation problem
 
Decision theory
Decision theoryDecision theory
Decision theory
 
Monte carlo
Monte carloMonte carlo
Monte carlo
 

Similaire à Dynamic Programming

DynamicProgramming.pptx
DynamicProgramming.pptxDynamicProgramming.pptx
DynamicProgramming.pptxSaimaShaheen14
 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptxAkswant
 
Approximation alogrithms
Approximation alogrithmsApproximation alogrithms
Approximation alogrithmsMohsen Fatemi
 
Design and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptQurbanAli72
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problemharsh kothari
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approachpadmeshagrekar
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack DynamicParas Patel
 
Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...
Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...
Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...faisalpiliang1
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 

Similaire à Dynamic Programming (20)

Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
 
DynamicProgramming.pptx
DynamicProgramming.pptxDynamicProgramming.pptx
DynamicProgramming.pptx
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptx
 
Approximation alogrithms
Approximation alogrithmsApproximation alogrithms
Approximation alogrithms
 
Design and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.ppt
 
Lop1
Lop1Lop1
Lop1
 
5617723.pptx
5617723.pptx5617723.pptx
5617723.pptx
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
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
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
 
Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...
Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...
Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
greedy method.pdf
greedy method.pdfgreedy method.pdf
greedy method.pdf
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 

Plus de Kasun Ranga Wijeweera

Algorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonAlgorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonKasun Ranga Wijeweera
 
Digital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmDigital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmKasun Ranga Wijeweera
 
Getting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingGetting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingKasun Ranga Wijeweera
 
Variables in Visual Basic Programming
Variables in Visual Basic ProgrammingVariables in Visual Basic Programming
Variables in Visual Basic ProgrammingKasun Ranga Wijeweera
 
Conditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingConditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingKasun Ranga Wijeweera
 
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Kasun Ranga Wijeweera
 

Plus de Kasun Ranga Wijeweera (20)

Decorator Design Pattern in C#
Decorator Design Pattern in C#Decorator Design Pattern in C#
Decorator Design Pattern in C#
 
Singleton Design Pattern in C#
Singleton Design Pattern in C#Singleton Design Pattern in C#
Singleton Design Pattern in C#
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Algorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonAlgorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a Polygon
 
Geometric Transformations II
Geometric Transformations IIGeometric Transformations II
Geometric Transformations II
 
Geometric Transformations I
Geometric Transformations IGeometric Transformations I
Geometric Transformations I
 
Introduction to Polygons
Introduction to PolygonsIntroduction to Polygons
Introduction to Polygons
 
Bresenham Line Drawing Algorithm
Bresenham Line Drawing AlgorithmBresenham Line Drawing Algorithm
Bresenham Line Drawing Algorithm
 
Digital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmDigital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing Algorithm
 
Loops in Visual Basic: Exercises
Loops in Visual Basic: ExercisesLoops in Visual Basic: Exercises
Loops in Visual Basic: Exercises
 
Conditional Logic: Exercises
Conditional Logic: ExercisesConditional Logic: Exercises
Conditional Logic: Exercises
 
Getting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingGetting Started with Visual Basic Programming
Getting Started with Visual Basic Programming
 
CheckBoxes and RadioButtons
CheckBoxes and RadioButtonsCheckBoxes and RadioButtons
CheckBoxes and RadioButtons
 
Variables in Visual Basic Programming
Variables in Visual Basic ProgrammingVariables in Visual Basic Programming
Variables in Visual Basic Programming
 
Loops in Visual Basic Programming
Loops in Visual Basic ProgrammingLoops in Visual Basic Programming
Loops in Visual Basic Programming
 
Conditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingConditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic Programming
 
Assignment for Variables
Assignment for VariablesAssignment for Variables
Assignment for Variables
 
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]
 
Assignment for Events
Assignment for EventsAssignment for Events
Assignment for Events
 
Mastering Arrays Assignment
Mastering Arrays AssignmentMastering Arrays Assignment
Mastering Arrays Assignment
 

Dernier

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Dernier (20)

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

Dynamic Programming

  • 1. Dynamic Programming Kasun Ranga Wijeweera (Email: krw19870829@gmail.com)
  • 2. Introduction • In divide and conquer principle, large problem is solved by breaking it up into smaller problems which can be solved independently • In dynamic programming, this principle is carried to an extreme • When we do not know exactly which smaller problems to solve, we simply solve them all, then store the answers away to be used later in solving larger problems
  • 3. Two Difficulties • It may not always be possible to combine the solutions of smaller problems to form the solution of a larger one • The number of small problems to solve may be unacceptably large
  • 4. Knapsack Problem • A thief robbing a safe finds it filled with N types of items of varying size and value • But has only a knapsack of capacity M to carry the goods • The knapsack problem is to find the combination of items which the thief should choose for his knapsack in order to maximize the total value of all the items he takes
  • 5. Example • Capacity of the knapsack = 17 • Then the thief can take five A’s for a total take of 20, or he can fill up his knapsack with a D and an E for a total take of 24 • He can try many other combinations • But which will maximize his total take? Size 3 4 7 8 9 Value 4 5 10 11 13 Name A B C D E
  • 6. Dynamic Programming Solution • We calculate the best combination for all knapsack sizes up to M • It turns out that we can perform this calculation very efficiently by doing things in an appropriate order
  • 7. The program for (j = 1; j <= N; j++) { for (i = 1; i <= M; i++) if( i >= size[j] ) if( cost[i] < cost[i – size[j]) + val[j] ) { cost[i] = cost[i – size[j]] + val[j]; best[i] = j; } }
  • 8. Details • cost[i] is the highest value that can be achieved with a knapsack of capacity i • best[i] is the last item that was added to achieve that maximum • cost[0] = 0 i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 cost[i] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 best[i]
  • 9. Contents of the Optimal Knapsack • The actual contents of the optimal knapsack can be computed with the aid of the best array • By definition, best[M] is included • The remaining contents are the same as for the optimal knapsack of size M – size[best[M]] • Therefore best[M – size[best[M]]] is included, and so forth
  • 10. For Our Example • First, best[17] = C • Then we find another type-C item at size 10 • Then a type-A item at size 3
  • 11. Conclusions • The dynamic programming solution to the knapsack problem takes time proportional to N * M • The knapsack problem is easily solved if M is not large • But the running time can become unacceptable for large capabilities • This method does not work at all if M and the sizes or values are, for example, real numbers instead of integers