From 7be0ae6313e0fdecca3fb34aeac63433990a9dc6 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sat, 14 Dec 2024 12:16:58 +0100 Subject: [PATCH] feat: :sparkles: Push notebook --- .idea/.gitignore | 8 - .../inspectionProfiles/profiles_settings.xml | 6 - .idea/misc.xml | 6 - .idea/modules.xml | 8 - .idea/test.iml | 10 - .idea/vcs.xml | 6 - README.md | 3 + graphs.ipynb | 374 ++++++++++++++---- 8 files changed, 292 insertions(+), 129 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/test.iml delete mode 100644 .idea/vcs.xml create mode 100644 README.md diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 722330d..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 51ab974..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/test.iml b/.idea/test.iml deleted file mode 100644 index d92f935..0000000 --- a/.idea/test.iml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6dba70f --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Click on the button bellow to launch this notebook + +[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/git/https%3A%2F%2Fgitea.louisgallet.fr%2FEPITA-ALGO%2Fnotebook-revision-finals.git/master?labpath=graphs.ipynb) \ No newline at end of file diff --git a/graphs.ipynb b/graphs.ipynb index 3beb3ef..2a824b3 100644 --- a/graphs.ipynb +++ b/graphs.ipynb @@ -6,8 +6,8 @@ "metadata": { "collapsed": true, "ExecuteTime": { - "end_time": "2024-12-02T17:41:32.959657Z", - "start_time": "2024-12-02T17:41:32.951161Z" + "end_time": "2024-12-14T10:54:32.699159Z", + "start_time": "2024-12-14T10:54:32.688460Z" } }, "source": [ @@ -227,13 +227,13 @@ " fout.close()\n" ], "outputs": [], - "execution_count": 2 + "execution_count": 3 }, { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T17:41:47.620425Z", - "start_time": "2024-12-02T17:41:47.614349Z" + "end_time": "2024-12-14T10:54:33.640420Z", + "start_time": "2024-12-14T10:54:33.634885Z" } }, "cell_type": "code", @@ -380,13 +380,13 @@ ], "id": "e12f5903c5348752", "outputs": [], - "execution_count": 3 + "execution_count": 4 }, { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T18:44:40.042481Z", - "start_time": "2024-12-02T18:44:40.039811Z" + "end_time": "2024-12-14T10:54:34.476061Z", + "start_time": "2024-12-14T10:54:34.472348Z" } }, "cell_type": "code", @@ -439,7 +439,7 @@ ], "id": "663b49e4f6763d11", "outputs": [], - "execution_count": 37 + "execution_count": 5 }, { "metadata": {}, @@ -450,8 +450,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T17:44:49.997870Z", - "start_time": "2024-12-02T17:44:49.994811Z" + "end_time": "2024-12-14T10:54:38.976275Z", + "start_time": "2024-12-14T10:54:38.973873Z" } }, "cell_type": "code", @@ -471,13 +471,13 @@ ], "id": "ca54f5a3f1a225b", "outputs": [], - "execution_count": 9 + "execution_count": 6 }, { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T17:46:47.125622Z", - "start_time": "2024-12-02T17:46:47.122992Z" + "end_time": "2024-12-14T10:54:39.957846Z", + "start_time": "2024-12-14T10:54:39.955269Z" } }, "cell_type": "code", @@ -498,7 +498,7 @@ ] } ], - "execution_count": 16 + "execution_count": 7 }, { "metadata": {}, @@ -509,8 +509,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T17:57:16.222216Z", - "start_time": "2024-12-02T17:57:16.219422Z" + "end_time": "2024-12-14T10:54:41.253023Z", + "start_time": "2024-12-14T10:54:41.250277Z" } }, "cell_type": "code", @@ -528,7 +528,7 @@ ], "id": "6920c423c0bdfc24", "outputs": [], - "execution_count": 22 + "execution_count": 8 }, { "metadata": {}, @@ -539,8 +539,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T17:56:13.875386Z", - "start_time": "2024-12-02T17:56:13.870731Z" + "end_time": "2024-12-09T14:48:04.596974Z", + "start_time": "2024-12-09T14:48:04.592782Z" } }, "cell_type": "code", @@ -561,18 +561,18 @@ "[4, 1, 5, 1, 3, 3, 4, 4, 3, 1]" ] }, - "execution_count": 21, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 21 + "execution_count": 8 }, { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T18:17:29.691716Z", - "start_time": "2024-12-02T18:17:29.688234Z" + "end_time": "2024-12-09T14:48:05.120730Z", + "start_time": "2024-12-09T14:48:05.116518Z" } }, "cell_type": "code", @@ -596,12 +596,12 @@ "[[3, 4, 1, 2, 3, 2, 3, 0, 2], [6, 1, 1, 2, 0, 3, 2, 3, 2]]" ] }, - "execution_count": 24, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 24 + "execution_count": 9 }, { "metadata": {}, @@ -612,8 +612,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T18:22:10.123287Z", - "start_time": "2024-12-02T18:22:10.119678Z" + "end_time": "2024-12-09T14:48:06.156919Z", + "start_time": "2024-12-09T14:48:06.154020Z" } }, "cell_type": "code", @@ -696,7 +696,7 @@ ] } ], - "execution_count": 31 + "execution_count": 10 }, { "metadata": {}, @@ -707,8 +707,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T18:44:59.949967Z", - "start_time": "2024-12-02T18:44:59.927772Z" + "end_time": "2024-12-14T10:59:44.343779Z", + "start_time": "2024-12-14T10:59:44.337510Z" } }, "cell_type": "code", @@ -719,20 +719,20 @@ " M[src] = True\n", " while not cur.isempty():\n", " x = cur.dequeue()\n", - " print(x, end= \"\")\n", - " for adj in range(G.order):\n", - " if G.adjlists[x][adj] and not M[adj]:\n", - " cur.enqueue(adj)\n", + " print(x, end=\" \")\n", + " for adj in G.adjlists[x]:\n", + " if not M[adj]:\n", " M[adj] = True\n", + " cur.enqueue(adj)\n", "\n", - "def bfs(G):\n", - " M = [False] * G.order\n", + "\n", + "def dfs(G):\n", + " M = [False]*G.order\n", " for src in range(G.order):\n", " if not M[src]:\n", " aux(G, M, src)\n", " print()\n", - "\n", - "bfs(G)" + "BFS(G)" ], "id": "b8bf6413a1d63515", "outputs": [ @@ -740,24 +740,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "0" - ] - }, - { - "ename": "IndexError", - "evalue": "list index out of range", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mIndexError\u001B[0m Traceback (most recent call last)", - "Cell \u001B[0;32mIn[40], line 20\u001B[0m\n\u001B[1;32m 17\u001B[0m aux(G, M, src)\n\u001B[1;32m 18\u001B[0m \u001B[38;5;28mprint\u001B[39m()\n\u001B[0;32m---> 20\u001B[0m \u001B[43mbfs\u001B[49m\u001B[43m(\u001B[49m\u001B[43mG\u001B[49m\u001B[43m)\u001B[49m\n", - "Cell \u001B[0;32mIn[40], line 17\u001B[0m, in \u001B[0;36mbfs\u001B[0;34m(G)\u001B[0m\n\u001B[1;32m 15\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m src \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(G\u001B[38;5;241m.\u001B[39morder):\n\u001B[1;32m 16\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m M[src]:\n\u001B[0;32m---> 17\u001B[0m \u001B[43maux\u001B[49m\u001B[43m(\u001B[49m\u001B[43mG\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mM\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43msrc\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 18\u001B[0m \u001B[38;5;28mprint\u001B[39m()\n", - "Cell \u001B[0;32mIn[40], line 9\u001B[0m, in \u001B[0;36maux\u001B[0;34m(G, M, src)\u001B[0m\n\u001B[1;32m 7\u001B[0m \u001B[38;5;28mprint\u001B[39m(x, end\u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m 8\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m adj \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(G\u001B[38;5;241m.\u001B[39morder):\n\u001B[0;32m----> 9\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[43mG\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43madjlists\u001B[49m\u001B[43m[\u001B[49m\u001B[43mx\u001B[49m\u001B[43m]\u001B[49m\u001B[43m[\u001B[49m\u001B[43madj\u001B[49m\u001B[43m]\u001B[49m \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m M[adj]:\n\u001B[1;32m 10\u001B[0m cur\u001B[38;5;241m.\u001B[39menqueue(adj)\n\u001B[1;32m 11\u001B[0m M[adj] \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mTrue\u001B[39;00m\n", - "\u001B[0;31mIndexError\u001B[0m: list index out of range" + "0 4 1 3 9 \n", + "2 6 5 7 8 \n" ] } ], - "execution_count": 40 + "execution_count": 20 }, { "metadata": {}, @@ -768,28 +756,26 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-02T18:41:49.173387Z", - "start_time": "2024-12-02T18:41:49.149806Z" + "end_time": "2024-12-14T11:01:46.103932Z", + "start_time": "2024-12-14T11:01:46.101045Z" } }, "cell_type": "code", "source": [ - "def aux(G, M, src):\n", + "def auxDFS(G, M, src):\n", " M[src] = True\n", - " print(src)\n", - " for i in range(G.order):\n", - " if G.adjlists[src][i] and not M[i]:\n", - " aux(G, M, i)\n", - "\n", + " print(src, end=\" \")\n", + " for adj in G.adjlists[src]:\n", + " if not M[adj]:\n", + " auxDFS(G, M, adj)\n", "\n", "def DFS(G):\n", - " M = [False]*G.order\n", - " for element in range(G.order):\n", - " if not M[element]:\n", - " aux(G, M, element)\n", + " M = [False] * G.order\n", + " for src in range(G.order):\n", + " if not M[src]:\n", + " aux(G, M, src)\n", " print()\n", - "\n", - "dfs(G)" + "DFS(G)" ], "id": "507b403020a6f941", "outputs": [ @@ -797,26 +783,244 @@ "name": "stdout", "output_type": "stream", "text": [ - "0\n", - "1\n" - ] - }, - { - "ename": "IndexError", - "evalue": "list index out of range", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mIndexError\u001B[0m Traceback (most recent call last)", - "Cell \u001B[0;32mIn[36], line 16\u001B[0m\n\u001B[1;32m 13\u001B[0m aux(G, M, element)\n\u001B[1;32m 14\u001B[0m \u001B[38;5;28mprint\u001B[39m()\n\u001B[0;32m---> 16\u001B[0m \u001B[43mdfs\u001B[49m\u001B[43m(\u001B[49m\u001B[43mG\u001B[49m\u001B[43m)\u001B[49m\n", - "Cell \u001B[0;32mIn[34], line 12\u001B[0m, in \u001B[0;36mdfs\u001B[0;34m(G)\u001B[0m\n\u001B[1;32m 10\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m src \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(G\u001B[38;5;241m.\u001B[39morder):\n\u001B[1;32m 11\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m M[src]:\n\u001B[0;32m---> 12\u001B[0m \u001B[43maux\u001B[49m\u001B[43m(\u001B[49m\u001B[43mG\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mM\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43msrc\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 13\u001B[0m \u001B[38;5;28mprint\u001B[39m()\n", - "Cell \u001B[0;32mIn[36], line 6\u001B[0m, in \u001B[0;36maux\u001B[0;34m(G, M, src)\u001B[0m\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m i \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(G\u001B[38;5;241m.\u001B[39morder):\n\u001B[1;32m 5\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m G\u001B[38;5;241m.\u001B[39madjlists[src][i] \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m M[i]:\n\u001B[0;32m----> 6\u001B[0m \u001B[43maux\u001B[49m\u001B[43m(\u001B[49m\u001B[43mG\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mM\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mi\u001B[49m\u001B[43m)\u001B[49m\n", - "Cell \u001B[0;32mIn[36], line 5\u001B[0m, in \u001B[0;36maux\u001B[0;34m(G, M, src)\u001B[0m\n\u001B[1;32m 3\u001B[0m \u001B[38;5;28mprint\u001B[39m(src)\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m i \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(G\u001B[38;5;241m.\u001B[39morder):\n\u001B[0;32m----> 5\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[43mG\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43madjlists\u001B[49m\u001B[43m[\u001B[49m\u001B[43msrc\u001B[49m\u001B[43m]\u001B[49m\u001B[43m[\u001B[49m\u001B[43mi\u001B[49m\u001B[43m]\u001B[49m \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m M[i]:\n\u001B[1;32m 6\u001B[0m aux(G, M, i)\n", - "\u001B[0;31mIndexError\u001B[0m: list index out of range" + "0 4 1 3 9 \n", + "2 6 5 7 8 \n" ] } ], - "execution_count": 36 + "execution_count": 23 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "# Exercise 3.1 (Connect)", + "id": "a30c671c43131bb2" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-09T15:03:14.627181Z", + "start_time": "2024-12-09T15:03:14.622512Z" + } + }, + "cell_type": "code", + "source": [ + "def __components(G, M, src, nbComponents):\n", + " M[src] = nbComponents\n", + " for adj in G.adjlists[src]:\n", + " if not M[adj]:\n", + " M[adj] = nbComponents\n", + " __components(G, M, adj, nbComponents)\n", + "\n", + "def components(G):\n", + " M = [False] * G.order\n", + " nbComponents = 0\n", + " for src in range(G.order):\n", + " if not M[src]:\n", + " nbComponents += 1\n", + " __components(G, M, src, nbComponents)\n", + " return (nbComponents, M)\n", + "\n", + "components(G)\n" + ], + "id": "b8b8a6ec16fc88a5", + "outputs": [ + { + "data": { + "text/plain": [ + "(2, [1, 1, 2, 1, 1, 2, 2, 2, 2, 1])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 16 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "# Exercise 3.2 (That's the way)", + "id": "dae9cdccb36ac9d5" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-09T15:09:02.024738Z", + "start_time": "2024-12-09T15:09:02.020400Z" + } + }, + "cell_type": "code", + "source": [ + "def __path(G, src, dst, M, path):\n", + " M[src] = True\n", + " for adj in G.adjlists[src]:\n", + " if not M[src]:\n", + " if adj == dst or __path(G, src, dst, M, path):\n", + " path.append(adj)\n", + " return True\n", + " return False\n", + "\n", + "def path(G, src, dst):\n", + " M = [False] * G.order\n", + " path = []\n", + " if __path(G, src, dst, M, path):\n", + " path.append(src)\n", + " path.reverse()\n", + " return path\n", + "\n", + "path(G, 0, 9)" + ], + "id": "67b1808a9c9996e1", + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 26 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "", + "id": "9b679ab534f329fb" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "", + "id": "71f4c39123fe94c1" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "# 2025 - Exercise 1", + "id": "b67ef07f6d7c99b0" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-09T15:48:15.121203Z", + "start_time": "2024-12-09T15:48:15.116918Z" + } + }, + "cell_type": "code", + "source": [ + "def aux(G, M, src):\n", + " newColor = not M[src]\n", + " for adj in G.adjlists[src]:\n", + " if M[adj] == None:\n", + " M[adj] = newColor\n", + " if not aux(G, M, adj):\n", + " return False\n", + " else:\n", + " if M[adj] != newColor:\n", + " return False\n", + " return True\n", + "\n", + "\n", + "def two_coloring(G):\n", + " M = [None] * G.order\n", + " for src in range(G.order):\n", + " if M[src] == None:\n", + " M[src] = True\n", + " if not aux(G, M, src):\n", + " return False\n", + " return True\n", + "\n", + "two_coloring(G1)" + ], + "id": "9ed534351df71362", + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 32 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "# 2025 - Exercise 2", + "id": "38c3d4e952147c50" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-09T16:08:14.367172Z", + "start_time": "2024-12-09T16:08:14.361769Z" + } + }, + "cell_type": "code", + "source": [ + "def __fakenews(G, M, src, truth):\n", + " cur = Queue()\n", + " cur.enqueue(src)\n", + " stop = False\n", + " while not cur.isempty() and not stop:\n", + " x = cur.dequeue()\n", + " if M[x] > truth + 1:\n", + " for adj in G.adjlists[x]:\n", + " if M[adj] == None:\n", + " M[adj] = M[x] - 1\n", + " cur.enqueue(adj)\n", + " else:\n", + " stop = True\n", + "\n", + "def fakenews(G, src, truth):\n", + " if (truth <= 2):\n", + " raise Exception(\"truth should be > 2\")\n", + " M = [False] * G.order\n", + " M[src] = truth\n", + " fake = []\n", + " __fakenews(G, M, src, truth // 2)\n", + " for i in range(G.order):\n", + " if M[i] == None:\n", + " fake.append(i)\n", + " return truth" + ], + "id": "c66ac4688a6975c9", + "outputs": [], + "execution_count": 34 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "# 2027 - Exercice 1", + "id": "b8fa23568b4bd3df" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": [ + "def __longest_path(G, dist, src):\n", + "\n", + "\n", + "def longest_path(G):\n", + " dist = [None] * G.order\n", + " for src in range(G.order):\n", + " __longest_path(G, dist, src)" + ], + "id": "9a3a85d15dccbb97" } ], "metadata": {