+ "sum(e.value for e in recursive_items)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Let's check that these two solutions include the same elements. Use the `backtrace` function to find when new items were added to the optimal order."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 78,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[Element(weight=301, value=134),\n",
+ " Element(weight=314, value=166),\n",
+ " Element(weight=320, value=154),\n",
+ " Element(weight=336, value=190),\n",
+ " Element(weight=337, value=140),\n",
+ " Element(weight=340, value=172),\n",
+ " Element(weight=353, value=191),\n",
+ " Element(weight=356, value=153),\n",
+ " Element(weight=359, value=171),\n",
+ " Element(weight=365, value=177),\n",
+ " Element(weight=381, value=166),\n",
+ " Element(weight=382, value=185),\n",
+ " Element(weight=414, value=189),\n",
+ " Element(weight=434, value=195)]"
+ ]
+ },
+ "execution_count": 78,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "dp_items = [element for (pre, post), element in zip(backtrace(vbr).items(), reversed(elements))\n",
+ " if pre[1] != post[1] ]\n",
+ "dp_items_fs = frozenset(\n",
+ " Element(weight=e['weight'], value=e['value']) for e in dp_items\n",
+ " )\n",
+ "sorted(dp_items_fs)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 79,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 79,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "dp_items_fs == recursive_items"