You are on page 1of 61

BBM 202 - ALGORITHMS

DEPT. OF COMPUTER ENGINEERING

DYNAMIC PROGRAMMING

Acknowledgement: The course slides are adapted from the slides prepared by K. Wayne of Princeton
University.


Algorithmic paradigms

Greed. Process the input in some order, myopically making irrevocable decisions.


Divide-and-conquer. Break up a problem into independent subproblems;



solve each subproblem; combine solutions to subproblems to form solution to
original problem.

Dynamic programming. Break up a problem into a series of overlapping
subproblems; combine solutions to smaller subproblems to form solution to large
subproblem.

fancy name fo
caching intermediate result
in a table for later reuse

2
r

Dynamic programming history

Bellman. Pioneered the systematic study of dynamic programming in 1950s



Etymology
・Dynamic programming = planning over time
・Secretary of Defense had pathological fear of mathematical research
・Bellman sought a “dynamic” adjective to avoid con ict.

THE THEORY OF DYNAMIC PROGRAMMING


RICHARD BELLMAN

1. Introduction. Before turning to a discussion of some representa-


tive problems which will permit us to exhibit various mathematical
features of the theory, let us present a brief survey of the funda-
mental concepts, hopes, and aspirations of dynamic programming.
To begin with, the theory was created to treat the mathematical
problems arising from the study of various multi-stage decision
processes, which may roughly be described in the following way: We
have a physical system whose state at any time / is determined by a
set of quantities which we call state parameters, or state variables.
At certain times, which may be prescribed in advance, or which may
be determined by the process itself, we are called upon to make de-
cisions which will affect the state of the system. These decisions are
equivalent to transformations of the state variables, the choice of a
decision being identical with the choice of a transformation. The out-
come of the preceding decisions is to be used to guide the choice of
future ones, with the purpose of the whole process that of maximizing
some function of the parameters describing the final state.
Examples of processes fitting this loose description are furnished
by virtually every phase of modern life, from the planning of indus-
trial production lines to the scheduling of patients at a medical
clinic ; from the determination of long-term investment programs for
universities to the determination of a replacement policy for ma-
chinery in factories; from the programming of training policies for
skilled and unskilled labor to the choice of optimal purchasing and in-
ventory policies for department stores and military establishments.
It is abundantly clear from the very brief description of possible
applications t h a t the problems arising from the study of these
processes are problems of the future as well as of the immediate 3
present.
.

fl
.

Dynamic programming applications

Application areas.
・Computer science: AI, compilers, systems, graphics, theory, …
・Operations research
・Information theory
・Control theory
・Bioinformatics

Some famous dynamic programming algorithms.
・Avidan–Shamir for seam carving
・Unix diff for comparing two les
・Viterbi for hidden Markov models
・De Boor for evaluating spline curves
・Bellman–Ford–Moore for shortest path
・Knuth–Plass for word wrapping text in T X
・Cocke–Kasami–Younger for parsing context-free grammars
<latexit sha1_base64="EhG7/UUHfhF6o3p74Glw1DH78ik=">AAACK3icbVBNS8NAEN34WetXW49egkXwVBIRrLeCF48VGltoQ9lspu3SzSbsTqQl9C941V/hr/GiePV/mKQ52NYHC4/3ZnZmnhcJrtGyPo2t7Z3dvf3SQfnw6PjktFKtPekwVgwcFopQ9TyqQXAJDnIU0IsU0MAT0PWm95nffQaleSg7OI/ADehY8hFnFDNp0IHesFK3GlYOc5PYBamTAu1h1agN/JDFAUhkgmrdt60I3YQq5EzAojyINUSUTekY+imVNADtJvmyC/MyVXxzFKr0STRz9W9HQgOt54GXVgYUJ3rdy8T/vH6Mo6abcBnFCJItB41iYWJoZpebPlfAUMxTQpni6a4mm1BFGab5rEzJ/46ArVySzGLJWejDmipwhoou0hTt9cw2iXPduGvYjzf1VrOIs0TOyQW5Ija5JS3yQNrEIYxMyAt5JW/Gu/FhfBnfy9Ito+g5Iyswfn4BrkOohg==</latexit>
sha1_base64="K4LIWrwvep29t51jV7N8ecuok+0=">AAACGXicbVBNT8JAEN3iFyIqePWykZh4Iq0X9WbixSMmVkigIdvtFDZst83u1EAa/oBXf4W/xpMx/huXwkHAl2zy8t7MzswLMykMuu6PU9nZ3ds/qB7Wjuq145PTRv3FpLnm4PNUproXMgNSKPBRoIRepoEloYRuOHlY+N1X0Eak6hlnGQQJGykRC87QSp1ho+W23RJ0m3gr0iIrDJvO2SBKeZ6AQi6ZMX3PzTAomEbBJcxrg9xAxviEjaBvqWIJmKAo95zTS6tENE61fQppqf7tKFhizCwJbWXCcGw2vYX4n9fPMb4NCqGyHEHx5aA4lxRTujiaRkIDRzmzhHEt7K6Uj5lmHG00a1PKvzPga5cU01wJnkawoUqcomZzG6K3Gdk28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helZaMouecrMH4+QWtA6iC</latexit>

・Needleman–Wunsch/Smith–Waterman for sequence alignment.


4
.

fi
.

Dynamic programming books


DYNAMIC PROGRAMMING

‣ Fibonacci number
‣ weighted interval schedulin
‣ segmented least square
‣ knapsack proble
‣ RNA secondary structur
‣ sequence alignment
m

mbers
Fibonacci numbers
Fibonacci numbers

Fibonacci
mbers. numbers.
0, 1, numbers.
Fibonacci 1, 0,13,
2, 3, 0,5,1,8,1, 1,3,1,5,
2, 21,2,8, 3,
34,
13,5,21,
8,34,
55,13,
89,21,
55, …34,
89, ... 55, 89, …
Fibonacci numbers
8 8 <latexit sha1_base64="lG5KV5dzjHqxJdRxPd6Wu2waeY8=">AAACsXicbVFda9swFJXdfXTZR9P2cRtcFlYGY8HqNloYG4VB2WMHS9sRB0+Wr1MRWTKSPBKMn/cb+yP6HyYnHixpLwgO5xzdq3uUllJYF0XXQbh17/6Dh9uPeo+fPH2209/dO7e6MhxHXEttLlNmUQqFIyecxMvSICtSiRfp7GurX/xGY4VWP9yixEnBpkrkgjPnqaT/5zQR8BniFKdC1dx3sk0v/gQRHMRFque1yKEBT7SuCOIYWpXepdJ/6mlSi3e0gbcrdNjAAWzavwDtxaiybiQk/UE0jJYFtwHtwIB0dZbsBntxpnlVoHJcMmvHNCrdpGbGCS7RL1FZLBmfsSmOPVSsQDupl4k18NozGeTa+KMcLNn/b9SssHZRpN5ZMHdlN7WWvEsbVy4/ntRClZVDxVeD8kqC09DGD5kwyJ1ceMC4Ef6twK+YYdz5T1qbsuxdIl/bpJ5XSnCd4QYr3dwZ1vgU6WZmt8H54ZB+HEbfPwxOjrs8t8lz8oq8IZQckRPyjZyREeHkJtgPXgQvw/fhz/BXmK6sYdDd2SdrFc7+Ai0pzMY=</latexit>

