Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example 2 You are traveling by a canoe down a river and there are n trading posts along the way. Before starting your journey, you are given for each 1<=i<=j<=n,

Similar presentations


Presentation on theme: "Example 2 You are traveling by a canoe down a river and there are n trading posts along the way. Before starting your journey, you are given for each 1<=i<=j<=n,"— Presentation transcript:

1 Example 2 You are traveling by a canoe down a river and there are n trading posts along the way. Before starting your journey, you are given for each 1<=i<=j<=n, the fee fi,j for renting a canoe from post i to post j. These fees are arbitrary. For example it is possible that f1,3=10 and f1,4=5. you begin at trading post 1 and must end at trading post n (using rented canoes). Your goal is to minimize the rental cost. Given the most efficient algorithm you can for this problem. Be sure to prove that your algorithm yields an optimal solution and analyze the time complexity.

2 Example 2: algorithm and proof
Let m[i] be the rental cost for the best solution to go from post 1 to post i for 1<=i<=n. The final answer is m[n]. We can recursively define m[i] as follows: m[i]=0 if i=1 = min 1<=j<i (m[j] + fj,i) otherwise We now prove this is correct. The canoe must be rented starting at post 1 and then finally be returned at a station n. For a canoe returned at post i, we try all possibilities with j being the station where the canoe was previously rented. Furthermore, since fj,i is independent from how the subproblem of going from 1 to j is solved, we have the optimal substructure property. For the time complexity there are n subproblems to be solved for each post, each of which takes O(n) time. Thus, the overall time complexity is O(n2).

3 DP example 3: alignment Here we look at a problem from computational biology. You can think of a DNA sequence as a sequence defined on {a, c, g, t}. Suppose you are given DNA sequence D1 of n1 characters and DNA sequence D2 of n2 characters. You want to know whether these two DNA sequences are “evolutionarily related” by aligning them. An alignment is defined by inserting any number of spaces in D1 and D2 so that the resulting strings D1’ and D2’ both have the same length. For a particular alignment A, we say cost(A) is the number of mismatches. For example, one legal alignment between “ctatg” and “ttaagc” is: ct-at-g- -tta-agc The cost is 5. Give the most efficient algorithm you can (analyzed as a function of n1 and n2) to compute the alignment of minimum cost.

4 Example 3: algorithm & proof
The general form of the subproblem we solve will be: find the best alignment for the first i characters of D1 and the first j characters of D2 for 1<=i<=n1 and 1<=j<=n2. Let D[i] be the ith character in string D. Let D[i..j] be the substring from ith character to jth charcter. Let c[i,j] be the cost of an optimal alignment for D1[1..i] and D2[1..j]. Then c[n1,n2] gives the optimal cost to the original problem. We can define c[i,j] recursively as shown: c[i,j] = i if j=0 = j if i =0 = c[i-1,j-1] if D1[i] == D2[j] = min {c[i-1,j-1], c[i-1,j], c[i, j-1]}+1 otherwise We now argue that this recursive definition is correct. You can form D1’ and D2’ (and hence the alignment) for the subproblem from the right to left as follows. In an optimal alignment either the last character of D1’ is a spacer or it is the last character i of D1and the last character of D2’ is a spacer or the last character of D2. There are thus only three possibilities for the last column of the alignment: D1[i] -, - D2[j], or D1[i] D2[j]. Hence the above recursive definition considers all possible cases that the optimal alignment could have. Since the solution to the original problem is either the value of the subproblem solution or otherwise one plus the subproblem solution, the optimal substructure property clearly holds. Thus the solution output is correct. For the time complexity it is O(n1 n2) since there are n1 * n2 subproblems each of which is solved in constant time.

5


Download ppt "Example 2 You are traveling by a canoe down a river and there are n trading posts along the way. Before starting your journey, you are given for each 1<=i<=j<=n,"

Similar presentations


Ads by Google