Download Dynamic Programming in Data Structures and Algorithms - Lecture Slides | CS 245 and more Study notes Data Structures and Algorithms in PDF only on Docsity!
Data Structures and Algorithms
CS245-2009S-22Dynamic Programming^ David GallesDepartment of Computer ScienceUniversity of San Francisco
22-0:^ Dynamic Programming^ Simple, recursive solution to a problem^ Naive solution recalculates same value many times^ Leads to exponential running time
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^ Many, many, times
22-5:^ Fibonacci Numbers^ Fib(3)Fib(2)Fib(1) Fib(1)^ Fib(0)^ Fib(1)^
Fib(5) Fib(4)^ Fib(3)Fib(2)Fib(1) Fib(1)^ Fib(0)Fib(2)Fib(0)^ Fib(3)Fib(2) Fib(1)^ Fib(0) Fib(6)^ Fib(1)^ Fib(4)Fib(2)Fib(1)^ Fib(1)^ Fib(0)
Fib(4) Fib(3)Fib(2)Fib(1)Fib(2)Fib(0)^ Fib(1)^ Fib(0)^ Fib(1) Fib(5)^ Fib(3)Fib(2)Fib(1)^ Fib(0) Fib(7)
22-6:^ Fibonacci Numbers^ Fib(4)^ Fib(3)Fib(2)Fib(1)Fib(2) Fib(1)^ Fib(0)^ Fib(1)^ Fib(0)
Fib(5)^ Fib(3)Fib(2)Fib(1) Fib(1)^ Fib(0)^ Fib(3)Fib(2)Fib(1)^ Fib(1)^ Fib(0) Fib(6)^ Fib(3)Fib(2)Fib(1)^ Fib(1)^ Fib(0)^ Fib(1)^ Fib(4)Fib(2)Fib(1)^ Fib(0) Fib(7)^ Fib(5)^ Fib(4)Fib(2)^ Fib(0)^ Fib(3)Fib(2)Fib(1)^ Fib(1)^ Fib(0)^ Fib(5)^ Fib(4)^ Fib(3)^ Fib(3)Fib(2)Fib(1)Fib(2)Fib(1)^ Fib(1)^ Fib(0)Fib(2) Fib(1)^ Fib(0)^ Fib(1)^ Fib(0)^ Fib(3)Fib(2)Fib(1)^ Fib(1)^ Fib(0)
Fib(8)^ Fib(6)^ Fib(4)Fib(2)^ Fib(1)^ Fib(0)
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:^ 9087 Years
22-9:^ 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:^ 9087 Years^ Fibonacci(200) will take:
22-11:^ Dynamic Programming^ Recalculating values can lead to unacceptable runtimes^ Even if the total number of values that needs tobe calculated is small^ Solution: Don’t recalculate values^ Calculate each value once^ Store results in a table^ Use the table to calculate larger results
22-12:^ Dynamic Programming^ To calculate Fibonacci(100), only need to calculate101 values^ Fibonacci(n) can be calculated in time
O(1)
Assuming we have values for Fibonacci(n-1)and Fibonacci(n-2)
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 aproblem:^ Create a simple recursive solution (that mayrequire a large number of repeat calculations^ Design a table to hold partial results^ Fill the table such that whenever a partial resultis needed, it is already in the table
22-17:^ World Series^ Calculate the probability that
Twill win the series,^1
given^ Tneeds to win^1
x^ more games, and
T^2
needs to win
y^ more games PT1win(x,y)
The probability that
Pwill win a best-of-seven^1
series is then PT1win(4,4) The probability that
Pwill win a best-of-seven^1
series, if^ P^1
has already one 2 games, and
Phas^2
won 1 game is then PT1win(2,3)
22-18:^ World Series^ Base cases:^ What is PT1win(0,x)?