From f3ec3df4aae25dd33eedd1759d3c5d34cc607ad8 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 15 Dec 2024 16:18:57 +0100 Subject: [PATCH] feat: :sparkles: Backup notebook --- graphs.ipynb | 464 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 443 insertions(+), 21 deletions(-) diff --git a/graphs.ipynb b/graphs.ipynb index 3b611b1..5f47c10 100644 --- a/graphs.ipynb +++ b/graphs.ipynb @@ -6,12 +6,14 @@ "metadata": { "collapsed": true, "ExecuteTime": { - "end_time": "2024-12-14T12:23:31.698030Z", - "start_time": "2024-12-14T12:23:31.686929Z" + "end_time": "2024-12-15T13:34:20.291013Z", + "start_time": "2024-12-15T13:34:20.279729Z" } }, "source": [ "import queue\n", + "from readline import get_current_history_length\n", + "from urllib.response import addinfourl\n", "\n", "# -*- coding: utf-8 -*-\n", "\"\"\"Graph module.\n", @@ -227,13 +229,13 @@ " fout.close()\n" ], "outputs": [], - "execution_count": 1 + "execution_count": 2 }, { "metadata": { "ExecuteTime": { - "end_time": "2024-12-14T12:23:31.740543Z", - "start_time": "2024-12-14T12:23:31.738488Z" + "end_time": "2024-12-15T13:34:20.415273Z", + "start_time": "2024-12-15T13:34:20.413571Z" } }, "cell_type": "code", @@ -245,8 +247,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-14T12:23:31.807702Z", - "start_time": "2024-12-14T12:23:31.804575Z" + "end_time": "2024-12-15T13:34:20.550666Z", + "start_time": "2024-12-15T13:34:20.548054Z" } }, "cell_type": "code", @@ -299,7 +301,7 @@ ], "id": "663b49e4f6763d11", "outputs": [], - "execution_count": 2 + "execution_count": 3 }, { "metadata": {}, @@ -310,8 +312,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-14T12:23:31.826260Z", - "start_time": "2024-12-14T12:23:31.824156Z" + "end_time": "2024-12-15T13:34:20.816394Z", + "start_time": "2024-12-15T13:34:20.814063Z" } }, "cell_type": "code", @@ -331,13 +333,13 @@ ], "id": "ca54f5a3f1a225b", "outputs": [], - "execution_count": 3 + "execution_count": 4 }, { "metadata": { "ExecuteTime": { - "end_time": "2024-12-14T12:23:31.853995Z", - "start_time": "2024-12-14T12:23:31.851762Z" + "end_time": "2024-12-15T13:34:20.992097Z", + "start_time": "2024-12-15T13:34:20.988967Z" } }, "cell_type": "code", @@ -358,7 +360,7 @@ ] } ], - "execution_count": 4 + "execution_count": 5 }, { "metadata": {}, @@ -369,8 +371,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-14T12:23:31.886996Z", - "start_time": "2024-12-14T12:23:31.884644Z" + "end_time": "2024-12-15T13:34:21.765995Z", + "start_time": "2024-12-15T13:34:21.763776Z" } }, "cell_type": "code", @@ -388,7 +390,7 @@ ], "id": "6920c423c0bdfc24", "outputs": [], - "execution_count": 5 + "execution_count": 6 }, { "metadata": {}, @@ -399,8 +401,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-12-14T12:23:31.913924Z", - "start_time": "2024-12-14T12:23:31.909906Z" + "end_time": "2024-12-15T13:34:22.368505Z", + "start_time": "2024-12-15T13:34:22.363049Z" } }, "cell_type": "code", @@ -421,12 +423,12 @@ "[4, 1, 5, 1, 3, 3, 4, 4, 3, 1]" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 6 + "execution_count": 7 }, { "metadata": { @@ -1046,6 +1048,426 @@ } ], "execution_count": 24 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "# 2024 - Exercise 3", + "id": "fc95daf0e8cb39e6" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-15T13:34:27.890752Z", + "start_time": "2024-12-15T13:34:27.886850Z" + } + }, + "cell_type": "code", + "source": [ + "# Reconstitution du graph du partiel\n", + "# Initialisation d'un graphe non orienté avec 10 sommets\n", + "G = Graph(order=12, directed=True)\n", + "\n", + "# Ajout des arêtes selon l'image fournie\n", + "G.addedge(0, 1)\n", + "G.addedge(0, 2)\n", + "G.addedge(0, 3)\n", + "G.addedge(0, 6)\n", + "G.addedge(1, 4)\n", + "G.addedge(2,4)\n", + "G.addedge(2, 5)\n", + "G.addedge(11, 5)\n", + "G.addedge(3, 6)\n", + "G.addedge(4, 7)\n", + "G.addedge(5, 8)\n", + "G.addedge(6, 9)\n", + "G.addedge(7, 10)\n", + "G.addedge(8, 7)\n", + "G.addedge(8,10)\n", + "G.addedge(9, 8)\n", + "G.addedge(9, 10)\n", + "\n", + "# Tri des listes d'adjacence pour une sortie cohérente\n", + "sortgraph(G)\n", + "\n", + "# Affichage du graphe au format DOT (si tu as graphviz configuré)\n", + "print(dot(G))\n", + "\n", + "# Affichage visuel dans Jupyter Notebook (optionnel)\n", + "# display(G, eng=\"neato\") # Utilise l'option \"neato\" pour une disposition automatique" + ], + "id": "9fa8bd36f36fbdde", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "digraph {\n", + " 0\n", + "0 -> 1\n", + "0 -> 2\n", + "0 -> 3\n", + "0 -> 6\n", + " 1\n", + "1 -> 4\n", + " 2\n", + "2 -> 4\n", + "2 -> 5\n", + " 3\n", + "3 -> 6\n", + " 4\n", + "4 -> 7\n", + " 5\n", + "5 -> 8\n", + " 6\n", + "6 -> 9\n", + " 7\n", + "7 -> 10\n", + " 8\n", + "8 -> 7\n", + "8 -> 10\n", + " 9\n", + "9 -> 8\n", + "9 -> 10\n", + " 10\n", + " 11\n", + "11 -> 5\n", + "}\n" + ] + } + ], + "execution_count": 8 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-15T13:49:16.391524Z", + "start_time": "2024-12-15T13:49:16.385573Z" + } + }, + "cell_type": "code", + "source": [ + "def __dist_range(G, M, src):\n", + " cur = Queue()\n", + " cur.enqueue(src)\n", + " d = 0\n", + " while not cur.isempty():\n", + " x = cur.dequeue()\n", + " if M[x] > d:\n", + " d += 1\n", + " for adj in G.adjlists[x]:\n", + " if M[adj] == None:\n", + " M[adj] = M[x] + 1\n", + " cur.enqueue(adj)\n", + "def dist_ragen(G, src, dmin, dmax):\n", + " M = [None] * G.order\n", + " result = []\n", + " M[src] = 0\n", + " __dist_range(G, M, src)\n", + " for i in range(len(M)):\n", + " if (M[i] != None) and dmin <= M[i] <= dmax:\n", + " result.append(i)\n", + " return result\n", + "\n", + "dist_ragen(G, 0, 2, 3)" + ], + "id": "40afbf22ad096b78", + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 5, 7, 8, 9, 10]" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 28 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-15T13:47:27.002306Z", + "start_time": "2024-12-15T13:47:26.997672Z" + } + }, + "cell_type": "code", + "source": [ + "def amaurypasbo(G, src, dmin, dmax):\n", + " q = Queue()\n", + " q.enqueue(src)\n", + " L = []\n", + " dist = [None] * G.order\n", + " dist[src] = 0\n", + " while not q.isempty():\n", + " x = q.dequeue()\n", + " if dist[x] >= dmin:\n", + " L.append(x)\n", + " if dist[x] < dmax:\n", + " for y in G.adjlists[x]:\n", + " if dist[y] is None:\n", + " dist[y] = dist[x] + 1\n", + " q.enqueue(y)\n", + " return L\n", + "amaurypasbo(G, 0, 2, 3)" + ], + "id": "6d6b88841d1c89e", + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 5, 9, 7, 8, 10]" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 27 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-15T13:50:09.757608Z", + "start_time": "2024-12-15T13:50:09.754120Z" + } + }, + "cell_type": "code", + "source": [ + "import time\n", + "start = time.time()\n", + "dist_ragen(G, 0, 2, 3)\n", + "end = time.time()\n", + "print(f\"Elapsed time for Louis: {end-start}ms\")\n", + "\n", + "start = time.time()\n", + "amaurypasbo(G, 0, 2, 3)\n", + "end = time.time()\n", + "print(f\"Elapsed time for Amaury pas bô: {end-start}ms\")" + ], + "id": "29ee33a593dd1896", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elapsed time for Louis: 5.5789947509765625e-05ms\n", + "Elapsed time for Amaury pas bô: 4.100799560546875e-05ms\n" + ] + } + ], + "execution_count": 29 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "# 2024 - Exercise 4", + "id": "5c9033d8eb1154a5" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-15T14:03:30.413537Z", + "start_time": "2024-12-15T14:03:30.410183Z" + } + }, + "cell_type": "code", + "source": [ + "# Reconstitution du graph du partiel\n", + "# Initialisation d'un graphe non orienté avec 10 sommets\n", + "G = Graph(order=10, directed=False)\n", + "\n", + "# Ajout des arêtes selon l'image fournie\n", + "G.addedge(0, 1)\n", + "G.addedge(0, 8)\n", + "G.addedge(1,3)\n", + "G.addedge(1,2)\n", + "G.addedge(2,3)\n", + "G.addedge(2,8)\n", + "G.addedge(8,6)\n", + "G.addedge(8,9)\n", + "G.addedge(3,4)\n", + "G.addedge(3,5)\n", + "G.addedge(3,6)\n", + "G.addedge(6,9)\n", + "G.addedge(9,7)\n", + "G.addedge(4,5)\n", + "G.addedge(5,7)\n", + "\n", + "# Tri des listes d'adjacence pour une sortie cohérente\n", + "sortgraph(G)\n", + "\n", + "# Affichage du graphe au format DOT (si tu as graphviz configuré)\n", + "print(dot(G))\n", + "\n", + "# Affichage visuel dans Jupyter Notebook (optionnel)\n", + "# display(G, eng=\"neato\") # Utilise l'option \"neato\" pour une disposition automatique" + ], + "id": "5cb5a950e355b0e2", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "graph {\n", + " 0\n", + "0 -- 1\n", + "0 -- 8\n", + " 1\n", + "1 -- 2\n", + "1 -- 3\n", + " 2\n", + "2 -- 3\n", + "2 -- 8\n", + " 3\n", + "3 -- 4\n", + "3 -- 5\n", + "3 -- 6\n", + " 4\n", + "4 -- 5\n", + " 5\n", + "5 -- 7\n", + " 6\n", + "6 -- 8\n", + "6 -- 9\n", + " 7\n", + "7 -- 9\n", + " 8\n", + "8 -- 9\n", + " 9\n", + "}\n" + ] + } + ], + "execution_count": 30 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-15T14:36:40.935741Z", + "start_time": "2024-12-15T14:36:40.929196Z" + } + }, + "cell_type": "code", + "source": [ + "def __get_cycle(G, x, parent):\n", + " for y in G.adjlists [x]:\n", + " if parent[y] == None:\n", + " parent [y] = x\n", + " get = __get_cycle(G, y, parent)\n", + " if get != None:\n", + " return get\n", + " else:\n", + " if y != parent [x]:\n", + " return (x, y)\n", + " return None\n", + "\n", + "def get_cycle(G):\n", + " parent = [None] * G. order\n", + " s = 0\n", + " get = None\n", + " while s < G.order and get == None:\n", + " if parent[s] == None:\n", + " parent[s] = -1\n", + " get = __get_cycle(G, s, parent)\n", + " s += 1\n", + " L = []\n", + " if get != None:\n", + " (x, y) = get\n", + " L = [x]\n", + " while x != y:\n", + " x = parent[x]\n", + " L.append(x)\n", + " L.append(L[0])\n", + " return L\n", + "\n", + "get_cycle(G)" + ], + "id": "d1736b826cb4a92", + "outputs": [ + { + "data": { + "text/plain": [ + "[3, 2, 1, 3]" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 53 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-12-15T14:14:21.270509Z", + "start_time": "2024-12-15T14:14:21.263983Z" + } + }, + "cell_type": "code", + "source": [ + "def __amaurycycle(G, src, M, L, P):\n", + " for y in G.adjlists[src]:\n", + " if M[y] == True and P[y] != src:\n", + " L.append(y)\n", + " return True\n", + " else:\n", + " L.append(y)\n", + " M[y] = True\n", + " P[y] = src\n", + " if __amaurycycle(G, y, M, L, P):\n", + " return True\n", + " return False\n", + "\n", + "def amaurycycle(G):\n", + " M = [False] * G.order\n", + " P = [None] * G.order\n", + " L = []\n", + " for x in range(G.order):\n", + " print(x)\n", + " if not M[x]:\n", + " M[x] = True\n", + "\n", + " if __amaurycycle(G, x, M, L, P):\n", + " return L\n", + " L = []\n", + " return L\n", + "\n", + "amaurycycle(G)\n" + ], + "id": "1ba0708151f432e2", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + }, + { + "data": { + "text/plain": [ + "[1, 0]" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 47 + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "", + "id": "d17cd6bd32656bbd" } ], "metadata": {