Computer Organization & Design
Download
Report
Transcript Computer Organization & Design
Data Structures and
Algorithms (AT70.02)
Comp. Sc. and Inf. Mgmt.
Asian Institute of Technology
Instructor: Dr. Sumanta Guha
Slide Sources: CLRS “Intro.
To Algorithms” book website
(copyright McGraw Hill)
adapted and supplemented
CLRS “Intro. To Algorithms”
Ch. 15: Dynamic Programming
What is the brute-force, i.e., exhaustive method, to solve the problem?
What is its running time?
fi[j] = fastest possible time from start through station Si,j
Fastest time to completion = f * = min{ f1[n] + x1, f2[n] + x2 }
f1[j] =
e1 + a1,1
min( f1[j-1] + a1,j , f2[j-1] + t2,j-1 + a1,j )
Similar equation for f2[j].
if j = 1
if j > 1
What’s the problem with a recursive algorithm
based on these formulae?
Think of the Fibonacci numbers!
Idea is to fill out the tables of f1,
f2, l1 and l2. Linear time!
Ques: How does
PRINT-STATIONS work?
Straight-forward matrix multiplication: if A is p q and B is q r then
C is p r and requires pqr scalar multiplications to compute.
Consider 3 matrices A1 (size 10 100), A2 (size 100 5) and A3 (size 5 50).
There are two ways to compute the product A1A2A3:
(A1A2)A3 = A1(A2A3)
Ques: Is the complexity in terms of the number scalar of multiplications the same?
Matrix-chain multiplication problem: Given a chain A1, A2, …, An of n matrices,
where matrix Ai has size pi-1 pi, fully parenthesize the product A1A2…An in a way
that minimizes the number of scalar multiplications.
Note: There are exponentially many ways to fully parenthesize the product so an
exhaustive solution is infeasible.
Let Ai..j for the product matrix AiAi+1…Aj, where i ≤ j.
Let m[i,j] be the minimum number of scalar multiplications to compute Ai..j.
The problem is to find m[1,n]. Now,
m[i,j] =
0
mini≤k≤j { m[i,k] + m[k+1,j] + pi-1pkpj }
if i = j
if i < j
Ques: What would be the performance of a recursive algorithm based on
this formula?
Ex. 15.2-1
Optimal Binary Search Trees
ki are keys, di are dummy values
representing “space between” keys.
←probability of ki
←probability of di
Check the expected
search cost of the
two trees!
Finding a Recursive Solution
through Dynamic Programming
Critical Observation: If an optimal BST T has a subtree T’ containing keys
ki, …, kj, then this must be the optimal BST for the subproblem with keys
ki, …, kj and dummy key di-1, di, …, dj.
e[i, j] is expected cost of searching an optimal BST containing the keys
ki, …, kj. Ultimately, we wish to compute e[1, n].
For subtree with keys ki, …, kj denote
w(i, j) = ∑l=i..j pl+ ∑l=i-1..j ql
If kr is the root of the optimal subtree containing ki, …, kj, we have
e[i, j] = pr + (e(i, r-1] + w(i, r-1)) + (e[r+1, j] + w(r+1, j))
which is equivalent to
e[i, j] = e[i, r-1] + e[r+1, j] + w(i, j)
because w(i, j) = w(i, r-1) + pr + w(r+1, j)
Final, recursive formula:
e[i, j] = qi-1
if j = i -1
mini≤r≤j {e[i, r-1] + e[r+1, j] + w(i, j)}
if i ≤ j
Problems
Read Sec. 15.3
Read Sec. 15.4: Longest common subsequences
Ex. 15.4-1
Ex. 15.5-2
Prob. 15-6
Prob. 15-7