-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/cpge-itc/itc2
- Loading branch information
Showing
4 changed files
with
4 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# Programmation dynamique\n\n<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>", "\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe><iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>"]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": ["def knapsack(c, w, v):\n", " \"\"\" \n", " Renvoie la valeur maximum que l'on peut mettre\n", " dans un sac \u00e0 dos de capacit\u00e9 c.\n", " Le i\u00e8me objet a pour poids w[i] et valeur v[i].\n", " \"\"\"\n", " n = len(w) # nombre d'objets\n", " dp = [[0]*(n + 1) for i in range(c + 1)]\n", " # dp[i][j] = valeur max dans un sac de capacit\u00e9 i\n", " # o\u00f9 j est le nombre d'objets autoris\u00e9s\n", " for i in range(1, c + 1):\n", " for j in range(1, n + 1):\n", " if w[j - 1] <= i:\n", " x = v[j - 1] + dp[i - w[j - 1]][j - 1]\n", " dp[i][j] = max(dp[i][j - 1], x)\n", " else:\n", " dp[i][j] = dp[i][j - 1]\n", " return dp[c][n]"]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [{"data": {"text/plain": ["19"]}, "execution_count": 6, "metadata": {}, "output_type": "execute_result"}], "source": ["w = [2, 2, 2, 3, 5, 6, 8]\n", "v = [1, 1, 1, 7, 10, 10, 18]\n", "knapsack(10, w, v)"]}, {"cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [{"data": {"text/plain": ["19"]}, "execution_count": 38, "metadata": {}, "output_type": "execute_result"}], "source": ["def knapsack2(c, w, v):\n", " n = len(w)\n", " dp = [0]*(c + 1)\n", " for j in range(n):\n", " dp_ = dp[:] # copie de dp\n", " for i in range(c + 1):\n", " if w[j] <= i:\n", " dp[i] = max(dp_[i], v[j] + dp_[i - w[j]])\n", " return dp[-1]\n", "\n", "knapsack2(10, w, v)"]}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [{"ename": "NameError", "evalue": "name 'w' is not defined", "output_type": "error", "traceback": ["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [2], line 13\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[39mreturn\u001b[39;00m dp[(i, j)]\n\u001b[1;32m 11\u001b[0m \u001b[39mreturn\u001b[39;00m aux(c, \u001b[39mlen\u001b[39m(w))\n\u001b[0;32m---> 13\u001b[0m knapsack_memo(\u001b[39m10\u001b[39m, w, v)\n", "\u001b[0;31mNameError\u001b[0m: name 'w' is not defined"]}], "source": ["def knapsack_memo(c, w, v):\n", " dp = {}\n", " def aux(i, j):\n", " if i == 0 or j == 0:\n", " return 0\n", " if (i, j) not in dp:\n", " dp[(i, j)] = aux(i, j - 1)\n", " if w[j - 1] <= i:\n", " dp[(i, j)] = max(dp[(i, j)], v[j - 1] + aux(i - w[j - 1], j - 1))\n", " return dp[(i, j)]\n", " return aux(c, len(w))\n", "\n", "knapsack_memo(10, w, v)"]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [{"ename": "NameError", "evalue": "name 'w' is not defined", "output_type": "error", "traceback": ["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [1], line 14\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[39mreturn\u001b[39;00m v\n\u001b[1;32m 12\u001b[0m \u001b[39mreturn\u001b[39;00m aux(c, \u001b[39mlen\u001b[39m(w))\n\u001b[0;32m---> 14\u001b[0m knapsack_memo2(\u001b[39m10\u001b[39m, w, v)\n", "\u001b[0;31mNameError\u001b[0m: name 'w' is not defined"]}], "source": ["from functools import cache\n", "\n", "def knapsack_memo2(c, w, v):\n", " @cache\n", " def aux(i, j):\n", " if i == 0 or j == 0:\n", " return 0\n", " v = aux(i, j - 1)\n", " if w[j - 1] <= i:\n", " v = max(v, v[j - 1] + aux(i - w[j - 1], j - 1))\n", " return v\n", " return aux(c, len(w))\n", "\n", "knapsack_memo2(10, w, v)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Plus courts chemins\n", "\n", "$$M = \n", "\\begin{pmatrix}\n", "0 & 3 & 8 & \\infty & -4\\\\\n", "\\infty & 0 & \\infty & 1 & 7\\\\\n", "\\infty & 4 & 0 & \\infty & \\infty\\\\\n", "2 & \\infty & -5 & 0 & \\infty\\\\\n", "\\infty & \\infty & \\infty & 6 & 0\\\\\n", "\\end{pmatrix} \n", "$$"]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [{"data": {"text/plain": ["[[0, 3, 8, inf, -4],\n", " [inf, 0, inf, 1, 7],\n", " [inf, 4, 0, inf, inf],\n", " [2, inf, -5, 0, inf],\n", " [inf, inf, inf, 6, 0]]"]}, "execution_count": 8, "metadata": {}, "output_type": "execute_result"}], "source": ["g = [\n", " [0, 3, 8, float(\"inf\"), -4],\n", " [float(\"inf\"), 0, float(\"inf\"), 1, 7],\n", " [float(\"inf\"), 4, 0, float(\"inf\"), float(\"inf\")],\n", " [2, float(\"inf\"), -5, 0, float(\"inf\")],\n", " [float(\"inf\"), float(\"inf\"), float(\"inf\"), 6, 0]\n", "]\n", "\n", "g"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\n", "### Bellman-Ford"]}, {"cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": ["def bellman(g, r):\n", " n = len(g)\n", " dist = [float(\"inf\") for i in range(n)]\n", " dist[r] = 0\n", " for k in range(n - 2):\n", " for u in range(n):\n", " for v in range(len(g[u])):\n", " if g[u][v] != 0:\n", " dist[v] = min(dist[v], dist[u] + g[u][v])\n", " return dist"]}, {"cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [{"data": {"text/plain": ["[0, 1, -3, 2, -4]"]}, "execution_count": 10, "metadata": {}, "output_type": "execute_result"}], "source": ["bellman(g, 0)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Floyd-Warshall"]}, {"cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [{"data": {"text/plain": ["[[0, 1, -3, 2, -4],\n", " [3, 0, -4, 1, -1],\n", " [7, 4, 0, 5, 3],\n", " [2, -1, -5, 0, -2],\n", " [8, 5, 1, 6, 0]]"]}, "execution_count": 12, "metadata": {}, "output_type": "execute_result"}], "source": ["import copy\n", "\n", "def floydwarshall(g):\n", " n = len(g)\n", " dist = copy.deepcopy(g)\n", " for k in range(n):\n", " for u in range(n):\n", " for v in range(n):\n", " dist[u][v] = min(dist[u][v], dist[u][k] + dist[k][v])\n", " return dist\n", "\n", "floydwarshall(g)"]}], "metadata": {"kernelspec": {"display_name": "Python 3.10.6 64-bit", "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.10.6"}, "vscode": {"interpreter": {"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"}}}, "nbformat": 4, "nbformat_minor": 2} | ||
{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# Programmation dynamique\n\n<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>", "\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>\n", "<iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe><iframe src=https://mozilla.github.io/pdf.js/web/viewer.html?file=https://raw.githubusercontent.com/fortierq/cours/main/algo/prog_dyn/cours/prog_dyn.pdf#zoom=page-fit&pagemode=none height=500 width=100% allowfullscreen></iframe>"]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": ["def knapsack(c, w, v):\n", " \"\"\" \n", " Renvoie la valeur maximum que l'on peut mettre\n", " dans un sac \u00e0 dos de capacit\u00e9 c.\n", " Le i\u00e8me objet a pour poids w[i] et valeur v[i].\n", " \"\"\"\n", " n = len(w) # nombre d'objets\n", " dp = [[0]*(n + 1) for i in range(c + 1)]\n", " # dp[i][j] = valeur max dans un sac de capacit\u00e9 i\n", " # o\u00f9 j est le nombre d'objets autoris\u00e9s\n", " for i in range(1, c + 1):\n", " for j in range(1, n + 1):\n", " if w[j - 1] <= i:\n", " x = v[j - 1] + dp[i - w[j - 1]][j - 1]\n", " dp[i][j] = max(dp[i][j - 1], x)\n", " else:\n", " dp[i][j] = dp[i][j - 1]\n", " return dp[c][n]"]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [{"data": {"text/plain": ["19"]}, "execution_count": 6, "metadata": {}, "output_type": "execute_result"}], "source": ["w = [2, 2, 2, 3, 5, 6, 8]\n", "v = [1, 1, 1, 7, 10, 10, 18]\n", "knapsack(10, w, v)"]}, {"cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [{"data": {"text/plain": ["19"]}, "execution_count": 38, "metadata": {}, "output_type": "execute_result"}], "source": ["def knapsack2(c, w, v):\n", " n = len(w)\n", " dp = [0]*(c + 1)\n", " for j in range(n):\n", " dp_ = dp[:] # copie de dp\n", " for i in range(c + 1):\n", " if w[j] <= i:\n", " dp[i] = max(dp_[i], v[j] + dp_[i - w[j]])\n", " return dp[-1]\n", "\n", "knapsack2(10, w, v)"]}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [{"ename": "NameError", "evalue": "name 'w' is not defined", "output_type": "error", "traceback": ["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [2], line 13\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[39mreturn\u001b[39;00m dp[(i, j)]\n\u001b[1;32m 11\u001b[0m \u001b[39mreturn\u001b[39;00m aux(c, \u001b[39mlen\u001b[39m(w))\n\u001b[0;32m---> 13\u001b[0m knapsack_memo(\u001b[39m10\u001b[39m, w, v)\n", "\u001b[0;31mNameError\u001b[0m: name 'w' is not defined"]}], "source": ["def knapsack_memo(c, w, v):\n", " dp = {}\n", " def aux(i, j):\n", " if i == 0 or j == 0:\n", " return 0\n", " if (i, j) not in dp:\n", " dp[(i, j)] = aux(i, j - 1)\n", " if w[j - 1] <= i:\n", " dp[(i, j)] = max(dp[(i, j)], v[j - 1] + aux(i - w[j - 1], j - 1))\n", " return dp[(i, j)]\n", " return aux(c, len(w))\n", "\n", "knapsack_memo(10, w, v)"]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [{"ename": "NameError", "evalue": "name 'w' is not defined", "output_type": "error", "traceback": ["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [1], line 14\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[39mreturn\u001b[39;00m v\n\u001b[1;32m 12\u001b[0m \u001b[39mreturn\u001b[39;00m aux(c, \u001b[39mlen\u001b[39m(w))\n\u001b[0;32m---> 14\u001b[0m knapsack_memo2(\u001b[39m10\u001b[39m, w, v)\n", "\u001b[0;31mNameError\u001b[0m: name 'w' is not defined"]}], "source": ["from functools import cache\n", "\n", "def knapsack_memo2(c, w, v):\n", " @cache\n", " def aux(i, j):\n", " if i == 0 or j == 0:\n", " return 0\n", " v = aux(i, j - 1)\n", " if w[j - 1] <= i:\n", " v = max(v, v[j - 1] + aux(i - w[j - 1], j - 1))\n", " return v\n", " return aux(c, len(w))\n", "\n", "knapsack_memo2(10, w, v)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Plus courts chemins\n", "\n", "$$M = \n", "\\begin{pmatrix}\n", "0 & 3 & 8 & \\infty & -4\\\\\n", "\\infty & 0 & \\infty & 1 & 7\\\\\n", "\\infty & 4 & 0 & \\infty & \\infty\\\\\n", "2 & \\infty & -5 & 0 & \\infty\\\\\n", "\\infty & \\infty & \\infty & 6 & 0\\\\\n", "\\end{pmatrix} \n", "$$"]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [{"data": {"text/plain": ["[[0, 3, 8, inf, -4],\n", " [inf, 0, inf, 1, 7],\n", " [inf, 4, 0, inf, inf],\n", " [2, inf, -5, 0, inf],\n", " [inf, inf, inf, 6, 0]]"]}, "execution_count": 8, "metadata": {}, "output_type": "execute_result"}], "source": ["g = [\n", " [0, 3, 8, float(\"inf\"), -4],\n", " [float(\"inf\"), 0, float(\"inf\"), 1, 7],\n", " [float(\"inf\"), 4, 0, float(\"inf\"), float(\"inf\")],\n", " [2, float(\"inf\"), -5, 0, float(\"inf\")],\n", " [float(\"inf\"), float(\"inf\"), float(\"inf\"), 6, 0]\n", "]\n", "\n", "g"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\n", "### Bellman-Ford"]}, {"cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": ["def bellman(g, r):\n", " n = len(g)\n", " dist = [float(\"inf\") for i in range(n)]\n", " dist[r] = 0\n", " for k in range(n - 2):\n", " for u in range(n):\n", " for v in range(len(g[u])):\n", " if g[u][v] != 0:\n", " dist[v] = min(dist[v], dist[u] + g[u][v])\n", " return dist"]}, {"cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [{"data": {"text/plain": ["[0, 1, -3, 2, -4]"]}, "execution_count": 10, "metadata": {}, "output_type": "execute_result"}], "source": ["bellman(g, 0)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Floyd-Warshall"]}, {"cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [{"data": {"text/plain": ["[[0, 1, -3, 2, -4],\n", " [3, 0, -4, 1, -1],\n", " [7, 4, 0, 5, 3],\n", " [2, -1, -5, 0, -2],\n", " [8, 5, 1, 6, 0]]"]}, "execution_count": 12, "metadata": {}, "output_type": "execute_result"}], "source": ["import copy\n", "\n", "def floydwarshall(g):\n", " n = len(g)\n", " dist = copy.deepcopy(g)\n", " for k in range(n):\n", " for u in range(n):\n", " for v in range(n):\n", " dist[u][v] = min(dist[u][v], dist[u][k] + dist[k][v])\n", " return dist\n", "\n", "floydwarshall(g)"]}], "metadata": {"kernelspec": {"display_name": "Python 3.10.6 64-bit", "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.10.6"}, "vscode": {"interpreter": {"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"}}}, "nbformat": 4, "nbformat_minor": 2} |
Oops, something went wrong.