>
< 0 i = 0
> 0
<Fibonacci numbers. 0, 1, 1, 2,i 3,=5, 08, 13, 21, 34, 55, 89, …
Fi = 1 i=1
= 1 8
> 0
>
: i = 1
<latexit sha1_base64="lG5KV5dzjHqxJdRxPd6Wu2waeY8=">AAACsXicbVFda9swFJXdfXTZR9P2cRtcFlYGY8HqNloYG4VB2WMHS9sRB0+Wr1MRWTKSPBKMn/cb+yP6HyYnHixpLwgO5xzdq3uUllJYF0XXQbh17/6Dh9uPeo+fPH2209/dO7e6MhxHXEttLlNmUQqFIyecxMvSICtSiRfp7GurX/xGY4VWP9yixEnBpkrkgjPnqaT/5zQR8BniFKdC1dx3sk0v/gQRHMRFque1yKEBT7SuCOIYWpXepdJ/6mlSi3e0gbcrdNjAAWzavwDtxaiybiQk/UE0jJYFtwHtwIB0dZbsBntxpnlVoHJcMmvHNCrdpGbGCS7RL1FZLBmfsSmOPVSsQDupl4k18NozGeTa+KMcLNn/b9SssHZRpN5ZMHdlN7WWvEsbVy4/ntRClZVDxVeD8kqC09DGD5kwyJ1ceMC4Ef6twK+YYdz5T1qbsuxdIl/bpJ5XSnCd4QYr3dwZ1vgU6WZmt8H54ZB+HEbfPwxOjrs8t8lz8oq8IZQckRPyjZyREeHkJtgPXgQvw/fhz/BXmK6sYdDd2SdrFc7+Ai0pzMY=</latexit>

i =0
>
:
<
Fi =
F i 1 + Fi 2 i>1
Fi + F>
1
i > i1= 1 Leonardo Fib
1 :i 2
Fi 1 + Fi 2 i>1 Leonardo Fibonacci
Leonardo Fibonacci

3 5 8 13

3 5 8 13

5 8 13
21 34 55 89
7

Fibonacci numbers naïve recursive approach


Fibonacci numbers:
Fibonacci numbers

Fibonacci numbers. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …


Fibonacci numbers.
Fibonacci numbers. 0,2,1,3,1,5,2,8, 3,
0, 1, 1, 13,5,21,8,34,
13,55,21,
89,34,
... 55, 89, …
8
<80
<latexit sha1_base64="lG5KV5dzjHqxJdRxPd6Wu2waeY8=">AAACsXicbVFda9swFJXdfXTZR9P2cRtcFlYGY8HqNloYG4VB2WMHS9sRB0+Wr1MRWTKSPBKMn/cb+yP6HyYnHixpLwgO5xzdq3uUllJYF0XXQbh17/6Dh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</latexit>

>
< 0 i = 0
Fi = 1 i=1
Fi =>
: 1
F + F i > i
1 = 1
>i 1 i 2
:
Fi 1 + Fi 2 i>1
Leonardo Fib

Goal. Given n, compute Fn.


Goal. Given n, compute Fn

Naïve recursive approach:


Naive recursive approach.

public static long fib(int i)


{
if (i == 0) return 0;
3 5 8 13
if (i == 1) return 1;
return fib(i-1) + fib(i-2);
}

onacci numbers: recursion tree and exponential growth


Fibonacci
Fibonaccinumbers: Recursion
numbers: recursion treetree
and and exponential
exponential growth growth
onential waste. Same overlapping subproblems are solved repeatedly.
Exponential
Exponentialwaste. SameSame
waste. overlapping subproblems
overlapping are solved
subproblems are repeatedly.
solved 

repeatedly.
To compute fib(6):
Ex. Ex.
To compute
To computeb(6):fib(6): p
fib(5) is called 1 time.
1+ 5
・ b(5) fib(5)
is calledis1called
time. 1 time. Fn ⇠ n
, = ⇡ 1.618 p
fib(4) is called 2 times. 2 n 1+ 5
・ b(4) fib(4)
is calledis2called
times.2 times. Fn ⇠ , =
2
⇡ 1.618
fib(3) is called 3 times.
・ b(3) fib(3)
is calledis3called
times.3
 times.
fib(2) is called 5 times.
・ b(2) fib(2)
is calledis5called
times.5
 times.
fib(1) is called Fn = F6 = 8 times. fib(6)
・ b(1) fib(1)
is calledisFcalled
n = F 6 = 8 times. 

Fn = F6 = 8 times. “overlapping subproblems”
fib(6) “overlapping subproblems”
F6 “overlapping subprob
F6
F5
F5
F4 F4
F4 F4
F3 F3 F3
F3 F3 F3
F2 F2 F2 F2 F2
F2 F2 F2 F2 F2
F1 F1 F1 F1 F1 F1 F1 F1
F1 F1 F1 F1 F1 F1 F1 F1
F0 F0 F0 F0 F0
F0 F0 F0 F0 F0

running time = # subproblems × cost per subproblem


running time = # subproblems × cost per subproblem 9
fi
fi
fi
fi
fi
fi

Fibonacci numbers: Top-down dynamic programming


Fibonacci numbers: top-down dynamic programming

Memoization.
Memoization.
・Maintain an array (or symbol table) to remember all computed values
Maintain an array (or symbol table) to remember all computed values.
・If value to compute is known, just return it;
If value to compute is known, just return it;
otherwise, compute
otherwise, it; remember
compute it; and
it; remember it; return it. it.
and return

public static long fib(int i)


{
if (i == 0) return 0;
if (i == 1) return 1;
if (f[i] == 0) f[i] = fib(i-1) + fib(i-2);
return f[i];
}

assume
assumeglobal long
global array
long initialized
f[],f[],
array to 0 to 0
initialized

Impact. Solves each subproblem Fi only once; Θ(n) time to compute Fn.
Impact. Solves each subproblem Fi only once; Θ(n) time to compute Fn.

10

Fibonacci numbers: Bottom-up dynamic programming


Fibonacci numbers: bottom-up dynamic programming

Bottom-up dynamic programming.


Bottom-up dynamic programming.
・Build computation from the “bottom up.”
Build computation from the “bottom up.”
・Solve small subproblems and save solutions
Solve small subproblems and save solutions.
・Use those solutions to solve larger subproblems.
Use those solutions to solve larger subproblems.

public static long fib(int n)


{
long[] f = new long[n+1];
f[0] = 0;
f[1] = 1;
for (int i = 2; i <= n; i++)
f[i] = f[i-1] + f[i-2];
return f[n];
} smaller subproblems

Impact. Solves each subproblem Fi only once; Θ(n) time to compute Fn ; no


Impact. Solves each subproblem Fi only once; Θ(n) time to compute Fn ; no recursio
recursion. 11

Fibonacci numbers: further improvements


Fibonacci numbers: Further improvements
Performance improvements.
PerformanceSave space by saving only two most recent Fibonacci numbers.
improvements.
Performance improvements.
Save space by saving only two most recent Fibonacci numbers.
・Save space by saving only two most recent Fibonacci numbers.

public static long fib(int n) {


f and g are consecutive
int f = 1, g = 0; Fibonacci numbers
public static long fib(int n) {
for (int i = 1; i < n-1; i++) { ff and
and gg are
are consecutive
consecutive 

int f = 1, g = 0; Fibonacci
f = f + g; Fibonaccinumbers
numbers
for (int i = 1; i < n-1; i++) {
g = f - g;
f = f + g;
}
g = f - g;
return f;
}
}
return f;
}

Exploit additional properties of problem:


・Exploit additional properties of problem:
Exploit additional properties of problem:
 n
p ✓ ◆n ✓ ◆n
1+ 5 1 1 Fn+1 Fn
Fn = p , = =
 n 5 p 2 ✓ ◆n1 0 ✓ Fn ◆nFn 1
1+ 5 1 1 Fn+1 Fn
Fn = p , = =
5 2 1 0 Fn Fn 1
12

DYNAMIC PROGRAMMING

‣ Fibonacci number
‣ weighted interval schedulin
‣ segmented least square
‣ knapsack proble
‣ RNA secondary structur
‣ sequence alignment
m

Weighted interval scheduling

・Job j starts at sj, nishes at fj, and has weight wj > 0


・Two jobs are compatible if they don’t overlap
・Goal: nd max-weight subset of mutually compatible jobs.

sj wj fj

h
time
0 1 2 3 4 5 6 7 8 9 10 11
14
fi
fi
.

Weighted interval scheduling

Convention. Jobs are in ascending order of nish time: f1 ≤ f2 ≤ . . . ≤ fn



Def. p( j) = largest index i < j such that job i is compatible with j
Ex. p(8) = 1, p(7) = 3, p(2) = 0. i is leftmost interva
that ends before j begins

