Dynamic Planning details

Source: Internet
Author: User

1

3 2, 2, 2

4 10 1 3, 1 3, 2 3

4 3 2 20, 4, 4, 4

(A) digital triangle (B) Status Number

 

Starting from the number of the first row, each time you can go down or to the right, you can go down to the bottom, and add all the numbers along the way. How can this sum be maximized?

If you are familiar with the Backtracking Method, you will find that this is a dynamic decision-making problem. Each time there are two options: Bottom left or bottom right. if we use the Backtracking Method to find all possible routes, we can select the optimal route from them. However, there are n-power lines of Route 2, which are intolerable in efficiency.

Abstract methods can be used to think about the problem. The current position is regarded as a State and expressed by the (I, j) position. then define the indicator function of the state (I, j). d (I, j) is the maximum value that can be obtained from the lattice (I, j ).

Let's see how different states are transferred. If we go to the left, we will go to (I + 1, J). If we go to the right, we will go (I + 1, J + 1, select a larger one.

State transition equation d (I, j) = a (I, j) + max {d (I + 1, J), D (I + 1, J + 1 )}

If the sum of the obtained parts starting from (I + 1, J) is the largest, the addition of a (I, j) is also the largest. This property is called the optimal sub-structure.

The core of dynamic planning is the state and state transition equation.

Computing

1. Recursive Calculation

int d(int i,int j){     return a[i][j]+(i==n? 0:d(i+1,j)>d(i+1,j+1));}

The efficiency is too low. Calling the link tree during recursion causes repeated computation.

2. Recursive Calculation

int i,j;for(j=1;j<=n;j++)     d[n][j]=a[n][j];for(i=n-1;i>=1;i—)   for(j=1;j<=i;j++)      d[i][j]=a[i][j]+d[i+1][j]>?d[i+1][j+1];

The recurrence time complexity is the total number of States X number of decisions for each State X Decision time

3. Memory-based search

int d(int i,int j){      if(d[i][j]>=0)        return d[i][j];      return d[i][j]=a[i][j]+(i==n?0:d(i+1,j)>?d(i+1,j+1));}


Save the calculation result in D [I] [J]. Whether or not the record has been computed

You can also use another array vis [] to save whether it has been computed.

0-1 backpack Problems

N items, each of which has an infinite number of items. The I-th type is VI, the weight is wi, and some items are selected in the C-type backpack, make the weight of the objects in the backpack as large as possible when the total volume does not exceed C

The method just now is no longer applicable. The original state transfer is too chaotic. To eliminate this confusion, we need to make decisions more orderly. This is a multi-segment decision-making problem.

Each time you make a decision, you can get a part of the solution. the final solution is obtained after all the decisions are made. in the Backtracking Method, each decision corresponds to a subtree, and each answer book corresponds to a solution. the number of node layers is the next cur to be filled, which is the sequence number of the decision to be completed.

Multi-segment decision-making can be solved by dynamic planning. d (I, j) indicates the I-layer, and the remaining capacity of the backpack is the maximum weight and size of J.

D (I, j) = max {d (I + 1, J), D (I + 1, J-V [I]) + W [I]}

When the boundary is I> N, d (I, j) = 0, and j <0 is negative infinity.

In other words, d (I, j) indicates that I, I + 1, I + 2, and N items are loaded into the maximum weight and number of items in the backpack with a capacity of J, in fact, this term is commonly used in terms such as stages and layers,

Code (Result: d [1] [c])

for(int i=n;i>=1;i—)    for(int j=0;j<=c;j++)    {           d[i][j]=(i==n? 0:d[i+1][j]);           if(j>=v[i])               d[i][j]>?=d[i+1][j-v[i]]+w[i];    }

I must be enumerated in reverse order, but the cyclic order of J is irrelevant.

There is also a symmetric state definition that uses f (I, j) to indicate the maximum weight and

State transition equation: f (I, j) = max {f (I-1, J), F (I-1, J-V [I]) + W [I]}

The final answer is g (N, C)

for(int i=1;i<=n;i++)    for(int j=0;j<=C;j++)    {          f[i][j]=(i==1? 0:f[i-1][j]);         if(j>=v[i])             f[i][j]>?=f[i-1][j-v[i]]+w[i];    }

There are many types of dynamic planning. Today we will introduce the optimal sub-structure and multi-segment decision-making, as well as tree-like dynamic planning, collective dynamic planning, and Dag dynamic planning. We will introduce them one by one in the future.

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.