Reworked graph challenge.

This commit is contained in:
Donne Martin
2015-08-04 19:37:05 -04:00
parent 77438319a5
commit ab69f7d066
4 changed files with 87 additions and 76 deletions

View File

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