Dynamic Programming & Algorithms: Shortest Paths, Palindromes, Scheduling, Exercises of Algorithms and Programming

[Week 8] Dynamic Programming, RNA Secondary Structure, Shortest Path

Typology: Exercises

2018/2019

Uploaded on 06/15/2019

kefart
kefart 🇺🇸

4.4

(11)

55 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Tutorial questions
Algorithms9007
20194emester Tutorial 7
The University of Sydney
School of Computer Science
Pre-tutorial questions
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.
1. Dynamic programming technique
(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?
2. RNA secondary structure
(a) What are the subproblems?
(b) What is the recurrence? Do you understand the recurrence?
(c) What are the base cases?
3. Shortest path
(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?
Tutorial
Problem 1
Run the Bellman-Ford algorithm on the directed graph below. Use vertex zas the destination and illustrate
how first changes throughout the execution.
7
9
7
5
-2
6-4 -3
2
8
s
yz
x
t
1
pf3
pf4

Partial preview of the text

Download Dynamic Programming & Algorithms: Shortest Paths, Palindromes, Scheduling and more Exercises Algorithms and Programming in PDF only on Docsity!

Algorithms 9007  Tutorial questions 2019  4emester  Tutorial 7

The University of Sydney School of Computer Science

Pre-tutorial questions

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.

  1. Dynamic programming technique

(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?

  1. RNA secondary structure

(a) What are the subproblems? (b) What is the recurrence? Do you understand the recurrence? (c) What are the base cases?

  1. Shortest path

(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?

Tutorial

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.

  • For t, check M [1, y] + 8 = 17, M [1, x] + 5 = ∞ and M [1, z] + (−4) = −4.
  • For x, check M [1, t] + (−2) = −6.
  • For y, check M [1, x] + (−3) = ∞ and M [1, z] + 9 = 9.

i = 3 : • For s, check M [2, t] + 6 = 2 and M [2, y] + 7 = 16.

  • For t, check M [2, y] + 8 = 17, M [2, x] + 5 = −1 and M [2, z] + (−4) = −4.
  • For x, check M [2, t] + (−2) = −6.
  • For y, check M [2, x] + (−3) = −9 and M [2, z] + 9 = 9.

i = 4 : • For s, check M [3, t] + 6 = 2 and M [3, y] + 7 = −2.

  • For t, check M [3, y] + 8 = −1, M [3, x] + 5 = −1 and M [3, z] + (−4) = −4.
  • For x, check M [3, t] + (−2) = −6.
  • For y, check M [3, x] + (−3) = −9 and M [3, z] + 9 = 9.

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 -

  • 0 1 2 3 4 first z NIL NIL NIL NIL NIL NIL s NIL NIL t t t t t NIL z z z z z x NIL NIL t t t t y NIL z z x x x

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 ).