Aaa 9
Aaa 9
Optimization Problems
Conclusion
Optimization Problems
Time complexity:
If there are polynomial number of sub-problems.
If each sub-problem can be computed in polynomial time.
Then the solution of whole problem can be found in polynomial time.
Remark:
Greedy also applies a top-down strategy but usually on one sub-problem
so that the order of computation is clear
Time Complexity in Dynamic Algorithms
Polynomial Time
•An algorithm is said to be solvable in polynomial
time if the number of steps required to complete
the algorithm for a given input is O(n^k) for some
nonnegative integer k, where n is the complexity
of the input.
•Polynomial-time algorithms are said to be "fast.“
•Most familiar mathematical operations such as
addition, subtraction, multiplication, and division,
as well as computing square roots, powers, and
logarithms, can be performed in polynomial time.
CATALAN NUMBERS
Multiplying n Numbers
n C(n)
1 1
2 1
3 2
4 5
5 14
6 42
7 132
Multiplying n Numbers - small n
Recursive equation:
where is the last multiplication?
n −1
C ( n) = C (k ) C (n − k )
k =1
1 2n − 2
Catalan numbers: C (n) = .
n n −1
4n
Asymptotic value: C ( n) 3 / 2
n
C(n)
→ 4 for n →
C(n - 1)
CHAIN-MATRIX MULTIPLICATION
Problem Statement: Chain Matrix Multiplication
Example
Given a sequence [A1, A2, A3, A4]
Order of A1 = 10 x 100
Order of A2 = 100 x 5
Order of A3 = 5x 50
Order of A4 = 50x 20
1 10 x 100 x 20 = 20000
10 x 20
A1 2 100 x 5 x 20 = 10000
10 x 100
100 x 20
3 5 x 50 x 20 = 5000
A2
100 x 5
5 x 20 Total Cost = 35000
A3 A4
5 x 50 50 x 20
Second Chain : (A1 · ((A2 . A3). A4))
1
10 x100 x 20 = 20000
10 x 20
3
A1 100 x 50 x 20 = 100000
10 x 100
2 100 x 20
A4 100 x 5 x 50 = 25000
50 x 20
100 x 50
Total Cost = 145000
A2 A3
100 x 5 5 x 50
Third Chain : ((A1 · A2). (A3 . A4))
10 x 5 x 20 = 1000
2
10x20
10 x 100 x 5 = 5000 5 x 50 x 20 = 5000
1
3
10 x 5 5 x 20
A1 A2 A3 A4
10 x 100 100 x 5 5 x 50 50 x 20
3
10 x 50 x 20 = 10000
10x20
10 x 100 x 50 = 50000
1 A4
50x20
A1 2
Total Cost = 85000 10x100
100x50
A3
A2
5x50
100x5
Fifth Chain : (((A1 · A2). A3). A4)
10 x 50 x 20 = 10000 3
10 x 20
10 x 5 x 50 = 2500 2 A4
50 x 20
10 x 50
10 x 100 x 5 = 5000
1 A3
5 x 50
Total Cost = 17500
10 x 5
A1 A2
10 x 100 100 x 5
Chain Matrix Cost
1
3
10 x 5 5 x 20
A1 A2 A3 A4
10 x 100 100 x 5 5 x 50 50 x 20
Generalization of Brute Force Approach
If there is sequence of n matrices, [A1, A2, . . . , An]
Ai has dimension pi-1 x pi, where for i = 1, 2, . . . , n
Find order of multiplication that minimizes number of
scalar multiplications using brute force approach
Recurrence Relation: After kth matrix, create two sub-lists, one
with k and other with n - k matrices i.e.
(A1 A2A3A4A5 . . . Ak) (Ak+1Ak+2…An)
Let P(n) be the number of different ways of
parenthesizing n items
Generalization of Brute Force Approach
If n = 2
P(2) = P(1).P(1) = 1.1 = 1
If n = 3
P(3) = P(1).P(2) + P(2).P(1) = 1.1 + 1.1 = 2
(A1 A2A3) = ((A1 . A2). A3) OR (A1 . (A2. A3))
If n = 4
P(4) = P(1).P(3) + P(2).P(2) + P(3).P(1) = 1.2 + 1.1 + 2.1
=5
Why Brute Force Approach not Economical
A1 A2 A3 A4
. . .
10 100 100 5 5 50 50 20
m[i, i ] = 0, i = 1,...,4
m[i, j ] = min (m[i, k ] + m[k + 1, j ] + pi −1. pk . p j )
ik j
Main Diagonal
m[1, 1] = 0
m[2, 2] = 0
m[3, 3] = 0
m[4, 4] = 0
Computing m[1, 2], m[2, 3], m[3, 4]
m[1,2] + m[3,3] + p0 . p2 . p3 ))
m[1, 3] = min(0+25000+10.100.50, 5000+0+10.5.50)
= min(75000, 2500) = 2500
s[1, 3] = k = 2
Computing m[2, 4]
1 5 8 10
2 6 9
Order of Computation 3 7
4
K,s Values Leading Minimum m[i, j]
0 1 2 2
0 2 2
0 3
0
Representing Order using Binary Tree
The above computation shows that the minimum cost for
multiplying those four matrices is 11000.
The optimal order for multiplication is
((A1 . A2) . (A3 . A4))
2
For, m(1, 4) 10x20
k=2 1
3
10 x 5 5 x 20
A1 A2 A3 A4
10 x 100 100 x 5 5 x 50 50 x 20
Chain-Matrix-Order(p)
1. n length[p] – 1
2. for i 1 to n m[1,1] m[1,2] m[1,3] m[1,4]
3. do m[i, i] 0 m[2,2] m[2,3] m[2,4]
4. for l 2 to n, m[3,3] m[3,4]
5. do for i 1 to n-l+1 m[4,4]
6. do j i+l-1
7. m[i, j]
8. for k i to j-1
9. do q m[i, k] + m[k+1, j] + pi-1 . pk . pj
10. if q < m[i, j]
11. then m[i, j] = q
12. s[i, j] k
Computational Cost
n n n n −i
T (n) = n + ( j − i ) = k
i =1 j =i +1 i =1 k =1
n
(n − i )(n − i + 1)
T ( n) = n +
i =1 2
1 n 2
T (n) = n + (n − 2ni + i 2 + n − i)
2 i =1
1 n 2 n n n n
T (n) = n + ( n − 2ni + i 2 + n − i)
2 i =1 i =1 i =1 i =1 i =1
Computational Cost
1 n 2 n n n n
T (n) = n + ( n − 2ni + i 2 + n − i)
2 i =1 i =1 i =1 i =1 i =1
1 2 n n n n n
T ( n) = n + ( n 1 − 2n i + i 2 + n 1 − i )
2 i =1 i =1 i =1 i =1 i =1
1
T (n) = n + (6n3 − 6n3 − 6n 2 + 2n3 + 3n 2 + n + 6n 2 − 3n 2 − 3n)
12
1 1 1
T (n) = (12n + 2n − 2n) = (10n + 2n ) = (5n + n3 )
3 3
12 12 6
Cost Comparison Brute Force Dynamic Programming
Dynamic Programming
There are three loop
The most two loop for i, j, satisfy the condition: 1
i j n
Cost = nC2 + n = n(n-1)/2 + n = (n2)
The third one most inner loop for k satisfies the
condition, i k < j, in worst case, it cost n and
Hence total cost = (n2 . n) = (n3)
Brute Force Approach
P(n) = C(n - 1) C(n) (4n/n3/2)
Generalization: Sequence of Objects