{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Challenge Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem: Find the shortest path between two nodes in a graph.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", "* [Algorithm](#Algorithm)\n", "* [Code](#Code)\n", "* [Unit Test](#Unit-Test)\n", "* [Solution Notebook](#Solution-Notebook)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constraints\n", "\n", "* Is this a directional graph?\n", " * Yes\n", "* Could the graph have cycles?\n", " * Yes\n", " * Note: If the answer were no, this would be a DAG. \n", " * DAGs can be solved with a [topological sort](http://www.geeksforgeeks.org/shortest-path-for-directed-acyclic-graphs/)\n", "* Are the edges weighted?\n", " * Yes\n", " * Note: If the edges were not weighted, we could do a BFS\n", "* Are the edges all non-negative numbers?\n", " * Yes\n", " * Note: Graphs with negative edges can be done with Bellman-Ford\n", " * Graphs with negative cost cycles do not have a defined shortest path\n", "* Do we have to check for non-negative edges?\n", " * No\n", "* Can we assume this is a connected graph?\n", " * Yes\n", "* Can we assume the inputs are valid?\n", " * No\n", "* Can we assume we already have a graph class?\n", " * Yes\n", "* Can we assume we already have a priority queue class?\n", " * Yes\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "The constraints state we don't have to check for negative edges, so we test with the general case.\n", "\n", "
\n",
    "graph.add_edge('a', 'b', weight=5)\n",
    "graph.add_edge('a', 'c', weight=3)\n",
    "graph.add_edge('a', 'e', weight=2)\n",
    "graph.add_edge('b', 'd', weight=2)\n",
    "graph.add_edge('c', 'b', weight=1)\n",
    "graph.add_edge('c', 'd', weight=1)\n",
    "graph.add_edge('d', 'a', weight=1)\n",
    "graph.add_edge('d', 'g', weight=2)\n",
    "graph.add_edge('d', 'h', weight=1)\n",
    "graph.add_edge('e', 'a', weight=1)\n",
    "graph.add_edge('e', 'h', weight=4)\n",
    "graph.add_edge('e', 'i', weight=7)\n",
    "graph.add_edge('f', 'b', weight=3)\n",
    "graph.add_edge('f', 'g', weight=1)\n",
    "graph.add_edge('g', 'c', weight=3)\n",
    "graph.add_edge('g', 'i', weight=2)\n",
    "graph.add_edge('h', 'c', weight=2)\n",
    "graph.add_edge('h', 'f', weight=2)\n",
    "graph.add_edge('h', 'g', weight=2)\n",
    "shortest_path = ShortestPath(graph)\n",
    "result = shortest_path.find_shortest_path('a', 'i')\n",
    "self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n",
    "self.assertEqual(shortest_path.path_weight['i'], 8)\n",
    "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Algorithm\n", "\n", "Refer to the [Solution Notebook](http://nbviewer.jupyter.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_shortest_path/graph_shortest_path_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%run ../../arrays_strings/priority_queue/priority_queue.py\n", "%load ../../arrays_strings/priority_queue/priority_queue.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%run ../graph/graph.py\n", "%load ../graph/graph.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class ShortestPath(object):\n", "\n", " def __init__(self, graph):\n", " # TODO: Implement me\n", " pass\n", "\n", " def find_shortest_path(self, start_node_key, end_node_key):\n", " # TODO: Implement me\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unit Test" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The following unit test is expected to fail until you solve the challenge.**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %load test_shortest_path.py\n", "import unittest\n", "\n", "\n", "class TestShortestPath(unittest.TestCase):\n", "\n", " def test_shortest_path(self):\n", " graph = Graph()\n", " graph.add_edge('a', 'b', weight=5)\n", " graph.add_edge('a', 'c', weight=3)\n", " graph.add_edge('a', 'e', weight=2)\n", " graph.add_edge('b', 'd', weight=2)\n", " graph.add_edge('c', 'b', weight=1)\n", " graph.add_edge('c', 'd', weight=1)\n", " graph.add_edge('d', 'a', weight=1)\n", " graph.add_edge('d', 'g', weight=2)\n", " graph.add_edge('d', 'h', weight=1)\n", " graph.add_edge('e', 'a', weight=1)\n", " graph.add_edge('e', 'h', weight=4)\n", " graph.add_edge('e', 'i', weight=7)\n", " graph.add_edge('f', 'b', weight=3)\n", " graph.add_edge('f', 'g', weight=1)\n", " graph.add_edge('g', 'c', weight=3)\n", " graph.add_edge('g', 'i', weight=2)\n", " graph.add_edge('h', 'c', weight=2)\n", " graph.add_edge('h', 'f', weight=2)\n", " graph.add_edge('h', 'g', weight=2)\n", " shortest_path = ShortestPath(graph)\n", " result = shortest_path.find_shortest_path('a', 'i')\n", " self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n", " self.assertEqual(shortest_path.path_weight['i'], 8)\n", "\n", " print('Success: test_shortest_path')\n", "\n", "\n", "def main():\n", " test = TestShortestPath()\n", " test.test_shortest_path()\n", "\n", "\n", "if __name__ == '__main__':\n", " main()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution Notebook\n", "\n", "Review the [Solution Notebook](https://github.com/donnemartin/interactive-coding-challenges/graphs_trees/graph_shortest_path/graph_shortest_path_solution.ipynb) for a discussion on algorithms and code solutions." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.2" } }, "nbformat": 4, "nbformat_minor": 1 }