


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Ruan; Class: Bioinformatics; Subject: Computer Science; University: University of Texas - San Antonio; Term: Fall 2007;
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



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. 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)
Enumeration: 3,695, Recursion: 1,048, DP: 420 Three essential properties of DP algorithms: (1) The optimal solution of the problem can be computed from the optimal solutions of sub- problems (2) Some sub-problems are used multiple times (3) Solutions to these sub-problems can be memorized and reused