From 4a8bf985599324e5ff6b2e141d60868499b2f531 Mon Sep 17 00:00:00 2001 From: br0kenpixel <23280129+br0kenpixel@users.noreply.github.com> Date: Wed, 5 Mar 2025 10:44:02 +0100 Subject: [PATCH] =?UTF-8?q?grafy=20a=20preh=C4=BEad=C3=A1vanie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notebook/cviko3.ipynb | 560 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 560 insertions(+) create mode 100644 notebook/cviko3.ipynb diff --git a/notebook/cviko3.ipynb b/notebook/cviko3.ipynb new file mode 100644 index 0000000..40809b2 --- /dev/null +++ b/notebook/cviko3.ipynb @@ -0,0 +1,560 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Queue / Fronta" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "queue = [\"a\", \"b\", \"c\", \"d\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'d'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "queue.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "queue" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reprezentácia grafu" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from typing import Optional" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [], + "source": [ + "my_graph = {\n", + " 'A': ['B', 'C'],\n", + " 'B': ['A','D','E'],\n", + " 'C': ['A','F'],\n", + " 'D': ['B','G'],\n", + " 'E': ['B','G'],\n", + " 'F': ['C','G'],\n", + " 'G': ['D','E','F']\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['B', 'C']" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_graph[\"A\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "B\n", + "C\n" + ] + } + ], + "source": [ + "for neighbor in my_graph[\"A\"]:\n", + " print(neighbor)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [], + "source": [ + "start_node = \"A\"\n", + "goal_node = \"G\"" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [], + "source": [ + "def search(graph: dict[str, list[str]], start: str, goal: str) -> Optional[dict[str, str]]:\n", + " fifo = [start]\n", + " visited = {start: None}\n", + "\n", + " while fifo:\n", + " current_node = fifo.pop(0)\n", + "\n", + " if current_node == goal: \n", + " return visited\n", + " \n", + " for neighbor in graph[current_node]:\n", + " if neighbor not in visited:\n", + " visited[neighbor] = current_node\n", + " fifo.append(neighbor)\n", + "\n", + " return None" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'A': None, 'B': 'A', 'C': 'A', 'D': 'B', 'E': 'B', 'F': 'C', 'G': 'D'}" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "search(my_graph, start_node, goal_node)" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [], + "source": [ + "def search(graph: dict[str, list[str]], start: str, goal: str) -> Optional[dict[str, str]]:\n", + " fifo = [start]\n", + " visited = {start: None}\n", + "\n", + " while fifo:\n", + " current_node = fifo.pop(0)\n", + "\n", + " if current_node == goal: \n", + " return visited\n", + " \n", + " for neighbor in graph[current_node]:\n", + " if neighbor not in visited:\n", + " visited[neighbor] = current_node\n", + " print(visited)\n", + " fifo.append(neighbor)\n", + "\n", + " return None" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'A': None, 'B': 'A'}\n", + "{'A': None, 'B': 'A', 'C': 'A'}\n", + "{'A': None, 'B': 'A', 'C': 'A', 'D': 'B'}\n", + "{'A': None, 'B': 'A', 'C': 'A', 'D': 'B', 'E': 'B'}\n", + "{'A': None, 'B': 'A', 'C': 'A', 'D': 'B', 'E': 'B', 'F': 'C'}\n", + "{'A': None, 'B': 'A', 'C': 'A', 'D': 'B', 'E': 'B', 'F': 'C', 'G': 'D'}\n" + ] + } + ], + "source": [ + "result = search(my_graph, start_node, goal_node)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [], + "source": [ + "def reconstruct_path(visited: dict[str, str], start: str, goal: str) -> tuple[int, str]:\n", + " steps = 0\n", + " path = \"\"\n", + " current = goal\n", + "\n", + " while current is not None:\n", + " path = current + \" -> \" + path\n", + " current = visited[current]\n", + " steps += 1\n", + " \n", + " path = path.removesuffix(\" -> \")\n", + " return steps, path" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [], + "source": [ + "steps, path = reconstruct_path(result, start_node, goal_node)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "steps" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'A -> B -> D -> G'" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "path" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Prehľadávanie do šírky + nájdenie najkratšej cesty" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [], + "source": [ + "import heapq" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": {}, + "outputs": [], + "source": [ + "a = [3, 5, 1, 2, 6, 8, 7]" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [], + "source": [ + "heapq.heapify(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 5, 6, 8, 7]" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [], + "source": [ + "a = [2, 5, 3, 7, 6, 8]\n", + "heapq.heappush(a, 4)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 5, 3, 7, 6, 8, 4]" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "heapq.heappop(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "heapq.heappop(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Uniform cost search" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```mermaid\n", + "flowchart LR\n", + " A<-->|4|C;\n", + " A<-->|1|B;\n", + " B<-->|1|D;\n", + " B<-->|3|E;\n", + " D<-->|4|G;\n", + " E<-->|1|G;\n", + " C<-->|2|F;\n", + " G<-->|2|F;\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "outputs": [], + "source": [ + "my_graph = {\n", + " 'A': [('B', 1), ('C', 4)],\n", + " 'B': [('A',1), ('D', 1), ('E', 3)],\n", + " 'C': [('A', 4), ('F', 2)],\n", + " 'D': [('B', 1), ('G', 4)],\n", + " 'E': [('B', 3), ('G', 1)],\n", + " 'F': [('C', 2), ('G', 2)],\n", + " 'G': [('D', 4), ('E', 1), ('F', 2)]\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "outputs": [], + "source": [ + "def reconstruct_path(visited: dict[str, tuple[int, str]], start: str, goal: str) -> str:\n", + " path = []\n", + " current = goal\n", + " while current is not None:\n", + " path.append(current)\n", + " current = visited[current][1]\n", + " path.reverse()\n", + " return path" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [], + "source": [ + "def uniform_cost_search(graph: dict[str, list[tuple[str, int]]], start: str, goal: str) -> Optional[str]:\n", + " priority_queue = [(0, start)]\n", + " visited = {start: (0, None)}\n", + "\n", + " while priority_queue:\n", + " current_cost, current_node = heapq.heappop(priority_queue)\n", + "\n", + " if current_node == goal:\n", + " return current_cost, reconstruct_path(visited, start, goal)\n", + " \n", + " for neighbor, cost in graph[current_node]:\n", + " total_cost = current_cost + cost\n", + "\n", + " if neighbor not in visited or total_cost < visited[neighbor][0]:\n", + " visited[neighbor] = (total_cost, current_node)\n", + " heapq.heappush(priority_queue, (total_cost, neighbor))\n", + "\n", + " return None" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": {}, + "outputs": [], + "source": [ + "result = uniform_cost_search(my_graph, start_node, goal_node)" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Least cost path from A to G: A -> B -> E -> G with total cost 5\n" + ] + } + ], + "source": [ + "if result:\n", + " total_cost, path = result\n", + " print(f\"Least cost path from {start_node} to {goal_node}: {' -> '.join(path)} with total cost {total_cost}\")\n", + "else:\n", + " print(f\"No path found from {start_node} to {goal_node}\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "ui-cviko1", + "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.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}