© All Rights Reserved

6 views

© All Rights Reserved

- KTLT C
- Unit Commitment
- A-Algorithm Design Methods
- Analysis & Design of Algorithm Dec 2010
- cpu
- MODI Method Examples, Transportation Problem
- ('Satish Rao', 'Final', '') Spring 2008
- Biochemical
- sql_08
- Package PongV2
- Assignment 3 - Using the AStar Algorithm.pdf
- 4299-1-8057-1-10-20130718
- Coal Graal.tmtheme 03
- 235384567 Bankers Algorithm Example
- Class_06_8
- Security Constrained UCP with Operational and Power Flow Constraints
- Group10_Lab6
- Space Madness Corona SDK
- Lec01
- A 1

You are on page 1of 39

Dynamic programming

0-1 Knapsack problem

09/28/16

problems

DP can be applied when the solution of a

problem includes solutions to subproblems

We need to find a recursive formula for the

solution

We can recursively solve subproblems, starting

from the trivial case, and save their solutions in

memory

In the end well get the solution of the whole

problem

09/28/16

solved with dynamic programming

Simple Subproblems

We should be able to break the original problem to

smaller subproblems that have the same structure

The solution to the problem must be a composition

of subproblem solutions

Subproblem Overlap

Optimal subproblems to unrelated problems can

contain subproblems in common

09/28/16

Subsequence (LCS)

characters that is common to two text

strings X and Y

Dynamic programming algorithm: solve

subproblems until we get the final solution

Subproblem: first find the LCS of prefixes

of X and Y.

this problem has optimal substructure: LCS

of two prefixes is always a part of LCS of

bigger strings

09/28/16

Subsequence (LCS) continued

length i and j; m = |X|, n = |Y|

empty (so c[0,j] = c[i,0] = 0 )

Recursive formula for c[i,j]:

if x[i ] y[ j ],

c[i 1, j 1] 1

c[i, j ]

max(c[i, j 1], c[i 1, j ]) otherwise

09/28/16

Subsequence (LCS)

use this data to find the characters that

constitute the Longest Common

Subsequence

better than the brute-force algorithm: O(n

2m)

09/28/16

W, and a set S consisting of n items

value bi (all wi , bi and W are integer values)

achieve maximum total value of packed

items?

09/28/16

a picture

Weight

Items

This is a knapsack

Max weight: W = 20

W = 20

09/28/16

Benefit value

wi

bi

2

3

10

max bi subject to wi W

iT

iT

because each item must be entirely accepted

or rejected.

Just another version of this problem is the

Fractional Knapsack Problem, where we

can take fractions of items.

09/28/16

straightforward algorithm

Since there are n items, there are 2n possible

combinations of items.

We go through all combinations and find

the one with the most total value and with

total weight less or equal to W

Running time will be O(2n)

09/28/16

10

Can we do better?

Yes, with an algorithm based on dynamic

programming

We need to carefully identify the

subproblems

If items are labeled 1..n, then a subproblem

would be to find an optimal solution for

Sk = {items labeled 1, 2, .. k}

09/28/16

11

Defining a Subproblem

If items are labeled 1..n, then a subproblem

would be to find an optimal solution for Sk

= {items labeled 1, 2, .. k}

This is a valid subproblem definition.

solution (Sn ) in terms of subproblems (Sk)?

follows.

09/28/16

12

Defining a Subproblem

w1 =2 w2 =4

b1 =3 b2 =5

w3 =5

b3 =8

Weight Benefit

w4 =3

b4 =4

?

Max weight: W = 20

For S4:

Total weight: 14;

total benefit: 20

w1 =2 w2 =4

b1 =3 b2 =5

w3 =5

b3 =8

#

S4

S5

w4 =9

b4 =10

For S5:

Total weight: 20

total benefit: 26

09/28/16

wi

bi

10

Item

Solution for S4 is

not part of the

solution for S5!!! 13

Defining a Subproblem

(continued)

part of the solution for S5

and we need another one!

Lets add another parameter: w, which will

represent the exact weight for each subset of

items

The subproblem then will be to compute

B[k,w]

09/28/16

14

subproblems

B[k 1, w]

if wk w

B[k , w]

max{B[k 1, w], B[k 1, w wk ] bk } else

total weight w is one of the two:

1) the best subset of Sk-1 that has total weight w,

or

2) the best subset of Sk-1 that has total weight wwk plus the item k

09/28/16

15

Recursive Formula

B[k 1, w]

if wk w

B[k , w]

max{B[k 1, w], B[k 1, w wk ] bk } else

w, either contains item k or not.

First case: wk>w. Item k cant be part of the

solution, since if it was, the total weight

would be > w, which is unacceptable

Second case: wk <=w. Then the item k can

be in the solution, and we choose the case

with greater value

09/28/16

16

for w = 0 to W

B[0,w] = 0

for i = 0 to n

B[i,0] = 0

for w = 0 to W

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

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

17

Running time

for w = 0 to W

O(W)

B[0,w] = 0

for i = 0 to n

Repeat n times

B[i,0] = 0

