Introduction to dynamic programming algorithms and comparisons with greedy algorithms

Source: Internet
Author: User
Tags radar

# # Problem Description:

1. What is dynamic programming algorithm

2. Why the dynamic programming algorithm can bring about efficiency improvement

3. Features of dynamic programming algorithms

# # Solution:

Question 1: What is a dynamic programming algorithm

Answer: About more than 60 years ago, dynamic programming algorithm began to appear and large-scale use, dynamic programming algorithm in English name is dynamic programming, originally used to solve the multi-stage decision problem, in fact, this is the most classical dynamic programming algorithm model, here, the definition of dynamic programming algorithm, A dynamic programming algorithm can be applied if a problem can be divided into multiple stages of tasks in time or space or other things, and when these tasks are advanced in some sort of "timing" to satisfy optimal substructure properties and no-effect.

Question 2: Why the dynamic programming algorithm can bring about efficiency improvement

Answer: The dynamic programming algorithm is not magical, the actual problem, often there are many overlapping sub-problems, dynamic programming algorithm does not blindly search, in obtaining a sub-problem, record the current sub-problem state, so that in the need to use this sub-problem, you can directly draw part of the results

Issue 3: Characteristics of dynamic programming algorithms

Answer: Many algorithms are mentioned in the book, the Dynamic programming algorithm has the optimal substructure properties, which is not perfect, if we want to use dynamic programming algorithm to solve the actual problem, first of all, we need to consider whether a large problem can be recursive from a number of sub-problems, and secondly, need to check the decomposition into sub-problem, Is there a large number of overlapping sub-problems, that is, when we want to use the dynamic programming algorithm, first of all, this problem should have a large number of overlapping sub-problems, and then consider a certain "timing" to advance, that is, the identification phase, Phase division is difficult, because we need to meet the optimal sub-structure after the partition, That is, if we take a certain strategy to carry out phase transfer, to obtain the optimal solution, then remove the part of the phase transfer, it can also obtain the optimal solution of this stage, and finally to investigate the no-effect problem, when we carry out the state transfer, the previous decision whether to affect the back state, so that the sub-problem has changed, Dynamic programming algorithms must meet no-no validity, that is, the decision made earlier cannot affect subsequent decisions

# # Objective: To help readers understand the conditions of application of dynamic programming algorithms, understand overlapping sub-problems, optimal sub-problems, no-effect and state transfer concepts with specific context.

# # Context Description: There is a mouse very much like to eat cheese, so it in every burrow has placed some cheese, the burrow is just a n*n matrix, the number of cheese in each hole is f[i][j], there is a cat always staring at the mouse, now known to the mouse can only walk in the matrix or longitudinal walk, each time up to step c, Because once the mouse has eaten the cheese will become heavy, so in order to ensure that the energy run is not caught by the cat, its next time to eat cheese must be more than the last time to eat cheese, the mouse initially in the first row in the first column, ask the mouse to eat up to how much cheese (N <= 100)

Sample input

3 1

1 2 5

10 11 6

12 12 7

Sample output

37

Sample description, 3*3 matrix, the mouse at most one step at a time, the mouse first eat 1, then eat 2, then eat 5, then eat 6, eat 11, then eat 12, the total cheese is 37

# # Solution:

A certain programming foundation, should be able to quickly think of a depth-first search algorithm to solve the above problem, from a point of departure, the state transfer, to meet the next point of the cheese number must be greater than the current point, when the search can not continue to get a set of solutions, traverse all the solution to take the best solution, it is clearly correct, The efficiency is very low, in the 100*100 data scale, the algorithm cannot withstand.

