DAA Lecture 9.pptx
DAA Lecture 9.pptx
Sequence of Decisions
The principle states that optimal solutions are achieved through
a sequence of interconnected decisions.
Stage-Wise Decisions
Dynamic programming differs from greedy methods by making
decisions at every stage of the problem-solving process.
02
TABULATION VS. MEMOIZATION
FIBONACCI SEQUENCE EXAMPLE
0 Recursive Definition
1 Illustrates the recursive
nature of Fibonacci numbers,
where each term is the sum of
the two preceding ones.
0 Inefficient Recursive
Calls
2 Naive recursion leads to
redundant calculations,
significantly increasing time
complexity.
FIBONACCI NUMBERS
F(n)
F(n-1) + F(n-2)
01 02 03
Storing Top-Down
Results Reduced Time Approach
Complexity
02
01 03
F(0) = 0
F(1) = 1
F(2) = 1+0 = 1
…
F(n-2) =
F(n-1) =
F(n) = F(n-1) + F(n-2)
Efficiency:
- time
- space 10
MATRIX-CHAIN MULTIPLICATION
◼ Matrix compatibility:
C=A⋅B C=A1 ⋅ A2 ⋅⋅⋅ Ai ⋅ Ai+1 ⋅⋅⋅ An
colA = rowB coli = rowi+1
rowC = rowA rowC = rowA1
colC = colB colC = colAn
11
MATRIX-CHAIN MULTIPLICATION
A1 ⋅ A2 ⋅ A3
◼ A1: 10 x 100
◼ A2: 100 x 5
◼ A3: 5 x 50
1. ((A1 ⋅ A2) ⋅ A3): A1 ⋅ A2 = 10 x 100 x 5 = 5,000 (10 x 5)
((A1 ⋅ A2) ⋅ A3) = 10 x 5 x 50 = 2,500
Total: 7,500 scalar multiplications
2. (A1 ⋅ (A2 ⋅ A3)): A2 ⋅ A3 = 100 x 5 x 50 = 25,000 (100 x 50)
(A1 ⋅ (A2 ⋅ A3)) = 10 x 100 x 50 = 50,000
Total: 75,000 scalar multiplications
one order of magnitude difference!!
13
MATRIX-CHAIN MULTIPLICATION:
◼PROBLEM
Given a chain of matrices 〈A1, A2, …, An〉,
STATEMENT
where Ai has dimensions pi-1x pi, fully
parenthesize the product A1 ⋅ A2 ⋅⋅⋅ An in a
way that minimizes the number of scalar
multiplications.
14
WHAT IS THE NUMBER OF POSSIBLE
PARENTHESIZATIONS?
15
1. THE STRUCTURE OF AN OPTIMAL
PARENTHESIZATION
◼ Notation:
Ai…j = Ai Ai+1 ⋅⋅⋅ Aj, i ≤ j
◼ i = j: Ai…i = Ai ⇒ m[i, i] =
18
2. A RECURSIVE SOLUTION
◼Consider the subproblem of parenthesizing
Ai…j = Ai Ai+1 ⋅⋅⋅ Aj for 1 ≤ i ≤ j ≤ n
m[i, k] m[k+1,j]
◼Assume that the optimal parenthesization
splits the product Ai Ai+1 ⋅⋅⋅ Aj at k (i ≤ k < j)
m[i, j] =
m[i, k] + m[k+1, j] +
pi-1pkpj
min # of min # of # of multiplications
multiplications multiplications to compute Ai…kAk…j
to compute Ai…k to compute Ak+1…j 19
2. A RECURSIVE SOLUTION (CONT.)
20
3. COMPUTING THE OPTIMAL COSTS
0 if i = j
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j
i≤k<j
0 if i = j
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j
i≤k<j
0 if i = j
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j
o n
c s
1 2 3 sen fir
d t
n
m[1, n] gives the
optimal
solution to the problem
i≤k<j j
3
◼ Length = 1: i = j, i = 1, 2, …, n
2
◼ Length = 2: j = i + 1, i = 1, 2, …, n-1
Compute rows from bottom to 1
top and from left to right
i 23
m[2, 2]
EXAMPLE: MIN {M[I, K] + M[K+1, J] +
+ m[3,
PI-1PKP5]
J
}+
p1p2p5 k=2
i
24
EXAMPLE MIN {M[I, K] + M[K+1, J] + PI-1PKPJ}
Compute A1 ⋅ A2 ⋅ A3 1 2 3
2 2
◼ A1: 10 x 100 (p0 x p1) 3 750 2500 0
1 0 0
◼ A2: 100 x 5 (p1 x p2) 2 500 0
◼ A3: 5 x 50 (p2 x p3) 0
1 0
m[i, i] = 0 for i = 1, 2, 3
m[1, 2] = m[1, 1] + m[2, 2] + p0p1p2 (A1A2)
= 0 + 0 + 10 *100* 5 = 5,000
m[2, 3] = m[2, 2] + m[3, 3] + p1p2p3 (A2A3)
= 0 + 0 + 100 * 5 * 50 = 25,000
m[1, 3] = min m[1, 1] + m[2, 3] + p0p1p3 = 75,000 (A1(A2A3))
m[1, 2] + m[3, 3] + p0p2p3 = 7,500 ((A1A2)A3)
25
MATRIX-CHAIN-ORDER(P)
O(N3)
26