Download What is Dynamic Programming - Bioinformatics | CS 5263 and more Study notes Computer Science in PDF only on Docsity! What is dynamic programming? “Programming”: has no particular connection to computer programming. It actually comes from the term "mathematical programming" (e.g. linear programming), a synonym for optimization. Here program means a plan of some events. For example, in a conference, the final schedule of the events is sometimes called a program. Motivating example: finding shortest path To find the shortest path from start to goal, we have to go to either A, B, or C. If we are greedy, we should go to the closest city, B. But this may not be the shortest path overall. Suppose that we have already found the shortest paths from A to goal, B to goal, and C to goal, we could calculate the shortest path from start to goal by: SP(start, goal) = min {Dist(start, x) + SP(x, goal)}, x = A, B, C We can use the same idea to get SP(x, goal), except that we are now closer to our goal than we originally were. Good idea. But does it always work? Not necessarily. Depending on how the smaller sub-problems can be solved and used. Example 1 Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, … F(n) = F(n-1) + F(n-2), F(0) = F(1) = 1. Analytically: F(n) ~ 1.618^n / 1.382 Pseudo-code for computing the n-th Fibonacci number by recursion: function fib(n) if (n == 0 or n == 1) return 1; else return fib(n-1) + fib(n-2) If we call fib(5), then: fib(5) = fib(4) + fib(3) = (fib(3)+fib(2)) + (fib(2)+fib(1)) = (fib(2)+fib(1)) + (fib(1)+fib(0)) + ((fib(1)+fib(0)) + 1) = ((fib(1) + fib(0)) + 1 + 1 + 1 + 1 + 1 + 1 = 8 The number of times fib is called: 15 If we call fib(10), the number of times fib is called = 177 In general, to calculate F(n), we need 2 x F(n) – 1 function calls, which is ~ 1.447 x 1.618^n, which is exponential. F(50): 40 billion fun calls. Try it on your computer. Can you do faster than your computer? function fib(n) F is an array with n+1 elements. F[0] = 0; F[1] = 1; For i = 2 to n F[n] = F[n-1] + F[n-2]; End Return F[n]; F[2] = F[1] + Fib[0] = 1 F[3] = F[2] + F[1] = 2 F[4] = F[3] + F[2] = 3 … F[10] = F[9] + F[8] = 55 Time: O(n) Memory: O(n). In fact, only need O(1) here since we don’t need to remember all F’s. just remember F[i-1] and F[i-2]. Example 2: Finding the shortest path (SP) from (0,0) to (n,n) on a nxn grid. N = 2: 6 distinct routes, each with 4 steps. To calculate path lengths for all routes by enumeration, we need 6 * 4 = 26 operations. Let sp(i, j) represents the shortest path from (0, 0) to (i, j), R(i, j) represents the distance from (i, j)