An introduction to algorithm for dynamic programming of steel bar cutting problem by bottom-up solution

Source: Internet
Author: User

Formal application of dynamic planning.

Issues that apply to dynamic planning solutions should have the following two elements:

1. Optimal sub-structure (best choice)

2. Sub-problem overlap (The final optimal solution for each partial step is the current optimal sub-solution.) Similar to the idea that the greedy algorithm tries to combine the optimal solution by the local optimal solution)

In the first version of the code below, there is still the same problem as the previous first version of the code-only the maximum given in the P array can be solved. N>=10, the code will not be able to solve the correct answer. (In the code you all know--sell a cute (O (∩_∩) o haha ~)

Mainly use the first version of the code to record ideas.

A total of two function functions are given. Max () and Bottom_cut_rod (), as the primary and secondary function functions.

Max has nothing to say.

Main function Bottom_cut_rod ()

By creating an auxiliary array, arr, records the optimal solution of the current n (optimal substructure) so that the next time the same requirement arises (that is, the sub-problem overlaps), the call can be positioned directly from the array. The redundancy and frequent recursive calls in the naïve solution are eliminated, and the time and memory space consumption is reduced.

ARR is a mechanism for forgetting.

The dual for loop guarantees the appearance of each case and is updated by continually calling the Max function to iterate Q. When each option is completed for the first time, it is logged by using the auxiliary memo array, arr, for the next occurrence of sub-problem overlap.

The following is the CPP implementation, and debugging has passed.

#include <iostream>using namespace std;//size comparison int max (int a,int b) {    if (a>=b) return A;    else return b;} Main function int  bottom_cut_rod (int *p,int n) {    int *arr;    arr = new int [n+1]; Create auxiliary array, record optimal substructure    arr[0]=0;    for (int j=1;j<=n;j++)    {       int  q=-1;//creates a variable as the optimal solution for the container for        (int i=1;i<=j;i++)        {            Q=max (q , P[i]+arr[j-i]);//update to Q        }        arr[j]=q;//record optimal solution    } return    arr[n];//returns the optimal solution for a specified length of steel bar}int main () {    int p[]={1,5,8,9,10,17,17,20,24,30};    int n;    cout<< "Please input a int number" <<endl;    cin>>n;    int R=bottom_cut_rod (p,n);    cout<<r<<endl;    return 0;}


An introduction to algorithm for dynamic programming of steel bar cutting problem by bottom-up solution

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.