---------------------------------------------------------------------- Square part ---------------------------------------------------------------------- Implement a function that computes the square part of an integer. The square part is the largest square dividing a positive integer. Syntax: sp = squarepart(n) ------------------------- where sp is the largest square dividing n n is a positive integer Example: n = 72 divisors: 1 2 3 4 6 8 9 12 18 24 36 72 Of those are 1, 4, 9 and 36 squares. Thus the largest square dividing 72 is 36. Basic functionality: >> sp = squarepart(72) sp = 36 ---------------------------------------------------------------------- ---------------------------------------------------------------------- Knapsack Problem ---------------------------------------------------------------------- Given a knapsack with a certain capacity and a number of items with different weights and values - the task is to fill the knapsack such that the total weight of items inside is less or equal the capacity of the knapsack and the total value of items inside is maximized. It is very hard to obtain an optimal solution for larger numbers of items as the number of possible combinations increases exponentially. Your task here is to implement a heuristic method that will give a suboptimal but still good solution to the knapsack problem. Sort the items by "value per weight" and start packing with the most valuable item (maximum value per weight). Keep on packing until there is no item left that could fit without exceeding the given capacity. Example: capacity: 20 items: index 1 2 3 4 5 6 value 3 5 8 3 4 7 weight 2 4 7 4 3 5 v per w 1.50 1.25 1.14 0.75 1.33 1.40 Start packing with the most valuable item (per weight) - the first one. The remaining capacity is 18, the value of the items inside is 3. Continue with items numer 6, 5 and 2. The remaining capacity is 6. There are two items left but the most valuable of these two does not fit into the knapsack (remaining capacity 6 - weight of item 3: 7). Thus item number four will be packed. Then there is no item left that could fit into the knapsack. Finally: items inside: 1, 6, 5, 2, 4 total value: 22 total weight: 18 Syntax: value = knapsack(cap,items) ------------------------- where value is the total value of all items inside the knapsack cap is the capacity of the knapsack items is a matrix of one column per item and two rows the first row contains the value and the second the weight of each item Basic functionality: >> cap = 20 cap = 20 >> items = items = 3 5 8 3 4 7 2 4 7 4 3 5 >> value = knapsack(cap,items) value = 22 ----------------------------------------------------------------------
You'll Never Walk Alone