Give pseudocode for the rod-cutting optimization problem using the bottom-up approach and the top-down approach
Rod cutting is another kind of problem which can be solved without using dynamic programming approach but we can optimize it greatly by using it. According to the problem, we are provided with a long rod of length n units. We can cut the rod in different sizes and each size has a different cost associated with it i.e., a rod of i units length will have a cost of ci$.
Pseudo code and explanation for Top Down Code for Rod Cutting
We need the cost array (c) and the length of the rod (n) to begin with, so we will start our function with these two - TOP-DOWN-ROD-CUTTING(c, n)
We already know that we are going to use dynamic programming, so we will start by making an array to store the maximum revenue that can be generated by different lengths i.e., r[n+1] so that we don't have to recalculate this value again and again.
Initially, we don't know the value of the maximum revenue that can be generated by these lengths, so we will set all the elements of this array to something negative i.e., −∞. We can't initialize all the elements with 0 because the maximum revenue generated by the rod of length 0 is 0, so we will make the r[0] = 0.
r[0] = 0
for i in 1 to n
r[i] = -INF
In the top down approach, we just start solving the problem naturally, so we will just start checking if the there is already a solution stored in the array or not.
if r[n] >= 0
return r[n]
If the revenue is not already in the array r, then we will have to calculate it and for that, we need to calculate the maximum value among
(ci+rn−i)
for i ranging from 1 to n. Basically,
(ci+rn−i)
means the ci + maximum revenue that can be generated by rod of length n-i i.e., TOP-DOWN-ROD-CUTTING(c, n-i). Take note that we can't directly use the array r to get the value of r[n-i] because the array r doesn't store this value yet or at least we are not sure that it does or doesn't. This would have been in the case of bottom-up implementation in which we start by generating the smaller values first.
Now, the calculation of the maximum of (ci+rn−i) is similar to calculating the sum of all the elements of an array. We will have a variable to store the maximum revenue and then we will iterate from 1 to n and compare the current maximum revenue stored in the variable with the revenue c[i] + TOP-DOWN-ROD-CUTTING(c, n-i).
maximum_revenue = -INF
for i in 1 to n
maximum_revenue = max(maximum_revenue, c[i] + TOP-DOWN-ROD-CUTTING(c, n-i))
As stated, we started by having a variable to store the maximum revenue i.e., maximum_revenue = -INF and then we iterate from 1 to n to find the maximum among c[i]+TOP-DOWN-ROD-CUTTING(c, n-i).
At last, we have to just fill up our array r and return this value.
r[n] = maximum_revenue
return r[n]
r[n+1]
r[0] = 0
for i = 1 to n
r[i] = -INF
TOP-DOWN-ROD-CUTTING(c, n)
if r[n] >= 0
return r[n]
maximum_revenue = -INF
for i in 1 to n
maximum_revenue = max(maximum_revenue, c[i] + TOP-DOWN-ROD-CUTTING(c, n-i))
r[n] = maximum_revenue
return r[n]
C Code:
#include <stdio.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
const int INF = 100000;
int r[5+1];
void init_r() {
int i;
r[0] = 0;
for(i=1; i<=5; i++) {
r[i] = -1*INF;
}
}
int top_down_rod_cutting(int c[], int n) {
if (r[n] >= 0) {
return r[n];
}
int maximum_revenue = -1*INF;
int i;
for(i=1; i<=n; i++) {
maximum_revenue = MAX(maximum_revenue, c[i] + top_down_rod_cutting(c, n-i));
}
r[n] = maximum_revenue;
return r[n];
}
int main() {
init_r();
// array starting from 1, element at index 0 is fake
int c[] = {0, 10, 24, 30, 40, 45};
printf("%d\n", top_down_rod_cutting(c, 5));
return 0;
}
Pseudo code and explanation for Bottom-Up Code for Rod Cutting
In the bottom-up technique, we start by filling the array from start. So, we will first initialize an array r and then we will iterate over it to fill it.
r[n+1]
r[0] = 0
for i in 1 to j
Now, the array r contains the maximum revenue that can pe generated by each length. For example, the 4th element of the array r contains the maximum revenue that can be generated by a rod of 4 units long. So to calculate it, we need to calculate the maximum of (ci+rn−i) for i ranging from 1 to 4. And thus, we need to iterate again for the different values of i.
for i in 1 to j
max_revenue = -INF
for i in 1 to j
max_revenue = max(max_revenue, c[i] + r[j-i])
r[j] = max_revenue
return r[n]
Take a note that we can directly use the value stored in the array r for
rn−i
because we know that we have already filled all the smaller values of the array.
BOTTOM-UP-ROD-CUTTING
r[n+1]
r[0] = 0
for j = i to n
maximum_revenue = -INF
for i in 1 to j
maximum_revenue = max(maximum_revenue, c[i] + r[j-i])
r[j] = maximum_revenue
return r[n]
C - code
INF = 100000;
r = [0] + [-1*INF]*5
def max(x, y):
if x > y:
return x
return y
def bottom_up_rod_cutting(c, n):
r = [0]*(n+1)
r[0] = 0
for j in range(1, n+1):
maximum_revenue = -1*INF
for i in range(1, j+1):
maximum_revenue = max(maximum_revenue, c[i] + r[j-i])
r[j] = maximum_revenue
return r[n]
if __name__ == '__main__':
#array starting from 1, element at index 0 is fake
c = [0, 10, 24, 30, 40, 45]
print(bottom_up_rod_cutting(c, 5))
The analysis of the bottom up code is simple. We are using nested loops, the first loop is iterating from 1 to n and the second loop is iterating from 1 to j (j ranging from 1 to n). So, it will result in Θ(n2) time. You can do the full analysis yourself if you are not convinced or can read the Analyze Your Algorithm chapter to see the examples of analysis of algorithms. You can even raise a doubt in the discussion section and find help from other members. The top-down approach also gives us a Θ(n2) running time.
Among the algorithms we studied so far, you can see that our first task was to come up with a solution for the problem and then we were focusing on making a code for it and this is what one should do while dealing with an unknown problem that is, focus on finding a solution for the problem first.
This is the solution for the given problem , this solution is as per my knowledge. I hope you accept this as solution for the given question.
Give pseudocode for the rod-cutting optimization problem using the bottom-up approach and the top-down approach
QUESTION 12 Chapter 7 discusses top-down budgeting vs. bottom-up budgeting. First, define top-down budgeting and bottom-up budgeting. Next, discuss the pros and cons of each. Finally, which method would you prefer as a manger? (you do not need to agree with the text) T T T T Paragraph Arial 3 (12pt) Q 5 Of Mashups HTML CSS Path: p Words:0
The drawback of using the top-down approach to compute the operating cash flow is that it Multiple Choice O ignores noncash expenses applies only if a project affects soles apoles only to cost cutting projects Prex 23 30 ili Next > Ο applies only if a project affects sales Ο applies only to cost cutting projects Ο is equal to sales - costs - taxes - depreciation Ο is used solely to compute a bid price < Prev 23 030...
budgeting stratwgy for building a house top down bottom up project management
Rod-cutting problem Design a dynamic programming algorithm for the following problem. Find the maximum total sale price that can be obtained by cutting a rod of n units long into integer-length pieces if the sale price of a piece i units long is pi for i = 1, 2, . . . , n. What are the time and space efficiencies of your algorithm? Code or pseudocode is not needed. Just need theoretical explanation with dynamic programming with recurrence relation...
Rod cutting. Recall the rod cutting problem we learned (ch 15). In the problem, we're given a rod of length n along with an array {p_1}i lessthanorequalto i lessthanorequalto n- in which jh denotes the price you can charge for a rod/piece of length i. The goal is to cut the given rod of length n into smaller pieces (or do nothing) so that the total price of the pieces is maximized. Fill out the following table using the recursion...
portfolio construction starts with asset allocation. Bottom-up Top-down C) Upside-down Side-to-side
Project management problem
Question 3 a) Compare and contrast the top-down and bottom-up cost estimation approaches in project management. (50%) b) Explain and describe the purpose of the British Standard BS 6079-3:2000 on project risk management, outlining the typical phases one would have to consider to effectively address project-related risks. (50%)
Explain how the outcomes of bottom-up and top-down community regulation are similar and different
discuss what is Top Down and Bottom Up Tiered Lan Strategies. Discuss Which strategy will be preference and why?
Just need help drawing sandwich approach and modified sandwich approach! Figure 8.22 illustrates the component hierarchy in a software system. Describe the sequence of tests for integrating the components using bottom-up approach, a top-down approach, a modified top-down approach, a big-bang approach, a sandwich approach, and a modified sandwich approach. MI IN
Just need help drawing sandwich approach and modified sandwich approach! Figure 8.22 illustrates the component hierarchy in a software system. Describe the sequence of tests for integrating the...