Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

28, 31Dynamic Programming (Fibonacci) (Dynamic Knapsack)

Dynamic Programming is an algorithm design technique used to solve optimization problems defined by recurrences with overlapping subproblems, invented by Richard Bellman in the 1950s. It can be implemented using top-down (memoization) or bottom-up (tabulation) approaches, with the latter being more efficient for large input sets. The document also discusses the Bellman-Ford algorithm for finding the shortest path in graphs with negative edges and the 0-1 Knapsack problem, emphasizing the importance of dynamic programming in solving these problems efficiently.

Uploaded by

Rohan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

28, 31Dynamic Programming (Fibonacci) (Dynamic Knapsack)

Dynamic Programming is an algorithm design technique used to solve optimization problems defined by recurrences with overlapping subproblems, invented by Richard Bellman in the 1950s. It can be implemented using top-down (memoization) or bottom-up (tabulation) approaches, with the latter being more efficient for large input sets. The document also discusses the Bellman-Ford algorithm for finding the shortest path in graphs with negative edges and the 0-1 Knapsack problem, emphasizing the importance of dynamic programming in solving these problems efficiently.

Uploaded by

Rohan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 75

Dynamic

Programming
Dynamic Programming is a
general algorithm design
technique for solving problems
defined by recurrences with
overlapping subproblems

Dynamic Invented by American


mathematician Richard Bellman
Programmin in the 1950s to solve
optimization problems and later
g assimilated by CS

“Programming” here means


“planning”

2
Dynamic Programming
•Not every sub-problem is new.
•Save time: retain prior results.
•Dynamic programming works by storing the result of
subproblems so that when their solutions are required, they
are at hand and we do not need to recalculate them.

3
Dynamic programming is mostly applied to recursive
algorithms. most optimization problems require recursion
and dynamic programming is used for optimization.

Recursion vs But not all problems that use recursion can use Dynamic
Programming.
Dynamic
Programmin Unless there is a presence of overlapping subproblems like
in the fibonacci sequence problem, a recursion can only
g reach the solution using a divide and conquer approach.

That is the reason why a recursive algorithm like Merge


Sort cannot use Dynamic Programming, because the
subproblems are not overlapping in any way.

4
Greedy Algorithms are similar to dynamic
programming in the sense that they are
both tools for optimization.

Greedy
Algorithms However, greedy algorithms look for locally
optimum solutions or in other words, a

vs Dynamic
greedy choice, in the hopes of finding a
global optimum. Hence greedy algorithms
can make a guess that looks optimum at the
time but becomes costly down the line and

Programmi do not guarantee a globally optimum.

ng Dynamic programming, on the other hand,


finds the optimal solution to subproblems
and then makes an informed choice to
combine the results of those subproblems to
find the most optimum solution.

5
Approaches of dynamic programming

The top-down approach follows


the memorization technique,
while bottom-up approach follows
the tabulation method.

6
Top-down approach
Here memorization is equal to the sum of recursion and
caching. Recursion means calling the function itself, while
caching means storing the intermediate results.

Advantages
•It is very easy to understand and implement.
•It solves the subproblems only when it is required.
•It is easy to debug.
Disadvantages
It uses the recursion technique that occupies more memory in the call
stack. Sometimes when the recursion is too deep, the stack overflow
condition will occur.
It occupies more memory that degrades the overall performance.
Bottom-Up approach
• The bottom-up approach is also one of the techniques which can be used to
implement the dynamic programming.

• It uses the tabulation technique to implement the dynamic programming


approach. It solves the same kind of problems but it removes the recursion.

• If we remove the recursion, there is no stack overflow issue and no overhead of


the recursive functions. In this tabulation technique, we solve the problems and
store the results in a matrix.
How to solve a
Dynamic
Programming
Problem?

•To dynamically solve a problem, we need to check


two necessary conditions:
•Overlapping Subproblems: When the solutions to
the same subproblems are needed repetitively for
solving the actual problem. The problem is said to
have overlapping subproblems property.

•Optimal Substructure Property: If the


optimal solution of the given problem can be
obtained by using optimal solutions of its
subproblems then the problem is said to have
Optimal Substructure Property.
Fibonacci Numbers
• Computing the nth Fibonacci number recursively: int Fib(int n)
{
• F(n) = F(n-1) + F(n-2) if (n <= 1)
• F(0) = 0 return 1;
else
• F(1) = 1
return Fib(n - 1) + Fib(n - 2);
• Top-down approach }

F(n)

F(n-1) + F(n-2)

F(n-2) + F(n-3) F(n-3) + F(n-4)


