lecture-15-dynamic-programming.pdf

download lecture-15-dynamic-programming.pdf

of 195

Transcript of lecture-15-dynamic-programming.pdf

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    1/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming

    T. M. Murali

    October 14, 19, 21, 26, 28, 2009

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    2/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Algorithm Design Techniques

    1. Goal: design efficient (polynomial-time) algorithms.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    3/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Algorithm Design Techniques

    1. Goal: design efficient (polynomial-time) algorithms.

    2. GreedyPro: natural approach to algorithm design.Con: many greedy approaches to a problem. Only some may work.Con: many problems for which no greedy approach is known.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    4/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Algorithm Design Techniques

    1. Goal: design efficient (polynomial-time) algorithms.

    2. GreedyPro: natural approach to algorithm design.Con: many greedy approaches to a problem. Only some may work.Con: many problems for which no greedy approach is known.

    3. Divide and conquer

    Pro: simple to develop algorithm skeleton.Con: conquer step can be very hard to implement efficiently.Con: usually reduces time for a problem known to be solvable in polynomialtime.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    5/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Algorithm Design Techniques

    1. Goal: design efficient (polynomial-time) algorithms.

    2. GreedyPro: natural approach to algorithm design.Con: many greedy approaches to a problem. Only some may work.Con: many problems for which no greedy approach is known.

    3. Divide and conquer

    Pro: simple to develop algorithm skeleton.Con: conquer step can be very hard to implement efficiently.Con: usually reduces time for a problem known to be solvable in polynomialtime.

    4. Dynamic programmingMore powerful than greedy and divide-and-conquer strategies.Implicitly explore space of all possible solutions.Solve multiple sub-problems and build up correct solutions to larger and largersub-problems.Careful analysis needed to ensure number of sub-problems solved is polynomialin the size of the input.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    6/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    History of Dynamic Programming

    Bellman pioneered the systematic study of dynamic programming in the

    1950s.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    7/195

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    8/195

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    9/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    10/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Review: Interval Scheduling

    Interval Scheduling

    INSTANCE: Nonempty set {(s i , f i ), 1 i n} of start and nish timesof n jobs.SOLUTION: The largest subset of mutually compatible jobs.

    Two jobs are compatible if they do not overlap.

    Greedy algorithm: sort jobs in increasing order of nish times. Add next jobto current subset only if it is compatible with previously-selected jobs.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    11/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Weighted Interval Scheduling

    Weighted Interval Scheduling

    INSTANCE: Nonempty set {(s i , f i ), 1 i n} of start and nish timesof n jobs and a weight v i 0 associated with each job.SOLUTION: A set S of mutually compatible jobs such that i S v i ismaximised.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    12/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Weighted Interval Scheduling

    Weighted Interval Scheduling

    INSTANCE: Nonempty set {(s i , f i ), 1 i n} of start and nish timesof n jobs and a weight v i 0 associated with each job.SOLUTION: A set S of mutually compatible jobs such that i S v i ismaximised.

    Greedy algorithm can produce arbitrarily bad results for this problem.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    13/195

    g g g q y q g p

    Approach

    Sort jobs in increasing order of nish time and relabel: f 1 f 2 . . . f n .

    Request i comes before request j if i < j . p(j) is the largest index i < j such that job i is compatible with job j .p ( j ) = 0 if there is no such job i .

    Jobs at indices {p ( j ) + 1 , p ( j ) + 2 , . . . , j 1} are incompatible with job j .

    We will develop optimal algorithm from obvious statements about theproblem.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    14/195

    g g g q y q g p

    Detour: a Binomial Identity

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    15/195

    Detour: a Binomial Identity

    Pascals triangle:Each element is a binomial co-efficient.Each element is the sum of the twoelements above it.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    16/195

    Detour: a Binomial Identity

    Pascals triangle:Each element is a binomial co-efficient.Each element is the sum of the twoelements above it.

    nr

    =n 1r 1

    +n 1

    r

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    17/195

    Detour: a Binomial Identity

    Pascals triangle:Each element is a binomial co-efficient.Each element is the sum of the twoelements above it.

    nr

    =n 1r 1

    +n 1

    r

    Proof: Consider any subset S of r elements.Case 1 S contains the nth element:

    n 1r 1 such subsets.

    Case 2 S does not contain the nthelement: n 1r such subsets.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    18/195

    Sub-problems

    Let O be the optimal solution. Let us reason about O.

    Case 1 job n is not in O.

    Case 2 job n is in O.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    19/195

    Sub-problems

    Let O be the optimal solution. Let us reason about O.

    Case 1 job n is not in O. O must be the optimal solution for jobs{1, 2, . . . , n 1}.

    Case 2 job n is in O.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    20/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    21/195

    Sub-problems

    Let O be the optimal solution. Let us reason about O.

    Case 1 job n is not in O. O must be the optimal solution for jobs{1, 2, . . . , n 1}.

    Case 2 job n is in O.O cannot use incompatible jobs{p (n) + 1 , p (n) + 2 , . . . , n 1}.Remaining jobs in O must be the optimal solution for jobs{1, 2, . . . , p (n)}.

    O must be the best of these two choices!

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    22/195

    Sub-problems

    Let O be the optimal solution. Let us reason about O.

    Case 1 job n is not in O. O must be the optimal solution for jobs{1, 2, . . . , n 1}.

    Case 2 job n is in O.O cannot use incompatible jobs{p (n) + 1 , p (n) + 2 , . . . , n 1}.Remaining jobs in O must be the optimal solution for jobs{1, 2, . . . , p (n)}.

    O must be the best of these two choices! Suggests nding optimal solution for sub-problems consisting of jobs{1, 2, . . . , j 1, j }, for all values of j .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    23/195

    Recursion Let O j be the optimal solution for jobs {1, 2, . . . , j } and OPT ( j ) be the valueof this solution (OPT(0) = 0).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    24/195

    Recursion Let O j be the optimal solution for jobs {1, 2, . . . , j } and OPT ( j ) be the valueof this solution (OPT(0) = 0).

    We are seeking On with a value of OPT(n).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    25/195

    Recursion Let O j be the optimal solution for jobs {1, 2, . . . , j } and OPT ( j ) be the valueof this solution (OPT(0) = 0).

    We are seeking On with a value of OPT(n). To compute OPT( j ):

    Case 1 j O j :

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    26/195

    Recursion Let O j be the optimal solution for jobs {1, 2, . . . , j } and OPT ( j ) be the valueof this solution (OPT(0) = 0).

    We are seeking On with a value of OPT(n). To compute OPT( j ):

    Case 1 j O j : OPT( j ) = OPT( j 1).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    27/195

    Recursion Let O j be the optimal solution for jobs {1, 2, . . . , j } and OPT ( j ) be the valueof this solution (OPT(0) = 0).

    We are seeking On with a value of OPT(n). To compute OPT( j ):

    Case 1 j O j : OPT( j ) = OPT( j 1).Case 2 j O j :

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    28/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    29/195

    Recursion Let O j be the optimal solution for jobs {1, 2, . . . , j } and OPT ( j ) be the valueof this solution (OPT(0) = 0).

    We are seeking On with a value of OPT(n). To compute OPT( j ):

    Case 1 j O j : OPT( j ) = OPT( j 1).Case 2 j O j : OPT( j ) = v j + OPT( p ( j ))

    Final recurrence:

    OPT( j ) = max( v j + OPT( p ( j )) , OPT( j 1))

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    30/195

    Recursion Let O j be the optimal solution for jobs {1, 2, . . . , j } and OPT ( j ) be the valueof this solution (OPT(0) = 0).

    We are seeking On with a value of OPT(n). To compute OPT( j ):

    Case 1 j O j : OPT( j ) = OPT( j 1).Case 2 j O j : OPT( j ) = v j + OPT( p ( j ))

    Final recurrence:

    OPT( j ) = max( v j + OPT( p ( j )) , OPT( j 1))

    To compute O j : when does request j belong to O j ?

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    31/195

    Recursion Let O j be the optimal solution for jobs {1, 2, . . . , j } and OPT ( j ) be the valueof this solution (OPT(0) = 0).

    We are seeking On with a value of OPT(n). To compute OPT( j ):

    Case 1 j O j : OPT( j ) = OPT( j 1).Case 2 j O j : OPT( j ) = v j + OPT( p ( j ))

    Final recurrence:

    OPT( j ) = max( v j + OPT( p ( j )) , OPT( j 1))

    To compute O j : when does request j belong to O j ? If and only if v j + OPT( p ( j )) OPT( j 1).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    32/195

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    33/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    34/195

    Example of Recursive Algorithm

    OPT(6) =OPT(5) =OPT(4) =OPT(3) =OPT(2) =OPT(1) =OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    35/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5))OPT(5) =OPT(4) =OPT(3) =OPT(2) =OPT(1) =OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    36/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    l f i l i h

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    37/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5))OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max(2 + OPT(3) , OPT(4))OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max(7 + OPT(0) , OPT(3))OPT(3) =OPT(2) =OPT(1) =OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    E l f R i Al i h

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    38/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5))OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max(2 + OPT(3) , OPT(4))OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max(7 + OPT(0) , OPT(3))OPT(3) = max( v 3 + OPT( p (3)) , OPT(2)) = max(4 + OPT(1) , OPT(2))OPT(2) =OPT(1) =OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    E l f R i Al ith

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    39/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5))OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max(2 + OPT(3) , OPT(4))OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max(7 + OPT(0) , OPT(3))OPT(3) = max( v 3 + OPT( p (3)) , OPT(2)) = max(4 + OPT(1) , OPT(2))OPT(2) = max( v 2 + OPT( p (2)) , OPT(1)) = max(4 + OPT(0) , OPT(1))OPT(1) =OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    E l f R i Alg ith

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    40/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5))OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max(2 + OPT(3) , OPT(4))OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max(7 + OPT(0) , OPT(3))OPT(3) = max( v 3 + OPT( p (3)) , OPT(2)) = max(4 + OPT(1) , OPT(2))OPT(2) = max( v 2 + OPT( p (2)) , OPT(1)) = max(4 + OPT(0) , OPT(1))OPT(1) = v 1 = 2OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Recursive Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    41/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5))OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max(2 + OPT(3) , OPT(4))OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max(7 + OPT(0) , OPT(3))

    OPT(3) = max( v 3 + OPT( p (3)) , OPT(2)) = max(4 + OPT(1) , OPT(2))OPT(2) = max( v 2 + OPT( p (2)) , OPT(1)) = max( 4 + OPT(0) , OPT(1)) = 4OPT(1) = v 1 = 2OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Recursive Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    42/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5))OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max(2 + OPT(3) , OPT(4))OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max(7 + OPT(0) , OPT(3))

    OPT(3) = max( v 3 + OPT( p (3)) , OPT(2)) = max( 4 + OPT(1) , OPT(2)) = 6OPT(2) = max( v 2 + OPT( p (2)) , OPT(1)) = max( 4 + OPT(0) , OPT(1)) = 4OPT(1) = v 1 = 2OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Recursive Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    43/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5))OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max(2 + OPT(3) , OPT(4))OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max( 7 + OPT(0) , OPT(3)) = 7

    OPT(3) = max( v 3 + OPT( p (3)) , OPT(2)) = max( 4 + OPT(1) , OPT(2)) = 6OPT(2) = max( v 2 + OPT( p (2)) , OPT(1)) = max( 4 + OPT(0) , OPT(1)) = 4OPT(1) = v 1 = 2OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    44/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Recursive Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    45/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5) ) = 8OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max( 2 + OPT(3) , OPT(4)) = 8OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max( 7 + OPT(0) , OPT(3)) = 7

    OPT(3) = max( v 3 + OPT( p (3)) , OPT(2)) = max( 4 + OPT(1) , OPT(2)) = 6OPT(2) = max( v 2 + OPT( p (2)) , OPT(1)) = max( 4 + OPT(0) , OPT(1)) = 4OPT(1) = v 1 = 2OPT(0) = 0

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Recursive Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    46/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5) ) = 8OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max( 2 + OPT(3) , OPT(4)) = 8OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max( 7 + OPT(0) , OPT(3)) = 7

    OPT(3) = max( v 3 + OPT( p (3)) , OPT(2)) = max( 4 + OPT(1) , OPT(2)) = 6OPT(2) = max( v 2 + OPT( p (2)) , OPT(1)) = max( 4 + OPT(0) , OPT(1)) = 4OPT(1) = v 1 = 2OPT(0) = 0

    Optimal solution is

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Recursive Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    47/195

    Example of Recursive Algorithm

    OPT(6) = max( v 6 + OPT( p (6)) , OPT(5)) = max(1 + OPT(3) , OPT(5) ) = 8OPT(5) = max( v 5 + OPT( p ( j )) , OPT(4)) = max( 2 + OPT(3) , OPT(4)) = 8OPT(4) = max( v 4 + OPT( p (4)) , OPT(3)) = max( 7 + OPT(0) , OPT(3)) = 7

    OPT(3) = max( v 3 + OPT( p (3)) , OPT(2)) = max( 4 + OPT(1) , OPT(2)) = 6OPT(2) = max( v 2 + OPT( p (2)) , OPT(1)) = max( 4 + OPT(0) , OPT(1)) = 4OPT(1) = v 1 = 2OPT(0) = 0

    Optimal solution is job 5, job 3, and job 1.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Running Time of Recursive Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    48/195

    Running Time of Recursive Algorithm

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Running Time of Recursive Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    49/195

    Running Time of Recursive Algorithm

    What is the running time of the algorithm?

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    50/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Running Time of Recursive Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    51/195

    g g

    What is the running time of the algorithm? Can be exponential in n. When p ( j ) = j 2, for all j 2: recursive calls are for j 1 and j 2.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Memoisation

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    52/195

    Store OPT( j ) values in a cache and reuse them rather than recompute them.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Memoisation

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    53/195

    Store OPT( j ) values in a cache and reuse them rather than recompute them.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Running Time of Memoisation

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    54/195

    g

    Claim: running time of this algorithm is O (n) (after sorting).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Running Time of Memoisation

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    55/195

    Claim: running time of this algorithm is O (n) (after sorting). Time spent in a single call to M-Compute-Opt is O (1) apart from time spent inrecursive calls.

    Total time spent is the order of the number of recursive calls to M-Compute-Opt . How many such recursive calls are there in total?

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Running Time of Memoisation

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    56/195

    Claim: running time of this algorithm is O (n) (after sorting). Time spent in a single call to M-Compute-Opt is O (1) apart from time spent inrecursive calls.

    Total time spent is the order of the number of recursive calls to M-Compute-Opt . How many such recursive calls are there in total? Use number of lled entries in M as a measure of progress. Each time M-Compute-Opt issues two recursive calls, it lls in a new entry in M . Therefore, total number of recursive calls is O (n).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Computing O in Addition to OPT (n )

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    57/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Computing O in Addition to OPT (n )

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    58/195

    Explicitly store O j in addition to OPT( j ).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Computing O in Addition to OPT (n )

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    59/195

    Explicitly store O j in addition to OPT( j ). Running time becomes O (n2 ).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    60/195

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    61/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    From Recursion to Iteration

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    62/195

    Unwind the recursion and convert it into iteration. Can compute values in M iteratively in O (n) time. Find-Solution works as before.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Basic Outline of Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    63/195

    To solve a problem, we need a collection of sub-problems that satisfy a fewproperties:

    1. There are a polynomial number of sub-problems.2. The solution to the problem can be computed easily from the solutions to the

    sub-problems.3. There is a natural ordering of the sub-problems from smallest to largest.4. There is an easy-to-compute recurrence that allows us to compute the solution

    to a sub-problem from the solutions to some smaller sub-problems.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Basic Outline of Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    64/195

    To solve a problem, we need a collection of sub-problems that satisfy a fewproperties:

    1. There are a polynomial number of sub-problems.2. The solution to the problem can be computed easily from the solutions to the

    sub-problems.3. There is a natural ordering of the sub-problems from smallest to largest.4. There is an easy-to-compute recurrence that allows us to compute the solution

    to a sub-problem from the solutions to some smaller sub-problems. Difficulties in designing dynamic programming algorithms:

    1. Which sub-problems to dene?2. How can we tie together sub-problems using a recurrence?3. How do we order the sub-problems (to allow iterative computation of optimal

    solutions to sub-problems)?

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Least Squares Problem

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    65/195

    Given scientic or statistical dataplotted on two axes.

    Find the best line that passesthrough these points.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Least Squares Problem

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    66/195

    Given scientic or statistical dataplotted on two axes.

    Find the best line that passesthrough these points.

    How do we formalise the problem?

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Least Squares Problem

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    67/195

    Given scientic or statistical dataplotted on two axes.

    Find the best line that passesthrough these points.

    How do we formalise the problem?Least SquaresINSTANCE: Set P = {(x 1 , y 1), (x 2 , y 2 ), . . . , (x n , y n )} of n points.SOLUTION: Line L : y = ax + b that minimises

    Error(L, P ) =n

    i =1

    (y i ax i b )2 .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Least Squares Problem

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    68/195

    Given scientic or statistical dataplotted on two axes.

    Find the best line that passesthrough these points.

    How do we formalise the problem?Least SquaresINSTANCE: Set P = {(x 1 , y 1), (x 2 , y 2 ), . . . , (x n , y n )} of n points.SOLUTION: Line L : y = ax + b that minimises

    Error(L, P ) =n

    i =1

    (y i ax i b )2 .

    Solution is achieved by

    a = n i x i y i ( i x i ) ( i y i )

    n i x 2i ( i x i )

    2 and b =i y i a i x i

    n

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Segmented Least Squares

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    69/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Segmented Least Squares

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    70/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Segmented Least Squares

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    71/195

    Want to t multiple lines through P . Each line must t contiguous set of x -coordinates. Lines must minimise total error.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Segmented Least Squares

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    72/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    73/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Segmented Least Squares

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    74/195

    Segmented Least Squares

    INSTANCE: Set P = {p i = ( x i , y i ), 1 i n} of n points,x 1 < x 2 < < x n and a parameter C > 0.SOLUTION: A integer k , a partition of P into k segments{P 1 , P 2 , . . . , P k }, k lines L j : y = a j x + b j , 1 j k that minimise

    k

    j =1Error(L j , P j ) + Ck .

    A subset P of P is a segment if 1 i < j n exist such thatP = {(x i , y i ), (x i +1 , y i +1 ), . . . , (x j 1 , y j 1), (x j , y j )}.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Formulating the Recursion I

    Ob i i f i h i l l i Thi

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    75/195

    Observation: p n is part of some segment in the optimal solution. Thissegment starts at some point p i .

    Let OPT (i ) be the optimal value for the points {p 1 , p 2 , . . . , p i }. Let e i , j denote the minimum error of any line that ts {p i , p 2 , . . . , p j }. We want to compute OPT( n).

    If the last segment in the optimal partition is {p i , p i +1 , . . . , p n }, then

    OPT( n) = e i , n + C + OPT( i 1)

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Formulating the Recursion II

    C id th b bl th i t { }

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    76/195

    Consider the sub-problem on the points {p 1 , p 2 , . . . p j } To obtain OPT( j ), if the last segment in the optimal partition is{p i , p i +1 , . . . , p j }, then

    OPT( j ) = e i , j + C + OPT( i 1)

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Formulating the Recursion II

    Consider the sub problem on the points {p p p }

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    77/195

    Consider the sub-problem on the points {p 1 , p 2 , . . . p j } To obtain OPT( j ), if the last segment in the optimal partition is{p i , p i +1 , . . . , p j }, then

    OPT( j ) = e i , j + C + OPT( i 1)

    Since i can take only j distinct values,OPT( j ) = min

    1 i j e i , j + C + OPT( i 1)

    Segment {p i , p i +1 , . . . p j } is part of the optimal solution for this sub-problem

    if and only if the minimum value of OPT( j ) is obtained using index i .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    78/195

    OPT( j ) = min1 i j

    e i , j + C + OPT( i 1)

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    79/195

    OPT( j ) = min1 i j

    e i , j + C + OPT( i 1)

    Running time is O (n3), can be improved to O (n2 ). We can nd the segments in the optimal solution by backtracking.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    RNA Molecules RNA is a basic biological molecule. It is single stranded.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    80/195

    g g RNA molecules fold into complex secondary structures.

    Secondary structure often governs the behaviour of an RNA molecule. Various rules govern secondary structure formation:

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    RNA Molecules RNA is a basic biological molecule. It is single stranded.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    81/195

    g g RNA molecules fold into complex secondary structures.

    Secondary structure often governs the behaviour of an RNA molecule. Various rules govern secondary structure formation:

    1. Pairs of bases match up; each basematches with 1 other base.

    2. Adenine always matches with Uracil.

    3. Cytosine always matches with Guanine.

    4. There are no kinks in the folded

    molecule.5. Structures are knot-free.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    RNA Molecules RNA is a basic biological molecule. It is single stranded.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    82/195

    g g RNA molecules fold into complex secondary structures.

    Secondary structure often governs the behaviour of an RNA molecule. Various rules govern secondary structure formation:

    1. Pairs of bases match up; each basematches with 1 other base.

    2. Adenine always matches with Uracil.

    3. Cytosine always matches with Guanine.

    4. There are no kinks in the folded

    molecule.5. Structures are knot-free.

    Problem: given an RNA molecule, predict its secondary structure.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    RNA Molecules RNA is a basic biological molecule. It is single stranded.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    83/195

    RNA molecules fold into complex secondary structures.

    Secondary structure often governs the behaviour of an RNA molecule. Various rules govern secondary structure formation:

    1. Pairs of bases match up; each basematches with 1 other base.

    2. Adenine always matches with Uracil.

    3. Cytosine always matches with Guanine.

    4. There are no kinks in the folded

    molecule.5. Structures are knot-free.

    Problem: given an RNA molecule, predict its secondary structure. Hypothesis: In the cell, RNA molecules form the secondary structure with thelowest total free energy.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Formulating the Problem

    An RNA molecule is a string B = b 1b 2 . . . b n ; each b i {A, C , G , U }.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    84/195

    g 1 2 n ; i { , , , } A secondary structure on B is a set of pairs S = {(i , j )}, where 1 i , j nand

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Formulating the Problem

    An RNA molecule is a string B = b 1b 2 . . . b n ; each b i {A, C , G , U }.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    85/195

    g ; { } A secondary structure on B is a set of pairs S = {(i , j )}, where 1 i , j nand

    1. (No kinks.) If (i , j ) S , then i < j 4.2. (Watson-Crick) The elements in each pair in S consist of either {A, U } or

    {C , G } (in either order).3. S is a matching : no index appears in more than one pair.4. (No knots) If (i , j ) and ( k , l ) are two pairs in S , then we cannot have

    i < k < j < l .

    The energy of a secondary structure the number of base pairs in it.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    OPT ( j ) is the maximum number of base pairs in a secondary structure for

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    86/195

    ( j) p yb 1b 2 . . . b j .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    OPT ( j ) is the maximum number of base pairs in a secondary structure for

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    87/195

    b 1b 2 . . . b j . OPT( j ) = 0, if j 5.

    T. M. Murali October 14 19 21 26 28 2009 CS 4104: Dynamic Programming Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    OPT ( j ) is the maximum number of base pairs in a secondary structure for

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    88/195

    b 1b 2 . . . b j . OPT( j ) = 0, if j 5.

    In the optimal secondary structure on b 1b 2 . . . b j

    T M Murali October 14 19 21 26 28 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    89/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    OPT ( j ) is the maximum number of base pairs in a secondary structure for

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    90/195

    b 1b 2 . . . b j . OPT( j ) = 0, if j 5.

    In the optimal secondary structure on b 1b 2 . . . b j 1. if j is not a member of any pair, use OPT( j 1).2. if j pairs with some t < j 4,

    T M Murali October 14 19 21 26 28 2009 CS 4104: Dynamic Programming Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    OPT ( j ) is the maximum number of base pairs in a secondary structure forb b b O ( j) 0 if j

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    91/195

    b 1b 2 . . . b j . OPT( j ) = 0, if j 5.

    In the optimal secondary structure on b 1b 2 . . . b j 1. if j is not a member of any pair, use OPT( j 1).2. if j pairs with some t < j 4, knot condition yields two independent

    sub-problems!

    T M Murali October 14 19 21 26 28 2009 CS 4104: Dynamic Programming Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    OPT ( j ) is the maximum number of base pairs in a secondary structure forb b b OPT( j) 0 if j 5

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    92/195

    b 1b 2 . . . b j . OPT( j ) = 0, if j 5.

    In the optimal secondary structure on b 1b 2 . . . b j 1. if j is not a member of any pair, use OPT( j 1).2. if j pairs with some t < j 4, knot condition yields two independent

    sub-problems! OPT( t 1) and ???

    T M Murali October 14 19 21 26 28 2009 CS 4104: Dynamic Programming Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    OPT ( j ) is the maximum number of base pairs in a secondary structure forb b b OPT( j) 0 if j 5

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    93/195

    b 1b 2 . . . b j . OPT( j ) = 0, if j 5.

    In the optimal secondary structure on b 1b 2 . . . b j 1. if j is not a member of any pair, use OPT( j 1).2. if j pairs with some t < j 4, knot condition yields two independent

    sub-problems! OPT( t 1) and ???

    Insight: need sub-problems indexed both by start and by end.

    T M Murali October 14 19 21 26 28 2009 CS 4104: Dynamic Programming Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Correct Dynamic Programming Approach

    OPT (i , j ) is the maximum number of base pairs in a secondary structure forb b b

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    94/195

    b i b 2 . . . b j .

    T M Murali October 14 19 21 26 28 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    95/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Correct Dynamic Programming Approach

    OPT (i , j ) is the maximum number of base pairs in a secondary structure forbi b2 bj OPT( i j) = 0 if i j 4

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    96/195

    b i b 2 . . . b j . OPT( i , j ) = 0, if i j 4.

    In the optimal secondary structure on b i b 2 . . . b j

    T M Murali October 14 19 21 26 28 2009 CS 4104: Dynamic Programming Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Correct Dynamic Programming Approach

    OPT (i , j ) is the maximum number of base pairs in a secondary structure forbi b2 bj OPT( i j) = 0 if i j 4

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    97/195

    b i b 2 . . . b j . OPT( i , j ) = 0, if i j 4.

    In the optimal secondary structure on b i b 2 . . . b j 1. if j is not a member of any pair, compute OPT( i , j 1).

    OPT( i , j ) = max OPT( i , j 1),

    T M Murali October 14 19 21 26 28 2009 CS 4104: Dynamic Programming Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Correct Dynamic Programming Approach

    OPT (i , j ) is the maximum number of base pairs in a secondary structure forbi b2 bj OPT( i j) = 0 if i j 4

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    98/195

    b i b 2 . . . b j . OPT( i , j ) 0, if i j 4.

    In the optimal secondary structure on b i b 2 . . . b j 1. if j is not a member of any pair, compute OPT( i , j 1).2. if j pairs with some t < j 4, compute OPT( i , t 1) and OPT( t + 1 , j 1).

    OPT( i , j ) = max OPT( i , j 1),

    T M M li O t b 14 19 21 26 28 2009 CS 4104 D i P g i g Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Correct Dynamic Programming Approach

    OPT (i , j ) is the maximum number of base pairs in a secondary structure forbi b2 . . . bj OPT( i, j) = 0 if i j 4

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    99/195

    b i b 2 . . . b j . OPT( i , j ) 0, if i j 4.

    In the optimal secondary structure on b i b 2 . . . b j 1. if j is not a member of any pair, compute OPT( i , j 1).2. if j pairs with some t < j 4, compute OPT( i , t 1) and OPT( t + 1 , j 1).

    Since t can range from i to j 5,

    OPT( i , j ) = max OPT( i , j 1),

    T M M li O t b 14 19 21 26 28 2009 CS 4104 D i P i Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Correct Dynamic Programming Approach

    OPT (i , j ) is the maximum number of base pairs in a secondary structure forbi b2 . . . bj . OPT( i, j) = 0, if i j 4.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    100/195

    b i b 2 . . . b j . OPT( i , j ) 0, if i j 4.

    In the optimal secondary structure on b i b 2 . . . b j 1. if j is not a member of any pair, compute OPT( i , j 1).2. if j pairs with some t < j 4, compute OPT( i , t 1) and OPT( t + 1 , j 1).

    Since t can range from i to j 5,

    OPT( i , j ) = max OPT( i , j 1), maxt 1 + OPT( i , t 1) + OPT( t + 1 , j 1)

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Correct Dynamic Programming Approach

    OPT (i , j ) is the maximum number of base pairs in a secondary structure forb i b 2 . . . b j . OPT( i , j ) = 0, if i j 4.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    101/195

    i 2 j ( , j) , j

    In the optimal secondary structure on b i b 2 . . . b j 1. if j is not a member of any pair, compute OPT( i , j 1).2. if j pairs with some t < j 4, compute OPT( i , t 1) and OPT( t + 1 , j 1).

    Since t can range from i to j 5,

    OPT( i , j ) = max OPT( i , j 1), maxt 1 + OPT( i , t 1) + OPT( t + 1 , j 1)

    In the inner maximisation, t runs over all indices between i and j 5 thatare allowed to pair with j .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    102/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Algorithm

    OPT( i , j ) = max OPT( i , j 1), maxt

    1 + OPT( i , t 1) + OPT( t + 1 , j 1)

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    103/195

    There are O (n2 ) sub-problems. How do we order them from smallest to largest? Note that computing OPT( i , j ) involves sub-problems OPT(l , m) wherem l < j i .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Algorithm

    OPT( i , j ) = max OPT( i , j 1), maxt

    1 + OPT( i , t 1) + OPT( t + 1 , j 1)

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    104/195

    There are O (n2 ) sub-problems. How do we order them from smallest to largest? Note that computing OPT( i , j ) involves sub-problems OPT(l , m) wherem l < j i .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Algorithm

    OPT( i , j ) = max OPT( i , j 1), maxt

    1 + OPT( i , t 1) + OPT( t + 1 , j 1)

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    105/195

    There are O (n2 ) sub-problems. How do we order them from smallest to largest? Note that computing OPT( i , j ) involves sub-problems OPT(l , m) wherem l < j i .

    Running time of the algorithm is O (n3).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Algorithm

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    106/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    107/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Sequence Similarity

    Given two strings, measure how similar they are. Given a database of strings and a query string, compute the string most

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    108/195

    similar to query in the database. Applications:

    Online searches (Web, dictionary).Spell-checkers.Computational biologySpeech recognition.Basis for Unix diff .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dening Sequence Similarity

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    109/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dening Sequence Similarity

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    110/195

    Edit distance model: how many changes must you to make to one string totransform it into another?

    Changes allowed are deleting a letter, adding a letter, changing a letter.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Edit Distanceo-currance o-curr-ance

    occurrence occurre-nce

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    111/195

    Proposed by Needleman and Wunsch in the early 1970s. Input: two strings x = x 1x 2x 3 . . . x m and y = y 1y 2 . . . y n . Sets {1, 2, . . . , m} and {1, 2, . . . , n} represent positions in x and y .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Edit Distanceo-currance o-curr-ance

    occurrence occurre-nce

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    112/195

    Proposed by Needleman and Wunsch in the early 1970s. Input: two strings x = x 1x 2x 3 . . . x m and y = y 1y 2 . . . y n . Sets {1, 2, . . . , m} and {1, 2, . . . , n} represent positions in x and y . A matching of these sets is a set M of ordered pairs such that

    1. in each pair (i , j ), 1 i m and 1 j n and2. no index from x (respectively, from y ) appears as the rst (respectively,

    second) element in more than one ordered pair. An index is not matched if it does not appear in the matching.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Edit Distanceo-currance o-curr-ance

    occurrence occurre-nce

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    113/195

    Proposed by Needleman and Wunsch in the early 1970s. Input: two strings x = x 1x 2x 3 . . . x m and y = y 1y 2 . . . y n . Sets {1, 2, . . . , m} and {1, 2, . . . , n} represent positions in x and y . A matching of these sets is a set M of ordered pairs such that

    1. in each pair (i , j ), 1 i m and 1 j n and2. no index from x (respectively, from y ) appears as the rst (respectively,

    second) element in more than one ordered pair. An index is not matched if it does not appear in the matching. A matching M is an alignment if there are no crossing pairs in M : if (i , j ) M and (i , j ) M and i < i then j < j .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Edit Distanceo-currance o-curr-ance

    occurrence occurre-nce

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    114/195

    Proposed by Needleman and Wunsch in the early 1970s. Input: two strings x = x 1x 2x 3 . . . x m and y = y 1y 2 . . . y n . Sets {1, 2, . . . , m} and {1, 2, . . . , n} represent positions in x and y . A matching of these sets is a set M of ordered pairs such that

    1. in each pair (i , j ), 1 i m and 1 j n and2. no index from x (respectively, from y ) appears as the rst (respectively,

    second) element in more than one ordered pair. An index is not matched if it does not appear in the matching. A matching M is an alignment if there are no crossing pairs in M : if (i , j ) M and (i , j ) M and i < i then j < j .

    Cost of an alignment is the sum of gap and mismatch penalties:Gap penalty Penalty > 0 for every unmatched index.

    Mismatch penalty Penalty x i y j > 0 if (i , j ) M and x i = y j .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Edit Distanceo-currance o-curr-ance

    occurrence occurre-nce

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    115/195

    Proposed by Needleman and Wunsch in the early 1970s. Input: two strings x = x 1x 2x 3 . . . x m and y = y 1y 2 . . . y n . Sets {1, 2, . . . , m} and {1, 2, . . . , n} represent positions in x and y . A matching of these sets is a set M of ordered pairs such that

    1. in each pair (i , j ), 1 i m and 1 j n and2. no index from x (respectively, from y ) appears as the rst (respectively,

    second) element in more than one ordered pair. An index is not matched if it does not appear in the matching. A matching M is an alignment if there are no crossing pairs in M : if (i , j ) M and (i , j ) M and i < i then j < j .

    Cost of an alignment is the sum of gap and mismatch penalties:Gap penalty Penalty > 0 for every unmatched index.

    Mismatch penalty Penalty x i y j > 0 if (i , j ) M and x i = y j . Output: compute an alignment of minimal cost.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Consider index mx and index n y . Is (m, n) M ?

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    116/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Consider index mx and index n y . Is (m, n) M ? Claim: (m, n) M mx not matched or ny not matched.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    117/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Consider index mx and index n y . Is (m, n) M ? Claim: (m, n) M mx not matched or ny not matched.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    118/195

    OPT (i , j ): cost of optimal alignment between x = x 1x 2x 3 . . . x i andy = y 1y 2 . . . y j .(i , j ) M :

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Consider index mx and index n y . Is (m, n) M ? Claim: (m, n) M mx not matched or ny not matched.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    119/195

    OPT (i , j ): cost of optimal alignment between x = x 1x 2x 3 . . . x i andy = y 1y 2 . . . y j .(i , j ) M : OPT( i , j ) = x i y j + OPT( i 1, j 1).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Consider index mx and index n y . Is (m, n) M ? Claim: (m, n) M mx not matched or ny not matched.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    120/195

    OPT (i , j ): cost of optimal alignment between x = x 1x 2x 3 . . . x i andy = y 1y 2 . . . y j .(i , j ) M : OPT( i , j ) = x i y j + OPT( i 1, j 1).i not matched:

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Consider index mx and index n y . Is (m, n) M ? Claim: (m, n) M mx not matched or ny not matched.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    121/195

    OPT (i , j ): cost of optimal alignment between x = x 1x 2x 3 . . . x i andy = y 1y 2 . . . y j .(i , j ) M : OPT( i , j ) = x i y j + OPT( i 1, j 1).i not matched: OPT( i , j ) = + OPT( i 1, j ).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    122/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Consider index mx and index n y . Is (m, n) M ? Claim: (m, n) M mx not matched or ny not matched.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    123/195

    OPT (i , j ): cost of optimal alignment between x = x 1x 2x 3 . . . x i andy = y 1y 2 . . . y j .(i , j ) M : OPT( i , j ) = x i y j + OPT( i 1, j 1).i not matched: OPT( i , j ) = + OPT( i 1, j ). j not matched: OPT( i , j ) = + OPT( i , j 1).

    OPT( i , j ) = min ` x i y j + OPT( i 1, j 1) , +OPT( i 1, j ), +OPT( i , j 1)(i , j ) M if and only if minimum is achieved by the rst term.

    What are the base cases?

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Consider index mx and index n y . Is (m, n) M ? Claim: (m, n) M mx not matched or ny not matched.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    124/195

    OPT (i , j ): cost of optimal alignment between x = x 1x 2x 3 . . . x i andy = y 1y 2 . . . y j .(i , j ) M : OPT( i , j ) = x i y j + OPT( i 1, j 1).i not matched: OPT( i , j ) = + OPT( i 1, j ). j not matched: OPT( i , j ) = + OPT( i , j 1).

    OPT( i , j ) = min ` x i y j + OPT( i 1, j 1) , +OPT( i 1, j ), +OPT( i , j 1)(i , j ) M if and only if minimum is achieved by the rst term.

    What are the base cases? OPT( i , 0) = OPT(0 , i ) = i .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Algorithm

    OPT( i , j ) = min x i y j + OPT( i 1, j 1), + OPT( i 1, j ), + OPT( i , j 1)

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    125/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Algorithm

    OPT( i , j ) = min x i y j + OPT( i 1, j 1), + OPT( i 1, j ), + OPT( i , j 1)

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    126/195

    Running time is O (mn). Space used in O (mn).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Algorithm

    OPT( i , j ) = min x i y j + OPT( i 1, j 1), + OPT( i 1, j ), + OPT( i , j 1)

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    127/195

    Running time is O (mn). Space used in O (mn).

    Can compute OPT( m, n) in O (mn) time and O (m + n) space (Hirschberg1975, Chapter 6.7).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Algorithm

    OPT( i , j ) = min x i y j + OPT( i 1, j 1), + OPT( i 1, j ), + OPT( i , j 1)

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    128/195

    Running time is O (mn). Space used in O (mn).

    Can compute OPT( m, n) in O (mn) time and O (m + n) space (Hirschberg1975, Chapter 6.7).

    Can compute alignment in the same bounds by combining dynamicprogramming with divide and conquer.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Graph-theoretic View of Sequence Alignment

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    129/195

    Grid graph G xy :Rows labelled by symbols in x and columns labelled by symbols in y .Edges from node ( i , j ) to ( i , j + 1)), to ( i + 1 , j ), and to ( i + 1 , j + 1).Edges directed upward and to the right have cost .

    Edge directed from ( i , j ) to ( i + 1 , j + 1) has cost x i +1 y j +1 .

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Graph-theoretic View of Sequence Alignment

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    130/195

    Grid graph G xy :Rows labelled by symbols in x and columns labelled by symbols in y .Edges from node ( i , j ) to ( i , j + 1)), to ( i + 1 , j ), and to ( i + 1 , j + 1).Edges directed upward and to the right have cost .

    Edge directed from ( i , j ) to ( i + 1 , j + 1) has cost x i +1 y j +1 . f(i, j): minimum cost of a path in G XY from (0, 0) to ( i , j ). Claim: f (i , j ) = OPT( i , j ) and diagonal edges in the shortest path are thematched pairs in the alignment.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Motivation Computational nance:

    Each node is a nancial agent.The cost c uv of an edge (u , v ) is the cost of a transaction in which we buy

    from agent u and sell to agent v

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    131/195

    from agent u and sell to agent v .Negative cost corresponds to a prot.

    Internet routing protocolsDijkstras algorithm needs knowledge of the entire network.Routers only know which other routers they are connected to.Algorithm for shortest paths with negative edges is decentralised.We will not study this algorithm in the class. See Chapter 6.9.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Problem Statement Input: a directed graph G = ( V , E ) with a cost function c : E R , i.e., c uv is the cost of the edge (u , v ) E .

    A negative cycle is a directed cycle whose edges have a total cost that isnegative

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    132/195

    negative. Two related problems:

    1. If G has no negative cycles, nd the shortest s-t path : a path of from source s to destination t with minimum total cost.

    2. Does G have a negative cycle ?

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    133/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Approaches for Shortest Path Algorithm

    1. Dijsktras algorithm.

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    134/195

    2. Add some large constant to eachedge.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Approaches for Shortest Path Algorithm

    1. Dijsktras algorithm. Computesincorrect answers because it isgreedy

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    135/195

    greedy.2. Add some large constant to each

    edge. Computes incorrect answersbecause the minimum cost pathchanges.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Assume G has no negative cycles. Claim: There is a shortest path from s to t that is simple (does not repeat anode)

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    136/195

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    137/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Assume G has no negative cycles. Claim: There is a shortest path from s to t that is simple (does not repeat anode) and hence has at most n 1 edges.

    How do we dene sub-problems?

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    138/195

    How do we dene sub problems?

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Assume G has no negative cycles. Claim: There is a shortest path from s to t that is simple (does not repeat anode) and hence has at most n 1 edges.

    How do we dene sub-problems?

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    139/195

    pShortest s -t path has n 1edges: how we can reach t using i edges, for different values of i ?We do not know which nodes will

    be in shortest s -t path: how wecan reach t from each node in V ?

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Approach

    Assume G has no negative cycles. Claim: There is a shortest path from s to t that is simple (does not repeat anode) and hence has at most n 1 edges.

    How do we dene sub-problems?

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    140/195

    pShortest s -t path has n 1edges: how we can reach t using i edges, for different values of i ?We do not know which nodes will

    be in shortest s -t path: how wecan reach t from each node in V ?

    Sub-problems dened by varying thenumber of edges in the shortestpath and by varying the starting

    node in the shortest path.

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Recursion

    OPT (i , v ): minimum cost of a v -t path that uses at most i edges. t is not explicitly mentioned in the sub-problems.

    Goal is to compute OPT(n 1, s ).

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    141/195

    p ( )

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Recursion

    OPT (i , v ): minimum cost of a v -t path that uses at most i edges. t is not explicitly mentioned in the sub-problems.

    Goal is to compute OPT(n 1, s ).

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    142/195

    p ( )

    Let P be the optimal path whose cost is OPT( i , v ).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Recursion

    OPT (i , v ): minimum cost of a v -t path that uses at most i edges. t is not explicitly mentioned in the sub-problems.

    Goal is to compute OPT(n 1, s ).

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    143/195

    Let P be the optimal path whose cost is OPT( i , v ).1. If P actually uses i 1 edges, then OPT( i , v ) = OPT( i 1, v ).2. If rst node on P is w , then OPT( i , v ) = c vw + OPT( i 1, w ).

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Dynamic Programming Recursion

    OPT (i , v ): minimum cost of a v -t path that uses at most i edges. t is not explicitly mentioned in the sub-problems.

    Goal is to compute OPT(n 1, s ).

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    144/195

    Let P be the optimal path whose cost is OPT( i , v ).1. If P actually uses i 1 edges, then OPT( i , v ) = OPT( i 1, v ).2. If rst node on P is w , then OPT( i , v ) = c vw + OPT( i 1, w ).

    OPT( i , v ) = min OPT( i 1, v ), minw V

    c vw + OPT( i 1, w )

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3t

    0 1 2 3 4

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    145/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3t

    0 1 2 3 4

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    146/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    147/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    148/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3

    t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    149/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    -3

    3

    4

    2

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3

    t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    150/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    -3

    3

    4

    2

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3

    t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    151/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    -3

    0

    3

    3

    0

    -3

    3

    4

    2

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    152/195

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3

    t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    153/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    -3

    0

    3

    3

    0

    -4

    -2

    3

    3

    0

    -3

    3

    4

    2

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3

    t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    154/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    -3

    0

    3

    3

    0

    -4

    -2

    3

    3

    0

    -3

    3

    4

    2

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3

    t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    155/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    -3

    0

    3

    3

    0

    -4

    -2

    3

    3

    0

    -6

    -2

    3

    2

    0

    -3

    3

    4

    2

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3

    t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    156/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    -3

    0

    3

    3

    0

    -4

    -2

    3

    3

    0

    -6

    -2

    3

    2

    0

    -3

    3

    4

    2

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3

    t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    157/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    -3

    0

    3

    3

    0

    -4

    -2

    3

    3

    0

    -6

    -2

    3

    2

    0

    -6

    -2

    3

    0

    0

    -3

    3

    4

    2

    T. M. Murali October 14, 19, 21, 26, 28, 2009 CS 4104: Dynamic Programming

    Weighted Interval Scheduling Segmented Least Squares RNA Secondary Structure Sequence Alignment Shortest Paths in Graphs

    Example of Dynamic Programming RecursionOPT( i , v ) = min OPT( i 1, v ), min

    w V c vw + OPT( i 1, w )

    a6

    -3

    t

    0 1 2 3 4

    0 0 0 0 0 0

  • 8/10/2019 lecture-15-dynamic-programming.pdf

    158/195

    b

    c

    d

    e

    t

    -4

    8-3

    -2

    -14

    2

    3

    a

    b

    c

    d

    e

    -3

    0

    3

    3

    0

    -4

    -2

    3

    3

    0

    -6

    -2

    3

    2

    0

    -6

    -2

    3

    0

    0