Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. 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.
  2.  
  3. Below is the alogtithm used to solve the above problem.
  4.  
  5. Algorithm GreedyKnapsack(m,n)
  6.  
  7. // p[i:n] and [1:n] contain the profits and weights respectively
  8.  
  9. // 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
  10.  
  11. {
  12.  
  13. For i:=1 to n do x[i]:=0.0
  14.  
  15. U:=m;
  16.  
  17. For i:=1 to n do
  18.  
  19. {
  20.  
  21. if(w[i]>U) then break;
  22.  
  23. x[i]:=1.0;
  24.  
  25. U:=U-w[i];
  26.  
  27. }
  28.  
  29. If(i<=n) then x[i]:=U/w[i];
  30.  
  31. }
  32.  
  33. OR)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement