{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Solution Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem: Given a knapsack with a total weight capacity and a list of items with weight w(i) and value v(i), determine which items to select to maximize total value.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", "* [Algorithm](#Algorithm)\n", "* [Code](#Code)\n", "* [Unit Test](#Unit-Test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constraints\n", "\n", "* Can we replace the items once they are placed in the knapsack?\n", " * No, this is the 0/1 knapsack problem\n", "* Can we split an item?\n", " * No\n", "* Can we get an input item with weight of 0 or value of 0?\n", " * No\n", "* Can we assume the inputs are valid?\n", " * No\n", "* Are the inputs in sorted order by val/weight?\n", " * Yes, if not we'd need to sort them first\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "* items or total weight is None -> Exception\n", "* items or total weight is 0 -> 0\n", "* General case\n", "\n", "
\n",
"total_weight = 8\n",
"items\n",
"  v | w\n",
"  0 | 0\n",
"a 2 | 2\n",
"b 4 | 2\n",
"c 6 | 4\n",
"d 9 | 5\n",
"\n",
"max value = 13\n",
"items\n",
"  v | w\n",
"b 4 | 2\n",
"d 9 | 5 \n",
"
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Algorithm\n", "\n", "We'll use bottom up dynamic programming to build a table.\n", "\n", "The solution for the top down approach is also provided below.\n", "\n", "
\n",
"v = value\n",
"w = weight\n",
"\n",
"               j              \n",
"    -------------------------------------------------\n",
"    | v | w || 0 | 1 | 2 | 3 | 4 | 5 | 6  | 7  | 8  |\n",
"    -------------------------------------------------\n",
"    | 0 | 0 || 0 | 0 | 0 | 0 | 0 | 0 | 0  | 0  | 0  |\n",
"i a | 2 | 2 || 0 | 0 | 2 | 2 | 2 | 2 | 2  | 2  | 2  |\n",
"  b | 4 | 2 || 0 | 0 | 4 | 4 | 6 | 6 | 6  | 6  | 6  |\n",
"  c | 6 | 4 || 0 | 0 | 4 | 4 | 6 | 6 | 10 | 10 | 12 |\n",
"  d | 9 | 5 || 0 | 0 | 4 | 4 | 6 | 9 | 10 | 13 | 13 |\n",
"    -------------------------------------------------\n",
"\n",
"i = row\n",
"j = col\n",
"\n",
"if j >= item[i].weight:\n",
"    T[i][j] = max(item[i].value + T[i - 1][j - item[i].weight],\n",
"                  T[i - 1][j])\n",
"else:\n",
"    T[i][j] = T[i - 1][j]\n",
"
\n", "\n", "Complexity:\n", "* Time: O(n * w), where n is the number of items and w is the total weight\n", "* Space: O(n * w), where n is the number of items and w is the total weight" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Item Class" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "class Item(object):\n", "\n", " def __init__(self, label, value, weight):\n", " self.label = label\n", " self.value = value\n", " self.weight = weight\n", "\n", " def __repr__(self):\n", " return self.label + ' v:' + str(self.value) + ' w:' + str(self.weight)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Knapsack Bottom Up" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class Knapsack(object):\n", "\n", " def fill_knapsack(self, input_items, total_weight):\n", " if input_items is None or total_weight is None:\n", " raise TypeError('input_items or total_weight cannot be None')\n", " if not input_items or total_weight == 0:\n", " return 0\n", " items = list([Item(label='', value=0, weight=0)] + input_items)\n", " num_rows = len(items)\n", " num_cols = total_weight + 1\n", " T = [[None] * num_cols for _ in range(num_rows)]\n", " for i in range(num_rows):\n", " for j in range(num_cols):\n", " if i == 0 or j == 0:\n", " T[i][j] = 0\n", " elif j >= items[i].weight:\n", " T[i][j] = max(items[i].value + T[i - 1][j - items[i].weight],\n", " T[i - 1][j])\n", " else:\n", " T[i][j] = T[i - 1][j]\n", " results = []\n", " i = num_rows - 1\n", " j = num_cols - 1\n", " while T[i][j] != 0:\n", " if T[i - 1][j] == T[i][j]:\n", " i -= 1\n", " elif T[i][j - 1] == T[i][j]:\n", " j -= 1\n", " else:\n", " results.append(items[i])\n", " i -= 1\n", " j -= items[i].weight\n", " return results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Knapsack Top Down" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "class KnapsackTopDown(object):\n", "\n", " def fill_knapsack(self, items, total_weight):\n", " if items is None or total_weight is None:\n", " raise TypeError('input_items or total_weight cannot be None')\n", " if not items or not total_weight:\n", " return 0\n", " memo = {}\n", " result = self._fill_knapsack(items, total_weight, memo, index=0)\n", " return result\n", "\n", "\n", " def _fill_knapsack(self, items, total_weight, memo, index):\n", " if total_weight < 0:\n", " return 0\n", " if not total_weight or index >= len(items):\n", " return items[index - 1].value\n", " if (total_weight, len(items) - index - 1) in memo:\n", " return memo[(total_weight, len(items) - index - 1)] + items[index - 1].value\n", " results = []\n", " for i in range(index, len(items)):\n", " total_weight -= items[i].weight\n", " result = self._fill_knapsack(items, total_weight, memo, index=i + 1)\n", " total_weight += items[i].weight\n", " results.append(result)\n", " results_index = 0\n", " for i in range(index, len(items)):\n", " memo[total_weight, len(items) - i] = max(results[results_index:])\n", " results_index += 1\n", " return max(results) + (items[index - 1].value if index > 0 else 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Knapsack Top Down Alternate" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "class Result(object):\n", "\n", " def __init__(self, total_weight, item):\n", " self.total_weight = total_weight\n", " self.item = item\n", "\n", " def __repr__(self):\n", " return 'w:' + str(self.total_weight) + ' i:' + str(self.item)\n", "\n", " def __lt__(self, other):\n", " return self.total_weight < other.total_weight\n", "\n", "\n", "def knapsack_top_down_alt(items, total_weight):\n", " if items is None or total_weight is None:\n", " raise TypeError('input_items or total_weight cannot be None')\n", " if not items or not total_weight:\n", " return 0\n", " memo = {}\n", " result = _knapsack_top_down_alt(items, total_weight, memo, index=0)\n", " curr_item = result.item\n", " curr_weight = curr_item.weight\n", " picked_items = [curr_item]\n", " while curr_weight > 0:\n", " total_weight -= curr_item.weight\n", " curr_item = memo[(total_weight, len(items) - len(picked_items))].item\n", " return result\n", "\n", "\n", "def _knapsack_top_down_alt(items, total_weight, memo, index):\n", " if total_weight < 0:\n", " return Result(total_weight=0, item=None)\n", " if not total_weight or index >= len(items):\n", " return Result(total_weight=items[index - 1].value, item=items[index - 1])\n", " if (total_weight, len(items) - index - 1) in memo:\n", " weight=memo[(total_weight, \n", " len(items) - index - 1)].total_weight + items[index - 1].value\n", " return Result(total_weight=weight,\n", " item=items[index-1])\n", " results = []\n", " for i in range(index, len(items)):\n", " total_weight -= items[i].weight\n", " result = _knapsack_top_down_alt(items, total_weight, memo, index=i + 1)\n", " total_weight += items[i].weight\n", " results.append(result)\n", " results_index = 0\n", " for i in range(index, len(items)):\n", " memo[(total_weight, len(items) - i)] = max(results[results_index:])\n", " results_index += 1\n", " if index == 0:\n", " result_item = memo[(total_weight, len(items) - 1)].item\n", " else:\n", " result_item = items[index - 1]\n", " weight = max(results).total_weight + (items[index - 1].value if index > 0 else 0)\n", " return Result(total_weight=weight,\n", " item=result_item)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unit Test" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting test_knapsack.py\n" ] } ], "source": [ "%%writefile test_knapsack.py\n", "import unittest\n", "\n", "\n", "class TestKnapsack(unittest.TestCase):\n", "\n", " def test_knapsack_bottom_up(self):\n", " knapsack = Knapsack()\n", " self.assertRaises(TypeError, knapsack.fill_knapsack, None, None)\n", " self.assertEqual(knapsack.fill_knapsack(0, 0), 0)\n", " items = []\n", " items.append(Item(label='a', value=2, weight=2))\n", " items.append(Item(label='b', value=4, weight=2))\n", " items.append(Item(label='c', value=6, weight=4))\n", " items.append(Item(label='d', value=9, weight=5))\n", " total_weight = 8\n", " expected_value = 13\n", " results = knapsack.fill_knapsack(items, total_weight)\n", " self.assertEqual(results[0].label, 'd')\n", " self.assertEqual(results[1].label, 'b')\n", " total_value = 0\n", " for item in results:\n", " total_value += item.value\n", " self.assertEqual(total_value, expected_value)\n", " print('Success: test_knapsack_bottom_up')\n", "\n", " def test_knapsack_top_down(self):\n", " knapsack = KnapsackTopDown()\n", " self.assertRaises(TypeError, knapsack.fill_knapsack, None, None)\n", " self.assertEqual(knapsack.fill_knapsack(0, 0), 0)\n", " items = []\n", " items.append(Item(label='a', value=2, weight=2))\n", " items.append(Item(label='b', value=4, weight=2))\n", " items.append(Item(label='c', value=6, weight=4))\n", " items.append(Item(label='d', value=9, weight=5))\n", " total_weight = 8\n", " expected_value = 13\n", " self.assertEqual(knapsack.fill_knapsack(items, total_weight), expected_value)\n", " print('Success: test_knapsack_top_down')\n", "\n", "def main():\n", " test = TestKnapsack()\n", " test.test_knapsack_bottom_up()\n", " test.test_knapsack_top_down()\n", "\n", "\n", "if __name__ == '__main__':\n", " main()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success: test_knapsack_bottom_up\n", "Success: test_knapsack_top_down\n" ] } ], "source": [ "%run -i test_knapsack.py" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.2" } }, "nbformat": 4, "nbformat_minor": 1 }