\n", "> Athens University of Economics and Business" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Dijkstra's Algorithm\n", "\n", "Dijkstra's Algorithm finds the shortest path from a given node to other nodes in the graph.\n", "\n", "It works by using a priority queue where it stores path distances.\n", "\n", "Initially all distances are set to $\\infty$ apart from the distance to the start, which is 0.\n", "\n", "Then, as long as there are elements in the queue, the algorithm takes off the minimum item from the queue, i.e., the node with the minimum distance, and relaxes the distance to its neighbors.\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "As an example, we will use a rectangular grid graph, which could represent a traffic grid.\n", "\n", "Such a graph is represented by the [traffic_grid_graph.txt](traffic_grid_graph.txt) file and looks as follows.\n", "\n", "The numbers correspond to weights. The nodes are the black bullet points, and we assume they are numbered from 0, starting from the top left and proceeding left-to-right, top-to-bottom.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The graph is undirected, so we'll use the following function to read it.\n", "\n", "The function reads a line, splits it in three parts, and interprets the first two as nodes and the third as weight." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "def read_graph(filename, directed=False):\n", " graph = {}\n", " with open(filename) as input_file:\n", " for line in input_file:\n", " parts = line.split()\n", " if len(parts) != 3:\n", " continue # not a valid line, ignore\n", " [n1, n2, w] = [ int (x) for x in parts ]\n", " if n1 not in graph:\n", " graph[n1] = []\n", " if n2 not in graph:\n", " graph[n2] = []\n", " graph[n1].append((n2, w))\n", " if not directed:\n", " graph[n2].append((n1, w))\n", " return graph" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Now we can read the grid graph." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0: [(1, 3), (4, 5)],\n", " 1: [(0, 3), (5, 9), (2, 1)],\n", " 2: [(1, 1), (6, 2), (3, 4)],\n", " 3: [(2, 4), (7, 6)],\n", " 4: [(0, 5), (5, 3), (8, 7)],\n", " 5: [(1, 9), (4, 3), (9, 9), (6, 5)],\n", " 6: [(2, 2), (5, 5), (10, 3), (7, 8)],\n", " 7: [(3, 6), (6, 8), (11, 2)],\n", " 8: [(4, 7), (12, 6), (9, 8)],\n", " 9: [(5, 9), (8, 8), (13, 4), (10, 4)],\n", " 10: [(6, 3), (9, 4), (14, 3), (11, 6)],\n", " 11: [(7, 2), (10, 6), (15, 3)],\n", " 12: [(8, 6), (13, 3)],\n", " 13: [(9, 4), (12, 3), (14, 2)],\n", " 14: [(10, 3), (13, 2), (15, 7)],\n", " 15: [(11, 3), (14, 7)]}\n" ] } ], "source": [ "import pprint\n", "g = read_graph(\"traffic_grid_graph.txt\")\n", "pprint.pprint(g)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "As we mentioned, Dijkstra's algorithm requires a priority queue.\n", "\n", "We can use a very simple implementation: just a list.\n", "\n", "In that case, we need a way to find the minimum element in the queue, that is, the list." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "To find the position of the minimum in a list we could use the following function.\n", "\n", "We use `sys.maxsize`, the maximum integer, as a substitute for $\\infty$.\n", "\n", "In Python there is no predefined maximum integer.\n", "\n", "`sys.maxsize` gives the maximum 32 bit or 64 bit integer, depending on the machine.\n", "\n", "We assume that the distances we are dealing with are less than that; but if not, we can simply use a bigger value standing for $\\infty$." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "import sys\n", "\n", "MAX_INT = sys.maxsize\n", "\n", "def find_min(a):\n", " min_index = -1\n", " min_val = MAX_INT\n", " for (i, v) in enumerate(a):\n", " if v < min_val:\n", " min_index = i\n", " min_val = v\n", " return min_index" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "How fast is `find_min(a)`?\n", "\n", "In the worst case it will go down the whole list, so for a list of $n$ elements, it is $O(n)$.\n", "\n", "Let's see how it performs in a random list of 10,000 elements.\n", "\n", "We'll create 1000 random lists and find the minimum in each of them, averaging the time required." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.004783692079999927 seconds\n" ] } ], "source": [ "import random\n", "import timeit\n", "\n", "total_elapsed = 0\n", "for i in range(1000):\n", " a = list(range(10000))\n", " # Put the elements of the list in random order\n", " random.shuffle(a)\n", " start = timeit.default_timer()\n", " min_index = find_min(a)\n", " end = timeit.default_timer()\n", " total_elapsed += end - start\n", "\n", "print(total_elapsed / 100, \"seconds\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Alternatively, we could use following one-liner to find the index of the minimum element of a list:\n", "```python\n", "a.index(min(a))\n", "```\n", "\n", "This requires two passes from the list, one to find the minimum element and then a second to find its index.\n", "\n", "The total time is still $O(n + n) = O(2n) = O(n)$.\n", "\n", "How does it compare to `find_min(a)` in practice?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0021569067499999763 seconds\n" ] } ], "source": [ "total_elapsed = 0\n", "for i in range(1000):\n", " a = list(range(10000))\n", " random.shuffle(a)\n", " start = timeit.default_timer()\n", " min_index = a.index(min(a))\n", " end = timeit.default_timer()\n", " total_elapsed += end - start\n", " \n", "print(total_elapsed / 100, \"seconds\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We see that it takes less than half the time!\n", "\n", "That is because functions `min()` and `list.index()` are implemented much more efficiently than our handcrafted `find_min(a)`.\n", "\n", "In fact, they are implemented in C, which is called by Python, which explains the big speed difference.\n", "\n", "As a general lesson: be sure to check whether an optimized implementation for what you want to do already exists." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can now implement Dijkstra's algorithm using this priority queue implementation." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "def dijkstra(g, s):\n", " nodes = g.keys()\n", " num_nodes = len(nodes)\n", " # Initialize array holding path lengths.\n", " dist = [ MAX_INT ] * num_nodes\n", " dist[s] = 0\n", " # Initialize array holding node predecessors.\n", " pred = [ -1 ] * num_nodes\n", " # Initialize the priority queue; initially it \n", " # is just the same with the distance array.\n", " pq = dist[:]\n", " elements_in_queue = num_nodes\n", "\n", " while elements_in_queue != 0:\n", " u = pq.index(min(pq))\n", " pq[u] = MAX_INT\n", " elements_in_queue -= 1\n", " for (v, w) in g[u]:\n", " # If a better path is found,\n", " # relax the distance and update\n", " # the priority queue.\n", " if dist[u] != MAX_INT and dist[v] > dist[u] + w:\n", " dist[v] = dist[u] + w\n", " pred[v] = u\n", " pq[v] = dist[v]\n", "\n", " return (pred, dist)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Using it on the traffic grid graph, we get:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pred [-1, 0, 1, 2, 0, 4, 2, 6, 4, 10, 6, 10, 13, 14, 10, 11]\n", "dist [0, 3, 4, 8, 5, 8, 6, 14, 12, 13, 9, 15, 17, 14, 12, 18]\n" ] } ], "source": [ "pred, dist = dijkstra(g, 0)\n", "print('pred', pred)\n", "print('dist', dist)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "So we see that the path to node 15 (bottom right) has length 18. \n", "\n", "The previous node is node 11, the node before 11 is 10, and so on.\n", "\n", "We can roll out our own function to show us the complete path and save us the trouble of doing it manually." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "path to 15 = [0, 1, 2, 6, 10, 11, 15] length = 18\n", "path to 12 = [0, 1, 2, 6, 10, 14, 13, 12] length = 17\n" ] } ], "source": [ "def get_path(pred, t):\n", " path = []\n", " while t != -1:\n", " path.append(t)\n", " t = pred[t]\n", " # Path is constructed end to start,\n", " # so we need to reverse it.\n", " path.reverse()\n", " return path\n", "\n", "print('path to 15 =', get_path(pred, 15), 'length =', dist[15])\n", "print('path to 12 = ', get_path(pred, 12), 'length =', dist[12])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Note that we are constructing the path by adding to the end of the list; so we need to reverse the list at the end to get it in the right order.\n", "\n", "That is because inserting in the start of a list in Python with `insert(0, v)` incurs $O(n)$ memory movement costs.\n", "\n", "We can indeed see for ourselves the relative costs, by creating a list with $10^5$ elements. Note that creating a list of $10^6$ elements with `insert(0, v)` may take too long, as the situation can deteriorate badly." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.019321988779999993 seconds\n" ] } ], "source": [ "total_elapsed = 0\n", "a = []\n", "start = timeit.default_timer()\n", "for i in range(int(1e5)):\n", " a.insert(0, i)\n", "end = timeit.default_timer()\n", "total_elapsed += end - start\n", " \n", "print(total_elapsed / 100, \"seconds\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.00015370600999998986 seconds\n" ] } ], "source": [ "total_elapsed = 0\n", "a = []\n", "start = timeit.default_timer()\n", "for i in range(int(1e5)):\n", " a.append(i)\n", "a = a[::-1]\n", "end = timeit.default_timer()\n", "total_elapsed += end - start\n", " \n", "print(total_elapsed / 100, \"seconds\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Dijkstra's algorithm is fast and easy to implement.\n", "\n", "However, it does not produce the right results on graphs that contain negative weigths.\n", "\n", "Consider for example the following graph, stored in [negative_weights_graph.txt](negative_weights_graph.txt):\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "If we read it and run Dijkstra's algorithm on it, we'll get:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pred [-1, 0, 1, 0]\n", "dist [0, 5, 1, 3]\n", "[0, 3]\n" ] } ], "source": [ "g = read_graph('negative_weights_graph.txt', directed=True)\n", "pred, dist = dijkstra(g, 0)\n", "print('pred', pred)\n", "print('dist', dist)\n", "print(get_path(pred, 3))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We see that it fails to find the correct path from 0 to 3, which goes through nodes 1 and 2, and reports just the direct path from node 0 instead.\n", "\n", "Reweighting the graph will not solve the problem.\n", "\n", "For example, if we add 4 to all weights on the previous graph we get:\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "But we still get wrong results:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pred [-1, 0, 0, 0]\n", "dist [0, 9, 8, 7]\n", "[0, 3]\n" ] } ], "source": [ "g = read_graph('re_weighted_graph.txt', directed=True)\n", "pred, dist = dijkstra(g, 0)\n", "print('pred', pred)\n", "print('dist', dist)\n", "print(get_path(pred, 3))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## All Pairs Shortest Paths\n", "\n", "Dijkstra's algorighm finds the shortest paths from one node to all others.\n", "\n", "If we run it from every node of a graph, it will find the shortest path between all pairs of nodes.\n", "\n", "This is the *all pairs shortest paths* problem." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "To implement the all pairs shortest paths problem, we'll call Dijkstra's algorithm once for each node, storing the results of each call.\n", "\n", "To hold all the results together, we'll use two lists, `preds` and `dists`, each one of which will contain the all the `pred` and `dist` lists from each call.\n", "\n", "The `preds` and `dists` lists will start out empty, and we will add to them each `pred` and `dist` list as it comes out from Dijkstra's algorithm." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "def all_pairs_shortest_paths(g):\n", " preds = [ ]\n", " dists = [ ]\n", " # g.keys() does not guarantee a specific\n", " # order for the keys, so we call sorted().\n", " for s in sorted(g.keys()):\n", " pred, dist = dijkstra(g, s)\n", " preds.append(pred)\n", " dists.append(dist)\n", " return (preds, dists)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Let's apply it to the traffic grid graph." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "starting node: 0\n", "[-1, 0, 1, 2, 0, 4, 2, 6, 4, 10, 6, 10, 13, 14, 10, 11]\n", "[0, 3, 4, 8, 5, 8, 6, 14, 12, 13, 9, 15, 17, 14, 12, 18]\n", "starting node: 1\n", "[1, -1, 1, 2, 0, 6, 2, 6, 4, 10, 6, 10, 13, 14, 10, 11]\n", "[3, 0, 1, 5, 8, 8, 3, 11, 15, 10, 6, 12, 14, 11, 9, 15]\n", "starting node: 2\n", "[1, 2, -1, 2, 0, 6, 2, 6, 4, 10, 6, 10, 13, 14, 10, 11]\n", "[4, 1, 0, 4, 9, 7, 2, 10, 16, 9, 5, 11, 13, 10, 8, 14]\n", "starting node: 3\n", "[1, 2, 3, -1, 0, 6, 2, 3, 4, 10, 6, 7, 13, 14, 10, 11]\n", "[8, 5, 4, 0, 13, 11, 6, 6, 20, 13, 9, 8, 17, 14, 12, 11]\n", "starting node: 4\n", "[4, 0, 1, 2, -1, 4, 5, 6, 4, 5, 6, 10, 8, 9, 10, 11]\n", "[5, 8, 9, 13, 0, 3, 8, 16, 7, 12, 11, 17, 13, 16, 14, 20]\n", "starting node: 5\n", "[4, 2, 6, 2, 5, -1, 5, 6, 4, 5, 6, 10, 8, 9, 10, 11]\n", "[8, 8, 7, 11, 3, 0, 5, 13, 10, 9, 8, 14, 16, 13, 11, 17]\n", "starting node: 6\n", "[1, 2, 6, 2, 5, 6, -1, 6, 9, 10, 6, 10, 13, 14, 10, 11]\n", "[6, 3, 2, 6, 8, 5, 0, 8, 15, 7, 3, 9, 11, 8, 6, 12]\n", "starting node: 7\n", "[1, 2, 3, 7, 5, 6, 7, -1, 9, 10, 11, 7, 13, 14, 10, 11]\n", "[14, 11, 10, 6, 16, 13, 8, 0, 20, 12, 8, 2, 16, 13, 11, 5]\n", "starting node: 8\n", "[4, 0, 1, 2, 8, 4, 5, 11, -1, 8, 9, 10, 8, 12, 13, 14]\n", "[12, 15, 16, 20, 7, 10, 15, 20, 0, 8, 12, 18, 6, 9, 11, 18]\n", "starting node: 9\n", "[1, 2, 6, 2, 5, 9, 10, 11, 9, -1, 9, 10, 13, 9, 13, 14]\n", "[13, 10, 9, 13, 12, 9, 7, 12, 8, 0, 4, 10, 7, 4, 6, 13]\n", "starting node: 10\n", "[1, 2, 6, 2, 5, 6, 10, 11, 9, 10, -1, 10, 13, 14, 10, 11]\n", "[9, 6, 5, 9, 11, 8, 3, 8, 12, 4, 0, 6, 8, 5, 3, 9]\n", "starting node: 11\n", "[1, 2, 6, 7, 5, 6, 10, 11, 9, 10, 11, -1, 13, 14, 10, 11]\n", "[15, 12, 11, 8, 17, 14, 9, 2, 18, 10, 6, 0, 14, 11, 9, 3]\n", "starting node: 12\n", "[1, 2, 6, 2, 8, 9, 10, 11, 12, 13, 14, 10, -1, 12, 13, 14]\n", "[17, 14, 13, 17, 13, 16, 11, 16, 6, 7, 8, 14, 0, 3, 5, 12]\n", "starting node: 13\n", "[1, 2, 6, 2, 8, 9, 10, 11, 12, 13, 14, 10, 13, -1, 13, 14]\n", "[14, 11, 10, 14, 16, 13, 8, 13, 9, 4, 5, 11, 3, 0, 2, 9]\n", "starting node: 14\n", "[1, 2, 6, 2, 5, 6, 10, 11, 12, 13, 14, 10, 13, 14, -1, 14]\n", "[12, 9, 8, 12, 14, 11, 6, 11, 11, 6, 3, 9, 5, 2, 0, 7]\n", "starting node: 15\n", "[1, 2, 6, 7, 5, 6, 10, 11, 12, 10, 11, 15, 13, 14, 15, -1]\n", "[18, 15, 14, 11, 20, 17, 12, 5, 18, 13, 9, 3, 12, 9, 7, 0]\n" ] } ], "source": [ "g = read_graph('traffic_grid_graph.txt')\n", "preds, dists = all_pairs_shortest_paths(g)\n", "for s in sorted(g.keys()):\n", " print('starting node:', s)\n", " print(preds[s])\n", " print(dists[s])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "With `all_pairs_shortest_paths(g)` we can find the *diameter* of the graph: the longest shortest path. \n", "\n", "In other words, the diameter of the graph is the longest distance between two nodes, if we walk along shortest paths. \n", "\n", "To find the diameter of a graph, we only need to search for the maximum possible distance in the `dists` list.\n", "\n", "The following function does that, while also keeping track of the start and end node of the diameter path." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "start: 3 end: 8 diameter: 20\n" ] } ], "source": [ "def graph_diameter(g, dists):\n", " diameter = 0 # minimum diameter\n", " max_s = -1 # start node of diameter path\n", " max_t = -1 # end node of diameter path\n", " # Do for all nodes in the graph.\n", " for s in sorted(g.keys()):\n", " # Get the corresponding dist list.\n", " dist = dists[s]\n", " # Get the index of the maximum distance;\n", " # that is also the node with the maximum\n", " # distance as nodes start from zero.\n", " max_index = dist.index(max(dist))\n", " # Get the maximum distance.\n", " max_distance = dist[max_index]\n", " # Update if necessary.\n", " if max_distance > diameter:\n", " diameter = max_distance\n", " max_s = s\n", " max_t = max_index\n", " return (max_s, max_t, diameter)\n", " \n", "s, t, diameter = graph_diameter(g, dists)\n", "print('start:', s, 'end:', t, 'diameter:', diameter)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Note that in this graph there is more than one path with length 20; `graph_diameter(g, dists)` just returns one of them.\n", "\n", "Also note that the graph is undirected, so the length of the path from 3 to 8 is the same with the length of the path from 8 to 3." ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 1 }