8
time
0 1 2 3 4 5 6 7 8 9 10 11
15
l

fi
.

Dynamic programming: Binary choice

Def. OPT( j) = max weight of any subset of mutually compatible jobs for subproblem
consisting only of jobs 1, 2, ..., j

Goal. OPT(n) = max weight of any subset of mutually compatible jobs

Case 1. OPT(j) does not select job j.
・Must be an optimal solution to problem consisting of remaining

jobs 1, 2, ..., j – 1.

 optimal substructure propert
Case 2. OPT(j) selects job j (proof via exchange argument)

・Collect pro t wj
・Can’t use incompatible jobs { p(j) + 1, p(j) + 2, ..., j – 1 }
・Must include optimal solution to problem consisting of remaining compatible
jobs 1, 2, ..., p( j)

0 j=0
Bellman equation. OP T (j) =
<latexit sha1_base64="qEOy02oDztIDL8rE3HZJCxx6JUo=">AAACxnicbVHbattAEF0pvaTuzUkf+zLUtMQ0NVJSaIpJCfSlfaoLcRPwCrNajex1Viuxu2pshKE/0g/r33Sl6KG2O7BwODN7ZuZMXEhhbBD88fy9e/cfPNx/1Hn85Omz592Dwx8mLzXHMc9lrq9jZlAKhWMrrMTrQiPLYolX8c3nOn/1E7URubq0qwKjjM2USAVn1lHT7u9vo8ujRR/o8JwOOzTGmVAVd4Jm3aHDAN4Atbi0UIFIYQ0LOIcAKJ0MTjGLXAXN2BKoxNQCrYAeQ6P3LuwfO0m4nS7gbUMVrkm/zlMtZnNXvN6V/tRIdyiqpB1h2u0Fg6AJ2AVhC3qkjdH0wDukSc7LDJXlkhkzCYPCRhXTVnCJbqfSYMH4DZvhxEHFMjRR1fi4hteOSSDNtXvKQsP++6NimTGrLHaVGbNzs52ryf/lJqVNz6JKqKK0qPhdo7SUYHOojwKJ0MitXDnAuBZuVuBzphm37nQbXRrtAvnGJtWyVILnCW6x0i6tZrWL4bZnu2B8Mvg4CL+/712ctXbuk5fkFTkiIflALsgXMiJjwr09r++deKf+Vz/3S//2rtT32j8vyEb4v/4CZY/TYA==</latexit>
max { OP T (j 1), wj + OP T (p(j)) } j>0
16
fi
.

Weighted interval scheduling: Brute force

BRUTE-FORCE (n, s1, …, sn, f1, …, fn, w1, …, wn)


_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Sort jobs by nish time and renumber so that f1 ≤ f2 ≤ … ≤ fn


Compute p[1], p[2], …, p[n] via binary search
RETURN COMPUTE-OPT(n).

COMPUTE-OPT( j )
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

IF (j = 0
RETURN 0

ELSE
RETURN max {COMPUTE-OPT( j – 1), wj + COMPUTE-OPT( p[ j ]) }.

17
)

fi
.

Weighted interval scheduling: Brute force

Observation. Recursive algorithm is spectacularly slow because of overlapping


subproblems ⇒ exponential-time algorithm.

Ex. Number of recursive calls for family of “layered” instances grows like Fibonacci
sequence.

1 4 3

2
3 2 2 1
3

4
2 1 1 0 1 0
5

p(1) = 0, p(j) = j-2 1 0

recursion tree

18

Weighted interval scheduling: Memoization

Top-down dynamic programming (memoization)


・Cache result of subproblem j in M[ j]
・Use M [ j] to avoid solving subproblem j more than once.

TOP-DOWN(n, s1, …, sn, f1, …, fn, w1, …, wn)


_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Sort jobs by nish time and renumber so that f1 ≤ f2 ≤ … ≤ fn


Compute p[1], p[2], …, p[n] via binary search
M [ 0 ] ← 0. global array

RETURN M-COMPUTE-OPT(n).

M-COMPUTE-OPT( j )
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

IF (M [ j] is uninitialized
M [ j] ← max { M-COMPUTE-OPT (j – 1), wj + M-COMPUTE-OPT(p[ j]) }
RETURN M [ j].
19
fi
)

Weighted interval scheduling: Running time

Claim. Memoized version of algorithm takes O(n log n) time


Pf
・Sort by nish time: O(n log n) via mergesort
・Compute p[ j] for each j : O(n log n) via binary search.

・M-COMPUTE-OPT(j): each invocation takes O(1) time and eithe
- (1) returns an initialized value M[ j]
- (2) initializes M [j] and makes two recursive calls


・Progress measure Φ = # initialized entries among M [1. . n]


- initially Φ = 0; throughout Φ ≤ n.
- (2) increases Φ by 1 ⇒ ≤ 2n recursive calls.


・Overall running time of M-COMPUTE-OPT(n) is O(n). ▪

20
.

fi

Weighted interval scheduling: Running time

Claim. Memoized version of algorithm takes O(n log n) time


Pf
・Sort by nish time: O(n log n) via mergesort
・Compute p[ j] for each j : O(n log n) via binary search.

・M-COMPUTE-OPT(j): each invocation takes O(1) time and eithe
- (1) returns an initialized value M[ j]
- (2) initializes M [j] and makes two recursive calls


・Progress measure Φ = # initialized entries among M [1. . n]


- initially Φ = 0; throughout Φ ≤ n.
- (2) increases Φ by 1 ⇒ ≤ 2n recursive calls.


・Overall running time of M-COMPUTE-OPT(n) is O(n). ▪

21
.

fi

22
Weighted interval scheduling: Finding a solution

Q. DP algorithm computes optimal value. How to nd optimal solution


