הרצאה 9
הרצאה 9
• Matrix-chain multiplication
• 0-1 Knapsack
Matrix Multiplication
for i=1 to p
for j=1 to r
C[i,j]=0
for i=1 to p
for j=1 to r
for k=1 to q
C[i,j] = C[i,j]+ A[i,k]B[k,j]
Matrix- Chain Multiplication
• Given a sequence of matrices A1 A2…An , and
dimensions p0 p1…pn where Ai is of
dimension pi-1 x pi , determine multiplication
sequence that minimizes the number of
operations.
• This algorithm does not perform the
multiplication, it just figures out the best
order in which to perform the multiplication.
Example 1: MCM
• Consider 3 matrices: A1 be 5 x 4, A2 be 4 x 6,
and A3 be 6 x 2.
• Consider 4 matrices:
A1: 3 5, A2: 5 4, A3: 4 2, A4: 2 5
((A1 A2) A3) A4, # of scalar multiplications:
3 * 5 * 4 + 3 * 4 * 2 + 3 * 2 * 5 = 114
(A1 (A2 A3)) A4, # of scalar multiplications:
5 * 4 * 2 + 3 * 5 * 2 + 3 * 2 * 5 = 100
(A1 A2) (A3 A4), # of scalar multiplications:
3 * 5 * 4 + 4 * 2 * 5 + 3 * 4 * 5 = 160
Cost of Naive Algorithm
• Is brute-force search efficient for this problem?
• Let P(n) be the number of different ways of
parenthesizing of n matrices. Then
8
DP Solution (II)
• For 1 i j n, let m[i, j] denote the minimum number of
multiplications needed to compute Ai…j .
• Example: Minimum number of multiplies for A3…7
A1 A2 A3 A4 A5 A6 A7 A8 A9
m[ 3, 7 ]
m[i, i] = 0
m[i, j] = mini k < j (m[i, k] + m[k+1, j] + pi-1pkpj ) for i < j
Computing m[i, j]
• For a specific k,
(Ai …Ak)( Ak+1 … Aj)
=
• Algorithm,
– array m[1..n,1..n], with m[i,j] records the optimal
cost for AiAi+1…Aj .
– array s[1..n,1..n], s[i,j] records index k which
achieved the optimal cost when computing m[i,j].
– Suppose the input to the algorithm is
p=< p0 , p1 ,…, pn >.
0 if i j
m(i, j)
min m(i, k) m(k 1, j) p i 1p k p j if i j
i k j-1
• Computation sequence :
m(1,4)
m(1,3) m(2,4)
1. if (j > i)
2. then k = s[i, j]
3. X = Mult(A, i, k) // X = A[i]...A[k]
4. Y = Mult(A, k+1, j) // Y = A[k+1]...A[j]
5. return X*Y // Multiply X*Y
6. else return A[i] // Return ith matrix
Example: DP
• The initial set of dimensions are <5, 4, 6, 2, 7>: we are
multiplying A1 (5x4) times A2 (4x6) times A3 (6x2) times A4
(2x7). Optimal sequence is (A1 (A2A3 )) A4.
Characteristics
1. optimal substructure: If s[1, n] = k, then an optimal full
parenthesization contains those of (A1 Ak) and
(Ak+1 An).
2. a small number of subproblems: The number of
subproblems is the number of (i; j) with 1 i j n,
which is n(n+1)/2 .
3. overlapping subproblems: m[i, j’] and m[i’, j] are referred
to during the computation of m[i, j], for every
i < i’ j and i j’ < j.
• Because of the last property, computing an m-entry by
recursive calls takes exponentially many steps.
The 0/1 Knapsack Problem
• Given: A set S of n items, with each item i
having
– bi - a positive benefit value
– wi - a positive weight
• Goal: Choose items with maximum total benefit
but with weight at most W.
– Objective: maximize bi
iT
– Constraint: w i W
iT
where 𝑥𝑖 𝜖 0,1
Example
• Given: A set S of n items, with each item i having
– bi - a positive benefit
– wi - a positive weight
• Goal: Choose items with maximum total benefit but with
weight at most W.
“knapsack”
Items: Solution:
• 5 (2 in)
1 2 3 4 5 • 3 (2 in)
• 1 (4 in)
Weight: 4 in 2 in 2 in 6 in 2 in
9 in
Benefit: $20 $3 $6 $25 $80
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)
A 0/1 Knapsack Algorithm,
First Attempt
• Sk: Set of items numbered 1 to k.
• Define V[k] = best selection from Sk.
• The question is: can we describe the final solution (Sn ) in
terms of subproblems (Sk)?
– Consider S={(4,20),(2,3),(2,6),(6,25),(2,80)} , W=9
It means, that the best subset of Sk that has total weight w is:
1) the best subset of Sk-1 that has total weight w, or
2) the best subset of Sk-1 that has total weight w-wk plus
the item k
Recursive Formula
V [ k 1, w ] if wk w
V [k , w]
max{ V [ k 1, w ],V [ k 1, w wk ] bk } else
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
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
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
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
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
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
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
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
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
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 =-
4 0
1
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
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
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
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
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
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
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
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
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
How to find actual Knapsack Items
where 0 fi 1.
Example
• Given: A set S of n items, with each item i having
– bi - a positive benefit
– wi - a positive weight
• Goal: Choose items with maximum total benefit but with
weight at most W.
“knapsack”
Solution:
• 1 ml of 5
Items:
1 2 3 4 5 • 2 ml of 3
• 6 ml of 4
Weight: 4 ml 8 ml 2 ml 6 ml 1 ml • 1 ml of 2
Benefit: $12 $32 $40 $30 $50 10 ml
Value: 3 4 20 5 50
($ per ml)
The Fractional Knapsack
Algorithm
• Greedy choice: Keep taking Algorithm fractionalKnapsack(S, W)
item with highest value Input: set S of items : benefit bi
(benefit to weight ratio) and weight wi; max. weight W
Output: amount xi of each item i
– Run time: O(n log n). Why? to maximize benefit with
weight at most W
for each item i in S
xi 0
vi bi / wi {value}
w0 {total weight}
while w < W
remove item i with highest vi
xi min{wi , W - w}
w w + min{wi , W - w}
The Knapsack Problem:
Greedy Vs. Dynamic
• The optimal solution to the fractional knapsack
problem can be found with a greedy algorithm
– Greedy strategy: take in order of dollars/pound
• The optimal solution to the 0-1 problem cannot
be found with the same greedy strategy
– Example: 3 items weighing 10, 20, and 30 pounds,
with values 80, 100, and 90 dollars, knapsack can
hold 50 pounds
bi/wi 80/10=8, 100/20=5, 90/30=3
• For the 0-1 problem, some special instances (i.e.,
where all wi and W are integer values) can be
solved with a dynamic programming approach