Download Longest Common Subsequence and Matrix Chain Multiplication - Prof. David J. Galles and more Study notes Data Structures and Algorithms in PDF only on Docsity!
22-0: Dynamic Programming
- Simple, recursive solution to a problem
- Naive solution recalculates same value many times
- Leads to exponential running time
22-1: Fibonacci Numbers
- Calculating the nth Fibonacci number
- Fib(0) = 1
- Fib(1) = 1
- Fib(n) = Fib(n-1) + Fib(n-2)
22-2: Fibonacci Numbers
int Fibonacci(int n) {
if (n == 0) return 1;
if (n == 1) return 1;
return Fibonacci(n-1) + Fibonacci(n-2); }
22-3: Fibonacci Numbers
- Why is this solution bad?
- Recalculate values many times
22-4: Fibonacci Numbers
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1) Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1)
Fib(5)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1) Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(6)
22-5: Fibonacci Numbers
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1)
Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1)
Fib(5)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1) Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(6)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1) Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1)
Fib(5)
Fib(7)
22-6: Fibonacci Numbers
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1) Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1)
Fib(5)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1) Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(6)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1) Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1)
Fib(5)
Fib(7)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1) Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1)
Fib(5)
Fib(1) Fib(0)
Fib(2)
Fib(3) Fib(1) Fib(1) Fib(0)
Fib(2)
Fib(4)
Fib(6)
Fib(8)
22-7: How Bad is Recalculation?
- Assume 2 GHz machine
- Add every cycle
- No time spent on recursive call overhead
- Lower bound on time required
- Fibonacci(100) will take:
22-8: How Bad is Recalculation?
- Assume 2 GHz machine
- Add every cycle
- No time spent on recursive call overhead
- Lower bound on time required
- Fibonacci(100) will take:
22-9: How Bad is Recalculation?
- Assume 2 GHz machine
- Add every cycle
- No time spent on recursive call overhead
22-14: Faster Fibonacci
int Fibonacci(int n) {
int[] FIB = new int[n+1];
FIB[0] = 1; FIB[1] = 1;
for (i=2; i<=n; i++) FIB[i] = FIB[i-1] + FIB[i-2];
return FIB[n]; }
22-15: Dynamic Programming
- To create a dynamic programming solution to a problem:
- Create a simple recursive solution (that may require a large number of repeat calculations
- Design a table to hold partial results
- Fill the table such that whenever a partial result is needed, it is already in the table
22-16: World Series
- Two teams T 1 and T 2
- T 1 will win any game with probability p
- T 2 will win any game with probability 1 − p
- What is the probability that T 1 will win a best-of-seven series?
- Answer is not p : why not?
22-17: World Series
- Calculate the probability that T 1 will win the series, given T 1 needs to win x more games, and T 2 needs to win y more games - PT1win(x,y)
- The probability that P 1 will win a best-of-seven series is then PT1win(4,4)
- The probability that P 1 will win a best-of-seven series, if P 1 has already one 2 games, and P 2 has won 1 game is then PT1win(2,3)
22-18: World Series
22-19: World Series
- Base cases:
- What is PT1win(0,x)?
- What is PT1win(x,0)?
22-20: World Series
- Base cases:
- What is PT1win(0,x)?
- What is PT1win(x,0)?
22-21: World Series
- Recursive Case: PT1win(x,y)
- If T 1 wins the next, game, then the probability that T 1 will win the rest of the series is
22-22: World Series
- Recursive Case: PT1win(x,y)
- If T 1 wins the next, game, then the probability that T 1 will win the rest of the series is
- If T 1 loses the next game, then the probability that T 1 will win the rest of the series is:
22-23: World Series
- Recursive Case: PT1win(x,y)
- If T 1 wins the next, game, then the probability that T 1 will win the rest of the series is
- If T 1 loses the next game, then the probability that T 1 will win the rest of the series is
22-24: World Series
- Recursive Case: PT1win(x,y)
- If T 1 wins the next, game, then the probability that T 1 will win the rest of the series is
- If T 1 loses the next game, then the probability that T 1 will win the rest of the series is
- Probability that T 1 will win is p
- Probability that T 1 will lose is 1 − p
- What then is PT1win(x,y)?
22-29: World Series
- P [x, y] = # of games required for T 1 to win, if T 1 needs to win x more games, and T 2 needs to win y more games. - P [0, x] = 1 for all x > 0 - P [x, 0] = 0 for all x > 0 - P [x, y] = p ∗ P [x − 1 , y] + (1 − p) ∗ P [x, y − 1]
- Need to fill out the table such that when we need a partial value, it has already been computed
22-30: World Series
P[x,y] x
y 0 1 2 3 4 5
0 1 2 3 4 5
22-31: World Series
P[x,y] x
y 0 1 2 3 4 5
0 1 2 3 4 5 0 0 0 0 0 1
1
1
1
1
22-32: World Series
P[x,y] x
y 0 1 2 3 4 5
0 1 2 3 4 5 0 0 0 0 0 1
1
1
1
1
22-33: World Series
22-34: World Series
22-35: World Series
22-36: World Series
22-37: World Series
22-38: World Series
22-39: World Series
22-46: World Series
22-47: World Series
22-48: World Series
22-49: World Series
22-50: World Series
22-51: World Series
22-52: Sequences & Subsequences
- A sequence is an ordered list of elements
- A subsequence is a sequence with some elements left out;
- Subsequences of <A, B, C, B, D, A, B>
- <B, B, A>
- <A, B, C>
- <B, D, A, B>
22-53: Sequences & Subsequences
- A sequence is an ordered list of elements
- A subsequence is a sequence with some elements left out
- NON- Subsequences of <A, B, C, B, D, A, B>
- <D, A, C>
- <A, B, B, C>
- <C, A, D>
- <B, D, B, A>
22-54: Common Subsequences
- Given two sequences S 1 and S 2 , a common subsequence is s subsequence of both sequences
- <A, B, C, B, D, A, B>, <B, D, C, A, B, A>
- Common Subsequences:
- <B, C, A>
- <B, D>
- <B, A, B>
- <B, C, B, A>
22-55: LCS
LCS(Seq1, Seq2) { if (Seq1 is empty) || (Seq2 is empty) return 0; if (last elem in Seq1 = last elem in Seq2) return 1 + LCS(Seq1 - last element, Seq2 - last element) return MAX(LCS(Seq1 - last element, Seq2), LCS(Seq1, Seq2 - last element)) }
22-61: LCS Pseudo-Code
LCS(int x, int y, String S1, String S2) { if ((x == 0) || (y == 0)) return 0;
if (S1.charAt(x) == S2.charAt(y)) return 1 + LCS(x-1, y-1, S1, S2); return MAX(LCS(x-1, y, S1, S2), LCS(x, y-1, S1, S2)); }
22-62: LCS Pseudo-Code
LCS(int x, int y, String S1, String S2) { if ((x == 0) || (y == 0)) return 0;
if (S1.charAt(x) == S2.charAt(y)) return 1 + LCS(x-1, y-1, S1, S2); return MAX(LCS(x-1, y, S1, S2), LCS(x, y-1, S1, S2)); }
- Requires exponential time in (x+y)
22-63: LCS
- For x,y:
- Total number of subproblems
22-64: LCS
- For x,y:
- Total number of subproblems
22-65: LCS
- Create a table T
- T[i,j] = LCS(i, j, S1, S2)
- T[x,0] = 0
- T[0,x] = 0
- T[x,y] =
- 1 + T[x-1,y-1] if S1[x] = S2[y]
- MAX(T[x-1,y], T[x,y-1]) otherwise
22-66: LCS B D C A B A 0 1 2 3 4 5 6 0 A 1 B 2 C 3 B 4 D 5 A 6 B 7 22-67: LCS B D C A B A 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 A 1 0 B 2 0 C 3 0 B 4 0 D 5 0 A 6 0 B 7 0 22-68: LCS B D C A B A 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 A 1 0 0 B 2 0 C 3 0 B 4 0 D 5 0 A 6 0 B 7 0
22-69: LCS
B D C A B A
A 1 0 0 0
B 2 0
C 3 0
B 4 0
D 5 0
A 6 0
B 7 0
22-70: LCS
B D C A B A
A 1 0 0 0 0 1 1 1
B 2 0 1
C 3 0
B 4 0
D 5 0
A 6 0
B 7 0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