CS481: Bioinformatics Algorithms Can Alkan EA224 calkan@cs.bilkent.edu.tr http://www.cs.bilkent.edu.tr/~calkan/teaching/cs481/
Outline DNA Sequence Comparison Change Problem Manhattan Tourist Problem Longest Paths in Graphs Sequence Alignment Edit Distance Longest Common Subsequence Problem Dot Matrices
DNA sequence comparison Gene similarities between two genes with known and unknown function alert biologists to some possibilities Computing a similarity score between two genes tells how likely it is that they have similar functions Dynamic programming is a technique for revealing similarities between genes The Change Problem is a good problem to introduce the idea of dynamic programming
The Change Problem Goal: Convert some amount of money M into given denominations, using the fewest possible number of coins Input: An amount of money M, and an array of d denominations c = (c 1 , c 2 , … , c d ), in a decreasing order of value (c 1 > c 2 > … > c d ) Output: A list of d integers i 1 , i 2 , … , i d such that c 1 i 1 + c c 2 i 2 + … + c d i d = M M and i 1 + i 2 + … + i d is minimal
Change Problem: Example Given the denominations 1, 3, and 5, what is the minimum number of coins needed to make change for a given value? Value 1 2 3 4 5 6 7 8 9 10 Min # of coins 1 1 1 Only one coin is needed to make change for the values 1, 3, and 5
Change Problem: Example (cont ’ d) Given the denominations 1, 3, and 5, what is the minimum number of coins needed to make change for a given value? Value 1 2 3 4 5 6 7 8 9 10 Min # of coins 1 2 1 2 1 2 2 2 However, two coins are needed to make change for the values 2, 4, 6, 8, and 10.
Change Problem: Example (cont ’ d) Given the denominations 1, 3, and 5, what is the minimum number of coins needed to make change for a given value? Value 1 2 3 4 5 6 7 8 9 10 Min # of coins 1 2 1 2 1 2 3 2 3 2 Lastly, three coins are needed to make change for the values 7 and 9
Change Problem: Recurrence This example is expressed by the following recurrence relation: minNumCoins(M-1) + 1 min of minNumCoins(M-3) + 1 minNumCoins(M) = minNumCoins(M-5) + 1
Change Problem: Recurrence (cont ’ d) Given the denominations c: c 1 , c 2 , … , c d , the recurrence relation is: minNumCoins(M-c 1 ) + 1 minNumCoins(M-c 2 ) + 1 min of minNumCoins(M) = … minNumCoins(M-c d ) + 1
Change Problem: A Recursive Algorithm Re Recu cursiveChange siveChange( M,c,d ) 1. 1. if if M = 0 2. 2. ret eturn rn 0 3. 3. bestNumCoins NumCoins infinity 4. for i 1 to d 5. if if M ≥ c i 6. 6. numCoins Re Recu cursiveChange iveChange( M – c i , c, d ) 7. 7. if if numCoins + 1 < bestNumCoins 8. bestNumCoins numCoins + 1 9. 9. return rn bestNum umCoins oins 10.
RecursiveChange Is Not Efficient It recalculates the optimal coin combination for a given amount of money repeatedly i.e., M = 77, c = (1,3,7): Optimal coin combo for 70 cents is computed 9 times!
The RecursiveChange Tree 77 76 74 70 75 73 69 73 71 67 69 67 63 74 72 68 68 66 62 70 68 64 68 66 62 62 60 56 72 70 66 72 70 66 66 64 60 66 64 60 . . . . . . 70 70 70 70 70
We Can Do Better We’re re -computing values in our algorithm more than once Save results of each computation for 0 to M This way, we can do a reference call to find an already computed value, instead of re-computing each time Running time M * d , where M is the value of money and d is the number of denominations
The Change Problem: Dynamic Programming 1. 1. DPCha hange( nge(M,c,d) 2. 2. bes estNumC NumCoins oins 0 0 3. 3. for m 1 t 1 to M 4. 4. bestNum NumCoins oins m infini nity ty 5. 5. for i i 1 t 1 to d 6. 6. if m ≥ c i 7. 7. if bestNum umCoins oins m m – ci + 1 < b bestNumCoins NumCoins m 8. 8. bes estNum umCoi Coins ns m bes estNumC NumCoins oins m m – ci + 1 9. 9. return n bestNum umCoins oins M
DPChange: Example 0 0 1 2 3 4 5 6 0 0 1 2 1 2 3 2 0 1 0 1 2 3 4 5 6 7 0 1 0 1 2 1 2 3 2 1 0 1 2 0 1 2 0 1 2 3 4 5 6 7 8 0 1 2 3 0 1 2 1 2 3 2 1 2 0 1 2 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 0 1 2 1 2 3 2 1 2 3 0 1 2 1 2 c = (1,3,7) 0 1 2 3 4 5 M = 9 0 1 2 1 2 3
Manhattan Tourist Problem (MTP) Imagine seeking a path (from source Source * * to sink) to travel (only eastward * * * and southward) * with the most * * number of * * * * attractions (*) in Sink the Manhattan grid
Manhattan Tourist Problem (MTP) Imagine seeking a path (from source Source * * to sink) to travel (only eastward * * * and southward) * with the most * * number of * * * * attractions (*) in Sink the Manhattan grid
Manhattan Tourist Problem: Formulation Goal: Find the longest path in a weighted grid. Input: A weighted grid G with two distinct vertices, one labeled “ source ” and the other labeled “ sink ” Output: A longest path in G from “ source ” to “ sink ”
MTP: An Example 0 1 2 3 4 j coordinate source 3 2 4 0 3 5 9 0 0 1 0 4 3 2 2 3 2 4 13 1 1 4 6 5 2 i coordinate 0 7 3 4 15 19 2 4 5 2 4 1 0 2 3 3 3 20 3 8 5 6 5 2 sink 1 3 2 23 4
MTP: Greedy Algorithm Is Not Optimal 1 2 5 source 3 10 5 5 2 1 5 3 5 3 1 4 2 3 promising 5 0 0 2 start, but 22 0 0 0 leads to bad sink choices! 18
MTP: Simple Recursive Program MT( T(n,m n,m) if n=0 or or m=0 return rn MT( T(n,m) m) x x MT( T(n-1, 1,m)+ m)+ len ength h of the e ed edge e from (n (n- 1, 1,m) to (n,m) y y MT( T(n,m-1) 1)+ length h of the edge from (n,m-1) 1) to (n,m) ret eturn n max ax{x, x,y} y}
MTP: Simple Recursive Program MT( T(n,m n,m) x x MT( T(n-1, 1,m)+ m)+ length h of the edge from (n (n- 1, 1,m) to (n,m) y y MT( T(n,m-1) 1)+ len ength h of the e ed edge e from (n,m-1) 1) to (n,m) return n min{x, x,y} y} What’s wrong with this approach?
MTP: Dynamic Programming j 0 1 source 1 0 1 S 0,1 = 1 i 5 1 5 S 1,0 = 5 • Calculate optimal path score for each vertex in the graph • Each vertex’s score is the maximum of the prior vertices score plus the weight of the respective edge in between
MTP: Dynamic Programming (cont’d) j 0 1 2 source 1 2 0 1 3 S 0,2 = 3 i 5 3 -5 1 5 4 S 1,1 = 4 3 2 8 S 2,0 = 8
MTP: Dynamic Programming (cont’d) j 0 1 2 3 source 1 2 5 0 1 3 8 S 3,0 = 8 i 5 3 10 -5 1 1 5 4 13 S 1,2 = 13 3 5 -5 2 8 9 S 2,1 = 9 0 3 8 S 3,0 = 8
MTP: Dynamic Programming (cont’d) j 0 1 2 3 source 1 2 5 0 1 3 8 i 5 3 10 -5 -5 1 -5 1 5 4 13 8 S 1,3 = 8 3 5 -3 3 -5 2 8 9 12 S 2,2 = 12 0 0 0 3 8 9 S 3,1 = 9 greedy alg. fails!
MTP: Dynamic Programming (cont’d) j 0 1 2 3 source 1 2 5 0 1 3 8 i 5 3 10 -5 -5 1 -5 1 5 4 13 8 3 5 -3 2 3 3 -5 2 8 9 12 15 S 2,3 = 15 0 0 -5 0 0 3 8 9 9 S 3,2 = 9
MTP: Dynamic Programming (cont’d) j 0 1 2 3 source 1 2 5 0 1 3 8 Done! i 5 3 10 -5 -5 1 -5 1 5 4 13 8 (showing all back-traces) 3 5 -3 2 3 3 -5 2 8 9 12 15 0 0 -5 1 0 0 0 3 8 9 9 16 S 3,3 = 16
MTP: Recurrence Computing the score for a point (i,j) by the recurrence relation: s i-1, j + weight of the edge between (i-1, j) and (i, j) s i, j = max s i, j-1 + weight of the edge between (i, j-1) and (i, j) The running time is n x m for a n by m grid (n = # of rows, m = # of columns)
Manhattan Is Not A Perfect Grid A 2 A 3 What about diagonals? A 1 B • The score at point B is given by: s A1 + weight of the edge (A 1 , B) max s B = s A2 + weight of the edge (A 2 , B) of s A3 + weight of the edge (A 3 , B)
Manhattan Is Not A Perfect Grid (cont ’ d) Computing the score for point x is given by the recurrence relation: max s y + weight of vertex (y, x) where s x = of y є Predecessors( x) • Predecessors (x) – set of vertices that have edges leading to x • The running time for a graph G(V, E) (V is the set of all vertices and E is the set of all edges) is O(E) since each edge is evaluated once
Traveling in the Grid • The only hitch is that one must decide on the order in which visit the vertices • By the time the vertex x is analyzed, the values s y for all its predecessors y should be computed – otherwise we are in trouble. • We need to traverse the vertices in some order • Try to find such order for a directed cycle
DAG: Directed Acyclic Graph • Since Manhattan is not a perfect regular grid, we represent it as a DAG
Longest Path in DAG Problem • Goal: Find a longest path between two vertices in a weighted DAG • Input: A weighted DAG G with source and sink vertices • Output: A longest path in G from source to sink
Longest Path in DAG: Dynamic Programming • Suppose vertex v has indegree 3 and predecessors {u 1 , u 2 , u 3 } • Longest path to v from source is: + we weight of edge from u 1 to v s u1 + max + we weight of edge from u 2 to to v s v = s u2 + of + we weig ight of edge from u 3 to v s u3 + In General: s v = max u (s u + weight of edge from u to v)
Recommend
More recommend