feat: Push notebook modification

This commit is contained in:
Louis Gallet 2024-12-02 19:45:21 +01:00
parent bca121e272
commit e403f9e88e
Signed by: lgallet
GPG Key ID: 84D3DF1528A84511
2 changed files with 172 additions and 5 deletions

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -11,6 +11,8 @@
} }
}, },
"source": [ "source": [
"import queue\n",
"\n",
"# -*- coding: utf-8 -*-\n", "# -*- coding: utf-8 -*-\n",
"\"\"\"Graph module.\n", "\"\"\"Graph module.\n",
"\n", "\n",
@ -380,6 +382,65 @@
"outputs": [], "outputs": [],
"execution_count": 3 "execution_count": 3
}, },
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-02T18:44:40.042481Z",
"start_time": "2024-12-02T18:44:40.039811Z"
}
},
"cell_type": "code",
"source": [
"# -*- coding: utf-8 -*-\n",
"\"\"\"Queue module.\"\"\"\n",
"\n",
"from collections import deque\n",
"\n",
"class Queue:\n",
" \"\"\"Simple class for FIFO (first-in-first-out) container.\"\"\"\n",
"\n",
" def __init__(self):\n",
" \"\"\"Init queue.\"\"\"\n",
"\n",
" self.elements = deque()\n",
"\n",
" def enqueue(self, elt):\n",
" \"\"\"Add an element to the queue.\n",
"\n",
" Args:\n",
" elt (Any): Element to enqueue.\n",
"\n",
" \"\"\"\n",
"\n",
" self.elements.append(elt)\n",
"\n",
" def dequeue(self):\n",
" \"\"\"Remove and return next element from the queue.\n",
"\n",
" Returns:\n",
" Any: Element from the queue.\n",
"\n",
" Raises:\n",
" IndexError: If queue is empty.\n",
"\n",
" \"\"\"\n",
"\n",
" return self.elements.popleft()\n",
"\n",
" def isempty(self):\n",
" \"\"\"Check whether queue is empty.\n",
"\n",
" Returns:\n",
" bool: True if queue is empty, False otherwise.\n",
"\n",
" \"\"\"\n",
"\n",
" return len(self.elements) == 0\n"
],
"id": "663b49e4f6763d11",
"outputs": [],
"execution_count": 37
},
{ {
"metadata": {}, "metadata": {},
"cell_type": "markdown", "cell_type": "markdown",
@ -644,18 +705,118 @@
"id": "ebfdd54aefc25650" "id": "ebfdd54aefc25650"
}, },
{ {
"metadata": {}, "metadata": {
"ExecuteTime": {
"end_time": "2024-12-02T18:44:59.949967Z",
"start_time": "2024-12-02T18:44:59.927772Z"
}
},
"cell_type": "code", "cell_type": "code",
"outputs": [], "source": [
"execution_count": null, "def aux(G, M, src):\n",
"source": "", " cur = Queue()\n",
"id": "b8bf6413a1d63515" " cur.enqueue(src)\n",
" 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",
" M[adj] = True\n",
"\n",
"def bfs(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)"
],
"id": "b8bf6413a1d63515",
"outputs": [
{
"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"
]
}
],
"execution_count": 40
}, },
{ {
"metadata": {}, "metadata": {},
"cell_type": "markdown", "cell_type": "markdown",
"source": "# Exercise 2.2 (DFS)", "source": "# Exercise 2.2 (DFS)",
"id": "55b1247fe428aa0d" "id": "55b1247fe428aa0d"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-02T18:41:49.173387Z",
"start_time": "2024-12-02T18:41:49.149806Z"
}
},
"cell_type": "code",
"source": [
"def aux(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",
"\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",
" print()\n",
"\n",
"dfs(G)"
],
"id": "507b403020a6f941",
"outputs": [
{
"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"
]
}
],
"execution_count": 36
} }
], ],
"metadata": { "metadata": {