Download presentation
Presentation is loading. Please wait.
Published byLynne Waters Modified over 6 years ago
1
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro. To Algorithms” book website (copyright McGraw Hill) adapted and supplemented
2
CLRS “Intro. To Algorithms” Ch. 15: Dynamic Programming
3
What is the brute-force, i. e
What is the brute-force, i.e., exhaustive method, to solve the problem? What is its running time?
4
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, if j = 1 min( f1[j-1] + a1,j , f2[j-1] + t2,j-1 + a1,j ) if j > 1 Similar equation for f2[j]. What’s the problem with a recursive algorithm based on these formulae? Think of the Fibonacci numbers!
5
Idea is to fill out the tables of f1,
f2, l1 and l2. Linear time! Ques: How does PRINT-STATIONS work?
6
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.
7
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] = if i = j mini≤k<j { m[i,k] + m[k+1,j] + pi-1pkpj } if i < j Ques: What would be the performance of a recursive algorithm based on this formula?
8
Ex
9
Optimal Binary Search Trees
0.1x1 0.15x2 0.1x2 0.05x3 0.2x3 0.05x3 0.1x3 0.05x4 0.05x4 0.05x4 0.1x4 Cost of node in red: total cost 2.80 ki are keys (k1 < k2 < … < k5), di are dummy values representing “space between” keys k-i and ki+1. Check the expected search cost of the two trees! ←probability of ki ←probability of di
10
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 keys di-1, di, …, dj. e[i, j] is the 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 if j = i -1 mini≤r≤j {e[i, r-1] + e[r+1, j] } + w(i, j) if i ≤ j Must add because the depth of each node in the left and right subtrees increases by one in the big tree.
13
Problems Read Sec. 15.3 Read Sec. 15.4: Longest common subsequences
Ex Ex Prob. 15-6 Prob. 15-7
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.