Can anyone help me with the greedy algorithm problem (Greedy Algorithm)?

Asked

Viewed 260 times

1

The problem asks to do the following: Cut a steel rod of length n to get the maximum possible recipe( Rod Cutting). Present in algorithm with backtraking(already done) and find a solution with greedy algorithm.

Code algorithm made using Backtraking:

int max(int a, int b) { return (a > b)? a : b;}

int cutRod(int price[], int n)
{
   if (n <= 0)
     return 0;
   int max_val = INT_MIN;

   for (int i = 0; i<n; i++)
        max_val = max(max_val, price[i] + cutRod(price, n-i-1));

   return max_val;
}

To make the code using greedy algorithm it is necessary to sort the vector?

  • Welcome to sopt. Format the code by selecting it and clicking on {}

  • 3

    Greedy Algorithm? This is new for me....

  • If I took the partitions with the highest values adding them up and ignoring the other partitions, this would be considered greedy algorithm???

  • Could you send the complete code? With an example of the preference vector

  • 1
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.