Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- If we do not consider the time considered for sorting the inputs then all of the three greedy strategies complexity will be O(n) and Hence maximum profit is 80.
- Below is the alogtithm used to solve the above problem.
- Algorithm GreedyKnapsack(m,n)
- // p[i:n] and [1:n] contain the profits and weights respectively
- // if the n-objects ordered such that p[i]/w[i]>=p[i+1]/w[i+1], m? size of knapsack and x[1:n]? the solution vector
- {
- For i:=1 to n do x[i]:=0.0
- U:=m;
- For i:=1 to n do
- {
- if(w[i]>U) then break;
- x[i]:=1.0;
- U:=U-w[i];
- }
- If(i<=n) then x[i]:=U/w[i];
- }
- OR)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement