Rework graph challenge and solution (#86)

This commit is contained in:
Donne Martin
2016-06-26 17:48:32 -04:00
committed by GitHub
parent 70e8fdd7a3
commit 5093103324
4 changed files with 166 additions and 73 deletions

View File

@@ -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,