diff --git a/graphs_trees/graph/graph.py b/graphs_trees/graph/graph.py index 614f997..cf0cc67 100644 --- a/graphs_trees/graph/graph.py +++ b/graphs_trees/graph/graph.py @@ -1,25 +1,18 @@ -# Python 2 users: Run pip install enum34 -from enum import Enum - - -class State(Enum): - unvisited = 1 - visited = 2 - visiting = 3 +from collections import OrderedDict class Node: def __init__(self, id): self.id = id - self.state = State.unvisited - self.connections = {} + self.visited = False + self.adjacent = OrderedDict() # key = node, val = weight class Graph: def __init__(self): - self.nodes = {} + self.nodes = OrderedDict() # key = node id, val = node def add_node(self, id): node = Node(id) @@ -31,4 +24,4 @@ class Graph: self.add_node(source) if dest not in self.nodes: self.add_node(dest) - self.nodes[source].connections[self.nodes[dest]] = weight + self.nodes[source].adjacent[self.nodes[dest]] = weight \ No newline at end of file diff --git a/graphs_trees/graph/graph_challenge.ipynb b/graphs_trees/graph/graph_challenge.ipynb index b02578c..eb26b03 100644 --- a/graphs_trees/graph/graph_challenge.ipynb +++ b/graphs_trees/graph/graph_challenge.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Implement a basic graph.\n", + "## Problem: Implement a graph.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -62,7 +62,11 @@ "```\n", "\n", "Result:\n", - "* `source` and `destination` nodes within `graph` are connected with specified `weight`." + "* `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." ] }, { @@ -89,32 +93,29 @@ }, "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", + "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", "\n", "\n", "class Graph:\n", "\n", " def __init__(self):\n", " # TODO: Implement me\n", + " self.nodes = OrderedDict() # key = node id, val = node\n", "\n", " def add_node(self, id):\n", " # TODO: Implement me\n", + " pass\n", "\n", " def add_edge(self, source, dest, weight=0):\n", - " # TODO: Implement me" + " # TODO: Implement me\n", + " pass" ] }, { @@ -147,8 +148,8 @@ "\n", " def test_graph(self):\n", " graph = Graph()\n", - " for key in range(0, 6):\n", - " graph.add_node(key)\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", @@ -159,15 +160,15 @@ " 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", + " assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n", + " assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n", + " assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)\n", + " assert_equal(graph.nodes[2].adjacent[graph.nodes[3]], 4)\n", + " assert_equal(graph.nodes[3].adjacent[graph.nodes[4]], 5)\n", + " assert_equal(graph.nodes[3].adjacent[graph.nodes[5]], 6)\n", + " assert_equal(graph.nodes[4].adjacent[graph.nodes[0]], 7)\n", + " assert_equal(graph.nodes[5].adjacent[graph.nodes[4]], 8)\n", + " assert_equal(graph.nodes[5].adjacent[graph.nodes[2]], 9)\n", "\n", " print('Success: test_graph')\n", "\n", diff --git a/graphs_trees/graph/graph_solution.ipynb b/graphs_trees/graph/graph_solution.ipynb index d9cb43c..25a5d38 100644 --- a/graphs_trees/graph/graph_solution.ipynb +++ b/graphs_trees/graph/graph_solution.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Implement a basic graph.\n", + "## Problem: Implement a graph.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -61,7 +61,11 @@ "```\n", "\n", "Result:\n", - "* `source` and `destination` nodes within `graph` are connected with specified `weight`." + "* `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." ] }, { @@ -75,14 +79,14 @@ "Node will keep track of its:\n", "* id\n", "* visit state\n", - "* connections\n", + "* adjacent\n", " * key: node\n", " * value: weight\n", "\n", "### Graph\n", "\n", "Graph will keep track of its:\n", - "* Nodes\n", + "* nodes\n", " * key: node id\n", " * value: node\n", "\n", @@ -117,32 +121,34 @@ "cell_type": "code", "execution_count": 1, "metadata": { - "collapsed": true + "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting graph.py\n" + ] + } + ], "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", + "%%writefile graph.py\n", + "from collections import OrderedDict\n", "\n", "\n", "class Node:\n", "\n", " def __init__(self, id):\n", " self.id = id\n", - " self.state = State.unvisited\n", - " self.connections = {} # key = node, val = weight\n", + " self.visited = False\n", + " self.adjacent = OrderedDict() # key = node, val = weight\n", "\n", "\n", "class Graph:\n", "\n", " def __init__(self):\n", - " self.nodes = {} # key = node id, val = node\n", + " self.nodes = OrderedDict() # key = node id, val = node\n", "\n", " def add_node(self, id):\n", " node = Node(id)\n", @@ -154,7 +160,18 @@ " 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" + " self.nodes[source].adjacent[self.nodes[dest]] = weight" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%run graph.py" ] }, { @@ -166,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -188,8 +205,8 @@ "\n", " def test_graph(self):\n", " graph = Graph()\n", - " for key in range(0, 6):\n", - " graph.add_node(key)\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", @@ -200,15 +217,15 @@ " 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", + " assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n", + " assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n", + " assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)\n", + " assert_equal(graph.nodes[2].adjacent[graph.nodes[3]], 4)\n", + " assert_equal(graph.nodes[3].adjacent[graph.nodes[4]], 5)\n", + " assert_equal(graph.nodes[3].adjacent[graph.nodes[5]], 6)\n", + " assert_equal(graph.nodes[4].adjacent[graph.nodes[0]], 7)\n", + " assert_equal(graph.nodes[5].adjacent[graph.nodes[4]], 8)\n", + " assert_equal(graph.nodes[5].adjacent[graph.nodes[2]], 9)\n", "\n", " print('Success: test_graph')\n", "\n", @@ -224,7 +241,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": false }, diff --git a/graphs_trees/graph/test_graph.py b/graphs_trees/graph/test_graph.py index 44d4b84..8dc5df9 100644 --- a/graphs_trees/graph/test_graph.py +++ b/graphs_trees/graph/test_graph.py @@ -5,8 +5,8 @@ class TestGraph(object): def test_graph(self): graph = Graph() - for key in range(0, 6): - graph.add_node(key) + 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) @@ -17,15 +17,15 @@ class TestGraph(object): 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) + assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5) + assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2) + assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3) + assert_equal(graph.nodes[2].adjacent[graph.nodes[3]], 4) + assert_equal(graph.nodes[3].adjacent[graph.nodes[4]], 5) + assert_equal(graph.nodes[3].adjacent[graph.nodes[5]], 6) + assert_equal(graph.nodes[4].adjacent[graph.nodes[0]], 7) + assert_equal(graph.nodes[5].adjacent[graph.nodes[4]], 8) + assert_equal(graph.nodes[5].adjacent[graph.nodes[2]], 9) print('Success: test_graph')