Docsity

Knapsack Problem Using Dynamic Programming-Advance Analysis Design-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Knapsack Problem Using Dynamic Programming-Advance Analysis Design-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Lecture No. 19

0-1 Knapsack Problem using

Dynamic Programming

Lecture No 18

Previous lecture

•^

The assembly line problem is well known in the area ofmultiprocessor scheduling.

-^

In this problem, we are given a set of tasks to beexecuted by a system with n identical processors.

-^

Each task, Ti, requires a fixed, known time pi to execute.

-^

Tasks are indivisible, so that at most one processor maybe executing a given task at any time

-^

They are un-interruptible, i.e., once assigned a task, maynot leave it until task is complete.

-^

The precedence ordering restrictions between tasks may

Application: Multiprocessor Scheduling

•^

0-1 Knapsack Problem

-^

Problem Analysis– Divide and Conquer– Dynamic Solution

-^

Algorithm using Dynamic Programming

-^

Time Complexity

-^

Generalization, Variations and Applications

-^

Conclusion

Today Covered

The knapsack problem arises whenever there isresource allocation with no financial constraints Problem Statement•^

A thief robbing a store and can carry a maximalweight of W into his knapsack. There are n itemsand ith item weight is w

and worth is vi^

dollars.i^

What items should thief take, not exceeding thebag capacity, to maximize value? Assumption

•^

the items may not be broken into smaller pieces,so thief may decide either to take an item or toleave it, but may not take a fraction of an item.

0-1 Knapsack Problem Statement

Problem Statement

-^

You are in Japan on an official visit and want tomake shopping from a store (Best Denki)

-^

A list of required items is available at the store

-^

You are given a bag (knapsack), of fixed capacity,and only you can fill this bag with the selecteditems from the list.

-^

Every item has a value (cost) and weight,

-^

0-1 Knapsack Problem Another StatementAnd your objective is to seek most valuable set ofitems which you can buy not exceeding bag limit.

Problem Construction•^

You have prepared a list of n objects for whichyou are interested to buy, The items arenumbered as i

, i 1

,.. ., i 2

n

•^

Capacity of bag is W

-^

Each item i has value v

, and weigh wi

i

•^

We want to select a set of items among i

, i 1

i^ which do not exceed (in total weight) capacity Wn^ of the bag

-^

Total value of selected items must be maximum

-^

How should we select the items?

Notations: 0-1 Knapsack Problem Construction

0-1 Knapsack Problem Construction Formal Construction of Problem•^

Given a list: i

, i 1

,.. ., i 2

, values: vn

, v 1

,.. ., v 2

andn

weights: w

, w 1

,.. ., w 2

respectivelyn

-^

Of course W

^

0, and we wish to find a set S of items

such that S

 {i^1

, i^2

,.. ., i

} thatn

maximizessubject to

Si

vi

Si

i^

W w

Model: 0-1 Knapsack Problem Construction

Approach•^

Partition the knapsack problem into sub-problems

-^

Find the solutions of the sub-problems

-^

Combine these solutions to solve original problem Comments•^

In this case the sub-problems are not independent

-^

And the sub-problems share sub-sub-problems

-^

Algorithm repeatedly solves common sub-sub-problems and takes more effort than required

-^

Because this is an optimization problem andhence dynamic approach is another solution if weare able to construct problem dynamically

Divide and Conquer Approach

Step1 (Structure):•^

Characterize the structure of an optimal solution

-^

Next decompose the problem into sub-problems

-^

Relate structure of the optimal solution of originalproblem and solutions of sub-problems Step 2 (Principal of Optimality)•^

Define value of an optimal solution recursively

-^

Then express solution of the main problem interms of optimal solutions of sub-problems.

Steps in Dynamic Programming

Step1 (Structure):•^

Decompose problem into smaller problems

-^

Construct an array V[0..n, 0..W]

-^

V[i, w] = maximum value of items selected from{1, 2,.. ., i}, that can fit into a bag with capacity w,where 1

i^ 

n, 1

w

W
•^

V[n, W] = contains maximum value of the itemsselected from {1,2,…,n} that can fit into the bagwith capacity W storage

-^

Hence V[n, W] is the required solution for ourknapsack problem

Mathematical Model: Dynamic Programming

Step 2 (Principal of Optimality)•^

Recursively define value of an optimal solution interms of solutions to sub-problemsBase Case: Since

-^

V[0, w] = 0, 0

w

W, no items are available

•^

V[0, w] = -

, w < 0, invalid

•^

V[i, 0] = 0, 0

i^ 

n, no capacity available

Recursion:V[i, w] = max(V[i-1, w], v

  • V[i-1, w - wi^

])i

for 1

i^ 

n, 0

w

W

Mathematical Model: Dynamic Programming

Select Item i (possible if w

i^

w)

•^

In this way, we gain value v

but use capacity wi^

i

•^

Items left = {1,2,... , i-1}, storage limit = w - w

,i

•^

Max. value, from items {1,2, …,i-1} = V[i-1,w – w

]i

•^

Total value if we select item i = v

  • V[i-1,w – wi^

]i

•^

Finally, the solution will be optimal if we take themaximum of

V[i-1,w] andvi+ V[i-1,w – w^

]i

•^

Hence V[i, w] = max(V[i-1,w], v

  • V[i-1,w – wi^

]i

Proof of Correctness

-^

V[1, 1] = 0,

-^

V[1, 2] = 0

-^

V[1, 3] = 0,

-^

V[1, 4] = 0

-^

V[i, j] = max(V[i-1, j], v

  • V[i-1, j – wi^

]);i

-^

V[1, 5] = max(V[0, 5], v

  • V[0, 5 – w 1

]); 1

= max(V[0, 5], 10 + V[0, 5 - 5])= max(V[0, 5], 10 + V[0, 0])= max(0, 10 + 0) = max(0, 10) = 10

Problem: Developing Algorithm for KnapsackKeep(1, 5) = 1

i^

1

2

3

4

v^ i^

10

40

30

50

w^ i^

5

4

6

3

Capacity = 10