- Home
- Documents
*1 Dynamic programming 叶德仕 yedeshi@zju.edu.cn. 2 Dynamic Programming History Bellman. Pioneered...*

prev

next

of 72

View

212Category

## Documents

Embed Size (px)

Dynamic programmingyedeshi@zju.edu.cn

Dynamic Programming HistoryBellman. Pioneered the systematic study of dynamic programming in the 1950s.Etymology. Dynamic programming = planning over time.Secretary of defense was hostile to mathematical research. Bellman sought an impressive name to avoid confrontation."it's impossible to use dynamic in a pejorative sense""something not even a Congressman could object to"Reference: Bellman, R. E. Eye of the Hurricane, An Autobiography.

Algorithmic ParadigmsGreedy. Build up a solution incrementally, myopically optimizing some local criterion.Divide-and-conquer. Break up a problem into two sub-problems, solve each sub-problem independently, and combine solution to sub-problems to form solution to original problem.Dynamic programming. Break up a problem into a series of overlapping sub-problems, and build up solutions to larger and larger sub-problems.

Dynamic Programming Applications

Areas.Bioinformatics.Control theory.Information theory.Operations research.Computer science: theory, graphics, AI, systems, ...Some famous dynamic programming algorithms.Viterbi for hidden Markov models. Unix diff for comparing two files. Smith-Waterman for sequence alignment. Bellman-Ford for shortest path routing in networks.Cocke-Kasami-Younger for parsing context free grammars.

Knapsack problem

0 - 1 Knapsack ProblemKnapsack problem.Given n objects and a "knapsack."Item i weighs wi > 0 kilograms and has value vi > 0.Knapsack has capacity of W kilograms.Goal: fill knapsack so as to maximize total value.

ExampleItems 3 and 4 havevalue 40

Greedy: repeatedly add item with maximum ratio vi / wi {5,2,1} achieves only value = 35, not optimal

W = 11

ItemValueWeight111262318542265287

Dynamic Programming: False Start

Def. OPT(i) = max profit subset of items 1, , i.Case 1: OPT does not select item i.OPT selects best of { 1, 2, , i-1 }Case 2: OPT selects item i.accepting item i does not immediately imply that we will have to reject other itemswithout knowing what other items were selected before i, we don't even know if we have enough room for iConclusion. Need more sub-problems!

Adding a New VariableDef. OPT(i, W) = max profit subset of items 1, , i with weight limit W.Case 1: OPT does not select item i.OPT selects best of { 1, 2, , i-1 } using weight limit WCase 2: OPT selects item i. new weight limit = W wiOPT selects best of { 1, 2, , i1 } using this new weight limit

Knapsack Problem: Bottom-UpKnapsack. Fill up an n-by-W array.

Input: n, W, w1,,wn, v1,,vnfor w = 0 to W M[0, w] = 0for i = 1 to n for w = 1 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]

Knapsack AlgorithmW+1n+1OPT = 40

01234567891011empty000000000000{1}011111111111{1,2}016777777777{1,2,3}0167718192425252525{1,2,3,4}0167718222428292940{1,2,3,4,5}0167718222428293440

Knapsack Problem: Running Time

Running time. O(n W).Not polynomial in input size!"Pseudo-polynomial."Decision version of Knapsack is NP-complete. Knapsack approximation algorithm. There exists a polynomial algorithm that produces a feasible solution that has value within 0.0001% of optimum.

- Knapsack problem: another DPLet V be the maximum value of all items, Clearly OPT
Running time: O(n2V), Since v is in {1,2, ..., nV }Still not polynomial time, input is: n, logV

Knapsack summaryIf all items have the same value, this problem can be solved in polynomial time

If v or w is bounded by polynomial of n, it is also P problem

Longest increasing subsequenceInput: a sequence of numbers a[1..n] A subsequence is any subset of these numbers taken in order, of the form

and an increasing subsequence is one in which the numbers are getting strictly larger.

Output: The increasing subsequence of greatest length.

ExampleInput. 5; 2; 8; 6; 3; 6; 9; 7 Output. 2; 3; 6; 9

5 2 8 6 3 6 9 7

directed pathA graph of all permissible transitions: establish a node i for each element ai, and add directed edges (i, j) whenever it is possible for ai and aj to be consecutive elements in an increasing subsequence, that is, whenever i < j and ai < aj

52863697

Longest pathDenote L(i) be the length of the longest path ending at i.for j = 1, 2, ..., n:L(j) = 1 + max {L(i) : (i, j) is an edge} return maxj L(j)O(n2)

How to solve it?Recursive? No thanks!

Notice that the same subproblems getsolved over and over again!

L(5)L(1)L(4)L(2)L(3)L(3)L(2)L(1)

Sequence AlignmentHow similar are two strings?ocurranceoccurrence

oecurranceoccurrenc-6 mismatches,1gapoecurranceoccurrenc-1 mismatch,1gapoecurranceoccurrenc---0 mismatch, 3gaps

Edit DistanceApplications.Basis for Unix diff.Speech recognition.Computational biology.Edit distance. [Levenshtein 1966, Needleman-Wunsch 1970]Gap penalty , mismatch penalty apqCost = sum of gap and mismatch penalties.

