Classical problem of dynamic programming

Source: Internet
Author: User
Tags comparison table pack

classical problem of dynamic programming

Directory
One, longest common child sequence O (MN)
Second, the optimal sort of binary tree O (n3)
Third, longest ascending subsequence O (nlogn)
Iv. Optimal Triangular split O (N3)
V. Maximum M Sub and O (MN)
Six, 0-1 knapsack problem O (min{nc, 2n, n1.44n})
Vii. optimal sort binary tree O (n2)
Viii. Optimal merger question O (NLOGN)

one, longest common child sequence

longest Common subsequence (LCS)


Consider prefixes x[1..i] and Y[1..J], define C[I,J] = | LCS (x[1..i], Y[1..J]) |, then c[m,n] = | LCS (x, y) |. The recursive formula is:


Very intuitive, consider the case of X[i]=y[j]:


key point one: Optimal substructure

in order to use dynamic programming, the problem needs to have the optimal substructure (Optimal substructure)


Direct Writing procedure:


Recursive Tree Analysis:


Key point two: overlapping sub-problems

in order for dynamic planning to work, the problem should include as many overlapping sub issues as possible (overlapping subproblems)


Solution: Memory, attention memoization not memorization


Push from bottom up


Space optimization

If you only need an optimal value, you can use a scrolling array to implement
• Calculated in ascending order of I, D[i,j] only has a relationship with D[I-1,J] and d[i,j-1] and d[i-1,j-1, so only two adjacent rows need to be preserved, and the space complexity is O (Min{m,n})
• Further, you can keep only one row, each time using a separate variable x to preserve d[i-1,j], then the recurrence equation is
If (i==j) d[j]=x;
else {x = d[j]; d[j]=max{d[j-1], D[j]}};

Metamorphosis-Palindrome words
• Give a string A, keep the order of the original characters unchanged, at least add a few characters to become a palindrome word?
• Example: ABFCBFA->AFBCFCBFA

Analysis
* Red, green to represent the original character, White for new characters
• Obviously, s and S ' can not be white in any position (no need to add that character!)
• Should make red characters as much as possible! The LCS, which is equal to the s and the reverse sequence s ', allows the corresponding character (red) in the LCS to be aligned, with each green character in the middle adding a character and equal to it


Two , the best sort of binary tree
• Give n key codes and their frequencies, and construct a sorted binary tree that minimizes the expected number of comparisons

Analysis
• Theorem: The optimal sort of binary tree is also the optimal sort of binary tree
• Give the key code-frequency comparison table (ascending order)
• Question: Which key code is the root. Then the left and right subtree can be recursively down.

Analysis
• Theorem: The optimal sort of binary tree is also the optimal sort of binary tree
• Give the key code-frequency comparison table (ascending order)
• Question: Which key code is the root. Then the left and right subtree can be recursively down.

To think with recursion, but to do with recursion
• Consider the case of two nodes first.



You can use matrices to save results
C[j,k] Represents the optimal sort binary tree consisting of key codes from J to K
Root[j,k] Record the root of this sorted binary tree


Consider the case of three nodes.
• The optimal value is placed in the c[b,d] and the root is placed in the root[b,d]



Similarly, update all C[J-2,J] and root[j-2,j]


The case of four nodes (e.g. a-d)


The final calculation results are



We can construct the optimal tree recursively by using the root matrix.



Time complexity: Calculate each c[i,j] and root[i,j] need to enumerate root nodes, so O (N3)
Space complexity: Requires two n*n matrices, O (n2)

three, the longest ascending subsequence
The longest ascending subsequence problem (LIS) gives a sequence, asks it to increment the subsequence, makes its element number as much as possible. For example, the longest ascending sequence of the sequence 1,6,2,5,4,7 is 1,2,5,7 (there are other, omitted here).

Define D[i] is the oldest sequence length from the 1th element to the first element, then the state transition equation is:



The direct use of this equation is the O (N2) algorithm
Next optimize it to O (NLOGN)

Organization of the State

A value with the same D value only needs to be kept to the minimum, so an array g[i] represents a minimum value of the number of the D value I, which is obviously
G[1]<=G[2]<=...<=G[K]
Compute D[i]: The first number of J that needs to be found in G is greater than or equal to A[i], then the D[I]=J
Update g: Due to g[j]>a[i], need to update g[j]=a[i]

Using STL Lower_bound can directly find the first number of larger than a[i], with a binary lookup to achieve, each transfer time O (LOGN), Total time O (Nlogn)

Fill (g,g+n,infinity);
for (inti=0;i<n;i++)

{
Intj=lower_bound (G,g+n,a[i])-G;
d[i]=j+1;
G[j]=a[i];

}

Variant 1: Route problems

There are two line points, n for each row. The first and second line points are one by one corresponding, wired connections, as shown in the following figure
Select as many lines as possible, 22 do not cross



Set to the 1th line I point corresponds to the 2nd line of the f[i] point
Assuming i<j, the necessary and sufficient conditions for the two lines (I, f[i]) and (J, F[j]) are f[i]<f[j, so the problem becomes the longest ascending subsequence of F.
Time Complexity of O (Nlogn).

Variant 2: LCS with two permutations
Give the 1~n two permutations P1, p2
Finding the longest common subsequence of P1 and P2
Example: 1 5 4-> 5 3 42 1

Analysis
• Algorithm one: Directly apply the LCS algorithm, Time O (n2)
• Algorithm two: Note that the two permutations to do the same permutation, the LCS unchanged, you can first arrange the P1 as 1,2,3...,n
1 5324->1 2345
• Mapping 5->2, 2->4, 4->5, p2 for the same permutation
53421->23541
• The LCS with 1,2,3..N is clearly the longest ascending subsequence, with the time descending to O (Nlogn)

Promotion: The shortest circuit on the DAG
• "Ascent" relies on the order relationship <=, which has a general
The longest path problem of Dag (a direction-free graph): to consider the biased relationship as a partial order, the algorithm of the subject is still applicable and the time complexity is O (N2). If the graph's number of edges m is compared, it can be further optimized to O (M), because each edge is just considered once (with an adjacency table or a forward star, rather than an adjacency matrix)
• Algorithm II no longer applies! Because a D value is the same, a may not be able to compare 22 to each other, there is no minimum value.

Iv. Optimal Triangulation

To a convex polygon of n vertices, there are many methods for triangulation of it (polygon triangulation)
Each triangle has a weight calculation formula (such as perimeter, vertex right and), the minimum total weight (large) of the triangular split scheme



Analysis
• Use D[i,j] to represent the minimum cost of a polygon consisting of vertices I, i+1, ..., j (note I can be greater than J)
-Scenario One: Enumerate three vertices, form a triangle, the decision is O (N3)
-Scheme two: side (i,j) must belong to a unique triangle, set the third vertex to K, then the decision is only O (n)
• Adopt program two, State O (N2), decision O (n), Total O (N3)

After determining K, the optimal solution should be d[i,k]+d[k,j]+w (I,K,J)
• So the transfer equation D[i,j]=max{d[i,k]+d[k,j]+w (i,k,j)}



deformation 1: optimal matrix multiplication Chain
• Need to compute the product A1A2 of n matrices ... An
Because the matrix multiplication satisfies the binding law, can have many kinds of calculation methods. For example A1 is 10*100, A2 is 100*5, A3 is 5*50, then
– Order 1: (A1A2) A3, at the cost of 10*100*5+10*5*50=7500
– Order 2:a1 (A2A3), at the cost of 100*5*10+10*100*50=75000
• Minimum cost solution (parenthesis method)

Common structure
• Use of binary trees (binary tree) can represent the same structure of two problems, each node represents an interval (node interval/matrix interval), Saozi the right subtree to represent divided into two sequences
If we let d[i,j] represent the optimal value of i-1~j in the original problem, it is also completely equivalent in the form of the equation



Variant 2. Duel
1~n n individuals in the counterclockwise direction in a circle, they want to duel n-1 field. Each game in a neighboring two rooms, the loser out of the circle, close to the loser on the right side of the winner is directly adjacent to the person.
• The duel between any two will be given in one matrix (if A[i,j]=1 I and J Duel I always win, if a[i,j]=0 I always lose when I duel with J),
• Find out the serial number of all those who may win the whole duel


v. Max. M sub-segment and
• Give a sequence of A1, A2, ..., an
• Find m a continuous sequence of disjoint (can be connected), the sum as large as possible
• For example m=2, 1 2-3 4 5-6 7
Analysis
• Set D[I,J] to the maximum of I and of the end of J, you need to enumerate the beginning Y of this segment and the end of the previous paragraph x, i.e.
D[I,J]=MAX{D[I-1,X] + a[y ... J]}
• Each need to enumerate x<y<=j, the decision amount is O (N2), State is O (nm), Total O (n3m)
• Note that if a[j-1] is also this paragraph, the answer becomes D[I,J-1]+A[J], so the equation is optimized to
D[I,J]=MAX{D[I,J-1]+A[J], D[i-1,x]+a[j]}, X<j
• After optimization the state is still two-dimensional, but the decision is reduced to O (n), total O (n2m)
• Can continue to optimize. Note that time is largely spent on an enumeration of x, computed Max{d[i-1,x]}. This value ...
We refer to the first dimension of D as the "stage", while the subject is a typical multistage decision problem.
-When calculating a phase, by the way, record the stage maximum
– Only two contiguous phases are preserved (scrolling array)
• Time reduced to O (nm), Space to O (n)

Six, 0-1 knapsack problem
• Given n items and a backpack, the weight of the goods I is WI, the value is VI, the backpack capacity for C
• For each item, either pack a backpack or do not install
• Select the collection of items to pack, so that the total weight of goods does not exceed the size of the backpack C, and the value and as much as possible

Set D[I,J] for the backpack capacity of J, only consider the first item of the maximum value and
– If I have the first item, the backpack is only left J-wi
-If not installed, the backpack capacity is unchanged
• Therefore D[i,j]=max{d[i,j-wi]+vi, d[i-1,j]}
• State has NC, each state decision only two, so the total time complexity of O (NC). With a scrolling array, the space complexity is only O (c)
• When C is large, the algorithm is very inefficient. In fact, because C is a numeric range parameter, it is generally not considered an input scale. Such an O (NC) is just a pseudo polynomial algorithm
• In fact, if the weight of the item and the backpack capacity are real numbers, the algorithm will fail because it looks like the weight of the item and can be "any real number".
• But the fact is: the weight of the item and only 2n possible values, not infinitely many
• Algorithm one: Enumerate 2n child sets, recalculate, enumerate 2n, compute N, Total n2n
• Algorithm two: Using recursive enumeration, a total of 2n
• Algorithm three: First consider half the elements, save 2N/2 and. Consider the second half of the element, each calculated with a and W, to find the maximum value of the value in the element of the <=c-w weight.
Consider implementing the details below
• The first half of the elements of the 2N/2 and by weight from small to large sort placed in table A. For any two and I, J, if Wi<wj and VI&GT;VJ, then J is not required to save, so by weight after the good is also sorted by value
• When considering the latter half of the element, each gets a weight of W, with a binary lookup to get the maximum weight of no more than c-w, then it is the most valuable.
• pretreatment time complexity 2N/2LOG2N/2, each weight w two-point search time for LOG2N/2, so Total 2n/2log2n/2=o (n1.44n)

on the optimal order binary tree
• Give n key codes and their frequencies, and construct a sorted binary tree that minimizes the expected number of comparisons
Basic analysis
• Set Node I.. The optimal cost of J is d[i,j], then
Where the W[I,J]=FI+FI+1+...+FJ, (if the cost of the root node is considered to be 0, W[i,j] to subtract the FK). Direct calculation is the optimal decision of O (N3) • Set D[i,j] for K[i,j, which is shown below k[i,j-1]<=k[i,j]<= k[i+1,j] to reduce the complexity of time to O (N2)
Quadrilateral Inequalities
• Convexity (Mongecondition/quadrangle inequality)
W[i,j]+w[i ' J ']<=w[i ', J]+w[i,j '], i<=i ' <j<=j '
• Monotonicity (interval containing lattice)
W (i ', j) <=w (I,j '), i<=i ' <j<=j '


Validating quadrilateral Inequalities
• Just verify
W[I,J]+W[I+1,J+1]&LT;=W[I+1,J]+W[I,J+1]
• Move Items to
W[I+1,J+1]-W[I+1,J]&LT;=W[I,J+1]-W[I,J]
• When J fixed-time function f (x) = W[x,j+1]-w[x,j], the upper is changed to: F (i+1) <=f (i), so
F (i) is a subtraction function, w is convex; F (i) is an increase function, W is concave
• Fixed I have the same conclusion (minus function is convex)

The W in the subject
• In this subject, W's convexity is better proved:
W[I,J]+W[I+1,J+1]
=w[i,j]+ (W[i,j]+f[j+1]-f[i])
=W[I+1,J]+W[I,J+1]
• The two sides are exactly equal. or calculate
F (x) =w[x,j+1]-w[x,j]=f[i+1]= constant
Constant is both an increase function and a subtraction function, so
In this subject, W is both convex and concave

theorem
• Consider recursive d[i,j]=min{d[i,k-1]+d[k,j]+w[i,j]}
• Theorem (F.yao): If the W satisfies the quadrilateral inequality, then D also satisfies the quadrilateral inequality, i.e.
D[i,j]+d[i ', J ']<=d[i ', J]+d[i,j '], i<=i ' <=j<=j '
• Proof: For length i=j '-I inductive, obviously i<=1 correct. I=i ' or j=j ' (same row or column), the equation is clearly established
-Case 1:i ' =j, degenerate to inverse triangular inequality
-Case 2:i ' <j

Scenario 1. Anti-triangular inequalities
I. ' =j, D[i,j]+d[i ', J ']<=d[i ', J]+d[i,j '] Retreat
D[i,j]+d[j,j ']<=d[i,j ']
• Set K to let D[i,j '] take the minimum value of the decision (there are multiple times take the largest k, after the same).
• If k<=j, then K is calculated d[i,j] considered legitimate decision
D[I,J]&LT;=W[I,J]+D[I,K-1]+D[K,J]
• Both sides plus d[j,j '],
D[i,j]+d[j,j ']<=w[i,j]+d[i,k-1]+d[k,j]+d[j,j ']
Scenario 2. Non-degenerate situations
When I. ' <j, the right side retains two d[i ', J ' and D[i,j ']. When the minimum value is set, the decision is Y and Z, which still need to be divided into two z<=y and z>y (symmetric). The following is only considered when z<=y
Y and Z are legitimate decisions, so y<=j, Z>i, and
D[I,J]&LT;=W[I,J]+D[I,Z-1]+D[Z,J]
D[i ', J ']<=w[i ', J ']+d[i ', Y-1]+d[y,j ']
• Two-type addition and collation, the corresponding items are written together, have two-type addition and collation, the corresponding items written together, right <=
W[i,j]+w[i ', J ']+d[i,z-1]+d[i ', Y-1]+d[z,j]+d[y,j ']
• Due to z<=y, red-blue with quadrilateral inequalities, right <=
W[i,j ']+w[i ', j]+d[i,z-1]+d[i ', Y-1]+d[z,j ']+d[y,j]
• According to the red and blue separate combinations,
D[i,j]+d[i ', J ']<=d[i,j ']+d[i ', j]
Z<=y When a proposition is to be verified. Z>y is similar
The monotony of decision making
• Further, the convexity of D can introduce the monotonicity of decision making
• Set K[I,J] to make the minimum value of the decision to D[I,J, the following proof
K[i,j]<=k[i,j+1]<=k[i+1,j+1], I<=j
* namely: K in the same row of peers are incremental
• Proof: I=j is clearly established. By symmetry, simply prove k[i,j]<=k[i,j+1]. Remember Dk[i,j]=d[i,k-1]+d[k,j]+w[i,j], then only need to prove for all i<k<=k ' <=j, there
DK ' [i,j]<=dk[i,j] In fact, we can prove a stronger formula DK[I,J]-DK ' [I,J]&LT;=DK[I,J+1]-DK ' [i,j+1] (i<k<=k ' <=j) K ' in [i,j] more excellent (left >=0)->k ' on [i,j+1] is also better (right >=0) • Set K ' is the optimal value of [i,j], for any k on the left, K ' may be better in [i,j] than K ' on [i,j+1], so on the interval [i,j+1], K ' to the left of the value is larger than it, the following figure

To permit dk[i,j]-dk ' [i,j]<=dk[i,j+1]-dk ' [i,j+1], move the item
DK[I,J]+DK ' [i,j+1]<=dk[i,j+1]+dk ' [I,j]
• Expand by definition, both sides eliminate w[i,j]+w[i,j+1], get
D[i,k-1]+d[k,j]+d[i,k ' -1]+d[k ', j+1]<=
D[i,k-1]+d[k,j+1]+d[i,k ' -1]+d[k ', j]
• Eliminate the red part at the same time
D[k,j]+d[k ', J+1]<=d[k,j+1]+d[k ', j]
* This is the convexity of K<=k ' <=j<j+1 D

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.