Fibonacci Numbers
• What is the Recurrence relationship?
• T(n) = T(n-1) + T(n-2) + 1
• What is the solution to this?
• Clearly it is O(2n)
• Obviously not very good, but we know that there is a better way to
solve it!
Algo with memoization
1.def fib(n, memo):
2. if memo[n] is not None:
Algo with recursive 3. return memo[n]
solution 4. if n==0 or n==1
1.def fib(n) 5. return n
2. if(n==0) or (n==1) 6.else:
3.return n 7.result = fib(n-1, memo) + fib(n-
4.else: 2, memo)
5.result = fib(n-1) + fib(n-2) 8.memo[n] = result
6.return result 9.return result
10.def fib_memo(n):
11.memo = [None] * (n+1)
12. return fib(n, memo)
Bottom-Up approach
• Tabulation:
• Bottom-up approach
Stores the results of subproblems in a table
Iterative implementation
• Well-suited for problems with a large set of inputs
• Used when the subproblems do not overlap
Fibonacci Numbers
• Computing the nth Fibonacci number using a bottom-up approach:
• F(0) = 0
• F(1) = 1
• F(2) = 1+0 = 1
• …
• F(n-2) =
• F(n-1) =
• F(n) = F(n-1) + F(n-2)
0 1 1 . . . F(n-2) F(n-1) F(n)

• Efficiency:
• Time – O(n)
Fibonacci Numbers
• The bottom-up approach is only (n).
• Why is the top-down so inefficient?
• Recomputes many sub-problems.
• How many times is F(n-5) computed?

F(n)

F(n-1) + F(n-2) n levels

F(n-2) + F(n-3) F(n-3) + F(n-4)


… … …
Bellman Ford
Algorithm – SSSP
• The Bellman-Ford algorithm is a very popular
algorithm used to find the shortest path from
one node to all the other nodes in a
weighted graph.

• It is very similar to the Dijkstra Algorithm.


However, unlike the Dijkstra Algorithm, the
Bellman-Ford algorithm can work on
graphs with negative-weighted edges.
This capability makes the Bellman-Ford
algorithm a popular choice.
Why Are Negative Edges
Important to Consider?

• In graph theory, negative


edges are more important as
they can create a negative
cycle in a given graph. Let’s
start with a simple weighted
graph with a negative cycle
and try to find out the shortest
distance from one node to
another:
Why Are Negative Edges
Important to Consider?

• go to city R from city M.


• There are three roads from the city M to reach
the city R: MNPR, MNQPR, MNOPR. The road
lengths are 5, 2, and 8.
• But, when we take a deeper look, we see that
there’s a negative cycle: NQP, which has a length
of -1. So, each time we cover the path NQP, our
road length will decrease.
• This leads to us not being able to get an
exact answer on the shortest path
since repeating the road NQP
infinite times would, by definition,
be the least expensive route.
•Negative weight cycles can give an incorrect result when trying to find out the shortest path

•Shortest path algorithms like Dijkstra's Algorithm that aren't able to detect such a cycle can
give an incorrect result because they can go through a negative weight cycle and reduce the
path length.
Steps of Bellman-Ford Algorithm
• This algorithm takes as input a directed weighted graph and a starting vertex.
• The first step is to initialize the vertices.
• The algorithm initially set the distance from starting vertex to all other vertices to
infinity. The distance between starting vertex to itself is 0.
• After the initialization step, the algorithm started calculating the shortest distance
from the starting vertex to all other vertices.
• This step runs (|V| - 1 ) times. Within this step, the algorithm tries to explore
different paths to reach other vertices and calculates the distances.
• If the algorithm finds any distance of a vertex that is smaller then the previously
stored value then it relaxes the edge and stores the new value.
• Finally, when the algorithm iterates (|V| - 1 ) times and relaxes all the required
edges, the algorithm gives a last check to find out if there is any negative cycle in
the graph.
Steps of Bellman-Ford Algorithm
• If there exists a negative cycle then the distances will keep decreasing.

• In such a case, the algorithm terminates and gives an output that the
graph contains a negative cycle hence the algorithm can’t compute the
shortest path.

• If there is no negative cycle found, the algorithm returns the shortest


distances.
A Graph
Without
Negative
Cycle
A Graph
With
Negative
Cycle
Knapsack 0-1 Problem
• The goal is to maximize
the value of a knapsack
that can hold at most W
units (i.e. lbs or kg)
worth of goods from a
list of items I0, I1, … In-1.

• Each item has 2 attributes:


1) Value – let this be vi for
item Ii
2) Weight – let this be wi for
item Ii
Knapsack 0-1 Problem
• The difference
between this problem
and the fractional
knapsack one is that
you CANNOT take a
fraction of an item.

• You can either take it


or not.
• Hence the name
Knapsack 0-1 problem.
Knapsack 0-1 Problem
Knapsack 0-1 Problem
Knapsack 0-1 Algorithm
for w = 0 to W { // Initialize 1st row to 0’s
B[0,w] = 0
}
for i = 1 to n { // Initialize 1 st column to 0’s
B[i,0] = 0
}
for i = 1 to n {
for w = 1 to W {
if wi <= w { //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
}
else B[i,w] = B[i-1,w] // w i > w
}
0/1 Knapsack Problem Using
Dynamic Programming-
• Consider-
• Knapsack weight capacity = w
• Number of items each having some weight and value = n
Step-01:
• Draw a table say ‘T’ with (n+1)
number of rows and (w+1)
number of columns.
• Fill all the boxes of 0th row and
0th column with zeroes as
shown-
• Start filling the table row wise top to bottom from
left to right.
• Use the following formula-

Step-02: • T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – weighti ) }


• Here, T(i , j) = maximum value of the selected
items if we can take items 1 to i and have weight
restrictions of j.
Step-03:

• To identify the items that must be put into the knapsack to obtain that
maximum profit,
• Consider the last column of the table.
• Start scanning the entries from bottom to top.
• On encountering an entry whose value is not same as the value
stored in the entry immediately above it, mark the row label of that
entry.
• After all the entries are scanned, the marked labels represent the
items that must be put into the knapsack.
Time Complexity-
• Each entry of the table requires constant time θ(1) for its
computation.
• It takes θ(nw) time to fill (n+1)(w+1) table entries.
• It takes θ(n) time for tracing the solution since tracing process
traces the n rows.
• Thus, overall θ(nw) time is taken to solve 0/1 knapsack problem
using dynamic programming.
PRACTICE PROBLEM BASED ON
0/1 KNAPSACK PROBLEM-
• Find the optimal solution for the 0/1 knapsack problem making
use of dynamic programming approach. Consider-
•n=4
• w = 5 kg
• (w1, w2, w3, w4) = (2, 3, 4, 5)
• (b1, b2, b3, b4) = (3, 4, 5, 6)

T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – weighti ) }


Finding T(1,1)-
Given-
• Knapsack capacity (w) = 5 kg
• Number of items (n) = 4
• Step-01:

Draw a table say ‘T’ with (n+1) = 4
+ 1 = 5 number of rows and (w+1)
= 5 + 1 = 6 number of columns.
• Fill all the boxes of 0th row and
0th column with 0.
Step-02: • Substituting the values, we get-
• Start filling the table row wise top
to bottom from left to right using
the formula- • T(1,1) = max { T(1-1 , 1) , 3 + T(1-
• T (i , j) = max { T ( i-1 , j ) , valuei +
1 , 1-2) }
T( i-1 , j – weighti ) } • T(1,1) = max { T(0,1) , 3 + T(0,-1) }
• Finding T(1,1)- • T(1,1) = T(0,1) { Ignore
• T(0,-1) }
• We have, • T(1,1) = 0
• i=1
• j=1
• (value)i = (value)1 = 3
• (weight)i = (weight)1 = 2
Finding T(1,2)-
• Start filling the table row wise top to bottom • Substituting the values, we get-
from left to right using the formula- • T(1,2) = max { T(1-1 , 2) , 3 + T(1-1 , 2-2) }
• T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – • T(1,2) = max { T(0,2) , 3 + T(0,0) }
weighti ) }
• T(1,2) = max {0 , 3+0}
• We have,
• T(1,2) = 3
• i=1
• j=2
• (value)i = (value)1 = 3
• (weight)i = (weight)1 = 2
Finding • We have,
• i=2
• Substituting the values, we
get-
T(2,5)- • j=5
• T(2,5) = max { T(2-1 , 5) , 4
+ T(2-1 , 5-3) }
• (value)i = (value)2 = 4
• T(2,5) = max { T(1,5) , 4 +
• (weight)i = (weight)2 = 3 T(1,2) }
• T(2,5) = max { 3 , 4+3 }
• T(2,5) = 7
Knapsack 0-1 Problem
• Let’s run our algorithm on the following data:
• n = 4 (# of elements)
• W = 5 (max weight)
• Elements (weight, value):
(2,3), (3,4), (4,5), (5,6)
Knapsack 0-1 Example
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0

// Initialize the base cases


for w = 0 to W
B[0,w] = 0

for i = 1 to n
B[i,0] = 0
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 vi = 3
2 0 wi = 2
3 0 w=1
4 0 w-wi = -1

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 vi = 3
2 0 wi = 2
3 0 w=2
4 0 w-wi = 0

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 vi = 3
2 0 wi = 2
3 0 w=3
4 0 w-wi = 1

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 vi = 3
2 0 wi = 2
3 0 w=4
4 0 w-wi = 2

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 3 vi = 3
2 0 wi = 2
3 0 w=5
4 0 w-wi = 3

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 wi = 3
3 0 w=1
4 0 w-wi = -2

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 3 wi = 3
3 0 w=2
4 0 w-wi = -1

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 3 4 wi = 3
3 0 w=3
4 0 w-wi = 0

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 3 4 4 wi = 3
3 0 w=4
4 0 w-wi = 1

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 3 4 4 7 wi = 3
3 0 w=5
4 0 w-wi = 2

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=3
1 0 0 3 3 3 3 vi = 5
2 0 0 3 4 4 7 wi = 4
3 0 0 3 4 w = 1..3
4 0 w-wi = -3..-1

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=3
1 0 0 3 3 3 3 vi = 5
2 0 0 3 4 4 7 wi = 4
3 0 0 3 4 5 w=4
4 0 w-wi = 0

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=3
1 0 0 3 3 3 3 vi = 5
2 0 0 3 4 4 7 wi = 4
3 0 0 3 4 5 7 w=5
4 0 w-wi = 1

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=4
1 0 0 3 3 3 3 vi = 6
2 0 0 3 4 4 7 wi = 5
3 0 0 3 4 5 7 w = 1..4
4 0 0 3 4 5 w-wi = -4..-1

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=4
1 0 0 3 3 3 3 vi = 6
2 0 0 3 4 4 7 wi = 5
3 0 0 3 4 5 7 w=5
4 0 0 3 4 5 7 w-wi = 0

if wi <= w //item i can be in the solution


if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 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

We’re DONE!!
The max possible value that can be carried in this knapsack is $7
Knapsack 0-1 Algorithm
• This algorithm only finds the max possible value that can be carried in
the knapsack
• The value in B[n,W]

• To know the items that make this maximum value, we need to trace
back through the table.
Knapsack 0-1 Algorithm
Finding the Items
• Let i = n and k = W
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1 // Assume the ith item is not in the knapsack
// Could it be in the optimally packed knapsack?
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=4
1 0 0 3 3 3 3 k=5
2 0 0 3 4 4 7 vi = 6
3 0 0 3 4 5 7 wi = 5
4 0 0 3 4 5 7 B[i,k] = 7
B[i-1,k] = 7

i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=3
1 0 0 3 3 3 3 k=5
2 0 0 3 4 4 7 vi = 5
3 0 0 3 4 5 7 wi = 4
4 0 0 3 4 5 7 B[i,k] = 7
B[i-1,k] = 7

i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3) Item 2
Finding the Items 2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 k=5
2 0 0 3 4 4 7 vi = 4
3 0 0 3 4 5 7 wi = 3
4 0 0 3 4 5 7 B[i,k] = 7
B[i-1,k] = 3
k – wi = 2
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3) Item 2
Finding the Items 2: (3,4) Item 1
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 3 k=2
2 0 0 3 4 4 7 vi = 3
3 0 0 3 4 5 7 wi = 2
4 0 0 3 4 5 7 B[i,k] = 3
B[i-1,k] = 0
k – wi = 0
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3) Item 2
Finding the Items 2: (3,4) Item 1
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 3 k=2
2 0 0 3 4 4 7 vi = 3
3 0 0 3 4 5 7 wi = 2
4 0 0 3 4 5 7 B[i,k] = 3
B[i-1,k] = 0
k – wi = 0
k = 0, so we’re DONE!

The optimal knapsack should contain:


Item 1 and Item 2
Knapsack 0-1 Problem – Run Time
for w = 0 to W
O(W)
B[0,w] = 0

for i = 1 to n O(n)
B[i,0] = 0
Repeat n times
for i = 1 to n
O(W)
for w = 0 to W
< the rest of the code >

What is the running time of this algorithm?


O(n*W)

n
Knapsack Problem
1) Fill out the dynamic
programming table
for the knapsack
problem to the
right.
2) Trace back through
the table to find the
items in the
knapsack.
Example
•Let us consider that the capacity of the
knapsack is W = 8 and the items are as
shown in the following table.
Item A B C D
Profit 2 4 7 10
Weigh 1 3 5 7
t

You might also like