Sequence AlignmentGoal: Given two strings X = x1 x2 . . . xm and Y = y1 y2 . . . yn find alignment of minimum cost.Def. An alignment M is a set of ordered pairs xi - yj such that each item occurs in at most one pair and no crossings.Def. The pair xi - yj and xi - yj cross if i < i', but j > j'.

Cost of Mmismatchgap

ExampleEx. X= ocurrance vs. Y= occurrenceoecurranceoccurrenc-x1x2x3x4x5x6x7x8x9y1y2y3y4y5y6y7y8y9y10

Sequence Alignment: Problem Structure

Def. OPT(i, j) = min cost of aligning strings x1 x2 . . . xi and y1 y2 . . . yj .Case 1: OPT matches xi - yj.pay mismatch for xi - yj + min cost of aligning two strings x1 x2 . . . xi-1and y1 y2 . . . yj-1 Case 2a: OPT leaves xi unmatched.pay gap for xi and min cost of aligning x1 x2 . . . xi-1and y1 y2 . . . yj Case 2b: OPT leaves yj unmatched.pay gap for yj and min cost of aligning x1 x2 . . . xi and y1 y2 . . . yj-1

Sequence Alignment:Dynamic programming

Sequence Alignment:AlgorithmSequence-Alignment (m, n, x1 x2 . . . xm , y1 y2 . . . yn ,,) {for i = 0 to m M[0, i] = i for j = 0 to n M[j, 0] = 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], + M[i, j-1])return M[m, n]}

AnalysisRunning time and space. O(mn) time and space.

English words or sentences: m, n

Sequence Alignment in Linear Space

Q. Can we avoid using quadratic space?Easy. Optimal value in O(m + n) space and O(mn) time. Compute OPT (i, ) from OPT (i-1, ). No longer a simple way to recover alignment itself.Theorem. [Hirschberg 1975] Optimal alignment in O(m + n) space and O(mn) time.Clever combination of divide-and-conquer and dynamic programming.Inspired by idea of Savitch from complexity theory.

Space efficientSpace-Efficient-Alignment (X, Y)Array B[0..m,0...1]Initialize B[i,0] = i for each iFor j = 1, ..., n B[0, 1] = j For i = 1, ..., m B[i, 1] = min {[xi, yj] + B[i-1, 0], + B[i-1, 1], + B[i, 0]} End forMove column 1 of B to column 0: Update B[i ,0] = B[i, 1] for each i End for% B[i, 1] holds the value of OPT(i, n) for i=1, ...,m j-1

Sequence Alignment: Linear Space

Edit distance graph. Let f(i, j) be shortest path from (0,0) to (i, j). Observation: f(i, j) = OPT(i, j).

Sequence Alignment: Linear Space

Edit distance graph.Let f(i, j) be shortest path from (0,0) to (i, j).Can compute f (, j) for any j in O(mn) time and O(m + n) space.

Sequence Alignment: Linear Space

Edit distance graph. Let g(i, j) be shortest path from (i, j) to (m, n).Can compute by reversing the edge orientations and inverting the roles of (0, 0) and (m, n)

Sequence Alignment: Linear Space

Edit distance graph. Let g(i, j) be shortest path from (i, j) to (m, n). Can compute g(, j) for any j in O(mn) time and O(m + n) space.

Sequence Alignment: Linear Space

Observation 1. The cost of the shortest path that uses (i, j) is f(i, j) + g(i, j).

Sequence Alignment: Linear SpaceObservation 2. let q be an index that minimizes f(q, n/2) + g(q, n/2). Then, the shortest path from (0, 0) to (m, n) uses (q, n/2).Divide: find index q that minimizes f(q, n/2) + g(q, n/2) using DP. Align xq and yn/2.Conquer: recursively compute optimal alignment in each piece.

- Divide-&-conquer-alignment (X, Y)DCA(X, Y)Let m be the number of symbols in XLet n be the number of symbols in YIf m
Running time Theorem. Let T(m, n) = max running time of algorithm on strings of length at most m and n. T(m, n) = O(mn log n).

Remark. Analysis is not tight because two sub-problems are of size(q, n/2) and (m - q, n/2). In next slide, we save log n factor.

Running timeTheorem. Let T(m, n) = max running time of algorithm on strings of length m and n. T(m, n) = O(mn).Pf.(by induction on n)O(mn) time to compute f( , n/2) and g ( , n/2) and find index q.T(q, n/2) + T(m - q, n/2) time for two recursive calls.Choose constant c so that:

Running timeBase cases: m = 2 or n = 2.Inductive hypothesis: T(m, n) 2cmn.

Longest Common Subsequence (LCS)Given two sequences x[1 . . m] and y[1 . . n], find a longest subsequence common to them both.

Example:x: A B C B D A By: B D C A B ABCBA = LCS(x, y)

a not the

Brute-force LCS algorithm

Check every subsequence of x[1 . . m] to see if it is also a subsequence of y[1 . . n].Analysis Checking = O(n) time per subsequence. 2m subsequences of x (each bit-vec