From 50931033243d4057706f469489e03be9c5099e82 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 26 Jun 2016 17:48:32 -0400 Subject: [PATCH] Rework graph challenge and solution (#86) --- graphs_trees/graph/graph.py | 28 ++++--- graphs_trees/graph/graph_challenge.ipynb | 73 ++++++++++++------ graphs_trees/graph/graph_solution.ipynb | 96 +++++++++++++++++------- graphs_trees/graph/test_graph.py | 42 ++++++++--- 4 files changed, 166 insertions(+), 73 deletions(-) diff --git a/graphs_trees/graph/graph.py b/graphs_trees/graph/graph.py index 7f8351e..1fad795 100644 --- a/graphs_trees/graph/graph.py +++ b/graphs_trees/graph/graph.py @@ -1,11 +1,10 @@ -from collections import OrderedDict from enum import Enum # Python 2 users: Run pip install enum34 class State(Enum): - unvisited = 1 + unvisited = 0 + visiting = 1 visited = 2 - visiting = 3 class Node: @@ -13,25 +12,32 @@ class Node: def __init__(self, id): self.id = id self.visit_state = State.unvisited - self.adjacent = OrderedDict() # key = node, val = weight + self.adjacent = {} # key = node, val = weight def __str__(self): return str(self.id) + def add_neighbor(self, neighbor, weight=0): + self.adjacent[neighbor] = weight + class Graph: def __init__(self): - self.nodes = OrderedDict() # key = node id, val = node + self.nodes = {} # key = node id, val = node 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].adjacent[self.nodes[dest]] = weight \ No newline at end of file + def add_edge(self, id_source, id_dest, weight=0): + if id_source not in self.nodes: + self.add_node(id_source) + if id_dest not in self.nodes: + self.add_node(id_dest) + self.nodes[id_source].add_neighbor(self.nodes[id_dest], weight) + + def add_undirected_edge(self, source, dest, weight=0): + self.add_edge(source, dest, weight) + self.nodes[dest].add_neighbor(self.nodes[source], weight) \ No newline at end of file diff --git a/graphs_trees/graph/graph_challenge.ipynb b/graphs_trees/graph/graph_challenge.ipynb index 6bb84bf..2e9f767 100644 --- a/graphs_trees/graph/graph_challenge.ipynb +++ b/graphs_trees/graph/graph_challenge.ipynb @@ -35,8 +35,10 @@ "## Constraints\n", "\n", "* Is the graph directed?\n", - " * Yes\n", + " * Implement both\n", "* Do the edges have weights?\n", + " * Yes\n", + "* Can we assume the inputs are valid?\n", " * Yes" ] }, @@ -65,8 +67,7 @@ "* `source` and `destination` nodes within `graph` are connected with specified `weight`.\n", "\n", "Note: \n", - "* We'll be using an OrderedDict to make the outputs more consistent and simplify our testing.\n", - "* Graph will be used as a building block for more complex graph challenges." + "* The Graph class will be used as a building block for more complex graph challenges." ] }, { @@ -93,24 +94,24 @@ }, "outputs": [], "source": [ - "from collections import OrderedDict\n", - "\n", - "\n", "class Node:\n", "\n", " def __init__(self, id):\n", " # TODO: Implement me\n", - " self.adjacent = OrderedDict() # key = node, val = weight\n", + " self.adjacent = {} # key = node, val = weight\n", "\n", " def __str__(self):\n", " return str(self.id)\n", "\n", + " def add_neighbor(self, neighbor, weight=0):\n", + " # TODO: Implement me\n", + " pass\n", "\n", "class Graph:\n", "\n", " def __init__(self):\n", " # TODO: Implement me\n", - " self.nodes = OrderedDict() # key = node id, val = node\n", + " self.nodes = {} # key = node id, val = node\n", "\n", " def add_node(self, id):\n", " # TODO: Implement me\n", @@ -118,6 +119,10 @@ "\n", " def add_edge(self, source, dest, weight=0):\n", " # TODO: Implement me\n", + " pass\n", + "\n", + " def add_undirected_edge(self, source, dest, weight=0):\n", + " # TODO: Implement me\n", " pass" ] }, @@ -149,19 +154,24 @@ "\n", "class TestGraph(object):\n", "\n", - " def test_graph(self):\n", + " def create_graph(self):\n", " graph = Graph()\n", " for id in range(0, 6):\n", " graph.add_node(id)\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", + " return graph\n", + "\n", + " def test_graph(self):\n", + " graph = self.create_graph()\n", + "\n", + " graph.add_edge(0, 1, weight=5)\n", + " graph.add_edge(0, 5, weight=2)\n", + " graph.add_edge(1, 2, weight=3)\n", + " graph.add_edge(2, 3, weight=4)\n", + " graph.add_edge(3, 4, weight=5)\n", + " graph.add_edge(3, 5, weight=6)\n", + " graph.add_edge(4, 0, weight=7)\n", + " graph.add_edge(5, 4, weight=8)\n", + " graph.add_edge(5, 2, weight=9)\n", "\n", " assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n", " assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n", @@ -175,10 +185,27 @@ "\n", " print('Success: test_graph')\n", "\n", + " def test_graph_undirected(self):\n", + " graph = self.create_graph()\n", + "\n", + " graph.add_undirected_edge(0, 1, weight=5)\n", + " graph.add_undirected_edge(0, 5, weight=2)\n", + " graph.add_undirected_edge(1, 2, weight=3)\n", + "\n", + " assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n", + " assert_equal(graph.nodes[1].adjacent[graph.nodes[0]], 5)\n", + " assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n", + " assert_equal(graph.nodes[5].adjacent[graph.nodes[0]], 2)\n", + " assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)\n", + " assert_equal(graph.nodes[2].adjacent[graph.nodes[1]], 3)\n", + "\n", + " print('Success: test_graph')\n", + "\n", "\n", "def main():\n", " test = TestGraph()\n", " test.test_graph()\n", + " test.test_graph_undirected()\n", "\n", "\n", "if __name__ == '__main__':\n", @@ -197,21 +224,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.0" } }, "nbformat": 4, diff --git a/graphs_trees/graph/graph_solution.ipynb b/graphs_trees/graph/graph_solution.ipynb index c0d8bee..b113d03 100644 --- a/graphs_trees/graph/graph_solution.ipynb +++ b/graphs_trees/graph/graph_solution.ipynb @@ -34,8 +34,10 @@ "## Constraints\n", "\n", "* Is the graph directed?\n", - " * Yes\n", + " * Implement both\n", "* Do the edges have weights?\n", + " * Yes\n", + "* Can we assume the inputs are valid?\n", " * Yes" ] }, @@ -64,8 +66,7 @@ "* `source` and `destination` nodes within `graph` are connected with specified `weight`.\n", "\n", "Note: \n", - "* We'll be using an OrderedDict to make the outputs more consistent and simplify our testing.\n", - "* Graph will be used as a building block for more complex graph challenges." + "* The Graph class will be used as a building block for more complex graph challenges." ] }, { @@ -83,6 +84,14 @@ " * key: node\n", " * value: weight\n", "\n", + "#### add_neighhbor\n", + "\n", + "* Add the neighbor as a key and the weight as the value to `adjacent`\n", + "\n", + "Complexity:\n", + "* Time: O(1)\n", + "* Space: O(1)\n", + "\n", "### Graph\n", "\n", "Graph will keep track of its:\n", @@ -134,14 +143,13 @@ ], "source": [ "%%writefile graph.py\n", - "from collections import OrderedDict\n", "from enum import Enum # Python 2 users: Run pip install enum34\n", "\n", "\n", "class State(Enum):\n", - " unvisited = 1\n", + " unvisited = 0\n", + " visiting = 1\n", " visited = 2\n", - " visiting = 3\n", "\n", "\n", "class Node:\n", @@ -149,28 +157,35 @@ " def __init__(self, id):\n", " self.id = id\n", " self.visit_state = State.unvisited\n", - " self.adjacent = OrderedDict() # key = node, val = weight\n", + " self.adjacent = {} # key = node, val = weight\n", "\n", " def __str__(self):\n", " return str(self.id)\n", "\n", + " def add_neighbor(self, neighbor, weight=0):\n", + " self.adjacent[neighbor] = weight\n", + "\n", "\n", "class Graph:\n", "\n", " def __init__(self):\n", - " self.nodes = OrderedDict() # key = node id, val = node\n", + " self.nodes = {} # key = node id, val = node\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].adjacent[self.nodes[dest]] = weight" + " def add_edge(self, id_source, id_dest, weight=0):\n", + " if id_source not in self.nodes:\n", + " self.add_node(id_source)\n", + " if id_dest not in self.nodes:\n", + " self.add_node(id_dest)\n", + " self.nodes[id_source].add_neighbor(self.nodes[id_dest], weight)\n", + "\n", + " def add_undirected_edge(self, source, dest, weight=0):\n", + " self.add_edge(source, dest, weight)\n", + " self.nodes[dest].add_neighbor(self.nodes[source], weight)" ] }, { @@ -213,19 +228,24 @@ "\n", "class TestGraph(object):\n", "\n", - " def test_graph(self):\n", + " def create_graph(self):\n", " graph = Graph()\n", " for id in range(0, 6):\n", " graph.add_node(id)\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", + " return graph\n", + "\n", + " def test_graph(self):\n", + " graph = self.create_graph()\n", + "\n", + " graph.add_edge(0, 1, weight=5)\n", + " graph.add_edge(0, 5, weight=2)\n", + " graph.add_edge(1, 2, weight=3)\n", + " graph.add_edge(2, 3, weight=4)\n", + " graph.add_edge(3, 4, weight=5)\n", + " graph.add_edge(3, 5, weight=6)\n", + " graph.add_edge(4, 0, weight=7)\n", + " graph.add_edge(5, 4, weight=8)\n", + " graph.add_edge(5, 2, weight=9)\n", "\n", " assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n", " assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n", @@ -239,10 +259,27 @@ "\n", " print('Success: test_graph')\n", "\n", + " def test_graph_undirected(self):\n", + " graph = self.create_graph()\n", + "\n", + " graph.add_undirected_edge(0, 1, weight=5)\n", + " graph.add_undirected_edge(0, 5, weight=2)\n", + " graph.add_undirected_edge(1, 2, weight=3)\n", + "\n", + " assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n", + " assert_equal(graph.nodes[1].adjacent[graph.nodes[0]], 5)\n", + " assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n", + " assert_equal(graph.nodes[5].adjacent[graph.nodes[0]], 2)\n", + " assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)\n", + " assert_equal(graph.nodes[2].adjacent[graph.nodes[1]], 3)\n", + "\n", + " print('Success: test_graph')\n", + "\n", "\n", "def main():\n", " test = TestGraph()\n", " test.test_graph()\n", + " test.test_graph_undirected()\n", "\n", "\n", "if __name__ == '__main__':\n", @@ -260,6 +297,7 @@ "name": "stdout", "output_type": "stream", "text": [ + "Success: test_graph\n", "Success: test_graph\n" ] } @@ -271,21 +309,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.0" } }, "nbformat": 4, diff --git a/graphs_trees/graph/test_graph.py b/graphs_trees/graph/test_graph.py index 8dc5df9..e52549e 100644 --- a/graphs_trees/graph/test_graph.py +++ b/graphs_trees/graph/test_graph.py @@ -3,19 +3,24 @@ from nose.tools import assert_equal class TestGraph(object): - def test_graph(self): + def create_graph(self): graph = Graph() for id in range(0, 6): graph.add_node(id) - 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) + return graph + + def test_graph(self): + graph = self.create_graph() + + graph.add_edge(0, 1, weight=5) + graph.add_edge(0, 5, weight=2) + graph.add_edge(1, 2, weight=3) + graph.add_edge(2, 3, weight=4) + graph.add_edge(3, 4, weight=5) + graph.add_edge(3, 5, weight=6) + graph.add_edge(4, 0, weight=7) + graph.add_edge(5, 4, weight=8) + graph.add_edge(5, 2, weight=9) assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5) assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2) @@ -29,10 +34,27 @@ class TestGraph(object): print('Success: test_graph') + def test_graph_undirected(self): + graph = self.create_graph() + + graph.add_undirected_edge(0, 1, weight=5) + graph.add_undirected_edge(0, 5, weight=2) + graph.add_undirected_edge(1, 2, weight=3) + + assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5) + assert_equal(graph.nodes[1].adjacent[graph.nodes[0]], 5) + assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2) + assert_equal(graph.nodes[5].adjacent[graph.nodes[0]], 2) + assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3) + assert_equal(graph.nodes[2].adjacent[graph.nodes[1]], 3) + + print('Success: test_graph') + def main(): test = TestGraph() test.test_graph() + test.test_graph_undirected() if __name__ == '__main__':