Question

In: Computer Science

The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible...

The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible value. Using greedy paradigm, we can solve this problem easily. Your task is the following:

(a) Write the pseudo-code of a greedy solution for knapsack problem.
(b) Give is the time complexity of your solution in part (a).
(c) Implement part (a) in C programming language.

Solutions

Expert Solution

Answer-

there are two types of Knapsack problem

-> 0-1 Knapsack

-> Fractional Knapsack

if problem type is Fractional Knapsack then we can apply Greedy Paradigm to optimal solution

Answer (a)

Pseudo-code of a greedy solution for knapsack problem

Greedy_fractional_knapsack (w, v, W) // w is weight of Items , v is value of Items and W is Capacity of Knapsack

FOR i =1 to n
do x[i] =0
weight = 0
while weight < W
do i = best remaining item
IF weight + w[i] ≤ W
then x[i] = 1
   weight = weight + w[i]
else
x[i] = (w - weight) / w[i]
weight = W
return x

Answer (b)  

Time complexity of above Pseudo-code
we keep the items in heap with largest vi/wi at the root. Then creating the heap takes O(n) time
while-loop now takes O(log n) time for extract largest vi/wi each time(since heap property must be restored after the removal of root) this process will happen n times so it will take O(n log n) time

so total Time complexity=O(n)+O(n log n)

=O(n log n)

Answer (c)

C program

# include<stdio.h>

void knapsack(int n, float weight[], float value[], float capacity)
{
float x[20], tp = 0;
int i, j, u;
u = capacity;
for (i = 0; i < n; i++)
x[i] = 0.0;
for (i = 0; i < n; i++)
{
if (weight[i] > u)
break;
else
{
x[i] = 1.0;
tp = tp + value[i];
u = u - weight[i];
}
}

if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * value[i]);

printf("\nThe result is:- ");
for (i = 0; i < n; i++)
printf("%f\t", x[i]);
printf("\nMaximum value is:- %f", tp);

}


int main()
{
float weight[20], value[20], capacity;
int num, i, j;
float ratio[20], temp;
printf("\nEnter the no. of items:- ");
scanf("%d", &n);
printf("\nEnter the weights and values of each items:- ");
for (i = 0; i < n; i++)
{
scanf("%f %f", &weight[i], &value[i]);
}
printf("\nEnter the capacityacity of knapsack:- ");
scanf("%f", &capacity);
for (i = 0; i < n; i++)
{
ratio[i] = value[i] / weight[i];
}

for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (ratio[i] < ratio[j])
{
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = value[j];
value[j] = value[i];
value[i] = temp;
}
}
}
  
knapsack(n, weight, value, capacity);

return(0);

}


Related Solutions

The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible...
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible value. Using greedy paradigm, we can solve this problem easily. Your task is the following: (a) Write the pseudo-code of a greedy solution for knapsack problem. (b) Give is the time complexity of your solution in part (a). (c) Implement part (a) in C programming language.
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...
In a typical optimization problem (max/min problem), we want to find a relative maximum or relative...
In a typical optimization problem (max/min problem), we want to find a relative maximum or relative minimum of a function. Our process is to • find the derivative of the function, • set that derivative equal to zero, • and solve for x. Use complete sentences to explain why this process makes sense.
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of...
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of items, let call them 1,2,3,4,...,n. There are exactly c_i copies of item i, and each such copy has value v_i and weight w_i. As before, the knapsack capacity is W, and the other constraint is that you can only take at most c_i copies of item i ( since no more are available). Show how to compute the optimal value that can be achieved...
Optimization Problem
We want to construct a box whose base length is three times the base width. The material used to build the top and bottom cost $10/ft2 and the material to build the sides cost $6/ft2 . If the box must have volume 50 ft3 , what is the minimum cost of the box?
Solve the following optimization problem (Be sure to include the statement of the optimization problem and...
Solve the following optimization problem (Be sure to include the statement of the optimization problem and a graph of the feasible in your solution): Jamie has joined a building contest. A dog shape requires 3 small blocks and one large block to build. A robot shape requires 5 small bricks and 5 large bricks to build. Jamie has a supply of 240 small bricks and 100 large bricks. If a dog is worth 2 points and a robot is worth...
Optimization problem for calculus 3. Possible use of Lagrange Multiple? Question: What are the largest and...
Optimization problem for calculus 3. Possible use of Lagrange Multiple? Question: What are the largest and smallest outputs of the function x 2+2y2 on the ellipse x2+y2+xy=3
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack...
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. **The arguments to the knapsack function are target weight and the array index where the remaining items start. For example if you want your knapsack to weigh exactly 20 pounds and you have five items with weights of 11,8,7,6 and 5 pounds. In this case only combination that works is 8,...
how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
create a genetic algorithm for knapsack problem in c++
create a genetic algorithm for knapsack problem in c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT