answersLogoWhite

0

#include<stdio.h>

#include<conio.h>

int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};

int max(int i,int j)

{

return ((i>j)?i:j);

}

int knap(int i,int j)

{

int value;

if(v[i][j]<0)

{

if(j<w[i])

value=knap(i-1,j);

else

value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));

v[i][j]=value;

}

return(v[i][j]);

}

void main()

{

int profit,count=0;

clrscr();

printf("\nEnter the number of elements\n");

scanf("%d",&n);

printf("Enter the profit and weights of the elements\n");

for(i=1;i<=n;i++)

{

printf("For item no %d\n",i);

scanf("%d%d",&p[i],&w[i]);

}

printf("\nEnter the capacity \n");

scanf("%d",&cap);

for(i=0;i<=n;i++)

for(j=0;j<=cap;j++)

if((i==0)(j==0))

v[i][j]=0;

else

v[i][j]=-1;

profit=knap(n,cap);

i=n;

j=cap;

while(j!=0&&i!=0)

{

if(v[i][j]!=v[i-1][j])

{

x[i]=1;

j=j-w[i];

i--;

}

else

i--;

}

printf("Items included are\n");

printf("Sl.no\tweight\tprofit\n");

for(i=1;i<=n;i++)

if(x[i])

printf("%d\t%d\t%d\n",++count,w[i],p[i]);

printf("Total profit = %d\n",profit);

getch();

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Is knapsack algorithm is a public key encryption algorithm?

yes


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


Can you give a C program about SJF algorithm?

no.


Can you provide an explanation of the greedy algorithm approach to solving the knapsack problem?

The greedy algorithm for the knapsack problem involves selecting items based on their value-to-weight ratio, prioritizing items with the highest ratio first. This approach aims to maximize the value of items placed in the knapsack while staying within its weight capacity. By iteratively selecting the most valuable item that fits, the greedy algorithm can provide a near-optimal solution for the knapsack problem.


Program in c to develop an algorithm that prints the n value of algorithm?

reymond rillera reymond rillera


What is the role of the greedy algorithm in solving the knapsack problem efficiently?

The greedy algorithm is used in solving the knapsack problem efficiently by selecting items based on their value-to-weight ratio, prioritizing those with the highest ratio first. This helps maximize the value of items that can fit into the knapsack without exceeding its weight capacity.


What is the time complexity of the knapsack greedy algorithm when solving a problem with a large number of items?

The time complexity of the knapsack greedy algorithm for solving a problem with a large number of items is O(n log n), where n is the number of items.


How does algorithm and c plus plus program get along?

They are bosom-friends.


C program algorithm for simple interest using while loop?

Not used


Can someone provide the C program for Prim's algorithm?

The C code for Prim's algorithm can be found in the following link. https://sites.google.com/site/itstudentjunction/lab-programming-solutions/data-structures-programs/program-to-find-minimal-spanning-tree-using--prims-algorithm


What is the role of the knapsack greedy algorithm in solving optimization problems involving resource allocation?

The knapsack greedy algorithm is used to solve optimization problems where resources need to be allocated efficiently. It works by selecting items based on their value-to-weight ratio, prioritizing those that offer the most value while staying within the weight limit of the knapsack. This algorithm helps find the best combination of items to maximize the overall value while respecting the constraints of the problem.


What is solution in c program?

The program itself is the solution. All programs are a solution to a given problem; that's the entire point of writing a program, to solve a problem. The program's algorithm specifies how the problem is solved and it's the programmer's job to convert that algorithm into working code.