0% found this document useful (0 votes)
10 views40 pages

L1-DP and Knapsack

The document covers key concepts in dynamic programming and backtracking, including design principles, strategies, and specific problems like the 0/1 knapsack problem and the traveling salesman problem. It outlines the steps for developing dynamic programming algorithms and compares brute-force and dynamic programming approaches for optimization problems. Additionally, it provides examples and explanations of various algorithmic problems and their solutions.

Uploaded by

cse21216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views40 pages

L1-DP and Knapsack

The document covers key concepts in dynamic programming and backtracking, including design principles, strategies, and specific problems like the 0/1 knapsack problem and the traveling salesman problem. It outlines the steps for developing dynamic programming algorithms and compares brute-force and dynamic programming approaches for optimization problems. Additionally, it provides examples and explanations of various algorithmic problems and their solutions.

Uploaded by

cse21216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Unit-4

1. Dynamic Programming
a) Design Principles and Strategy,
b) 0/1 Knapsack Problem,
c) All-pairs Shortest Path Problem
d) Resource allocation problem.
2. Backtracking, Branch and Bound with examples such as
a) Travelling Salesman Problem,
b) Graph Coloring,
c) n-Queen Problem,
d) Hamiltonian Cycles and
e) Sum of subsets.

Faculty: Dr Sansar Singh Chauhan


Dynamic Programming: Design Principles and Strategy
An algorithm design technique (like divide and conquer)
Divide and conquer
Partition the problem into independent subproblems
Solve the subproblems recursively
Combine the solutions to solve the original problem
Dynamic Programming
Applicable when subproblems are not independent
Subproblems share subsubproblems

A divide and conquer approach would repeatedly solve the common subproblems
Dynamic programming solves every subproblem just once and stores the answer
in a table

Faculty: Dr Sansar Singh Chauhan


Dynamic Programming: Design Principles and Strategy
Used for optimization problems
• A set of choices must be made to get an optimal solution
• Find a solution with the optimal value (minimum or
maximum)
• There may be many solutions that lead to an optimal value
• Our goal: find an optimal solution

Faculty: Dr Sansar Singh Chauhan


Dynamic programming algorithm

The development of a dynamic programming also can be


broken into a sequence of four steps
1. Characterize the structure of an optimal solution
2. Recursively define the value of an optimal solution
3. Compute the value of an optimal solution in a bottom-up
fashion
4. Construct an optimal solution from computed information
(not always necessary)

Faculty: Dr Sansar Singh Chauhan


Knapsack problem

Given some items, pack the knapsack to get


the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.

Item # Weight Value


1 1 8
2 3 6
3 5 5
Faculty: Dr Sansar Singh Chauhan
Knapsack problem
There are two versions of the problem:
1. “0-1 knapsack problem”
• Items are indivisible; you either take an item or
not. Some special instances can be solved with
dynamic programming

2. “Fractional knapsack problem”


• Items are divisible: you can take any fraction of
an item

Faculty: Dr Sansar Singh Chauhan


0-1 Knapsack problem

Given a knapsack with maximum capacity W, and a set S consisting of n


items
Each item i has some weight wi and benefit value bi (all wi and W are integer
values)
Problem: How to pack the knapsack to achieve maximum total value of
packed items?

Faculty: Dr Sansar Singh Chauhan


0-1 Knapsack problem
Problem, in other words, is to find

max å bi subject to å wi £ W
iÎT iÎT

 The problem is called a “0-1” problem,


because each item must be entirely
accepted or rejected.

Faculty: Dr Sansar Singh Chauhan


0-1 Knapsack problem: brute-force
approach

Let’s first solve this problem with a straightforward algorithm


Since there are n items, there are 2n possible combinations of items.
We go through all combinations and find the one with maximum value
and with total weight less or equal to W
Running time will be O(2n)

Faculty: Dr Sansar Singh Chauhan


0-1 Knapsack problem:
dynamic programming approach

We can do better with an algorithm based on dynamic programming

We need to carefully identify the subproblems

Faculty: Dr Sansar Singh Chauhan


0-1 Knapsack Algorithm
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w

Faculty: Dr Sansar Singh Chauhan


Running time
for w = 0 to W
V[0,w] = 0 O(W)
for i = 1 to n
V[i,0] = 0
for i = 1 to n Repeat n times
for w = 0 to W
O(W)
< the rest of the code >
What is the running time of this algorithm?
O(n*W)
Remember that the brute-force algorithm
takes O(2n)

Faculty: Dr Sansar Singh Chauhan


Example

Let’s run our algorithm on the


following data:

n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)

Faculty: Dr Sansar Singh Chauhan


Example (2)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1
2
3
4

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

Faculty: Dr Sansar Singh Chauhan


Example (3)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0

for i = 1 to n
V[i,0] = 0

Faculty: Dr Sansar Singh Chauhan


Items:
Example (4) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0
wi=2
2 0
w=1
3 0
w-wi =-1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (5) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0 3
wi=2
2 0
w=2
3 0
w-wi =0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (6) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0 3 3
wi=2
2 0
w=3
3 0
w-wi =1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (7) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 3
wi=2
2 0
w=4
3 0
w-wi =2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (8) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 3 3
wi=2
2 0
w=5
3 0
w-wi =3
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (9) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0
w=1
3 0
w-wi =-2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (10) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3
w=2
3 0
w-wi =-1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (11) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4
w=3
3 0
w-wi =0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (12) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4 4
w=4
3 0
w-wi =1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (13) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4 4 7
w=5
3 0
w-wi =2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (14) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
w= 1..3
3 0 0 3 4
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (15) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
w= 4
3 0 0 3 4 5
w- wi=0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (16) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
w= 5
3 0 0 3 4 5 7
w- wi=1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (17) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0 bi=6
1 0 0 3 3 3 3
wi=5
2 0 0 3 4 4 7
w= 1..4
3 0 0 3 4 5 7
4 0 0 3 4 5
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Items:
Example (18) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0 bi=6
1 0 0 3 3 3 3
wi=5
2 0 0 3 4 4 7
w= 5
3 0 0 3 4 5 7
w- wi=0
4 0 0 3 4 5 7
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Faculty: Dr Sansar Singh Chauhan
Comments

This algorithm only finds the max possible value that can be carried in the
knapsack
– i.e., the value in V[n,W]
To know the items that make this maximum value, an addition to this
algorithm is necessary

Faculty: Dr Sansar Singh Chauhan


How to find actual Knapsack Items
All of the information we need is in the table.
V[n,W] is the maximal value of items that can be placed in the
Knapsack.
Let i=n and k=W
if V[i,k] ¹ V[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?

Faculty: Dr Sansar Singh Chauhan


Items:
Finding the Items 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = 7
V[i-1,k] =7
4 0 0 3 4 5 7
i=n, k=W
while i,k > 0
if V[i,k] ¹ V[i-1,k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1 Faculty: Dr Sansar Singh Chauhan
Items:
Finding the Items (2) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = 7
V[i-1,k] =7
4 0 0 3 4 5 7
i=n, k=W
while i,k > 0
if V[i,k] ¹ V[i-1,k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i- 1 Faculty: Dr Sansar Singh Chauhan
Items:
Finding the Items (3) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=5
2 0 0 3 4 4 7 wi=4
3 0 0 3 4 5 7 V[i,k] = 7
V[i-1,k] =7
4 0 0 3 4 5 7
i=n, k=W
while i,k > 0
if V[i,k] ¹ V[i-1,k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i- 1 Faculty: Dr Sansar Singh Chauhan
Items:
Finding the Items (4) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=4
2 0 0 3 4 4 7 wi=3
3 0 0 3 4 5 7 V[i,k] = 7
V[i-1,k] =3
4 0 0 3 4 5 7
k - wi=2
i=n, k=W
while i,k > 0
if V[i,k] ¹ V[i-1,k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i- 1 Faculty: Dr Sansar Singh Chauhan
Items:
Finding the Items (5) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 k= 2
1 0 0 3 3 3 3 bi=3
2 0 0 3 4 4 7 wi=2
3 0 0 3 4 5 7 V[i,k] = 3
V[i-1,k] =0
4 0 0 3 4 5 7
k - wi=0
i=n, k=W
while i,k > 0
if V[i,k] ¹ V[i-1,k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i- 1 Faculty: Dr Sansar Singh Chauhan
Items:
Finding the Items (6) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=0 4: (5,6)
0 0 0 0 0 0 0 k= 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7 The optimal
knapsack
4 0 0 3 4 5 7
should contain
i=n, k=W {1, 2}
while i,k > 0
if V[i,k] ¹ V[i-1,k] then
mark the nth item as in the knapsack
i = i-1, k = k-wi
else
i = i-1 Faculty: Dr Sansar Singh Chauhan
Items:
Finding the Items (7) 1: (2,3)
2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 4: (5,6)
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 The optimal
knapsack
4 0 0 3 4 5 7
should contain
i=n, k=W {1, 2}
while i,k > 0
if V[i,k] ¹ V[i-1,k] then
mark the nth item as in the knapsack
i = i-1, k = k-wi
else
i = i-1 Faculty: Dr Sansar Singh Chauhan
Faculty: Dr Sansar Singh Chauhan

You might also like