Download presentation

Presentation is loading. Please wait.

Published byHana Hincks Modified over 3 years ago

1
Knapsack Problem Section 7.6

2
Problem Suppose we have n items U={u 1,..u n }, that we would like to insert into a knapsack of size C. Each item u i has a size s i and a value v i. A choose a subset of items S U such that

3
Dynamic Programming Solution For 1 j C & 1 I n, let V[i,j] be the optimal value obtained by filling a knapsack of size j with items taken from {u 1,..u i }. Also V[i,0]=0 and V[0,j]=0. Our Goal is to find V[n,C]

4
Recursive Formula

5
Algorithm: Knapsack Input: Knapsack of size C, items U={u 1,..u n }, with sizes s 1,.., s n, and values v 1,.., v n Output: For i 0 to n V[i,0] 0 end for For j 0 to C V[0,j] 0 end for for i 1 to n for j 1 to C V[i,j] = V[i-1,j] if s i j then V[i,j] =max{V[i,j], V[i-1,j- s i ] + v i } end for Return V[n,C]

6
Performance Analysis Time = (nC) Space = (nC) But can be modified to use only (C) space by storing two rows only.

7
How does it work? 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000000000000000 0 1 2 3.......................s1...................s2.............................................C 012.....n012.....n 0 0 0 0 0 0 0 0 0 0 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 Copy the above red line here = max{ + v2, }

8
How does it work? 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000000000000000 0 1 2 3.......................s1...................s2.............................................C 012.....n012.....n 0 0 0 0 0 0 0 0 0 0 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 Copy the above red line here = max{ + v2, }

9
How does it work? 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000000000000000 0 1 2 3.......................s1...................s2.............................................C 012.....n012.....n 0 0 0 0 0 0 0 0 0 0 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 Copy the above red line here = max{ + v2, }

10
How does it work? 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000000000000000 0 1 2 3.......................s1...................s2.............................................C 012.....n012.....n 0 0 0 0 0 0 0 0 0 0 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 Copy the above red line here = max{ + v2, }

11
How does it work? 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000000000000000 0 1 2 3.......................s1...................s2.............................................C 012.....n012.....n 0 0 0 0 0 0 0 0 0 0 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 v1 Copy the above red line here = max{ + v2, }

Similar presentations

OK

The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:

The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on network theory and analysis Ppt on north bengal tourism Slides for ppt on it act 2008 Ppt on history of irrational numbers Ppt on design of earthen dam Ppt on kingdom monera and protista Ppt on introduction to object-oriented programming in java Ppt on summary writing worksheets Ppt on classical economics graph Best ppt on forest society and colonialism notes