{ "cells": [ { "cell_type": "code", "execution_count": 1, "source": [ "import sys\n", "sys.path.append('.')\n", "sys.path.append('..')\n", "from problem_loader import ProblemLoader\n", "from helpers import obfuscate\n", "from collections import namedtuple\n", "\n", "data_urls = {\n", " 'problem1': 'https://d18ky98rnyall9.cloudfront.net/_dcf1d02570e57d23ab526b1e33ba6f12_dijkstraData.txt?Expires=1623542400&Signature=Uw4b5BkLSsE2gX7900tYHPA-9oLQ1r1MiC76kkAVKkYiqjKsL26WwOzThW9xmES6dAilHGVEQ1oE3l2WDW8c9Qy8opwN799Dut-54931i1lZPkcQq96dDFWRLsgwAiWjN3u9X7MAQxJpDiI47YUyAhHjY5fyc6l5nhZZyeJ2UGA_&Key-Pair-Id=APKAJLTNE6QMUY6HBC5A'\n", "}" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "# Problem 1\n", "\n", "The file contains an adjacency list representation of an undirected weighted graph with 200 vertices labeled 1 to 200. Each row consists of the node tuples that are adjacent to that particular vertex along with the length of that edge. For example, the 6th row has `6` as the first entry indicating that this row corresponds to the vertex labeled $6$. The next entry of this row `\"141,8200\"` indicates that there is an edge between $vertex_6$ and $vertex_{141}$ that has length $8200$. The rest of the pairs of this row indicate the other vertices adjacent to $vertex_6$ and the lengths of the corresponding edges.\n", "\n", "Your task is to run Dijkstra's shortest-path algorithm on this graph, using $1$ (the first vertex) as the source vertex, and to compute the shortest-path distances between 1 and every other vertex of the graph. If there is no path between a $vertex_v$ and $vertex_1$, we'll define the shortest-path distance between $1$ and $v$ to be $1000000$. \n", "\n", "You should report the shortest-path distances to the following ten vertices, in order: \n", "$$7,37,59,82,99,115,133,165,188,197$$ \n", "You should encode the distances as a comma-separated string of integers. So if you find that all ten of these vertices except 115 are at distance 1000 away from vertex 1 and 115 is 2000 distance away, then your answer should be \n", "$$1000,1000,1000,1000,1000,2000,1000,1000,1000,1000$$ \n", "Remember the order of reporting DOES MATTER, and the string should be in the same order in which the above ten vertices are given. The string should not contain any spaces. Please type your answer in the space provided.\n", "\n", "## IMPLEMENTATION NOTES: \n", "This graph is small enough that the straightforward $O(mn)$ time implementation of Dijkstra's algorithm should work fine. \n", "\n", "### OPTIONAL: \n", "For those of you seeking an additional challenge, try implementing the heap-based version. Note this requires a heap that supports deletions, and you'll probably need to maintain some kind of mapping between vertices and their positions in the heap.\n", "\n" ], "metadata": {} }, { "cell_type": "code", "execution_count": 2, "source": [ "from collections import defaultdict\n", "\n", "Edge = namedtuple('Edge', ['to', 'weight'])\n", "\n", "def process_weighted_adjacencies(data):\n", " res = defaultdict(list)\n", " for node in data.split(b'\\n'):\n", " if len(node):\n", " x, edges = tuple(node.decode('utf-8').strip().split('\\t', 1))\n", " for y in edges.split('\\t'):\n", " dest, w = y.split(',')\n", " res[int(x)].append(Edge(to=int(dest), weight=int(w)))\n", " return dict(res)\n" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 3, "source": [ "values = ProblemLoader(\n", " data_urls['problem1'], \n", " fname=\"edges.p\", \n", " preprocessor=process_weighted_adjacencies\n", ").fetch()\n", "print(values[2])" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[Edge(to=42, weight=1689), Edge(to=127, weight=9365), Edge(to=5, weight=8026), Edge(to=170, weight=9342), Edge(to=131, weight=7005), Edge(to=172, weight=1438), Edge(to=34, weight=315), Edge(to=30, weight=2455), Edge(to=26, weight=2328), Edge(to=6, weight=8847), Edge(to=11, weight=1873), Edge(to=17, weight=5409), Edge(to=157, weight=8643), Edge(to=159, weight=1397), Edge(to=142, weight=7731), Edge(to=182, weight=7908), Edge(to=93, weight=8177)]\n" ] } ], "metadata": {} }, { "cell_type": "code", "execution_count": 4, "source": [ "from dataclasses import dataclass, field\n", "from typing import Any\n", "from heapq import heappush, heappop, heapify\n", "\n", "@dataclass(order=True)\n", "class PrioritizedItem:\n", " priority: int\n", " item: Any=field(compare=False)\n", "\n", "class PriorityQueue():\n", " def __init__(self):\n", " self.pq = []\n", " self.entry_finder = {}\n", " \n", " def add_with_priority(self, task, priority=0):\n", " entry = PrioritizedItem(priority=priority, item=task)\n", " self.entry_finder[task] = entry\n", " heappush(self.pq, entry)\n", " \n", " def decrease_priority(self, task, priority=0):\n", " entry = PrioritizedItem(priority=priority, item=task)\n", " i = self.pq.index(self.entry_finder[task])\n", " del self.pq[i]\n", " self.pq.append(entry)\n", " heapify(self.pq)\n", " self.entry_finder[task] = entry\n", "\n", " def extract_min(self):\n", " while self.pq:\n", " task = heappop(self.pq)\n", " del self.entry_finder[task.item]\n", " return task.item\n", " raise KeyError('pop from an empty Z')\n" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 5, "source": [ "def dijkstra(graph={}, start=None, target=None, max_distance=1000000):\n", " \"\"\"find distances and paths in graph\"\"\"\n", " X,A,B,Q = preprocess_graph(graph, start, max_distance)\n", " \n", " while len(Q.pq) > 0:\n", " u = Q.extract_min() # Remove and return best vertex\n", " if u == target:\n", " return A[target], get_path(start=start, target=target, prev=B)\n", " X.append(u)\n", " for edge in list(filter(lambda x: x.to not in X, graph[u])): \n", " v = edge.to\n", " d = A[u] + edge.weight\n", " if d < A[v]:\n", " A[v] = d\n", " B[v] = u\n", " Q.decrease_priority(v, d)\n", "\n", "def preprocess_graph(graph, start, max_distance):\n", " \"\"\"build the initial dj variables for distance and pathing\"\"\"\n", " X = [] # vertices processed so far\n", " A = {} # computed shortest path distances\n", " B = {} # computed shortest paths\n", "\n", " A[start] = 0 \n", " Q = PriorityQueue()\n", " for key in graph.keys():\n", " v = int(key) \n", " if v != start:\n", " A[v] = max_distance # Unknown distance from source to v\n", " B[v] = None # Predecessor of v\n", " Q.add_with_priority(v, A[v])\n", "\n", " return X,A,B,Q\n", "\n", "\n", "def get_path(start, target, prev):\n", " \"\"\"traverse a dict of previous vertices to vertex to produce the path \n", " from start to target\"\"\"\n", " S = []\n", " u = target\n", " if (u in prev) and (prev[u] != None): # Do something only if the vertex is reachable\n", " while (u != None) and (u != start): # Construct the shortest path with a stack S\n", " S.insert(0, u) # Push the vertex onto the stack\n", " u = prev[u] if (u in prev) else None # Traverse from target to source\n", " return S\n" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 6, "source": [ "distances = []\n", "#for v in list(filter(lambda x: x != 1, list(values.keys()))):\n", "for v in [7,37,59,82,99,115,133,165,188,197]:\n", " d, p = dijkstra(values, 1, v)\n", " #if v == 59:\n", " # print('\\n'.join([str(v) + ': ' + str(values[v]) for v in p]))\n", " distances.append(str(d))\n", "\n", "obfuscate(','.join(distances))\n" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 7, "source": [ "### test case\n", "v2 = process_weighted_adjacencies(\"\"\"1\t2,3\t3,2\n", "2\t1,3\t4,3\t4,7\t6,1\n", "3\t1,2\t5,2\t6,2\t7,2\n", "4\t2,3\t2,7\t8,3\t7,7\n", "5\t3,2\t8,8\n", "6\t2,1\t3,2\n", "7\t3,2\t4,7\n", "8\t4,3\t5,8\"\"\".encode('utf-8'))\n", "dijkstra(v2, 1, 8)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(9, [2, 4, 8])" ] }, "metadata": {}, "execution_count": 7 } ], "metadata": {} } ], "metadata": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" }, "kernelspec": { "name": "python3", "display_name": "Python 3.9.1 64-bit" }, "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.9.1" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }