Professional Documents
Culture Documents
1
3 STRATEGY GREEDY
Solution
for i 1 to n do
SELECT the next input x.
If fxg Solution is FEASIBLE then
solution COMBINE(Solution, x)
SELECT appropriately nds the next input to
be considered.
A FEASIBLE solution satises the constraints
required for the output.
COMBINE enlarges the current solution to
include a new input.
Examples: Max nding, Selection Sort, and
Kruskal's Smallest Edge First algorithm for Min-
imum
2
Spanning Tree.
4 STRATEGY DYNAMIC PROGRAMMING
Fibonacci Numbers:
Fn = Fn;1 + Fn;2
F1 = F0 = 1.
| Recursive solution requires exponential time:
has overlapping subproblems.
| Bottom-up iterative solution is linear {
compute once, store, and use many
times.
3
4.1 Matrix Sequence Multiplication
eg. 1:
A30x1 X B1x40 X C40x10 X D10x25 X E25x1
| Left to right evaluation requires more than
12K multiplications.
| (A X ( (B X C ) X (D X E ) ) ) needs only
690 multiplications (minimum needed).
| Greedy Algorithm: Largest Common
Dimension First
eg. 2:
A1x2 X B2x3 X C3x4 X D4x5 X E5x6
| Largest Common Dimension First imposes
following order:
(A X ( B X (C X (D X E ) ) ) )
4
which needs 240 multiplications.
| Best order:
((((A X B ) X C ) X D) X E )
which needs 68 multiplications.
| Another Greedy Algorithm: Small-
est Common Dimension First but did
not work for eg. 1.
5
4.2 Divide and Conquer Solution
A * A : : : * A
Input: d0 d1 d1 d2 : : : dn;1 n dn
1 2
7
4.3 Recursive Formulation of M i j ]
A1 X A2 = (A1) X (A2)
9
4.4 Recursive Formula and Time Taken
Recursively,
M i j ] =
!
min
ikj ;1
M i k ] + M k + 1 j ] + d i; 1 d k dj
M (i i) = 0
Optimal Substructure
10
We can recursively solve for
M 1 n] =
min1kn;1 (M 1 k] + M k + 1 n] + d0dkdn)
= minM 1 1] + M 2 n] + d0d1dn,
M 1 2] + M 3 n] + d0d2dn,
M 1 3] + M 4 n] + d0d3dn,
..
M 1 n ; 1] + M n n] + d0dn;1dn
Time Complexity:
Tn = n +T1 + Tn;1
+T2 + Tn;2
+T3 + Tn;3
..
+Tn;2 + T2
+Tn;1 + T1
11
Tn = n + 2T1 + 2T2 + + 2Tn;1 (1)
Tn;1 = n ; 1 + 2T1 + 2T2 + + 2Tn;2(2)
Subtracting (I)-(II) yields
Tn ; Tn;1 = 1 + 2Tn;1
Tn = 1 + 3Tn;1
= 1 + 3(1 + 3Tn;2)
Tn = 1 + 3 + 32 + 33 + + 3n;1T1
= 1 + 3 + 32 + 33 + + 3n;2
12
Recursive Solution is exponential time (3n;2)
Space O(n) stack depth.
Overlapping subproblems: e.g. Recursion
tree for M 1 4].
26 recursive calls for just 10 subproblems
M 1 1] M 2 2] M 3 3] M 4 4] M 1 2] M 2 3] M
13
So we turn to dynamic Programming,
the same formulation
approach the problem bottom-to-top
nd a suitable table to store the sub-solutions.
How many sub-solutions do we have?
M 1 1] M 2 2] M 3 3] M n n] n
M 1 2] M 2 3] M n ; 1 n] n ; 1
M 1 3] M 2 4] M n ; 2 n] n ; 2
..
M 1 n ; 1] M 2 n] 2
M 1 n] 1
n(n;1)
2
) we need O(n2) space
14
4.5 Matrix Parenthesization Order
M,Factor: Matrix
for i 1 to n do M i i] 0
/* main diagonal*/
for diagonal 1 to n ; 1 do
for i 1 to n;diagonal do
j = i + diagonal
M i j ] = minikj ;1(M i k]+ M k +1 j ]
+di;1dkdj )
Factori j ] = k that gave the minimum value
for M i j ].
endfor
endfor
15
4.6 Work out
M 1 3] = min1k3;1M i k] + M k + 1 j ] +
di;1dkdj ]
= minM 1 1] + M 2 3] + d0d1d3
M 1 2] + M 3 3] + d0d2d3]
= min0 + 400 + 30 1 10
1200 + 0 + 30 40 10]
= min700 12000 + 1200]
= 700
16
M 2 4] = minM i k] + M k + 1 j ] + di;1dk dj ]
2k3
= minM 2 2] + M 3 4] + d1d2d4
M 2 3] + M 4 4] + d1d3d4]
= min0 + 10000 + 1 40 25 400 + 0 + 1 10 25]
= min10100 650] = 650
M 3 5] = minM i k] + M k + 1 j ] + di;1dk dj ]
3k4
= minM 3 3] + M 4 5] + d2d3d5
M 3 4] + M 5 5] + d2d4d5]
= min0 + 250 + 40 10 1 10000 + 0 + 40 25 1]
= min650 ;] = 650
M 1 4] = minM 1 1] + M 2 4] + d0d1d4
M 1 2] + M 3 4] + d0d2d4
M 1 3] + M 4 4] + d0d3d4]
= min0 + 650 + 30 1 25
1200 + 10000 + 30 40 25
700 + 0 + 30 10 25]
= min1400 ; ;] = 1400
17
18
5 DYNAMIC PROGRAMMING REQUIREMENTS
20
5.2
21
5.3 Memorize
22
5.4 Dynamic Programming Implimentation
Compute table bottom-up instead of starting at
(m n), start at (1 1)
Demonstrate algorithm
| time = (m n)
| space = (min(m n))
Initialize top row & left column to 0
23
Produce from top row, left to right, xi] = yj ]
ll diagonal neighbor+1 & chaw . slre ll max
of the other neighbors.
24