11#include " chapter15.h"
22#include " common.h"
3+
4+
5+
6+ // *************************************************************************************
7+ // * *
8+ // * DYNAMIC PROGRAMMING *
9+ // * *
10+ // * The four steps to follow when we are developing a *
11+ // * dynamic-programming algorithm *
12+ // * 1. Characterize the structure of an optimal solution *
13+ // * 2. Recursively define the value of an optimal solution *
14+ // * 3. Compute the value of an optimal solution, typically in a bottom-up solution *
15+ // * 4. Construct an optimal solution from computed information *
16+ // * *
17+ // * Step 1-3 is the basis of a dynamic-programming solution to a problem, if we *
18+ // * need only the value of the optimal solution and not the solution itself, we *
19+ // * omit step4. *
20+ // * When we do perform the step 4, we sometimes maintain addtional information *
21+ // * during step 3 so that we can easily construct an optimal solution *
22+ // * *
23+ // *************************************************************************************
24+
25+
26+
327// each rod of i inches length earns price[i] dollars of revenue
428static int price[10 ] = {1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 , 24 , 30 };
529
630
7- // recursive top-down implementation,
8- // n is the length of the rod, this programm calculate the maximum revenue
9- int cutRod (int * price_list, int n)
31+ // / @brief calculate the maximum revenue of n inches rod based on the price-list
32+ // / @param price_list I- the price_list for i inches rod is price_list[i]
33+ // / @param price_list_len I- the price_list length
34+ // / @param n I- n is the length of the rod, this programm calculate the maximum revenue
35+ // / @return the maximum revenue of a n inches rod
36+ // / @note recursive top-down implementation
37+ int cutRod (int * price_list, int price_list_len, int n)
1038{
1139 if (0 == n)
1240 return 0 ;
13- int revenue = -MAXIMUM ;
14- for (int i = 0 ; i < n; i++){
15- int tmp_revenue = cutRod (price_list, n - i - 1 ) + price_list[i];
41+ int revenue = 0x80000001 ;
42+ for (int i = 1 ; i <= n; i++){
43+ printf (" enter %d\n " , i);
44+ int price4part = 0 ;
45+ if (i <= price_list_len)
46+ price4part = price_list[i - 1 ];
47+ int tmp_revenue = cutRod (price_list, price_list_len, n - i) + price4part;
1648 revenue = tmp_revenue > revenue ? tmp_revenue : revenue;
1749 }
1850 return revenue;
1951}
2052
53+ // / @brief initialize auxiliary array which is a memoized version of 'cutRod'
54+ // / @param price_list I- the price_list for i inches rod is price_list[i]
55+ // / @param price_list_len I- the price_list length
56+ // / @param n I- n is the length of the rod, this programm calculate the maximum revenue
57+ // / @param r_array IO- auxiliary array that remembers previous procedure
58+ // / @return the maximum revenue of a n inches rod
59+ int memoizedCutRodAux (int * price_list, int price_list_len, int n, int * r_array)
60+ {
61+ if (r_array[n] >= 0 ){
62+ return r_array[n];
63+ }
64+ int q;
65+ if (n == 0 )
66+ q = 0 ;
67+ else {
68+ q = 0x80000001 ;
69+ for (int i = 1 ; i <= n; i++){
70+ int price4part = 0 ; // the price for the part of cut down rod
71+ if (i <= price_list_len)
72+ price4part = price_list[i - 1 ];
73+ int tmp_q = price4part + memoizedCutRodAux (price_list, price_list_len, n - i, r_array);
74+ q = q > tmp_q ? q : tmp_q;
75+ }
76+ }
77+ r_array[n] = q;
78+ for (int i = 0 ; i <= n; i++)
79+ printf (" r_array[%d]=%d\n " ,i, r_array[i]);
80+ return q;
81+ }
82+
83+ // / @brief calculate the maximum revenue of n inches rod based on the price-list
84+ // / @param price_list I the price_list for i inches rod is price_list[i]
85+ // / @param price_list_len I the price_list length
86+ // / @param n n is the length of the rod, this programm calculate the maximum revenue
87+ // / @return the maximum revenue of a n inches rod
88+ // / @note recursive top-down with memoization implementation
89+ int memoizedCutRod (int *price_list, int price_list_len, int n)
90+ {
91+ int * r = (int *)malloc (sizeof (int ) * (n + 1 ));
92+ for (int i = 0 ; i <= n; i++){
93+ r[i] = 0x80000001 ;
94+ }
95+ int q = memoizedCutRodAux (price_list, price_list_len, n, r);
96+ free (r);
97+ return q;
98+ }
2199
22100
101+ // / @brief
102+ // / @param price_list
103+ // / @param price_list_len
104+ // / @param n
105+ // / @return
106+ // / @note
107+ int bottomUpCutRod (int *price_list, int price_list_len, int n)
108+ {
109+ int * r = (int *)malloc (sizeof (int ) * (n + 1 ));
110+ r[0 ] = 0 ;
111+ for (int j = 1 ; j <= n; j++){
112+ int q = 0x80000001 ;
113+ for (int i = 1 ; i <= j; i++){
114+ int tmp_q = price_list[i - 1 ] + r[j - i];
115+ q = q > tmp_q ? q : tmp_q;
116+ }
117+ r[j] = q;
118+ }
119+ int revenue = r[n];
120+ free (r);
121+ return revenue;
122+ }
23123
24124void callCutRod ()
25125{
26126 printf (" > Running test for chapter 15\n " );
27- int length = 4 ;
28- int revenue = cutRod (price, length);
29- printf (" > The maximum revenue of %d inches rod is %d\n " , length, revenue);
127+ // when the lenght is bigger than 20, the speed difference is apparently to tell
128+ int length = 20 ;
129+ int revenue0 = cutRod (price, 10 , length);
130+ int revenue1 = memoizedCutRod (price, 10 , length);
131+ printf (" > The maximum revenue of %d inches rod is %d\n " , length, revenue0);
132+ printf (" > The maximum revenue of %d inches rod is %d(using memoized method to remember steps that have been worked through)\n " , length, revenue1);
30133 printf (" > Finish test for chapter 15\n " );
31-
32134}
0 commit comments