Summary of dynamic Programming algorithm learning

Source: Internet
Author: User

The difference between dynamic programming and greedy and divided treatment
  • The greedy algorithm (greed Alalgorithm) is a choice that takes the best or optimal (ie most advantageous) position in the current state in each step of the selection, hoping to cause the global result to be the best or most optimal algorithm.
  • The divide-and- Conquer algorithm (Divide and conquer Alalgorithm) is literally interpreted as "divide and conquer", which is to divide a complex problem into two or more identical or similar sub-problems until the last sub-problem can be simply solved directly, The solution of the original problem is the merger of the solution of the sub-problem.
  • Dynamic Programming Algorithm (PROGRAMMING,DP) solves complex problems by decomposing the original problem into relatively simple sub-problems. Often many sub-problems are very similar, for this reason the dynamic programming method attempts to solve only one sub-problem at a time, thus reducing the amount of computation: Once the solution of a given sub-problem has been calculated, it is stored so that the next time the same sub-problem can be solved directly to the table.

The greedy method cannot be rolled back when dealing with each sub-problem, while the dynamic programming can save the previous result, the preferred choice. In order to solve the problem of interval scheduling, the application of dynamic programming in practical problems is analyzed.

Interval Scheduling problems
    • As shown, each bar represents a job, there are always several jobs A, b ... h, the horizontal axis is the time, and the start and end points of the block represent the starting and ending times of the work respectively.

    • When two working hours are not crossed, i.e. two blocks do not overlap, the two jobs are compatible (compatible).

    • When assigning weights to each job is 1 o'clock, it is called unweighted Interval scheduling problem; When assigning different positive weights to each job, it is called Weighted Interval scheduling Problem.

    • The problem is ultimately to find a working subset, with the sum of all the work weights in the collection and the compatibility of each work within the collection .

For the unweighted Interval scheduling problem, the greedy algorithm can be solved by sorting all the work according to the end time , then starting from the end of the work, and then removing the previous incompatible work, The rest of the work is made up of the set that is asked for.

However, for the Weighted Interval scheduling problem, the solution found by the greedy method may not be optimal. In this case, the dynamic programming algorithm is considered to solve the problem, which takes into account the weight selection and compatibility relationship.

Define P (j)

1, the first still according to the end time for all the work to be sorted;

2, the definition of P (j) is before the work J, and the maximum number of J-compatible work, by analyzing the start time and end time of each work, it is easy to calculate P (j);

3, for example, p (8) = 5, because work 7 and 6 are incompatible with 8, work 1 to 5 are compatible with 8, and 5 is the largest of the index, so p (8) = 5. Similarly, p (7) =3,p (2) = 0.

Analyze Recursive relationships

1, the definition of opt (j) is a J work, can choose the best option, that is, opt (j) is the largest weight and;

2. For the work of section J, there are two situations:

    • Case 1: Work J is included in the optimal solution, then forward one step ahead, J can select the best solution is opt (P (j)), that is,

    • Case 2: The work J is not in the optimal solution, then it is the same to select the solution set from J work and choose the solution set from j-1 work, i.e.

3, when j=0, the display result is 0, this is the boundary condition.

The result of the next step takes the maximum of all possible cases in the previous step, so in summary, the recursive relationships that can be dynamically programmed are:

Code implementation

1. Recursive method

Recursion can make the complexity of space become higher, it is generally not recommended.

2. bottom-up method

From small to large calculations, so that each time you can use the previous step calculated value to calculate the value of the next step, the algorithm time complexity of O (Nlogn), where the ordering cost O (NLOGN), the back of the Loop cost O (n).

The definition of knapsack problem in knapsack problem problem
  • As shown, given a backpack knapsack, there are several items item
  • Each item has its own weight weight, corresponding to a value of
  • The total weight of the backpack is limited to W
  • The goal is to fill the backpack, in the case of not overweight, so that the total weight of the items in the backpack.

For example, a common greedy idea is to choose items of higher value when the backpack can be loaded. Then when the backpack capacity is w=11, first select Item5, then select Item2, finally can only drop ITEM1, the total value of 28+6+1=35. In fact, the optimal solution is to choose Item3 and Item4, value 18+22=40. This shows that the greedy algorithm may not be zuiyou to solve the knapsack problem. Consider using the dynamic programming algorithm to solve the problem, first of all to derive a recursive relationship.

Derivation of recursive relational formula

Similar to the weighted Interval scheduling problem, the definition of opt (i, W) represents the maximum value that can be obtained when there is an I item and the remaining capacity of the backpack is W .

Consider item I, there are two cases of choice and no choice:

    • Case 1: If you select Item I, then

    • Case 2: If Item I is not selected, then

Boundary conditions: When i=0, it is obvious that opt (i,w) = 0.

The result of the next step takes the maximum of all possible cases in the previous step, so in summary, the recursive relationships that can be dynamically programmed are:

Bottom-up solution

The iterative process of the algorithm is shown in the following table:

Algorithm Run time analysis

It is worth noting that the algorithm is not a polynomial algorithm relative to the input size, although O (NW) looks much like a polynomial solution, the knapsack problem is actually a NP-complete problem .

For ease of understanding, it can be written in this form:

W is just a number in the computer, with the length of the LOGW space stored, very small. But in the actual operation, as W changes, the NW times need to be computed, which is very large (as opposed to LOGW). For example, when W is 5kg, in kg as the base unit, the O (5n) times need to be calculated, when W is 5t, still in kg, you need to calculate O (5000n) times, and the amount of change in the computer w is relatively small.

Sequence Alignmentdefine Edit Distance

Given two sequences of X1,x2...xi and Y1,y2,..., YJ. To match these two sequences, make the similarity large enough. First you need to define a quantity-edit distance that represents the cost, and only the optimization minimizes this amount, which is equivalent to maximizing the matching of the two sequences.

The definition of Edit distance is as follows.

Where, match to empty, set distance to delta, otherwise the letter P and Q match distance is recorded as alpha (P,Q), if p=q, then alpha=0;

Then the total cost of two sequence matches is:

Setting up a recurrence relationship

Set opt (I,J) is the minimum cost of matching between sequence x1,x2...xi and Y1,y2,..., YJ. When the i,j is not all 0 o'clock, there are three cases, respectively, Xi-gap,yj-gap,xi-yj, respectively, calculate the cost of the different matching situation, plus the result of the previous step, you can establish a recursive relationship, as shown below.

Algorithm implementation

Complexity of the algorithm

Both time and space complexity are O (MN).

The following analysis of a specific programming problem, the use of dynamic programming algorithm, but with the above DP and some differences.

The choir question question definition

There are n students standing in a row, each student has a competency value, the cattle to choose from the N students in order to select K students, the next two students are required to place the difference in the location number is not more than D, so that the K students ability value of the product maximum, you can return the maximum product?

Enter a description

Each input consists of 1 test cases. The first line of each test data contains an integer n (1 <= n <= 50), representing the number of students, the next line, containing n integers, in order to represent each student's ability value ai ( -50 <= ai <=). The next line consists of two integers, K and D (1 <= k <=, 1 <= D <= 50).

Output description

The output line represents the maximum product.

Problem analysis
    • The first key point of this question is "require the location number of the next two students not more than D", if according to the traditional DP idea, the definition of opt (i,k) to select the largest number of K students in the first I students to establish a recursive relationship:

It is not possible to achieve "the difference of position number of the next two students not exceeding D". Therefore, you need to define an auxiliary quantity to contain the positioning information for the current student.

    • The definition F (i,k) indicates that a student of the first I selected K students, and the first I student must choose , the ability value of the selected students product, so that the current student's positioning information, F, the recurrence of the relationship can be expressed as

Among them, J is a smaller than I value, the maximum is i-1,i, J difference not more than d,f (J,k-1) in the former J students, select K-1 students, and the first J students must choose. F (i,k) selected the first student, F (j,k-1) selected the first J students, I, J difference not more than D, so that can meet the requirements of the topic.

    • The auxiliary amount F (i,k) is not the final result we are going to get, the final result of opt (i,k) is to select the maximum number of K students among the first I students, so the relationship between opt (i,k) and F (i,k) can be:

    • The second key point of the problem is that the student's ability value is between 50 and +50, and each student's ability value is positively negative, so it takes two F to record the maximum and minimum values, define Fmax and Fmin, in the process of each iteration f:

    • When K=k,i=n, the final request:

    • When the boundary condition K=1, F (i,k=1) =v (i)
Code implementation
/*********************************************************************** Ran Chen <[email protected]>* * Dynamic Programming algorithm**********************************************************************/#include <iostream> #include <vector> #include <climits> #include <algorithm>using namespace Std;int  Main () {int N, D, K;    A total of n students vector <int> value;            while (CIN >> N) {for (int i = 0; i < N; ++i) {int V;            Cin >> v;        Value.push_back (v);    } break;  } cin >> K;  Choose K students CIN >> D; The ordinal difference of the next selected student//Fmax/fmin[i, K] denotes the maximum/small product vector <vector <long long>> fmax (n+1, vector <) in the case of number I selection    Long long> (k+1));    Vector <vector <long long>> fmin (n+1, Vector <long long> (k+1));        Boundary conditions k=1 for (int i = 1; I <= N; ++i) {fmax[i][1] = value[i-1];    FMIN[I][1] = value[i-1]; }    //Bottom-up DP, k>=1 for (int k = 2; k <= K; ++k) {//I >= K for (int i = k; I <= N; ++i)  {//0 <= J <= i-1 && i-j <= D && J >= k-1 Long long *max_j = new long Long            *max_j = Llong_min; Long Long *min_j = new long long;                *min_j = Llong_max;            F (i, k) = Max_j {f (J, k-1) * Value (i)} int j = max (i-d, Max (k-1, 1)); for (; J <= i-1; ++j) {*max_j = max (*max_j, Max (fmax[j][k-1] * value[i-1], fmin[j][k                                -1] * value[i-1]));                        *min_j = min (*min_j, min (fmax[j][k-1] * value[i-1], fmin[j][k-1] * value[i-1]));            } Fmax[i][k] = *max_j;                        Fmin[i][k] = *min_j;             Delete Max_j;        Delete Min_j;    }}//opt (N, k) = max_i {f (i, k)}, K <= i <= N long long *temp = new long long;    *temp = Fmax[k][k]; For(int i = k+1; I <= N; ++i)    {*temp = max (*temp, fmax[i][k]);    } cout << *temp;    Delete temp;    System ("pause"); return 0;}

Dynamic Programming Algorithm Learning summary

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.