A. Make a second pass by calling FIND-SOLUTION(n)


 FIND-SOLUTION(j)

 _____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________


 IF (j = 0

 RETURN ∅

 ELSE IF (wj + M[p[ j]] > M [ j – 1]

 RETURN { j } ∪ FIND-SOLUTION(p[ j])

 ELSE

 RETURN FIND-SOLUTION( j – 1).


 M [ j] = max { M[j – 1], wj + M[p[j]] }.


Analysis. # of recursive calls ≤ n ⇒ O(n).

23
)

fi
.

Weighted interval scheduling: Bottom-up dynamic programming

Bottom-up dynamic programming. Unwind recursion






 BOTTOM-UP(n, s1, …, sn, f1, …, fn, w1, …, wn)
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________


Sort jobs by nish time and renumber so that f1 ≤ f2 ≤ … ≤ fn

Compute p[1], p[2], …, p[n]

M[0] ← 0 previously computed values


 FOR j = 1 TO n


 M [ j] ← max { M[ j – 1], wj + M[ p[ j]] }


________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________





Running time. The bottom-up version takes O(n log n) time.

24
.

fi
.

Weighted interval scheduling: Bottom-up dynamic programming

Bottom-up dynamic programming. Unwind recursion

BOTTOM-UP(n, s1, …, sn, f1, …, fn, w1, …, wn)


_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Sort jobs by nish time and renumber so that f1 ≤ f2 ≤ … ≤ fn


Chapter 6 Dynamic Programming
Compute p[1], p[2], …, p[n]
M [0 ] ← 0
FOR j = 1 TO n 0 1 2 3 4 5 6
w1 = 2 M= 0 2
M [ j] ← max { M[ j – 1], wj + M[p[j]] }. p(1) = 0
260 w2 = 4
Chapter 6 Dynamic Programming
p(2) = 0 0 2 4
w3 = 4
Index p(3) = 1 0 1 2 3 4 5 6
w1 = 2 w4 = 7 M= 0 02 2 4 6
1 p(1)==00
p(4)
w2 = 4 w5 = 2
2 p(2) = 0
w3 = 4
p(5) =3 0
0
2 4
2 4 6 7
3 w6 = 1 p(3) = 1
w4 = 7 p(6) = 3 0 2 4 6
4 p(4) = 0
w5 = 2 0 2 4 6 7 8
5 p(5) = 3 0 2 4 6 7
w6 = 1
6 p(6) = 3 0 2 4 6 7 8 8
0 2 4 6 7 8
(a) (b) 25
.

fi
.

DYNAMIC PROGRAMMING

‣ Fibonacci number
‣ weighted interval schedulin
‣ segmented least square
‣ knapsack proble
‣ RNA secondary structur
‣ sequence alignment
m

Least squares

Least squares. Foundational problem in statistics.


・Given n points in the plane: (x1, y1), (x2, y2) , …, (xn, yn)
・Find a line y = ax + b that minimizes the sum of the squared error:

 n
!

 SSE = (yi − axi − b)2
y
i=1

<latexit sha1_base64="Zy1LiC11wpnyrhTk6pqMmahVKW8=">AAACV3icbVBNb9NAEJ0YaEP5SsKRy4oIKRyw7F5aVFWqhJA4BpXQSPmw1ptJsup6be2Oq0SW/0V/DVf4E/BrWLs+kISRdvftezM7Oy/OlLQUBL9b3qPHT46O209Pnj1/8fJVp9v7btPcCByJVKVmHHOLSmockSSF48wgT2KFN/Htp0q/uUNjZaq/0TbDWcJXWi6l4OSoqONfX39m0wt2WW1TmydRIS/Dcq6r+2AbSfaB8U19xO/np1GnH/hBHewQhA3oQxPDqNvqTRepyBPUJBS3dhIGGc0KbkgKheXJNLeYcXHLVzhxUPME7ayoByvZO8cs2DI1bmliNftvRcETa7dJ7DITTmu7r1Xk/7RJTsvzWSF1lhNq8dBomStGKatcYgtpUJDaOsCFke6vTKy54YKclztd6rczFDuTFJtcS5EucI9VtCHDS+diuO/ZIRid+h/98GvQvzpv7GzDG3gLAwjhDK7gCwxhBALu4Qf8hF+tPx54R177IdVrNTWvYSe87l8eaLJF</latexit>
sha1_base64="Pz37uMf9ZkSqUadkulfgYpEBW/M=">AAACV3icbVBNTxsxEJ0slAb6laRHLhaoUnpotMulVFWqSAipxyAIRErCyutMEguvd2XPtolW+Rf8FySu7Z9ofw3eDQcSeJLt5zczHs+LUiUt+f6/ire1/WrndXV3783bd+8/1OqNS5tkRmBPJCox/YhbVFJjjyQp7KcGeRwpvIpuTor41S80Vib6ghYpjmI+1XIiBScnhbXW+fkpG35n7WIb2iwOc9kOlte6uDcXoWRfGJ+XR/T5+iisHfotvwR7ToJHctj5cfcbHLphvdIYjhORxahJKG7tIPBTGuXckBQKl3vDzGLKxQ2f4sBRzWO0o7wcbMk+OWXMJolxSxMr1acVOY+tXcSRy4w5zexmrBBfig0ymhyPcqnTjFCLVaNJphglrHCJjaVBQWrhCBdGur8yMeOGC3JernUp305RrE2SzzMtRTLGDVXRnAxfOheDTc+ek95R61srOHNuHsMKVdiHA2hCAF+hAz+hCz0QcAv38Af+Vv574O141VWqV3ms+Qhr8OoPzEWz8Q==</latexit>
sha1_base64="kNKMdq3i/R2HN6/wEHFPEtvhKsQ=">AAACV3icbVDNThsxEJ4sLQT6l4QjF6uoEj002uVSEAJFQkgcqSBAlYSV15mAhde7smch0SpvwZnXQOIKLwGv0RfAu+FAQkey/fn7ZjyeL0qVtOT7TxVv7sPH+YXq4tKnz1++fqvVG8c2yYzAtkhUYk4jblFJjW2SpPA0NcjjSOFJdLlb6CdXaKxM9BGNUuzF/FzLgRScHBXWmoeHe6y7xbaLrWuzOMzldjA+08V9bRRK9ovxYXlEP8/Ww9qq3/TLYO9B8ApWWzt31wu3f/8dhPVKo9tPRBajJqG4tZ3AT6mXc0NSKBwvdTOLKReX/Bw7Dmoeo+3l5WBj9sMxfTZIjFuaWMm+rch5bO0ojlxmzOnCzmoF+T+tk9Fgo5dLnWaEWkwaDTLFKGGFS6wvDQpSIwe4MNL9lYkLbrgg5+VUl/LtFMXUJPkw01IkfZxhFQ3J8LFzMZj17D1orzc3m8Ef5+YGTKIKK/Ad1iCA39CCfTiANgi4gXt4gMfKswfevFedpHqV15plmAqv/gLgYbXi</latexit>





x


Solution. Calculus ⇒ min error is achieved when

! ! ! ! !
n ix i yi − ( i xi )( i yi ) i yi − a i xi
a = ! 2 ! 2
, b =
<latexit sha1_base64="uSR6MIDaEDPJwkbPgs/ygO2X/DQ=">AAACunicbVHbatwwEJXdW5reNuljKYgubRNoF3tfklICgb70MYVuE1hvzFgeJ2Jl2ZVGYRfjj+jn9Uv6WnmzJVmnA5IOZ87MSEdZraSlKPodhPfuP3j4aOvx9pOnz56/GOzs/rCVMwInolKVOcvAopIaJyRJ4VltEMpM4Wk2/9LlT6/QWFnp77SscVbChZaFFECeSge/gCef+VG3JYUB0WieWFemki9SufTHR753Q+z/wz6z325oz8c96fm4/cCTnw5ynvVm3DTxNXCriW/ZpoNhNIpWwe+CeA2GbB0n6U6wm+SVcCVqEgqsncZRTbMGDEmhsN1OnMUaxBwucOqhhhLtrFl51/K3nsl5URm/NPEVe7uigdLaZZl5ZQl0afu5jvxfbuqoOJw1UteOUIvrQYVTnCrefQTPpUFBaukBCCP9Xbm4BG8P+e/amLLqXaPYeEmzcFqKKsceq2hBBjoX475nd8FkPPo0ir9Fw+PDtZ1b7BV7w/ZYzA7YMfvKTtiECfYneB28C96HR6EIZTi/lobBuuYl24iQ/gJmldVD</latexit>
sha1_base64="h5fKMH7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LtXXI295JqbJVgRcpbrMQxamhcDNuePQX9XvegG35zbu6TWSySd+QD2SQh2SNH5JickD5h5K/33vvkffYPfeYLfzST+t59zVsyFz7eARtS2Xg=</latexit>
n i xi − ( i x i ) n

27
.

Segmented least squares

Segmented least squares


・Points lie roughly on a sequence of several line segments
・Given n points in the plane: (x1, y1), (x2, y2) , …, (xn, yn) with

x1 < x2 < ... < xn, nd a sequence of lines that minimizes f (x)

Q. What is a reasonable choice for f (x) to balance accuracy and parsimony?

goodness of t number of lines

x 28
fi
fi
.

Segmented least squares

Segmented least squares


・Points lie roughly on a sequence of several line segments
・Given n points in the plane: (x1, y1), (x2, y2) , …, (xn, yn) with

x1 < x2 < ... < xn, nd a sequence of lines that minimizes f (x)

Goal. Minimize f (x) = E + c L for some constant c > 0, where
・E = sum of the sums of the squared errors in each segment
・L = number of lines.
y

x 29
fi
.

Dynamic programming: Multiway choice

Notation
・OPT( j) = minimum cost for points p1, p2, …, pj
・ei = SSE for for points pi, pi+1, …, pj

To compute OPT( j)
・Last segment uses points pi, pi+1, …, pj for some i ≤ j
・Cost = eij + c + OPT(i – 1) optimal substructure propert


 (proof via exchange argument)


Bellman equation.

0 j=0
OP T (j) =
min { eij + c + OP T (i 1) } j>0
<latexit sha1_base64="3Vqhon9XuZlMDxUy0KO6LsmTpKw=">AAAC6XicbVFNaxsxENVuv1L3I0567GWoaUgoNbtpoQkmJdBLD4W6ECcByxitPGvL0WoXSRtsxP6Jnkqv/SP9G/031W58qO0OSHq8edKbGSWFFMZG0Z8gvHf/wcNHO49bT54+e77b3tu/NHmpOQ54LnN9nTCDUigcWGElXhcaWZZIvEpuPtX5q1vURuTqwi4LHGVsqkQqOLOeGrd/f+1fHM6PgPbOaK9FE5wK5bh/0FQt2ovgAKjFhQUHIoUK5nAGEVA67L7DbOQVNBOKSpEJa8aOmjIxlvEbVyuOvQJioBJBNPu8quoztUCdNwQcOzGv4A1wv+o6xNv4yPNUi+nMi6pt94+Ne4uimqyqHLc7UTdqArZBvAIdsor+eC/Yp5OclxkqyyUzZhhHhR05pq3gEn3bpcHCN8GmOPRQsQzNyDWjruC1ZyaQ5tovZaFh/73hWGbMMku8MmN2ZjZzNfm/3LC06cnICVWUFhW/M0pLCTaH+t9gIjRyK5ceMK6FrxX4jGnGrf/dNZfm7QL5WiduUSrB8wlusNIurGb1FOPNmW2DwXH3tBt/e985P1mNc4e8JK/IIYnJB3JOPpM+GRAeHARfgkFwGcrwe/gj/HknDYPVnRdkLcJffwFz2eL9</latexit>
1 i j

30
.

Segmented least squares algorithm

SEGMENTED-LEAST-SQUARES(n, p1, …, pn, c)


__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

FOR j = 1 TO n
FOR i = 1 TO j
Compute the SSE eij for the points pi, pi+1, …, pj

M [ 0] ← 0
previously computed value
FOR j = 1 TO n
M [ j] ← min 1 ≤ i ≤ j { eij + c + M [ i – 1] }

RETURN M [ n]
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

31
.

Segmented least squares analysis

Theorem. [Bellman 1961] DP algorithm solves the segmented least squares


problem in O(n3) time and O(n2) space

Pf.
・Bottleneck = computing SSE eij for each i and j.


! ! ! ! !

 n k yk − (
k x! k xk )( k yk ) k yk − aij k xk
aij = 2
! 2
, bij =

 <latexit sha1_base64="BChx1F3fNhGq6mUCFDX2bORLMoA=">AAACyXicbVFdb9MwFHUCjDG+uvHIi0WFtEmjSioBQwhpEi888DAkyiY1XXTj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CEz5v/5CzSm3bw=</latexit>
sha1_base64="CRJSkf/k/PXSE8sZbaW7avsGjhU=">AAACyXicbVHdTtRAFJ5WBQTUBS69mUhMIMFNu4ksRk1IvOHCC0xcIdkuzen0FIadTuv8mF2bXvEiJj6CT0PgYZzugrgLJ5mZL9/5ne8kpeDaBMGl5z96/GRhcenp8srqs+cvWmvr33RhFcMeK0ShjhPQKLjEnuFG4HGpEPJE4FEy/NT4j36g0ryQX824xEEOp5JnnIFxVNz6BXHFz2savacfmyvKFLBK0kjbPB7SUTwcu+cN3bojtm+x82zXM7EnnbnQk069Q6PvFlKaPNTorpJLvB3lX7orXsetzaAdTIzeB+EN2Nzv/rnu/r76cBiveetRWjCbozRMgNb9MCjNoAJlOBNYL0dWYwlsCKfYd1BCjnpQTaSs6WvHpDQrlDvS0An7f0YFudbjPHGROZgzPe9ryId8fWuyvUHFZWkNSjZtlFlBTUGbvdCUK2RGjB0ApriblbIzcBoZt72ZLpPaJbKZn1QjKzkrUpxjhRkZBY2K4bxm90Gv037XDr84NffI1JbIS/KKbJGQdMk+OSCHpEeYt+DteG+9Xf+zr/yR/3Ma6ns3ORtkxvyLv+PP320=</latexit>
n k x k − ( k xk ) n

・O(n) to compute eij. ▪




Remark. Can be improved to O(n2) time i i i i

・For each i : precompute cumulative sums


k=1
<latexit sha1_base64="E9Bo6rLIFL0/cuHYqsa/2SHtfn0=">AAACh3icbVDLSgMxFE3Hd33VunQTLIILKTMiPhZCxY1LBatCH0Mmc6uhmcyY3EjL0L/zJ/wFt/oBpnUQWz0QOJxzT25yokwKg77/VvLm5hcWl5ZXyqtr6xubla3qnUmt5tDkqUz1Q8QMSKGgiQIlPGQaWBJJuI/6l2P//gW0Eam6xWEGnYQ9KtETnKGTwkq3bWwS5v3zYNQVdBD2D2j72bKYTunDH708O989/DfhnHEqrNT8uj8B/UuCgtRIgetwq1Rtxym3CSjkkhnTCvwMOznTKLiEUbltDWSM99kjtBxVLAHTySdFjOieU2LaS7U7CulE/Z3IWWLMMIncZMLwycx6Y/E/r2Wxd9rJhcosguLfi3pWUkzpuFUaCw0c5dARxrVwb6X8iWnG0XU/tWVydwZ86if5wCrB0xhmVIkD1GzkWgxmO/tLmof1s3pwc1RrnBZ1LpMdskv2SUBOSINckWvSJJy8knfyQT69sud7x14x65WKzDaZgnfxBVkmxzI=</latexit>
xk ,
k=1
yk ,
k=1
x2k , . 
 xk yk
k=1

・Using cumulative sums, can compute eij in O(1) time.


32
.

DYNAMIC PROGRAMMING

‣ Fibonacci number
‣ weighted interval schedulin
‣ segmented least square
‣ knapsack proble
‣ RNA secondary structur
‣ sequence alignment
m

Knapsack problem

Goal. Pack knapsack so as to maximize total value of items taken


・There are n items: item i provides value vi > 0 and weighs wi >0
・Value of a subset of items = sum of values of individual items
・Knapsack has weight limit of W

Ex. The subset { 1, 2, 5 } has value $35 (and weight 10)
Ex. The subset { 3, 4 } has value $40 (and weight 11)

Assumption. All values and weights are integral.
i vi wi

$18
1 $1 1 kg
g weights and value
5k
$22 g
6k can be arbitrar
2 $6 2 kg
11 kg positive integers
3 $18 5 kg
$6 2 kg
4 $22 6 kg

g
5 $28 7 kg
$28
7k
$1 g
1k
knapsack instanc
Creative Commons Attribution-Share Alike 2.
(weight limit W = 11)
by Dake 34
y

Dynamic programming: Two variables

Def. OPT(i, w) = optimal value of knapsack problem with items 1, …, i, subject to


weight limit w
Goal. OPT(n, W)

 possibly because wi > w

Case 1. OPT(i, w) does not select item i.


・OPT(i, w) selects best of { 1, 2, …, i – 1 } subject to weight limit w.

Case 2. OPT(i, w) selects item i optimal substructure propert

・ Collect value vi
(proof via exchange argument)

・New weight limit = w – wi


・OPT(i, w) selects best of { 1, 2, …, i – 1 } subject to new weight limit

Bellman equation.
0 i=0

OP T (i, w) = OP T (i 1, w) wi > w

<latexit sha1_base64="yGc+dBk4QwRLdPr1J0EKEi2YjzI=">AAADBHicbVFNb9NAEF2brxK+0nLkMiICFZFGNkKlKCoq4sKNIDW0UjaK1puxs+p67e6um0RWzvwaTogr/wPxZ1i7OTgpI6309Gbem9mZKJfC2CD44/m3bt+5e2/nfuvBw0ePn7R3976ZrNAchzyTmT6PmEEpFA6tsBLPc40sjSSeRRefqvzZFWojMnVqlzmOU5YoEQvOrKMm7b9fBqf7ogvzV0D7x7TfohEmQpXceZpVi/YDeAnU4sJCCSKGFQg4hgAoHfUOMR27itrhIKw9tmrnEwEfYN6spilbAJUYW6Cl6wkNeRdot6KunOx1I3HgfKr5gGqRzJxw1WiU2RnquTDo+lHaoqim6+kn7U7QC+qAmyBcgw5Zx2Cy6+3RacaLFJXlkhkzCoPcjkumreAS3ToKgznjFyzBkYOKpWjGZX2FFbxwzBTiTLunLNRsU1Gy1JhlGrnKlNmZ2c5V5P9yo8LGR+NSqLywqPh1o7iQYDOoTgpToZFbuXSAcS3crMBnTDNu3eE3utTeOfKNn5SLQgmeTXGLlXZhNau2GG7v7CYYvum974Vf33ZOjtbr3CHPyHOyT0LyjpyQz2RAhoR7H73Ey71L/7v/w//p/7ou9b215inZCP/3P7Vj6Os=</latexit>
max { OP T (i 1, w), vi + OP T (i 1, w wi ) }
35
.

Knapsack problem: Bottom-up dynamic programming

KNAPSACK(n, W, w1, …, wn, v1, …, vn )


__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

FOR w = 0 TO W
M [ 0, w] ← 0.

previously computed values


FOR i = 1 TO
FOR w = 0 TO W
IF (wi > w) M[i, w ] ← M[i – 1, w]
ELSE M[i, w ] ← max { M [i – 1, w], vi + M [i – 1, w – wi] }.

RETURN M [ n, W]
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

0 i=0

OP T (i, w) = OP T (i 1, w) wi > w

<latexit sha1_base64="yGc+dBk4QwRLdPr1J0EKEi2YjzI=">AAADBHicbVFNb9NAEF2brxK+0nLkMiICFZFGNkKlKCoq4sKNIDW0UjaK1puxs+p67e6um0RWzvwaTogr/wPxZ1i7OTgpI6309Gbem9mZKJfC2CD44/m3bt+5e2/nfuvBw0ePn7R3976ZrNAchzyTmT6PmEEpFA6tsBLPc40sjSSeRRefqvzZFWojMnVqlzmOU5YoEQvOrKMm7b9fBqf7ogvzV0D7x7TfohEmQpXceZpVi/YDeAnU4sJCCSKGFQg4hgAoHfUOMR27itrhIKw9tmrnEwEfYN6spilbAJUYW6Cl6wkNeRdot6KunOx1I3HgfKr5gGqRzJxw1WiU2RnquTDo+lHaoqim6+kn7U7QC+qAmyBcgw5Zx2Cy6+3RacaLFJXlkhkzCoPcjkumreAS3ToKgznjFyzBkYOKpWjGZX2FFbxwzBTiTLunLNRsU1Gy1JhlGrnKlNmZ2c5V5P9yo8LGR+NSqLywqPh1o7iQYDOoTgpToZFbuXSAcS3crMBnTDNu3eE3utTeOfKNn5SLQgmeTXGLlXZhNau2GG7v7CYYvum974Vf33ZOjtbr3CHPyHOyT0LyjpyQz2RAhoR7H73Ey71L/7v/w//p/7ou9b215inZCP/3P7Vj6Os=</latexit>
max { OP T (i 1, w), vi + OP T (i 1, w wi ) }
36
n


.


Knapsack problem: Bottom-up dynamic programming

i vi wi
1 $1 1 kg 0 i=0
2 $6 2 kg OP T (i, w) = OP T (i 1, w) wi > w
3 $18 5 kg max {OP T (i 1, w), vi + OP T (i 1, w wi }
<latexit sha1_base64="Z2vromkJx+wQ2wpWsn/gLtchm2Q=">AAAC+nicbVFda9swFJW9ry77SrvHvVwWNjqWBnuMtSN0FPayt2XQrIXIBFm5TkRl2Uhyk2D8a/Y09ro/ssf9m8luHpx0FwSHc889VzqKcymMDYK/nn/n7r37D/Yedh49fvL0WXf/4LvJCs1xzDOZ6cuYGZRC4dgKK/Ey18jSWOJFfPW57l9cozYiU+d2nWOUsrkSieDMOmra/fN1dH4o+rB8A3R4SocdGuNcqJI7T1N16DCA10AtriyUIBKoQMApBEDpZPAB08gpGoejsPHY0S6nAj7Bsq2mKVsBlZhYoCW0ZvtA+3DtBt622KPagWoxXzh51bLP7AL1Uhh0WyjtUFSzzZ2n3V4wCJqC2yDcgB7Z1Gi67x3QWcaLFJXlkhkzCYPcRiXTVnCJLoTCYM74FZvjxEHFUjRR2WRfwSvHzCDJtDvKQsO2J0qWGrNOY6dMmV2Y3V5N/q83KWxyEpVC5YVFxW8WJYUEm0H9kTATGrmVawcY18LdFfiCacat++6tLY13jnzrJeWqUIJnM9xhpV1ZzeoUw93MboPxu8HHQfjtfe/sZBPnHnlBXpJDEpJjcka+kBEZE+4de5GXeHO/8n/4P/1fN1Lf28w8J1vl//4H1VHmOQ==</latexit>

4 $22 6 kg
5 $28 7 kg
weight limit w

0 1 2 3 4 5 6 7 8 9 10 11

{ } 0 0 0 0 0 0 0 0 0 0 0 0

{1} 0 1 1 1 1 1 1 1 1 1 1 1

subset
 { 1, 2 } 0 1 6 7 7 7 7 7 7 7 7 7
of item
1, …, i { 1, 2, 3 } 0 1 6 7 7 18 19 24 25 25 25 25

{ 1, 2, 3, 4 } 0 1 6 7 7 18 22 24 28 29 29 40

{ 1, 2, 3, 4, 5 } 0 1 6 7 7 18 22 28 29 34 35 40

OPT(i, w) = optimal value of knapsack problem with items 1, …, i, subject to weight limit w
37
s

Knapsack problem: Running time

Theorem. The DP algorithm solves the knapsack problem with n items



and maximum weight W in Θ(n W) time and Θ(n W) space
Pf weights are integer

・Takes O(1) time per table entry between 1 and W

・There are Θ(n W) table entries


・After computing optimal values, can trace back to nd solution:

OPT(i, w) takes item i iff M [i, w] > M [i – 1, w]. ▪



Remarks
・Algorithm depends critically on assumption that weights are integral
・Assumption that values are integral was not used.

38
.

fi
.

Coin changing

Problem. Given n coin denominations { c1, c2, …, cn } and a target value V, nd the
fewest coins needed to make change for V (or report impossible)

Ex. { 1, 10, 21, 34, 70, 100, 350, 1295, 1500 }.
Optimal. 140¢ = 70 + 70.

39
.

fi
Coin changing

Def. OPT(v) = min number of coins to make change for v



Goal. OPT(V)

Multiway choice. To compute OPT(v),
・Select a coin of denomination ci for some i
optimal substructure propert
(proof via exchange argument)

・Select fewest coins to make change for v – ci



Bellman equation

 v<0

 OP T (v) = 0 v=0

min { 1 + OP T (v ci ) }

 <latexit sha1_base64="x0As4nC8UT70WRwhhJ4Lh5hH/bg=">AAAC/XicbVFNb9NAEF2brxI+mpYjlxERqAgR2QhBUahUiQs3gtS0lbKRtdmMk1XXa2t3HBJZEb+GG+LKD+HEv2Ht5tAkjGTv85u3O89vx4VWjqLobxDeun3n7r29+60HDx893m8fHJ67vLQSBzLXub0cC4daGRyQIo2XhUWRjTVejK8+1f2LOVqncnNGywJHmZgalSopyFNJ+8+X/tnR/CXw3gnvtfgYp8pU0h/oVi3e48qktAR4AZxwQVCBSmEFc/gIEXA+7L7DbOR1/mtXc7Kp4ZkyXKtMkUuqGLhGUM3brOolJeCVtwExvILGFLwGmajaGnCrpjMvWN2YktMM7Tfl0A/jvMXRTNbGk3Yn6kZNwS6I16DD1tVPDoJDPsllmaEhqYVzwzgqaFQJS0pq9EmUDgshr8QUhx4akaEbVU36K3jumQmkufWPIWjYmzsqkTm3zMZemQmaue1eTf6vNywpPR5VyhQloZHXg9JSA+VQXyVMlEVJeumBkFZ5ryBnwgpJ/sI3pjRnFyg3/qRalEbJfIJbrKYFWVGnGG9ntgvO33TjqBt/fds5PV7nuceesmfsiMXsPTtln1mfDZgMPgRJMAtU+D38Ef4Mf11Lw2C95wnbqPD3P7o46Ns=</latexit>
1 i n


Running time. O(n V).

40
.

DYNAMIC PROGRAMMING

‣ Fibonacci number
‣ weighted interval schedulin
‣ segmented least square
‣ knapsack proble
‣ RNA secondary structur
‣ sequence alignment
m

RNA secondary structure

RNA. String B = b1b2…bn over alphabet { A, C, G, U }



Secondary structure. RNA is single-stranded so it tends to loop back and form base
pairs with itself. This structure is essential for understanding behavior of molecule.

C A

A A

A U G C
base
C G U A A G

G
U A U U A
base pair G
A C G C U
G

C G C G A G C

G
A U

RNA secondary structure for GUCGAUUGAGCGAAUGUAACAACGUGGCUACGGCGAGA


42
.

RNA secondary structure

Secondary structure. A set of pairs S = { (bi, bj) } that satisfy:


・[Watson–Crick] S is a matching and each pair in S is a Watson–Crick
complement: A–U, U–A, C–G, or G–C.

G G

C U

C G
A C G U G G C C A U

base pai S is not a secondary structur


in secondary structure A C (C-A is not a valid Watson-Crick pair)

U A

B = ACGUGGCCCAU
S = { (b1, b10), (b2, b9), (b3, b8) } 43
r

RNA secondary structure

Secondary structure. A set of pairs S = { (bi, bj) } that satisfy:


・[Watson–Crick] S is a matching and each pair in S is a Watson–Crick
complement: A–U, U–A, C–G, or G–C
・[No sharp turns] The ends of each pair are separated by at least 4 intervening
bases. If (bi, bj) ∈ S, then i < j – 4.

G G

C G

A U G G G G C A U
A U

S is not a secondary structure



U A (≤4 intervening bases between G and C)

B = AUGGGGCAU
S = { (b1, b9), (b2, b8), (b3, b7) } 44
.

RNA secondary structure

Secondary structure. A set of pairs S = { (bi, bj) } that satisfy:


・[Watson–Crick] S is a matching and each pair in S is a Watson–Crick
complement: A–U, U–A, C–G, or G–C
・[No sharp turns] The ends of each pair are separated by at least 4 intervening
bases. If (bi, bj) ∈ S, then i < j – 4
・[Non-crossing] If (bi, bj) and (bk, bℓ) are two pairs in S, then we cannot have i <
k < j < ℓ.

G G

C U

C U

A G
A G U U G G C C A U

U A S is not a secondary structur


(G-C and U-A cross)
B = ACUUGGCCAU
S = { (b1, b10), (b2, b8), (b3, b9) } 45
e

RNA secondary structure

Secondary structure. A set of pairs S = { (bi, bj) } that satisfy:


・[Watson–Crick] S is a matching and each pair in S is a Watson–Crick
complement: A–U, U–A, C–G, or G–C
・[No sharp turns] The ends of each pair are separated by at least 4 intervening
bases. If (bi, bj) ∈ S, then i < j – 4
・[Non-crossing] If (bi, bj) and (bk, bℓ) are two pairs in S, then we cannot have i <
k < j < ℓ.

G G

C U

C G

A U
A U G U G G C C A U

S is a secondary structur
U A
(with 3 base pairs)
B = AUGUGGCCAU
S = { (b1, b10), (b2, b9), (b3, b8) } 46
e

RNA secondary structure

Secondary structure. A set of pairs S = { (bi, bj) } that satisfy:


・[Watson–Crick] S is a matching and each pair in S is a Watson–Crick
complement: A–U, U–A, C–G, or G–C
・[No sharp turns] The ends of each pair are separated by at least 4 intervening
bases. If (bi, bj) ∈ S, then i < j – 4
・[Non-crossing] If (bi, bj) and (bk, bℓ) are two pairs in S, then we cannot have i <
k<j<ℓ


Free-energy hypothesis. RNA molecule will form the secondary structure with the
minimum total free energy


 approximate by number of base pair
(more base pairs ⇒ lower free energy)

Goal. Given an RNA molecule B = b1b2…bn, nd a secondary structure S
that maximizes the number of base pairs.

47
.

fi

RNA secondary structure: Subproblems

First attempt. OPT( j) = maximum number of base pairs in a secondary structure of


the substring b1b2 … bj.

Goal. OPT(n)

match bases bt and bn
Choice. Match bases bt and bj.



 1 t j last base



Dif culty. Results in two subproblems (but one of wrong form)
・Find secondary structure in b1b2 … bt–1 OPT(t–1)

・Find secondary structure in bt+1bt+2 … bj–1. need more subproblem


( rst base no longer b1)

48
fi
fi
.

Dynamic programming over intervals

Def. OPT(i, j) = maximum number of base pairs in a secondary structure



of the substring bi bi+1 … bj.

Case 1. If i ≥ j – 4.
・OPT(i, j) = 0 by no-sharp-turns condition

Case 2. Base bj is not involved in a pair
・OPT(i, j) = OPT(i, j – 1)

Case 3. Base bj pairs with bt for some i ≤ t < j – 4.
・Non-crossing condition decouples resulting two subproblems
・OPT(i, j) = 1 + max t { OPT(i, t – 1) + OPT(t + 1, j – 1) }.
match bases bj and bt
take max over t such that i ≤ t < j – 4 and

bt and bj are Watson–Crick complements

i t j
49
.

Bottom-up dynamic programming over intervals

Q. In which order to solve the subproblems


A. Do shortest intervals rst—increasing order of ⎜j − i⎟.




 RNA-SECONDARY-STRUCTURE(n, b1, …, bn ) j
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

6 7 8 9 10

FOR k = 5 TO n – 1 4 0 0 0

 all needed value
FOR i = 1 TO n – k are already computed 3 0 0

 i
j ← i+ k 2 0


 Compute M[i, j] using formula 1


 RETURN M[1, n] order in which to solve subproblems


__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________




Theorem. The DP algorithm solves the RNA secondary structure problem in O(n3)
time and O(n2) space.

50
.

fi
.

Bottom-up dynamic programming over intervals

RNA-SECONDARY-STRUCTURE(n, b1, …, bn )
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Case 1. If i ≥ j – 4.
FOR k = 5 TO n – 1 ・ OPT(i, j) = 0 by no-sharp-turns condition
6.5 RNA Secondary Structure: Dynamic P
FOR i = 1 TO n – k Case 2. Base bj is not involved in a pair
j ← i+ k ・ OPT(i, j) = OPT(i, j – 1) RNA sequence ACCGGUAGU

Compute M[i, j] using formula Case 3. Base bj4pairs


0 0with
0 bt for some i 4≤ t0 < 0j –
0 4.0

RETURN M[1, n] ・ OPT(i, 3 0 0 3 0 0 1


j) = 1 + max t { OPT(i, t – 1) + OPT(t + 1, j – 1) }
6.5 RNA Secondary Structure: Dynamic2 Programming
0 over
2 0Intervals
0
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

i=1 i=1 1
6.5 RNA Secondary Structure: Dynamic Programming over Intervals j = 6 7 8 9 277
j=6 7 8 9
Initial values Filling in the values
for k = 5
RNA sequence ACCGGUAGU
RNA sequence ACCGGUAGU

4 0 0 0
4 0 0
4 00 0 0 4 0 0 0 0
4 04 00 0
00 00 4 00 0 0 4 00 0 0 0
3 0 0 3 0 0 1 3 0 0 1 1 3 0 0 1 1 3 0 0 1 1
2 0 3 0 0 2 0 0 3 02 00 0 11 2 0 0 13 1 0 0 1 2 10 0 1 1
i=1 i=1 1 i=1 1 1 1 i=1 1 1 1 2
2 0 2 i =01 10 1
2 0 0 1
j=6 7 8 9 j=6 7 8 9 j=6 7 8 9 j=6 7 8 9 j=6 7 8 9
= 1 values
i Initial i=
Filling in the values 1 1Filling in the values i=
Filling in the 1
1 values 1 Filling in the values
for k = 5 for k = 6 for k = 7 for k = 8
j=6 7 8 9 j=6 7 8 9 j=6 7 8 9
Figure 6.16 The iterations of the algorithm on a sample
51 inst
Initial values Filling in the values Filling in the values
.

DYNAMIC PROGRAMMING

‣ Fibonacci number
‣ weighted interval schedulin
‣ segmented least square
‣ knapsack proble
‣ RNA secondary structur
‣ sequence alignment
m

String similarity

Q. How similar are two strings



Ex. ocurrance and occurrence.

o c u r r a n c e – o c – u r r a n c e

o c c u r r e n c e o c c u r r e n c e

6 mismatches, 1 gap 1 mismatch, 1 gap

o c – u r r – a n c e

o c c u r r e – n c e

0 mismatches, 3 gaps

53
?

Edit distance

Edit distance. [Levenshtein 1966, Needleman–Wunsch 1970]


・Gap penalty δ; match penalty αpq
・Cost = sum of gap and match penalties



C T – G A C C T A C G


 C T G G A C G A A C G


 cost = δ + αCG + αTA



 assuming αAA = αCC = αGG = αTT = 0


Applications. Bioinformatics, spell correction, machine translation,

speech recognition, information extraction, ...

Spokesperson confirms senior government adviser was found 



Spokesperson said the senior adviser was found

54
.

BLOSUM matrix for proteins

55
Sequence alignment

Goal. Given two strings x1 x2 ... xm and y1 y2 ... yn , nd a min-cost alignment



Def. An alignment M is a set of ordered pairs xi – yj such that each character
appears in at most one pair and no crossings

 xi – yj and xiʹ – yj′ cross if i < i ′, but j > j ʹ
Def. The cost of an alignment M is:
cost(M ) = ∑ α xi y j + ∑ δ+ ∑ δ
(x , y j ) ∈ M i : x unmatched j : y unmatched
!i## "## $ !#i### #"#j#### $
mismatch gap

€ x1 x2 x3 x4 x5 x6

C T A C C – G

– T A C A T G

y1 y2 y3 y4 y5 y6

an alignment of CTACCG and TACATG


M = { x2–y1, x3–y2, x4–y3, x5–y4, x6–y6 }
56
.

fi
.

Sequence alignment: Problem structure

Def. OPT(i, j) = min cost of aligning pre x strings x1 x2 ... xi and y1 y2 ... yj
Goal. OPT(m, n)

Case 1. OPT(i, j) matches xi – yj
Pay match for xi – yj + min cost of aligning x1 x2 ... xi–1 and y1 y2 ... yj–1.

Case 2a. OPT(i, j) leaves xi unmatched
Pay gap for xi + min cost of aligning x1 x2 ... xi–1 and y1 y2 ... yj.

optimal substructure propert
Case 2b. OPT(i, j) leaves yj unmatched (proof via exchange argument)
Pay gap for yj + min cost of aligning x1 x2 ... xi and y1 y2 ... yj–1

j i=0
Bellman equation.
i j=0
OP T (i, j) = x i yj + OP T (i 1, j 1)
min + OP T (i 1, j)
+ OP T (i, j 1)
<latexit sha1_base64="60wUnMrLdnu23FwG/Nh5FwbDvxY=">AAADS3ichVJdaxNBFJ3dWq3xq62PvgwGpUUbdsViIRQKvvhmhKYtZEKYnb1JJp2dXWbu1oQlz/4aX/VX+AP8Hb6JD85uFslHwQsDl3PPPefOnYkyJS0GwU/P37qzfffezv3Gg4ePHj/Z3du/sGluBHRFqlJzFXELSmrookQFV5kBnkQKLqPr92X98gaMlak+x1kG/YSPtBxKwdFBgz2PfuycH8jXdHJIWfuUtRssgpHUhXCidt5gbTqhLAaFnL6kDGGKhRzOHZdKekoDylivdQxJv1EhtzMnG0yWSL3hxLjKxnxQTAdyNpiUna8ctZrvKHQTHoWHyyK11zrrP5yFToOBjmvnf9OmOAbzWVqYL5cHu82gFVRBN5OwTpqkjo7b6T6LU5EnoFEobm0vDDLsF9ygFKoUzy1kXFzzEfRcqnkCtl9UbzmnLxwS02Fq3NFIK3S5o+CJtbMkcsyE49iu10rwtlovx+FJv5A6yxG0WBgNc0UxpeXHoLE0IFDNXMKFkW5WKsbccIHu+6y4VNoZiJWbFNNcS5HGsIYqnKLh5RbD9Z1tJhdvWmHQCj+9bZ6d1PvcIc/Ic3JAQvKOnJEPpEO6RHhfvK/eN++7/8P/5f/2/yyovlf3PCUrsbX9Fz1PAsg=</latexit>

57
.

fi
.

Sequence alignment: Bottom-up algorithm

SEQUENCE-ALIGNMENT(m, n, x1, …, xm, y1, …, yn, δ, α)


________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

FOR i = 0 TO m
M [i, 0] ← i δ
FOR j = 0 TO n
M [0, j] ← j δ.

FOR i = 1 TO m
FOR j = 1 TO n
M [i, j] ← min { αxi yj + M [i – 1, j – 1]
δ + M [i – 1, j], alread
δ + M [i, j – 1] } computed

RETURN M [m, n]
________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

58

y


.

FOR i = 0 TO m
Sequence alignment: Traceback M [i, 0] ← i δ
FOR j = 0 TO n
M [0, j] ← j δ
FOR i = 1 TO m
FOR j = 1 TO n
gap penalty = 2, mismatch penalty = 2, match penalty = -1 M [i, j] ← min { αxi yj + M [i – 1, j – 1]
δ + M [i – 1, j],
δ + M [i, j – 1]

— S I M I L A R I T Y

— 0 2 4 6 8 10 12 14 16 18 20

I 2 2 1 3 5 7 9 11 13 15 17

D 4 4 3 3 5 7 9 11 13 15 17

E 6 6 5 5 5 7 9 11 13 15 17

N 8 8 7 7 7 7 9 11 13 15 17

T 10 10 9 9 9 9 9 11 13 12 14

I 12 12 9 11 8 10 11 11 10 12 14

T 14 14 11 11 10 10 12 13 12 9 11

Y 16 16 13 13 12 12 12 14 14 11 8
59

.




,

Sequence alignment: Analysis

Theorem. The DP algorithm computes the edit distance (and an optimal alignment)
of two strings of lengths m and n in Θ(mn) time and space
Pf
・Algorithm computes edit distance
・Can trace back to extract optimal alignment itself. ▪



Theorem. [Backurs–Indyk 2015] If can compute edit distance of two strings

of length n in O(n2−ε) time for some constant ε > 0, then can solve SAT

with n variables and m clauses in poly(m) 2(1−δ) n time for some constant δ > 0.

which would disprove SET


Edit Distance Cannot Be Computed (strong exponential time hypothesis)
in Strongly Subquadratic Time
(unless SETH is false)∗

Arturs Backurs† Piotr Indyk‡


MIT MIT

60
Abstract
.

Dynamic programming summary

typically, only a polynomia


Outline number of subproblems

・De ne a collection of subproblems


・Solution to original problem can be computed from subproblems
・Natural ordering of subproblems from “smallest” to “largest” that enables
determining a solution to a subproblem from solutions to smaller subproblems

Techniques
・Binary choice: weighted interval scheduling
・Multiway choice: segmented least squares
・Adding a new variable: knapsack problem
・Intervals: RNA secondary structure


Top-down vs. bottom-up dynamic programming. Opinions differ.

61
fi
.

You might also like