Introduction to Algorithms Summary ~ Dynamic planning

Source: Internet
Author: User

  Explanation: Dynamic Programming (dynamical programming) is similar to partition method, which solves the original problem by the solution of combinatorial sub-problem.

  Advantages: Dynamic Programming score is a good thing to do is to solve each sub-sub-problem only once, save it without recalculation.

  Dynamic planning and Design steps:

      1. Describe the structural characteristics of an optimal solution.
      2. The value of the optimal solution is defined recursively.
      3. Calculates the value of the optimal solution, usually using the bottom-up method. (Maintain some additional information in order to construct step 4)
      4. An optimal solution is constructed by using the computed information.
Steel bar Cutting problem

Description: Given a length of n-inch steel bars and a price list pi (i = 1,2,...,n), the plan for cutting steel bars, resulting in the largest sales yield rn.

Price list:

Length I 1 2 3 4 5 6 7 8 9 10

Price PI 1 5 8 9 10 17 17 20 24 30

  

We can clearly see that

R1 = 1, cut solution 1 = 1 (no cut)

R1 = 5, cut solution 2 = 2 (no cut)

R1 = 8, cut solution 3 = 3 (no cut)

R1 = 10, cutting scheme 4 = 2 + 2

This can be summed up in a formula Rn = Max (Pn, R1 + Rn-1, R2 + Rn-2,..., Rn-1 + R1)

This allows you to write a top-down recursive implementation pseudo-code

Cut-rod (P,n)//p is the price array [1..n],n is the maximum benefit of length n    ifn = =0          //if n = 0 does not yield, 0 is returned        return 0Q=-Infinity//initialize maximum profit to negative infinity     fori =1to n//Circular 1 to n cutting modeQ = max (Q,p[i] + cut-rod (p,n-i))//Maximum return is equal to the maximum value of the current gain or the yield cut into n segments        returnQ//return maximum benefit

  The disadvantage of recursion: you will find a problem, this computation is very slow, in the case of n=40, it is basically counted for several minutes. The reason for this poor efficiency is that this function repeatedly computes the recursive invocation of the same parameter. The same sub-problem is solved repeatedly.

  The essence of Dynamic planning: This is the time for dynamic programming to occur, and the dynamic programming will carefully arrange the solution order, solve each sub-problem only once, and save the results. The solution to this sub-problem is then needed again, just to find the saved result without having to recalculate it. so in the final analysis, dynamic planning is a typical time-space tradeoff by paying extra memory space to save compute time.

Two implementation methods of dynamic programming

The first top-down method with a memo

The second method of bottom-up

  The first is still recursive, but an array is added to store the solution for each sub-problem.

The second is to solve the problem from childhood, in the gradual introduction of large problems, similar to a problem of the natural derivative process.

  

The first method of pseudo-code for steel strip cutting

memoized-cut-ROD (p,n) let r[0.. N] Be aNewArray//creates a new array R     fori =0to n//Initialize RR[i] =-Infinityreturnmemoized-cut-rod-AUX (p,n,r) memoized-cut-rod-AUX (p,n,r)ifR[n] >=0 //if R[n] >= 0 indicates that the cut was counted once and returned directly to the result        returnR[n]ifn = =0 //returns 0 if the number of cut bars is 0Q =0    ElseQ=-Infinity//Initialize         fori =1To N q= Max (q, P[i] + memoized-cut-rod-aux (P, N-i, R))//the best cut price is the maximum value of the current price or cut number of n-iR[n] = q//record the optimal cut value    returnQ

Pseudo-code for the second method

bottom-up-cut-ROD (p,n) let r[0.. N] Be aNewArray//new R Arrayr[0] =0     //Initialize 0 cut back 0     forj =1to n//optimal solution for loops from 1 to n lengthsQ =-Infinity fori =1To J//the optimal solution for the cycle from 1 to J cuttingQ = max (q, p[i] + r[j-i])//Optimal Solution q = The price and length of the current value or length of I is the optimal value of the optimal solution of J-iR[J] = q//record the optimal solution    returnQ

  

Introduction to Algorithms Summary ~ Dynamic planning

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.