diff --git a/README.md b/README.md index c1be112..25cbf71 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,7 @@ Challenges, solutions, and unit tests are presented in the form of **IPython/Jup | Implement depth-first search (pre-, in-, post-order) | [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 | [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 basic 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/__init__.py b/graphs_trees/graph/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/graphs_trees/graph/graph.py b/graphs_trees/graph/graph.py new file mode 100644 index 0000000..614f997 --- /dev/null +++ b/graphs_trees/graph/graph.py @@ -0,0 +1,34 @@ +# Python 2 users: Run pip install enum34 +from enum import Enum + + +class State(Enum): + unvisited = 1 + visited = 2 + visiting = 3 + + +class Node: + + def __init__(self, id): + self.id = id + self.state = State.unvisited + self.connections = {} + + +class Graph: + + def __init__(self): + self.nodes = {} + + def add_node(self, id): + node = Node(id) + self.nodes[id] = node + return node + + def add_edge(self, source, dest, weight=0): + if source not in self.nodes: + self.add_node(source) + if dest not in self.nodes: + self.add_node(dest) + self.nodes[source].connections[self.nodes[dest]] = weight diff --git a/graphs_trees/graph/graph_challenge.ipynb b/graphs_trees/graph/graph_challenge.ipynb new file mode 100644 index 0000000..dd08de2 --- /dev/null +++ b/graphs_trees/graph/graph_challenge.ipynb @@ -0,0 +1,217 @@ +{ + "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: Implement a basic 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", + "* I plan to use a Node and Graph class. \n", + " * Node will keep track of its id, visit state, and connections. \n", + " * Connections will be a dictionary with the key = node id and value being the edge weight\n", + " * Graph will hold all of the nodes and have functions to add nodes and edges. \n", + " * Is this in line with what you have in mind?\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, 5, 2)\n", + "graph.add_edge(1, 2, 3)\n", + "graph.add_edge(2, 3, 4)\n", + "graph.add_edge(3, 4, 5)\n", + "graph.add_edge(3, 5, 6)\n", + "graph.add_edge(4, 0, 7)\n", + "graph.add_edge(5, 4, 8)\n", + "graph.add_edge(5, 2, 9)\n", + "```\n", + "\n", + "Result:\n", + "* `source` and `destination` nodes within `graph` are connected with specified `weight`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](https://github.com/donnemartin/interactive-coding-challenges/graphs_trees/graphs/graph_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": false + }, + "outputs": [], + "source": [ + "# Python 2 users: Run pip install enum34\n", + "from enum import Enum\n", + "\n", + "\n", + "class State(Enum):\n", + " unvisited = 1\n", + " visited = 2\n", + " visiting = 3\n", + "\n", + "\n", + "class Node:\n", + "\n", + " def __init__(self, id):\n", + " # TODO: Implement me\n", + "\n", + "\n", + "class Graph:\n", + "\n", + " def __init__(self):\n", + " # TODO: Implement me\n", + "\n", + " def add_node(self, id):\n", + " # TODO: Implement me\n", + "\n", + " def add_edge(self, source, dest, weight=0):\n", + " # TODO: Implement me" + ] + }, + { + "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_graph.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestGraph(object):\n", + "\n", + " def test_graph(self):\n", + " graph = Graph()\n", + " for key in range(0, 6):\n", + " graph.add_node(key)\n", + " graph.add_edge(0, 1, 5)\n", + " graph.add_edge(0, 5, 2)\n", + " graph.add_edge(1, 2, 3)\n", + " graph.add_edge(2, 3, 4)\n", + " graph.add_edge(3, 4, 5)\n", + " graph.add_edge(3, 5, 6)\n", + " graph.add_edge(4, 0, 7)\n", + " graph.add_edge(5, 4, 8)\n", + " graph.add_edge(5, 2, 9)\n", + "\n", + " assert_equal(graph.nodes[0].connections[graph.nodes[1]], 5)\n", + " assert_equal(graph.nodes[0].connections[graph.nodes[5]], 2)\n", + " assert_equal(graph.nodes[1].connections[graph.nodes[2]], 3)\n", + " assert_equal(graph.nodes[2].connections[graph.nodes[3]], 4)\n", + " assert_equal(graph.nodes[3].connections[graph.nodes[4]], 5)\n", + " assert_equal(graph.nodes[3].connections[graph.nodes[5]], 6)\n", + " assert_equal(graph.nodes[4].connections[graph.nodes[0]], 7)\n", + " assert_equal(graph.nodes[5].connections[graph.nodes[4]], 8)\n", + " assert_equal(graph.nodes[5].connections[graph.nodes[2]], 9)\n", + "\n", + " print('Success: test_graph')\n", + "\n", + "\n", + "def main():\n", + " test = TestGraph()\n", + " test.test_graph()\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/graphs/graph_solution.ipynb) 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/graph_solution.ipynb b/graphs_trees/graph/graph_solution.ipynb new file mode 100644 index 0000000..acb6055 --- /dev/null +++ b/graphs_trees/graph/graph_solution.ipynb @@ -0,0 +1,252 @@ +{ + "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: Implement a basic 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", + "* I plan to use a Node and Graph class. \n", + " * Node will keep track of its id, visit state, and connections. \n", + " * Connections will be a dictionary with the key = node id and value being the edge weight\n", + " * Graph will hold all of the nodes and have functions to add nodes and edges. \n", + " * Is this in line with what you have in mind?\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, 5, 2)\n", + "graph.add_edge(1, 2, 3)\n", + "graph.add_edge(2, 3, 4)\n", + "graph.add_edge(3, 4, 5)\n", + "graph.add_edge(3, 5, 6)\n", + "graph.add_edge(4, 0, 7)\n", + "graph.add_edge(5, 4, 8)\n", + "graph.add_edge(5, 2, 9)\n", + "```\n", + "\n", + "Result:\n", + "* `source` and `destination` nodes within `graph` are connected with specified `weight`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "### add_node\n", + "\n", + "* Create a node with the input id\n", + "* Add the newly created node to the list of nodes\n", + "\n", + "Complexity:\n", + "* Time: O(1)\n", + "* Space: O(1)\n", + "\n", + "### add_edge\n", + "\n", + "* If the source node is not in the list of nodes, add it\n", + "* If the dest node is not in the list of nodes, add it\n", + "* Add a connection from the source node to the dest node with the given edge weight\n", + "\n", + "Complexity:\n", + "* Time: O(1)\n", + "* Space: O(1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Python 2 users: Run pip install enum34\n", + "from enum import Enum\n", + "\n", + "\n", + "class State(Enum):\n", + " unvisited = 1\n", + " visited = 2\n", + " visiting = 3\n", + "\n", + "\n", + "class Node:\n", + "\n", + " def __init__(self, id):\n", + " self.id = id\n", + " self.state = State.unvisited\n", + " self.connections = {}\n", + "\n", + "\n", + "class Graph:\n", + "\n", + " def __init__(self):\n", + " self.nodes = {}\n", + "\n", + " def add_node(self, id):\n", + " node = Node(id)\n", + " self.nodes[id] = node\n", + " return node\n", + "\n", + " def add_edge(self, source, dest, weight=0):\n", + " if source not in self.nodes:\n", + " self.add_node(source)\n", + " if dest not in self.nodes:\n", + " self.add_node(dest)\n", + " self.nodes[source].connections[self.nodes[dest]] = weight" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting test_graph.py\n" + ] + } + ], + "source": [ + "%%writefile test_graph.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestGraph(object):\n", + "\n", + " def test_graph(self):\n", + " graph = Graph()\n", + " for key in range(0, 6):\n", + " graph.add_node(key)\n", + " graph.add_edge(0, 1, 5)\n", + " graph.add_edge(0, 5, 2)\n", + " graph.add_edge(1, 2, 3)\n", + " graph.add_edge(2, 3, 4)\n", + " graph.add_edge(3, 4, 5)\n", + " graph.add_edge(3, 5, 6)\n", + " graph.add_edge(4, 0, 7)\n", + " graph.add_edge(5, 4, 8)\n", + " graph.add_edge(5, 2, 9)\n", + "\n", + " assert_equal(graph.nodes[0].connections[graph.nodes[1]], 5)\n", + " assert_equal(graph.nodes[0].connections[graph.nodes[5]], 2)\n", + " assert_equal(graph.nodes[1].connections[graph.nodes[2]], 3)\n", + " assert_equal(graph.nodes[2].connections[graph.nodes[3]], 4)\n", + " assert_equal(graph.nodes[3].connections[graph.nodes[4]], 5)\n", + " assert_equal(graph.nodes[3].connections[graph.nodes[5]], 6)\n", + " assert_equal(graph.nodes[4].connections[graph.nodes[0]], 7)\n", + " assert_equal(graph.nodes[5].connections[graph.nodes[4]], 8)\n", + " assert_equal(graph.nodes[5].connections[graph.nodes[2]], 9)\n", + "\n", + " print('Success: test_graph')\n", + "\n", + "\n", + "def main():\n", + " test = TestGraph()\n", + " test.test_graph()\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_graph\n" + ] + } + ], + "source": [ + "%run -i test_graph.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/test_graph.py b/graphs_trees/graph/test_graph.py new file mode 100644 index 0000000..44d4b84 --- /dev/null +++ b/graphs_trees/graph/test_graph.py @@ -0,0 +1,39 @@ +from nose.tools import assert_equal + + +class TestGraph(object): + + def test_graph(self): + graph = Graph() + for key in range(0, 6): + graph.add_node(key) + graph.add_edge(0, 1, 5) + graph.add_edge(0, 5, 2) + graph.add_edge(1, 2, 3) + graph.add_edge(2, 3, 4) + graph.add_edge(3, 4, 5) + graph.add_edge(3, 5, 6) + graph.add_edge(4, 0, 7) + graph.add_edge(5, 4, 8) + graph.add_edge(5, 2, 9) + + assert_equal(graph.nodes[0].connections[graph.nodes[1]], 5) + assert_equal(graph.nodes[0].connections[graph.nodes[5]], 2) + assert_equal(graph.nodes[1].connections[graph.nodes[2]], 3) + assert_equal(graph.nodes[2].connections[graph.nodes[3]], 4) + assert_equal(graph.nodes[3].connections[graph.nodes[4]], 5) + assert_equal(graph.nodes[3].connections[graph.nodes[5]], 6) + assert_equal(graph.nodes[4].connections[graph.nodes[0]], 7) + assert_equal(graph.nodes[5].connections[graph.nodes[4]], 8) + assert_equal(graph.nodes[5].connections[graph.nodes[2]], 9) + + print('Success: test_graph') + + +def main(): + test = TestGraph() + test.test_graph() + + +if __name__ == '__main__': + main() \ No newline at end of file