Question: Implement the following java algorithm and modify it to count the number of nodes visited and include the set of items in the solution. public

Implement the following java algorithm and modify it to count the number of nodes visited and include the set of items in the solution.

public class Algorithmsixone {

void knapsack2 (int n, const int p[], const int w[], int W, int& maxprofit){

queue_of_node Q;

node u, v;

initialize (Q);

v.level = 0; v.profit = 0; v.weight = 0;

maxprofit = 0;

enqueue(Q, v);

while (!empty(Q)){

dequeue(Q, v);

u.level = v.level +1;

u.weight = v.weight + w[u.level];

u.profit = v.profit + p[u.level];

if(u.weight <= W && u.profit > maxprofit)

maxprofit = u.profit;

if(bound(u) > maxprofit)

enqueue(Q, u);

u.weight = v.weight;

u.profit = v.profit;

if(bound(u) > maxprofit)

enqueue(Q, u);

}

}

float bound (node u){

index j, k;

int totweight;

float result;

if(u.weight >= W)

return 0;

else{

result = u.profit;

j = u.level + 1;

totweight = u.weight;

while(j <= n && totweight + w[j] <= W){

totweight = totweight + w[j];

result = result + p[j];

j++;

}

k = j;

if(k <= n)

result = result + (W - totweight) * p[k]/w[k];

return result;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!