0% found this document useful (0 votes)
4 views2 pages

Knap Sack Using Dynamic

The document contains a C program that implements the knapsack problem using recursion. It defines a function to calculate the maximum value that can be put in a knapsack given weight constraints and item values. The main function initializes item values and weights, then calls the knapsack function to display the maximum value.

Uploaded by

gurpreet.321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Knap Sack Using Dynamic

The document contains a C program that implements the knapsack problem using recursion. It defines a function to calculate the maximum value that can be put in a knapsack given weight constraints and item values. The main function initializes item values and weights, then calls the knapsack function to display the maximum value.

Uploaded by

gurpreet.321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<stdio.

h
>

int max
(inta ,intb)

{return (a>b)? a:b;}

int knapsack (int W , int wt[], int val[], int n)

if (n==0 ||
W==0

) return 0;

if (wt [n-1]>W)

return knapsack(W , wt ,
val ,n-1);

else

return max(val[n-1]+knapsack(W-wt[n-1],wt,val, n-1), knapsack (W , wt, val, n-1));

int main()

int val[] = {3 ,4, 5,6};

int wt [] = {2 ,3, 4,5};

int W =
5; int n
=4;
printf ("maximum value we can put in knapsack is %d" ,knapsack (W , wt,val,n));

return0;

OUTPUT:

You might also like