Question

In: Computer Science

C Program: How do I write a Greedy function for 0-1 knapsack, to find total value...

C Program: How do I write a Greedy function for 0-1 knapsack, to find total value only( replace struct Knapsack)

# include
#include
#include

struct Knapsack {
   int value;
   int weight;
};

   // returns maximum of two integers
   int max(int a, int b) { return (a > b)? a : b; }
   // Returns the maximum value that can be put in a knapsack of capacity W
   struct Knapsack knapSackExhaustive(int W, int wt[], int val[], int n){
   int i, w;
   int K[n+1][W+1];
   int totalWeight=0;

   // Build table K[][] in bottom up manner

   for (i = 0; i <= n; i++){
      
       for (w = 0; w <= W; w++){
      
   if (i==0 || w==0)
       K[i][w] = 0;
   else if (wt[i-1] <= w){
       K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
   }
   else
       K[i][w] = K[i-1][w];
   }
  
   }
  
   //For calculation of totalweight;
   int res = K[n][W];   
  
   w = W;
  
   for (i = n; i > 0 && res > 0; i--) {
   // either the result comes from the top
   // (K[i-1][w]) or from (val[i-1] + K[i-1]
   // [w-wt[i-1]]) as in Knapsack table. If
   // it comes from the latter one/ it means
   // the item is included.
   if (res == K[i - 1][w])
  
   continue;   
  
   else {
   // This item is included.
   //printf("%d ", wt[i - 1]);
  
   totalWeight=totalWeight+wt[i-1];
  
   // Since this weight is included its
   // value is deducted
      
   res = res - val[i - 1];
  
   w = w - wt[i - 1];
   }
}

   struct Knapsack knap;
   knap.value=K[n][W];
   knap.weight=totalWeight;
   return knap;
}// end struct knapSackExhaustive


int main(void) {
  
   struct Knapsack aSack;  
   int i;
   time_t t;
   int val[5];
   int wt[5];
  
   //Intializes random number generator
   srand((unsigned) time(&t));

   // Print 5 random values from 3 to 15
printf("Five Random Values:\n");
for( i = 0 ; i < 5 ; i++ ) {
   
   val[i]=rand()%15+3;
   printf("%d\n",val[i]);
  
}
  
   int j;
   //Print 5 random weights from 1 and 10000
   printf("Five Random Weights:\n");
   for( j = 0 ; j < 5 ; j++ ) {
   wt[j]=rand() % 10000;
   printf(" %d\n", wt[j]);
  
}

   int W = 10000;
   int n = sizeof(val)/sizeof(val[0]);
  
   aSack = knapSackExhaustive(W, wt, val, n);

   printf("Total Value: %d\t\n", aSack.value);
   printf("Total Weight:%d\t",aSack.weight);
  
   return 0;
}

Solutions

Expert Solution

GREEDY APPROACH FOR 0/1 KNAPSACK PROBLEM

#include<stdio.h>

int main() {

int wt[5]={0,2,3,4,5};//weight of different items
int b[5]={0,3,4,5,6};//corresponding benefit(profit/value) related to that particular item
//b[i] is the benefit of item having weight wt[i]
//0 is placed at starting of weight and benefit array because in the below nested loop i will go from 1 to 4 so b[4] will be benefit of 4th item which then create error as no index will be available
int W=5;//total weight of bag available
int n=4;//number of items
int i,w;
int v[n+1][W+1];
for(w=0;w<W+1;w++)
{v[0][w]=0;
}
for(i=0;i<n+1;i++)
{
v[i][0]=0;
}
for(i=1;i<=n;i++)
{
for(w=1;w<=W;w++)
{
if(wt[i]<=w)//that means items can be a part of the bag
{
if((b[i]+v[i-1][w-wt[i]])>v[i-1][w])
{
v[i][w]=v[i-1][w-wt[i]]+b[i];
  
}
else
v[i][w]=v[i-1][w];
}
else
{
v[i][w]=v[i-1][w];
  
}
}
}
for(i=0;i<=n;i++)
{
for(w=0;w<=W;w++)
{
printf("%d ",v[i][w]);
}
printf("\n");
}
  
printf("benefit =%d",v[n][W]);
  
  
}


Related Solutions

In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
Part1: Write a program in C/C++ to solve the 0/1 knapsack problem using (i) Dynamic Programming...
Part1: Write a program in C/C++ to solve the 0/1 knapsack problem using (i) Dynamic Programming based algorithm and (ii) Branch and Bound Search based algorithm Go through the related text and implement each of these algorithms using the efficient data structure. Show the results of different steps of these algorithms for an instance of the 0/1 Knapsack problem with number of items, n=4. The capacity of the knapsack, weights and profits of the items may be generated randomly with...
how do i find the percentage of total value
how do i find the percentage of total value
C Program: (Knapsack Exhaustive): How do I add the 5 random generated values into the val...
C Program: (Knapsack Exhaustive): How do I add the 5 random generated values into the val [] arrays and how to add the total maximum weights ?    // returns maximum of two integers    int max(int a, int b) { return (a > b)? a : b; }       // Returns the maximum value that can be put in a knapsack of capacity W    int knapSackExhaustive(int W, int wt[], int val[], int n){    if (n ==...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
C++ How do I initialize a clock to 0 and run it constantly throughout the program?...
C++ How do I initialize a clock to 0 and run it constantly throughout the program? THEN, how do I get the time at that moment multiple times? As in id like to set the clock = 0, do stuff do stuff get time now, store it do stuff get time now, store it then also be able to do arithmetic on it, for instance, last time - current time = 2 seconds. I made it sound complicated but I...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
KNAPSACK Optimization Problem using Greedy Method Problem 1: Item Weight Value 1 14 20 2 6...
KNAPSACK Optimization Problem using Greedy Method Problem 1: Item Weight Value 1 14 20 2 6 16 3 10 8 4 5 10 5 4 12 Allowed weight = 24 Kg Problem 2: Item Weight Value 1 6 30 2 8 40 3 15 45 4 22 88 5 25 80 Allowed weight = 60 Kg Problem 3: Item Weight Value 1 6 30 2 8 40 3 15 45 4 22 88 5 25 80 Allowed weight = 60...
I want c++ /c program to build a double linked list and find with a function...
I want c++ /c program to build a double linked list and find with a function the middle element and its position and the linked list must be inserted by the user aand note You should handle the both cases of whether the size of the list even or odd
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT