From 74d25491cae0afb1d4f03961570e5aefbccc5257 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Wed, 5 Aug 2015 05:47:02 -0400 Subject: [PATCH] Tweaked ordering of graph and bst challenges. --- README.md | 4 +- graphs_trees/graph_path_exists/__init__.py | 0 .../path_exists_challenge.ipynb | 199 +++++++++++++++ .../path_exists_solution.ipynb | 240 ++++++++++++++++++ .../graph_path_exists/test_path_exists.py | 33 +++ 5 files changed, 474 insertions(+), 2 deletions(-) create mode 100644 graphs_trees/graph_path_exists/__init__.py create mode 100644 graphs_trees/graph_path_exists/path_exists_challenge.ipynb create mode 100644 graphs_trees/graph_path_exists/path_exists_solution.ipynb create mode 100644 graphs_trees/graph_path_exists/test_path_exists.py diff --git a/README.md b/README.md index 3061cbe..8edc4cf 100644 --- a/README.md +++ b/README.md @@ -203,13 +203,13 @@ Challenges, solutions, and unit tests are presented in the form of **IPython/Jup | Challenge | Static Notebooks | |--------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------| -| Implement a binary search tree | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst/bst_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst/bst_solution.ipynb) | | Implement depth-first search (pre-, in-, post-order) on a tree | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_dfs/dfs_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_dfs/dfs_solution.ipynb) | | Implement breadth-first search on a tree | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_bfs/bfs_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_bfs/bfs_solution.ipynb) | | Determine the height of a tree | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_height/height_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_height/height_solution.ipynb) | -| Implement a graph | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph/graph_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph/graph_solution.ipynb) | +| Implement a binary search tree | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst/bst_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst/bst_solution.ipynb) | | Implement depth-first search on a graph | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_dfs/dfs_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_dfs/dfs_solution.ipynb) | | Implement breadth-first search on a graph | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_bfs/bfs_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_bfs/bfs_solution.ipynb) | +| Implement a graph | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph/graph_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph/graph_solution.ipynb) | | Print a tree using pre-order traversal without recursion | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | | Determine the lowest common ancestor of two nodes | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | | Transform a binary tree into a heap | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | diff --git a/graphs_trees/graph_path_exists/__init__.py b/graphs_trees/graph_path_exists/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/graphs_trees/graph_path_exists/path_exists_challenge.ipynb b/graphs_trees/graph_path_exists/path_exists_challenge.ipynb new file mode 100644 index 0000000..ab5626d --- /dev/null +++ b/graphs_trees/graph_path_exists/path_exists_challenge.ipynb @@ -0,0 +1,199 @@ +{ + "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: Determine whether there is a 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 the graph directed?\n", + " * Yes\n", + "* Can we assume we already have Graph and Node classes?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "Input:\n", + "* `add_edge(source, destination, weight)`\n", + "\n", + "```\n", + "graph.add_edge(0, 1, 5)\n", + "graph.add_edge(0, 4, 3)\n", + "graph.add_edge(0, 5, 2)\n", + "graph.add_edge(1, 3, 5)\n", + "graph.add_edge(1, 4, 4)\n", + "graph.add_edge(2, 1, 6)\n", + "graph.add_edge(3, 2, 7)\n", + "graph.add_edge(3, 4, 8)\n", + "```\n", + "\n", + "Result:\n", + "* search_path(start=0, end=2) -> True\n", + "* search_path(start=0, end=0) -> True\n", + "* search_path(start=4, end=5) -> False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](). 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 ../graph/graph.py\n", + "%load ../graph/graph.py" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def path_exists(start, end):\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": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# %load test_path_exists.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestPathExists(object):\n", + "\n", + " def test_path_exists(self):\n", + " nodes = []\n", + " graph = Graph()\n", + " for id in range(0, 6):\n", + " nodes.append(graph.add_node(id))\n", + " graph.add_edge(0, 1, 5)\n", + " graph.add_edge(0, 4, 3)\n", + " graph.add_edge(0, 5, 2)\n", + " graph.add_edge(1, 3, 5)\n", + " graph.add_edge(1, 4, 4)\n", + " graph.add_edge(2, 1, 6)\n", + " graph.add_edge(3, 2, 7)\n", + " graph.add_edge(3, 4, 8)\n", + "\n", + " assert_equal(path_exists(nodes[0], nodes[2]), True)\n", + " assert_equal(path_exists(nodes[0], nodes[0]), True)\n", + " assert_equal(path_exists(nodes[4], nodes[5]), False)\n", + "\n", + " print('Success: test_path_exists')\n", + "\n", + "\n", + "def main():\n", + " test = TestPathExists()\n", + " test.test_path_exists()\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook]() for a discussion on algorithms and code solutions." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.10" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/graphs_trees/graph_path_exists/path_exists_solution.ipynb b/graphs_trees/graph_path_exists/path_exists_solution.ipynb new file mode 100644 index 0000000..c6ac3ed --- /dev/null +++ b/graphs_trees/graph_path_exists/path_exists_solution.ipynb @@ -0,0 +1,240 @@ +{ + "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": [ + "# Solution Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Determine whether there is a 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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "\n", + "* Is the graph directed?\n", + " * Yes\n", + "* Can we assume we already have Graph and Node classes?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "Input:\n", + "* `add_edge(source, destination, weight)`\n", + "\n", + "```\n", + "graph.add_edge(0, 1, 5)\n", + "graph.add_edge(0, 4, 3)\n", + "graph.add_edge(0, 5, 2)\n", + "graph.add_edge(1, 3, 5)\n", + "graph.add_edge(1, 4, 4)\n", + "graph.add_edge(2, 1, 6)\n", + "graph.add_edge(3, 2, 7)\n", + "graph.add_edge(3, 4, 8)\n", + "```\n", + "\n", + "Result:\n", + "* search_path(start=0, end=2) -> True\n", + "* search_path(start=0, end=0) -> True\n", + "* search_path(start=4, end=5) -> False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "We use breadth-first search to determine the shortest path. Depth-first search is easier to implement with just straight recursion, but often results in a longer path.\n", + "\n", + "* Add the start node to the queue and mark it as visited\n", + "* If the start node is the end node, return True\n", + "* While the queue is not empty\n", + " * Dequeue a node and visit it\n", + " * If the node is the end node, return True\n", + " * Iterate through each adjacent node\n", + " * If the node has not been visited, add it to the queue and mark it as visited\n", + "* Return False\n", + "\n", + "Complexity:\n", + "* Time: O(V + E), where V = number of vertices and E = number of edges\n", + "* Space: O(V + E)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%run ../graph/graph.py" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from collections import deque\n", + "\n", + "\n", + "def path_exists(start, end):\n", + " if start is None or end is None:\n", + " return False\n", + " if start is end:\n", + " return True\n", + " queue = deque()\n", + " queue.append(start)\n", + " start.visited = True\n", + " while queue:\n", + " node = queue.popleft()\n", + " if node is None:\n", + " continue\n", + " if node is end:\n", + " return True\n", + " for adj_node in node.adjacent:\n", + " if not adj_node.visited:\n", + " queue.append(adj_node)\n", + " adj_node.visited = True\n", + " return False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting test_path_exists.py\n" + ] + } + ], + "source": [ + "%%writefile test_path_exists.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestPathExists(object):\n", + "\n", + " def test_path_exists(self):\n", + " nodes = []\n", + " graph = Graph()\n", + " for id in range(0, 6):\n", + " nodes.append(graph.add_node(id))\n", + " graph.add_edge(0, 1, 5)\n", + " graph.add_edge(0, 4, 3)\n", + " graph.add_edge(0, 5, 2)\n", + " graph.add_edge(1, 3, 5)\n", + " graph.add_edge(1, 4, 4)\n", + " graph.add_edge(2, 1, 6)\n", + " graph.add_edge(3, 2, 7)\n", + " graph.add_edge(3, 4, 8)\n", + "\n", + " assert_equal(path_exists(nodes[0], nodes[2]), True)\n", + " assert_equal(path_exists(nodes[0], nodes[0]), True)\n", + " assert_equal(path_exists(nodes[4], nodes[5]), False)\n", + "\n", + " print('Success: test_path_exists')\n", + "\n", + "\n", + "def main():\n", + " test = TestPathExists()\n", + " test.test_path_exists()\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_path_exists\n" + ] + } + ], + "source": [ + "%run -i test_path_exists.py" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.10" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/graphs_trees/graph_path_exists/test_path_exists.py b/graphs_trees/graph_path_exists/test_path_exists.py new file mode 100644 index 0000000..a99bca0 --- /dev/null +++ b/graphs_trees/graph_path_exists/test_path_exists.py @@ -0,0 +1,33 @@ +from nose.tools import assert_equal + + +class TestPathExists(object): + + def test_path_exists(self): + nodes = [] + graph = Graph() + for id in range(0, 6): + nodes.append(graph.add_node(id)) + graph.add_edge(0, 1, 5) + graph.add_edge(0, 4, 3) + graph.add_edge(0, 5, 2) + graph.add_edge(1, 3, 5) + graph.add_edge(1, 4, 4) + graph.add_edge(2, 1, 6) + graph.add_edge(3, 2, 7) + graph.add_edge(3, 4, 8) + + assert_equal(path_exists(nodes[0], nodes[2]), True) + assert_equal(path_exists(nodes[0], nodes[0]), True) + assert_equal(path_exists(nodes[4], nodes[5]), False) + + print('Success: test_path_exists') + + +def main(): + test = TestPathExists() + test.test_path_exists() + + +if __name__ == '__main__': + main() \ No newline at end of file