How to resolve the algorithm Knapsack problem/Continuous step by step in the Ruby programming language
How to resolve the algorithm Knapsack problem/Continuous step by step in the Ruby programming language
Table of Contents
Problem Statement
A thief burgles a butcher's shop, where he can select from some items. The thief knows the weights and prices of each items. Because he has a knapsack with 15 kg maximal capacity, he wants to select the items such that he would have his profit maximized. He may cut the items; the item has a reduced price after cutting that is proportional to the original price by the ratio of masses. That means: half of an item has half the price of the original.
This is the item list in the butcher's shop:
Show which items the thief carries in his knapsack so that their total weight does not exceed 15 kg, and their total value is maximized.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Knapsack problem/Continuous step by step in the Ruby programming language
This Ruby code is a simple greedy algorithm that tries to maximize the value of items that can be packed into a knapsack with a given maximum weight. The code defines an array of items, each represented by a tuple of the item's name, its weight, and its price. The array is then sorted in descending order of the ratio of price to weight.
The code then iterates over the items in the sorted array, and for each item, it checks if the remaining capacity in the knapsack is sufficient to hold the item. If it is, the item is added to the knapsack and its value is added to the total value. If the remaining capacity is not sufficient, the amount of the item that can be added to the knapsack is calculated, and the corresponding value is added to the total value.
The code is implemented in a straightforward manner, and the comments provide clear explanations of what the code is doing. The time complexity of the code is O(n log n), where n is the number of items in the array, due to the sorting operation. The space complexity is O(n), due to the array of items.
Overall, the code is a good example of a simple greedy algorithm, and it provides a clear demonstration of how such algorithms can be used to solve optimization problems.
Source code in the ruby programming language
items = [ [:beef , 3.8, 36],
[:pork , 5.4, 43],
[:ham , 3.6, 90],
[:greaves, 2.4, 45],
[:flitch , 4.0, 30],
[:brawn , 2.5, 56],
[:welt , 3.7, 67],
[:salami , 3.0, 95],
[:sausage, 5.9, 98] ].sort_by{|item, weight, price| -price / weight}
maxW, value = 15.0, 0
items.each do |item, weight, price|
if (maxW -= weight) > 0
puts "Take all #{item}"
value += price
else
puts "Take %gkg of %s" % [t=weight+maxW, item], "",
"Total value of swag is %g" % (value+(price/weight)*t)
break
end
end
You may also check:How to resolve the algorithm 24 game step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Dot product step by step in the PostScript programming language
You may also check:How to resolve the algorithm Date format step by step in the OCaml programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Lasso programming language
You may also check:How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the C# programming language