0% found this document useful (0 votes)
32 views3 pages

Dynamic Programming: 0-1 Knapsack Problem

The document discusses the 0-1 knapsack problem and dynamic programming. It provides an example of the knapsack problem with 4 items A, B, C, D and a capacity of 25. It then shows the recursive formulation and bottom-up implementation of dynamic programming to find the minimum number of multiplications to compute a polynomial with variables A, B, C, D.

Uploaded by

Rash Styles
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)
32 views3 pages

Dynamic Programming: 0-1 Knapsack Problem

The document discusses the 0-1 knapsack problem and dynamic programming. It provides an example of the knapsack problem with 4 items A, B, C, D and a capacity of 25. It then shows the recursive formulation and bottom-up implementation of dynamic programming to find the minimum number of multiplications to compute a polynomial with variables A, B, C, D.

Uploaded by

Rash Styles
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/ 3

Dynamic Programming

0-1 Knapsack problem:


Brute force method

Capacity: 25

{}-0-0

{A}-24-24

{B}-10-18

{C}-10-18

{D}-7-10

{A,B}-34- ?

{A,C}-34-?

{A,D}-31-?

{B,C}-20-36

{B,D}-17-28

{C,D}-17-28

{A,B,C}-?

{A,B,D}-?

{A,C,D}-?

{B,C,D}-27-?

{A,B,C,D}-?
MCM

A – 2x4 B -- 4x3 C–3x3


AB – 2x3 (multiplications 2*4*3 = 24)
ABC = ?

1527 216 112


2213 341 441
122 221
334
(AB)C = 24+18=42 multiplications

A(BC)=36+24=60 multiplications

pxq qxr  pxr (multiplications p*q*r)

ABCD

(AB)(CD), (A (BC))D, ((AB)C)D, A((BC)D), A(B(CD)), ….

AB=?

BC=?

CD=?

(AB)C=?

A(BC)=?

B(CD)=?

..

(AB)(CD)=?

(A (BC))D=?

((AB)C)D = ?
 0 if i  j

M(i,j) 
 min
 
M(i,k) M(k  1,j) di1 dk dj if i  j 

i k j

ABCD

A – d0 x d1, B—d1 x d2, C – d2 x d3, D—d3 x d4

M[1,4]

k=1,2,3

k=1, M[1,1]+M[2,4]+d0*d1*d4

k=2, M[1,2]+M[3,4]+d0*d2*d4

k=3, M[1,3]+M[4,4]+d0*d3*d4

M[1,1]=0 M[2,4]=?

M[1,2]=? M[3,4]=?

M[1,3]=? M[4,4]=0

You might also like