SlideShare une entreprise Scribd logo
1  sur  42
Knapsack Problem
Knapsack Problem




Item #   Size   Value
   1       1      8
   2       3      6
   3       5      5
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
Knapsack Problem
Sample Problem


          A   B   C   D     E   F   G
  value   7   9   5   12   14   6   12
   time   3   4   2    6   7    3    5
Solutions to Knapsack
         Problems
Brute-Force Approach – solve the
 problem with a straightforward
 algorithm
Solutions to Knapsack
           Problems
                                         {}
                                   {1, 2, 3, 4, 5}

                 {}                                             {1}
            {2, 3, 4, 5}                                    {2, 3, 4, 5}

   {}                        {2}                     {1}                    {1, 2}
{3, 4, 5}                  {3, 4, 5}            {3, 4, 5}                  {3, 4, 5}
Solutions to Knapsack
      Problems
Example
Greedy Algorithm
Greedy Algorithm

        A   B   C   D
 cost  200 240 140 150
weight 1    3   2   5
value 200 80 70 30
Greedy Algorithm
 The optimal solution to the fractional
  knapsack
 Not an optimal solution to the 0-1
  knapsack
Dynamic Programming
Dynamic Programming
Dynamic Programming
Example
Example
is   0   1   2   3   4   5

0

1

2

3

4
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1
2
3
4
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0
2     0
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0
2     0
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3
2     0
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3
2     0
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3
2     0
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5   7
4     0
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5   7
4     0   0   3   4   5
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5   7
4     0   0   3   4   5   7
Dynamic Programming
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5   7
4     0   0   3   4   5   7
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5   7
4     0   0   3   4   5   7
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5   7
4     0   0   3   4   5   7
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5   7
4     0   0   3   4   5   7
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5   7
4     0   0   3   4   5   7
Example



is   0   1   2   3   4   5
 0    0   0   0   0   0   0
 1    0   0   3   3   3   3
2     0   0   3   4   4   7
3     0   0   3   4   5   7
4     0   0   3   4   5   7
Example
The optimal knapsack should
contain {1,2} = 7


 is   0     1    2     3     4   5
  0    0     0    0     0     0   0
  1    0     0    3     3     3   3
 2     0     0    3     4     4   7
 3     0     0    3     4     5   7
 4     0     0    3     4     5   7
Knapsack Problem
Common Applications
• Resource allocation with financial
  constraints
• Construction and scoring of
  heterogeneous test
• Selection of capital investments

Contenu connexe

Tendances

Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 

Tendances (20)

Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
Master method
Master method Master method
Master method
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Dynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack ProblemDynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack Problem
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 

En vedette

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
Maher Alshammari
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure
 

En vedette (12)

Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
 
Knapsack
KnapsackKnapsack
Knapsack
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
 
Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.com
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
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...
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
 

Plus de Jenny Galino (8)

Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow Graphs
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
Semantic Networks
Semantic NetworksSemantic Networks
Semantic Networks
 
Upper OSI Layers
Upper OSI LayersUpper OSI Layers
Upper OSI Layers
 
Main Memory
Main MemoryMain Memory
Main Memory
 
Genetically Modified Food
Genetically Modified FoodGenetically Modified Food
Genetically Modified Food
 
Geometry
GeometryGeometry
Geometry
 
Hierarchical Memory System
Hierarchical Memory SystemHierarchical Memory System
Hierarchical Memory System
 

Dernier

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Knapsack Problem