for w = 0 to W

O(W)

< the rest of the code >

algorithm?

O(n*W)

Remember that the brute-force algorithm

takes O(2n)

09/28/16

18

Example

Lets 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)

09/28/16

19

W

0

1

2

3

4

5

Example (2)

0

0

0

0

0

0

0

for w = 0 to W

B[0,w] = 0

09/28/16

20

W

0

1

2

3

4

5

Example (3)

0

0

0

0

0

0

0

for i = 0 to n

B[i,0] = 0

09/28/16

21

W

0

1

2

3

4

5

Example (4)

0

0

0

0

0

0

0

0

0

i=1

bi=3

wi=2

w=1

w-wi =-1

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

22

W

0

1

2

3

4

5

Example (5)

0

0

0

0

0

0

0

0

0

3

i=1

bi=3

wi=2

w=2

w-wi =0

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

23

W

0

1

2

3

4

5

Example (6)

0

0

0

0

0

0

0

0

0

3

3

i=1

bi=3

wi=2

w=3

w-wi=1

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

24

W

0

1

2

3

4

5

Example (7)

0

0

0

0

0

0

0

0

0

3

3

3

i=1

bi=3

wi=2

w=4

w-wi=2

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

25

W

0

1

2

3

4

5

Example (8)

0

0

0

0

0

0

0

0

0

3

3

3

3

i=1

bi=3

wi=2

w=5

w-wi=2

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

26

W

0

1

2

3

4

5

Example (9)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

i=2

bi=4

wi=3

w=1

w-wi=-2

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

27

W

0

1

2

3

4

5

Example (10)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

3

i=2

bi=4

wi=3

w=2

w-wi=-1

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

28

W

0

1

2

3

4

5

Example (11)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

3

4

i=2

bi=4

wi=3

w=3

w-wi=0

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

29

W

0

1

2

3

4

5

Example (12)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

3

4

4

i=2

bi=4

wi=3

w=4

w-wi=1

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

30

W

0

1

2

3

4

5

Example (13)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

3

4

4

7

i=2

bi=4

wi=3

w=5

w-wi=2

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

31

W

0

1

2

3

4

5

Example (14)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

3

4

4

7

0

0

3

4

i=3

bi=5

wi=4

w=1..3

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

32

W

0

1

2

3

4

5

Example (15)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

3

4

4

7

0

0

3

4

5

i=3

bi=5

wi=4

w=4

w- wi=0

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

33

W

0

1

2

3

4

5

Example (15)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

3

4

4

7

0

0

3

4

5

7

i=3

bi=5

wi=4

w=5

w- wi=1

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

34

W

0

1

2

3

4

5

Example (16)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

3

4

4

7

0

0

3

4

5

7

0

0

3

4

5

i=3

bi=5

wi=4

w=1..4

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

35

W

0

1

2

3

4

5

Example (17)

0

0

0

0

0

0

0

0

0

3

3

3

3

0

0

3

4

4

7

0

0

3

4

5

7

0

0

3

4

5

7

i=3

bi=5

wi=4

w=5

if bi + B[i-1,w-wi] > B[i-1,w]

B[i,w] = bi + B[i-1,w- wi]

else

B[i,w] = B[i-1,w]

else B[i,w] = B[i-1,w] // wi > w

09/28/16

Items:

1: (2,3)

2: (3,4)

3: (4,5)

4: (5,6)

36

Comments

value that can be carried in the knapsack

To know the items that make this maximum

value, an addition to this algorithm is

necessary

Please see LCS algorithm from the previous

lecture for the example how to extract this

data from the table we built

09/28/16

37

Conclusion

of solving certain kind of problems

When the solution can be recursively

described in terms of partial solutions, we

can store these partial solutions and re-use

them as necessary

Running time (Dynamic Programming

algorithm vs. nave algorithm):

LCS: O(m*n) vs. O(n * 2m)

0-1 Knapsack problem: O(W*n) vs. O(2n)

09/28/16

38

The End

09/28/16

39

- KTLT CUploaded byĐức_văn_12
- Unit CommitmentUploaded byKornepati Suresh
- A-Algorithm Design MethodsUploaded bySeema Kashyap
- Analysis & Design of Algorithm Dec 2010Uploaded byPrasad C M
- cpuUploaded byBhushan Bakshi
- MODI Method Examples, Transportation ProblemUploaded byNikhil Kumbhar
- ('Satish Rao', 'Final', '') Spring 2008Uploaded byJohn Smith
- BiochemicalUploaded byrioR
- sql_08Uploaded bySanjay Nandan
- Package PongV2Uploaded byKristian Gatchalian
- Assignment 3 - Using the AStar Algorithm.pdfUploaded byAnonymous kFHpVU
- 4299-1-8057-1-10-20130718Uploaded byjhdmss
- Coal Graal.tmtheme 03Uploaded byaivexuviet6396
- 235384567 Bankers Algorithm ExampleUploaded byVINUTHA G.R
- Class_06_8Uploaded byRam Bhagat Soni
- Security Constrained UCP with Operational and Power Flow ConstraintsUploaded byIDES
- Group10_Lab6Uploaded byMarko Irwin
- Space Madness Corona SDKUploaded byMitchell Hudson
- Lec01Uploaded byRogzJrBernz
- A 1Uploaded byNaning Putrinya Budi Astutik
- Global EsizeUploaded bysamy Afifi
- UCO Undergraduate Registration form 0510 - UG14Uploaded byRobin Awal
- provjera4Uploaded byJennifer Robinett
- Oracle Guide 221Uploaded bytangocharlie4
- Chat javaUploaded byRicardo Sousa Pimentel
- Ethics Understanding of Software Professional In Risk Reducing Reusability Coding Using Inclusion Set TheoryUploaded byijcsis
- Why learning code is hard.Uploaded bymohammad_abubaker
- 2019-ICpEP-Challenge-Mechanics-Programming-Regional.docxUploaded byTaht Cue
- f[1].txtUploaded byHenrry Manuel Gonzales Damian
- EmacsUploaded byDeveloper

- lecture34.pptUploaded byAnonymous niE5VQOH
- lecture32.pptUploaded byAnonymous niE5VQOH
- lecture35.pptUploaded byAnonymous niE5VQOH
- Np Completnes 2Uploaded byNishant Verma
- lecture28.pptUploaded byAnonymous niE5VQOH
- lecture30.pptUploaded byAnonymous niE5VQOH
- lecture31.pptUploaded byAnonymous niE5VQOH
- lecture29.pptUploaded byAnonymous niE5VQOH
- lecture27.pptUploaded byAnonymous niE5VQOH
- lecture25.pptUploaded byAnonymous niE5VQOH
- lecture21.pptUploaded byAnonymous niE5VQOH
- Lecture algoUploaded byArqam Syed
- lecture20.pptUploaded byAnonymous niE5VQOH
- lecture22.pptUploaded byAnonymous niE5VQOH
- lecture24.pptUploaded byAnonymous niE5VQOH
- lecture19.pptUploaded byAnonymous niE5VQOH
- lecture16.pptUploaded byAnonymous niE5VQOH
- lecture14.pptUploaded byAnonymous niE5VQOH
- lecture18.pptUploaded byAnonymous niE5VQOH
- lecture12.pptUploaded byAnonymous niE5VQOH
- lecture9.pptUploaded byAnonymous niE5VQOH
- lecture17.pptUploaded byAnonymous niE5VQOH
- lecture15.pptUploaded byAnonymous niE5VQOH
- lecture8.pptUploaded byAnonymous niE5VQOH
- lecture13.pptUploaded byAnonymous niE5VQOH
- lecture11.pptUploaded byAnonymous niE5VQOH
- lecture10.pptUploaded byAnonymous niE5VQOH
- lecture6.pptUploaded byAnonymous niE5VQOH
- Lecture 3Uploaded byAnonymous niE5VQOH

- Fourier TransformsUploaded byVictor Ponce
- Evaluation of Real Yarn Diameter _ Processing, Dyeing & Finishing _ Features _ the ITJUploaded byBoubker Kharchafi
- M112.pdfUploaded byshu
- The Usborne Illustrated Dictionary of Maths (gnv64).pdfUploaded byAnonymous 6HADGUEXD
- Unit of Instruction Geometry Grade 6Uploaded byJohn Crescitelli
- Perspective Drawing EguideUploaded bymoniquelim
- lab_exam_2_quesUploaded bynarun9895152299
- External Interface Guide for EnergyPlus_externalinterfaces_application_guideUploaded byTud Bar
- Process dynamics and Control seborg 2nd ch11 SolutionsUploaded byUtkarsh Jain
- The Search for Real[871]Uploaded byRichyMcNolan
- Motion CaptureUploaded byJazeel
- Handbook No 1 Balancing of Control LoopsUploaded bytomislav_vucinic
- Stock Transaction CostUploaded byfkkfox
- Flip FlopsUploaded byAnurag Thota
- Edit DistanceUploaded byPranav Ganorkar
- model cs 8Uploaded bySourabh Mishra
- unit1_topic1_mechanics_exam_question_comp.pdfUploaded byMaruf Hassan
- CHAPTER 9 Hypothesis Testing.pptxUploaded byXander Clock
- Elec2142 Tute 2Uploaded bySara Brown
- Lesson plan in mathematics grade 9jenchen new(2).docxUploaded byanna
- 501_2019_0_b (1).pdfUploaded byMatome
- Chemistry Teacher Practical GuideUploaded byGazar
- wb_femUploaded byharsh14363
- 401_expt8Uploaded byAgustian Chuagestu
- Data Analysis-handout-22!7!2018.PDF (1)Uploaded bySridher Bashatty
- Neuro fuzzyUploaded byAdam Anugrah
- MATLAB Project Assembling the Global Stiffness MatrixUploaded bySuneel Kanhar
- BlackCat USB Script EngineUploaded byCraig Spencer
- instructional materialsUploaded byapi-341104695
- SchneiderAltivar 31 InverterUploaded byKar Keong Lew