


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
[Week 8] Dynamic Programming, RNA Secondary Structure, Shortest Path
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Algorithms 9007 Tutorial questions 2019 4emester Tutorial 7
The University of Sydney School of Computer Science
Do you know the basic concepts of this week’s lecture content? These questions are only to test yourself. They will not be explicitly discussed in the tutorial, and no solutions will be given to them.
(a) What are the three main ingredients in developing a dynamic programming algorithm? (b) What is the standard technique to prove correctness of a dynamic programming algorithm?
(a) What are the subproblems? (b) What is the recurrence? Do you understand the recurrence? (c) What are the base cases?
(a) What are the subproblems? (b) What is the recurrence? Do you understand the recurrence? (c) What are the base cases? (d) Why do we use dynamic programming instead of using Dijkstra’s algorithm?
Problem 1 Run the Bellman-Ford algorithm on the directed graph below. Use vertex z as the destination and illustrate how first changes throughout the execution.
s
y z
t x
Solution: Use vertex z as the destination and the weights as shown in the figure
i = 1 : Only need to consider edges ending in z; the rest are ∞+? = ∞. (t, z) → −4, (y, z) → 9
i = 2 : • For s, check M [1, t] + 6 = 2 and M [1, y] + 7 = 16.
i = 3 : • For s, check M [2, t] + 6 = 2 and M [2, y] + 7 = 16.
i = 4 : • For s, check M [3, t] + 6 = 2 and M [3, y] + 7 = −2.
M 0 1 2 3 4 z 0 0 0 0 0 s ∞ ∞ ∞ → 2 2 2 → − 2 t ∞ -4 -4 -4 - x ∞ ∞ ∞ → − 6 -6 - y ∞ 9 9 9 → − 9 -
So we have: Shortest path from s to z is: stz Shortest path from t to z is: tz Shortest path from x to z is: xtz Shortest path from y to z is: yxtz
Problem 2 A palindrome is a string that reads the same left to right as right to left. Given a string A of length n over some alphabet Σ, your task is to design O(n^2 ) time algorithm that will delete the fewest characters from A so that what remains of the string is a palindrome. For example
A D B C D B C A,
can be turn into A D C D A,
by deleting only three characters.
the there could be multiple shortest paths going from s to t. Design a dynamic programming algorithm to compute the number of shortest paths connecting s and t. Notice that the number of shortest paths connecting s and t may be exponentially large, so we don’t want to list them, just count them.
Solution: First compute the distances from s to every other node in the graph using BFS, let dist(u) be the length of the shortest path (in terms of number of edges) from s to u. We define M [u] to be the number of shortest s-u paths in the graphs. We define M [s] = 1 as we see the vertex s by itself as a path from s to s having length 0. For vertices u at distance k > 0 from s, every shortest s-u path can arrive at u through some vertex v such that dist(v) = k − 1 and v and u are connected by an edge. This observation leads to the following recurrence: M [u] =
v:(v,u)∈E dist(v)=dist(u)− 1
M [v].
Suppose the graph has n vertices and m edges. There are n DP states and each takes O(n) to fill in the worst case; thus, the algorithm runs in O(n^2 ). A sharper bound on the running time is possible if we are more careful about how much time we spend at each node. Assuming the graph is represented with adjacency lists, computing M [u] takes O(deg(u)) time; thus, the overall all time to compute all M -values is O(
u deg(u)) =^ O(m).
Problem 5 You are given a string with n characters. The string comes from a corrupted text where all white spaces have been deleted (so it looks somethings like “thefoxjumpedoverthelazydog”). Suppose that you are given a function lookup(w) that takes as input a some string w and return True iff w is a valid word. Design an algorithm based on dynamic programming to test whether it is possible to insert spaces into the input string to obtain a valid text (we don’t care about meaning.)
Solution: Let s be the input string, and let s[i, j] be the substring consisting of characters in positions i through j. Let M [i] be True if it is possible to insert spaces into s[1, i] to obtain a valid text. Then we can define the base case as M [0] = True by regarding the empty string as a valid text. Then the recursive case is
M [i] =
1 ≤j<i
lookup(s[j, i]) ∧ M [j − 1]).
There are n states and each takes O(n) time to fill. The overall time is O(n^2 ).
Problem 6 Suppose you are given n biased coins h 1 ,... , hn; here hi is the probability that the ith coin comes up heads. Consider the following random experiment: Flip all n coins and let X be the number of heads. Define pi to be the probability that X = i. Design an efficient algorithm to compute pi for i = 0,... , n.
Solution: Let M [i, j] be the probability that flipping the first j coins yields i heads. For j = 1 we get M [0, 1] = (1 − h 0 ) and M [1, 1] = h 0. For j > 1, we can define M [j, j] = h 1 h 2... hj. For j > 1 and i < j the event that we flip i heads can be divided into two subcases: that the jth coin came up heads (in which case we need i − 1 heads from the first j − 1 coins, or that the jth coin came up tails (in which case we need i heads from the first j − 1 coins. The two events are mutually exclusive, so we get the following recurrence
M [i, j] = hj M [i − 1 , j − 1] + (1 − hj )M [i, j − 1].
In order for the recurrence to work for the boundary case i, j we can define M [i, i − 1] = 0. There are n^2 states and each takes O(1) time to compute. Thus, the overall time is O(n^2 ).