Notes de l'éditeur

  1. 1. Items are indivisible: you either take an item or not. Solved with dynamic programming2.Items are divisible: you can take any fraction of an item. Solved with a greedy algorithm
  2. Imagine you have a problem set with different parts labelled A through G. Each part has a “value” (in points) and a “size” (time in hours to complete). Say the value and time for the problem set are as follows… And say you have a total of 15 hours – the knapsack – : which parts should you do? With the 0-1 Knapsack, you need to know which parts you should do to get the best total value possible. We want maximizing our chance to get more points.If there was partial credit that was proportional to the amount of work done (e.g., one hour spent on problem C earns you 2.5 points), that is what we call now the Fractional Knapsack the best approach is to work on problems in order of points/hour (a greedy strategy).
  3. Since there are nitems, there are 2𝑛 possible combinations of itemsWe need to go through all combinations to find the one with maximum value with total size <= SNOT OPTIMALSince there are nitems, there are 2^𝑛 possible combinations of itemsWe need to go through all combinations to find the one with max value with total weight <= W
  4. Legend:Red – walapudkokasabot
  5. Solution is 1 pds A3 pds B1 pd C
  6. This means that the best subset of 𝑆𝑘 that has the total size 𝑆,can either contains item k or not.First case: 𝑠𝑘>𝑠. Item k can’t be part of the solution, since if it was, the total size would be >s, which is unacceptableSecond case: 𝑤𝑘≤𝑤. Then the item k can be in the solution, and we choose the case with greater value.It means, that the best subset of 𝑆_𝑘 that has total weight w is:The best subset of 𝑆_(𝑘−1) that has total weight 𝑠,orThe best subset of 𝑆_(𝑘−1) that has total weight s−𝑠_𝑘 plus the item 𝑘The best subset of 𝑆_𝑘that has the total size 𝑆, either contains item k or not.First case: 𝑠_𝑘>𝑠. Item k can’t be part of the solution, since if it was, the total size would be >s, which is unacceptableSecond case: 𝑤_𝑘≤𝑤. Then the item k can be in the solution, and we choose the case with greater value.
  7. First for-loop: 𝑠=0 𝑡𝑜 𝑆. We go through all the possible sizes of our knapsack until S and if item i is equal to 0, which is “V[0,s]” its corresponding maximum value is of course, 0. Because when i = 0, this means that we are not taking any item.Second for-loop: i=1 𝑡𝑜 𝑛. We go through all the items from 1 to n and if the knapsack’s size is equal to 0, which is “V[i, 0]” its corresponding values is again 0. Because when s = 0, this means that we can’t put anything in the knapsack.It means, that the best subset of 𝑆_𝑘 that has total weight w is:The best subset of 𝑆_(𝑘−1) that has total weight 𝑠,orThe best subset of 𝑆_(𝑘−1) that has total weight s−𝑠_𝑘 plus the item 𝑘The best subset of 𝑆_𝑘that has the total size 𝑆, either contains item k or not.First case: 𝑠_𝑘>𝑠. Item k can’t be part of the solution, since if it was, the total size would be >s, which is unacceptableSecond case: 𝑤_𝑘≤𝑤. Then the item k can be in the solution, and we choose the case with greater value.
  8. Again, we go through all the items and:Outer if: 𝑠𝑖≤𝑠. This means that the size of the item can fit in the current size of the knapsack and we should consider its possible maximum value.Outer else: 𝑠𝑖>𝑠. Item i can’t be part of the solution, since its size is bigger than the knapsack’s current limit. Then, we’ll just copy the value above it.Inner if: 𝑣𝑖+𝑉𝑖−1, 𝑠−𝑠𝑖>𝑉[𝑖−1, 𝑠]. This means that if the current item’s value + 𝑉𝑖−1, 𝑠−𝑠𝑖 is greater than the value above it, we must use the current item’s value + 𝑉𝑖−1, 𝑠−𝑠𝑖.Inner else:𝑣𝑖+𝑉𝑖−1, 𝑠−𝑠𝑖≤𝑉[𝑖−1, 𝑠]. This means that if the current item’s value + 𝑉𝑖−1, 𝑠−𝑠𝑖 is less than or equal to the value above it, we must use the value on the previous item (or simple the value above it).The outer if and else conditions check if the knapsack can hold the current item or not.The inner if and else conditions check if the current value is bigger than the previous value so as to maximize the values the knapsack can hold.It means, that the best subset of 𝑆_𝑘 that has total weight w is:The best subset of 𝑆_(𝑘−1) that has total weight 𝑠,orThe best subset of 𝑆_(𝑘−1) that has total weight s−𝑠_𝑘 plus the item 𝑘The best subset of 𝑆_𝑘that has the total size 𝑆, either contains item k or not.First case: 𝑠_𝑘>𝑠. Item k can’t be part of the solution, since if it was, the total size would be >s, which is unacceptableSecond case: 𝑤_𝑘≤𝑤. Then the item k can be in the solution, and we choose the case with greater value.
  9. i = 1vi = 3si = 2s = 1s - si = -1
  10. i = 1vi = 3si = 2s = 2s - si = 0
  11. i = 1vi = 3si = 2s = 3s - si = 1
  12. i = 1vi = 3si = 2s = 4s - si = 2
  13. i = 1vi = 3si = 2s = 5s - si = 3
  14. i = 2vi = 4si = 3s = 1s - si = -2
  15. i = 2vi = 4si = 3s = 2s - si = -1
  16. i = 2vi = 4si = 3s = 3s - si = 0
  17. i = 2vi = 4si = 3s = 4s - si = 1
  18. i = 2vi = 4si = 3s = 5s - si = 2
  19. i = 3vi = 5si = 4s = 1…3
  20. i = 3vi = 5si = 4s = 4s - si = 0
  21. i = 3vi = 5si = 4s = 5s - si = 1
  22. i = 4vi = 6si= 5s = 1…4
  23. i = 4vi = 6si = 5s = 5s - si = 0
  24. To identify which items to include in the knapsack…All of the information we need is in the tableV[n,S] is the maximal value of items that can be placed in the Knapsackif...mark the ith items as in the knapsackelse...assume the ith item is not in the knapsack
  25. 𝑖 = 4𝑘 = 5𝑣𝑖=6𝑠𝑖 = 5𝑉𝑖,𝑘=7𝑉𝑖−1,𝑘=7𝑖 = 4𝑘 = 5𝑣_𝑖=6𝑠_𝑖 = 5𝑉[𝑖,𝑘]=7𝑉[𝑖−1,𝑘]=7
  26. 𝑖 = 4𝑘 = 5𝑣𝑖=6𝑠𝑖 = 5𝑉𝑖,𝑘=7𝑉𝑖−1,𝑘=7𝑖 = 4𝑘 = 5𝑣_𝑖=6𝑠_𝑖 = 5𝑉[𝑖,𝑘]=7𝑉[𝑖−1,𝑘]=7
  27. 𝑖 = 3𝑘 = 5𝑣𝑖=6𝑠𝑖 = 4𝑉𝑖,𝑘=7𝑉𝑖−1,𝑘=7𝑖 = 3𝑘 = 5𝑣_𝑖=6𝑠_𝑖 = 4𝑉[𝑖,𝑘]=7𝑉[𝑖−1,𝑘]=7
  28. 𝑖 = 1𝑘 = 5𝑣𝑖=4𝑠𝑖 = 3𝑉𝑖,𝑘=7𝑉𝑖−1,𝑘=3𝑘−𝑠𝑖=2𝑖 = 1𝑘 = 5𝑣_𝑖=4𝑠_𝑖 = 3𝑉[𝑖,𝑘]=7𝑉[𝑖−1,𝑘]=3𝑘−𝑠_𝑖=2
  29. 𝑖 = 1𝑘 = 2𝑣𝑖=3𝑠𝑖 = 2𝑉𝑖,𝑘=3𝑉𝑖−1,𝑘=0𝑘−𝑠𝑖=0𝑖 = 1𝑘 = 2𝑣_𝑖=3𝑠_𝑖 = 2𝑉[𝑖,𝑘]=3𝑉[𝑖−1,𝑘]=0𝑘−𝑠_𝑖=0
  30. 𝑖 = 0𝑘 = 0The optimal knapsack should contain {1, 2}𝑖 = 0𝑘 = 0The optimal knapsack should contain {1, 2}
  31. 𝑖 = 0𝑘 = 0The optimal knapsack should contain {1, 2}𝑖 = 0𝑘 = 0The optimal knapsack should contain {1, 2}