Thinking about our depth-first search process, point I arrives at Point J, Point J will not search for point I anyway, because the next search point of the cheese number must be more than the last time, if we take each step as a stage division, this satisfies the non-aftereffect (the previous decision does not affect the subsequent decision, regardless of (1, 1) How to get to the status (X, y), the search from (x, y) is not affected by (x, Y) Any decision, whether there is a large number of overlapping sub-problems? This is obvious, for the search from (x, y), the optimal solution is certain, so whenever the search (x, y), there is no need for multiple searches, deep search for a large number of sub-problems overlap (deep search every time to try to continue from (x, y) to find the best solution), whether the optimal substructure? This is even more obvious, for the final decision-making, its arbitrary section of decision-making is also the optimal choice, the sample from the (from) the optimal solution is optimal, from one of its state, such as f[1][3]=5, can also be obtained from (1,3) the optimal solution

To sum up, the above problems can be fully used dynamic programming algorithm, the following code to explain the state of the transfer and dynamic planning is very important to use the idea-record the status of the search

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int MAXN = 105;const int dir[4][2] = {{ -1,0},{1,0},{0,-1},{0,1}};int g[maxn][maxn],dp[maxn][maxn],n,k;    DP[I][J] records the optimal solution to be obtained from (i,j) void Dfs (int i,int j) {int res = g[i][j];        for (int x = i-1; x >= 0 && i-x <= k; x--) {if (G[i][j] >= g[x][j]) continue;        if (dp[x][j] = =-1) DFS (X,J);    res = max (Res,g[i][j] + dp[x][j]);        } for (int x = i + 1; x < n && x-i <= k; x + +) {if (G[i][j] >= g[x][j]) continue;        if (dp[x][j] = =-1) DFS (X,J);    res = max (Res,g[i][j] + dp[x][j]);        } for (int y = j-1; y >= 0 && j-y <= k; y--) {if (G[i][j] >= g[i][y]) continue;        if (dp[i][y] = =-1) DFS (i,y);    res = max (Res,g[i][j] + dp[i][y]);        } for (int y = j + 1; y < n && y-j <= K; y++) {if (G[i][j] >= g[i][y]) continue; IfDp[i][y] = =-1) DFS (i,y);    res = max (Res,g[i][j] + dp[i][y]); } Dp[i][j] = res;}        int main () {while (scanf ("%d%d", &n,&k)! = EOF) {if (n = =-1 && k = =-1) break; for (int i = 0; i < n; i++) {for (int j = 0; J < N; j + +) {scanf ("%d", &A mp            G[I][J]);        }} memset (Dp,-1,sizeof (DP));        DFS (0,0);    printf ("%d\n", dp[0][0]); } return 0;}

  

# # Article Goal

After learning the important concept of dynamic planning, of course, it needs to be tested with practice, the following is one of the controversial exercises I completed, on-line dynamic programming algorithm I can not prove the correctness of the algorithm, here I give my argument right greedy algorithm

# # Problem Description 1

A missile interception system has been developed by a country to defend against enemy missile attacks. But there is a flaw in the missile interception system: Although its first artillery shells can reach any height, each shot cannot exceed the height of the previous pitch. One day, the radar caught the enemy's missiles. As the system is still in the trial phase, So there is only one set of systems, so it is possible not to intercept all missiles.

What do we do? How many systems do you have? It's easy to say it. Cost is a big problem. So I came here to call for help, please figure out how many sets of interception systems you need at least.

Input enters several sets of data. Each group of data includes: Total number of missiles (positive integers), the height at which missiles fly (radar-given height data is a positive integer not greater than 30000, separated by a space)

Output corresponds to each set of data outputs to intercept all missiles with a minimum of how many sets of such missile interception systems.

Sample INPUT8 389 207 155 300 299 170 158 65

Sample Output 2

The problem is difficult to determine an appropriate stage, so that the problem to meet the optimal sub-structural properties and no effect, the facts to satisfy the greedy nature, the internet most of the problem of dynamic programming solutions are problematic, here give my greedy code (can use mathematical induction to prove correct)

#include <cstdio>#include<cstring>#include<vector>#include<iostream>#include<algorithm>using namespacestd;Const intINF =0x3f3f3f3f;intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {Vector<int>VEC;  for(inti =0; I < n; i++)        {            intx; scanf ("%d",&x); if(vec.size () = =0) Vec.push_back (x); Else            {                intMinx = Inf,index =-1;  for(intj =0; J < Vec.size (); J + +)                {                    if(Vec[j] >= x && vec[j]-x < minx) index = J,minx = Vec[j]-x; }                if(Index = =-1) Vec.push_back (x); ElseVec[index] =x; }} printf ("%u\n", Vec.size ()); }    return 0;}

Introduction to dynamic programming algorithms and comparisons with greedy algorithms

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.