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

@ -1,25 +1,18 @@
# Python 2 users: Run pip install enum34 from collections import OrderedDict
from enum import Enum
class State(Enum):
unvisited = 1
visited = 2
visiting = 3
class Node: class Node:
def __init__(self, id): def __init__(self, id):
self.id = id self.id = id
self.state = State.unvisited self.visited = False
self.connections = {} self.adjacent = OrderedDict() # key = node, val = weight
class Graph: class Graph:
def __init__(self): def __init__(self):
self.nodes = {} self.nodes = OrderedDict() # key = node id, val = node
def add_node(self, id): def add_node(self, id):
node = Node(id) node = Node(id)
@ -31,4 +24,4 @@ class Graph:
self.add_node(source) self.add_node(source)
if dest not in self.nodes: if dest not in self.nodes:
self.add_node(dest) self.add_node(dest)
self.nodes[source].connections[self.nodes[dest]] = weight self.nodes[source].adjacent[self.nodes[dest]] = weight

View File

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Problem: Implement a basic graph.\n", "## Problem: Implement a graph.\n",
"\n", "\n",
"* [Constraints](#Constraints)\n", "* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n", "* [Test Cases](#Test-Cases)\n",
@ -62,7 +62,11 @@
"```\n", "```\n",
"\n", "\n",
"Result:\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": [], "outputs": [],
"source": [ "source": [
"# Python 2 users: Run pip install enum34\n", "from collections import OrderedDict\n",
"from enum import Enum\n",
"\n",
"\n",
"class State(Enum):\n",
" unvisited = 1\n",
" visited = 2\n",
" visiting = 3\n",
"\n", "\n",
"\n", "\n",
"class Node:\n", "class Node:\n",
"\n", "\n",
" def __init__(self, id):\n", " def __init__(self, id):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" self.adjacent = OrderedDict() # key = node, val = weight\n",
"\n", "\n",
"\n", "\n",
"class Graph:\n", "class Graph:\n",
"\n", "\n",
" def __init__(self):\n", " def __init__(self):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" self.nodes = OrderedDict() # key = node id, val = node\n",
"\n", "\n",
" def add_node(self, id):\n", " def add_node(self, id):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass\n",
"\n", "\n",
" def add_edge(self, source, dest, weight=0):\n", " def add_edge(self, source, dest, weight=0):\n",
" # TODO: Implement me" " # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@ -147,8 +148,8 @@
"\n", "\n",
" def test_graph(self):\n", " def test_graph(self):\n",
" graph = Graph()\n", " graph = Graph()\n",
" for key in range(0, 6):\n", " for id in range(0, 6):\n",
" graph.add_node(key)\n", " graph.add_node(id)\n",
" graph.add_edge(0, 1, 5)\n", " graph.add_edge(0, 1, 5)\n",
" graph.add_edge(0, 5, 2)\n", " graph.add_edge(0, 5, 2)\n",
" graph.add_edge(1, 2, 3)\n", " graph.add_edge(1, 2, 3)\n",
@ -159,15 +160,15 @@
" graph.add_edge(5, 4, 8)\n", " graph.add_edge(5, 4, 8)\n",
" graph.add_edge(5, 2, 9)\n", " graph.add_edge(5, 2, 9)\n",
"\n", "\n",
" assert_equal(graph.nodes[0].connections[graph.nodes[1]], 5)\n", " assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n",
" assert_equal(graph.nodes[0].connections[graph.nodes[5]], 2)\n", " assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n",
" assert_equal(graph.nodes[1].connections[graph.nodes[2]], 3)\n", " assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)\n",
" assert_equal(graph.nodes[2].connections[graph.nodes[3]], 4)\n", " assert_equal(graph.nodes[2].adjacent[graph.nodes[3]], 4)\n",
" assert_equal(graph.nodes[3].connections[graph.nodes[4]], 5)\n", " assert_equal(graph.nodes[3].adjacent[graph.nodes[4]], 5)\n",
" assert_equal(graph.nodes[3].connections[graph.nodes[5]], 6)\n", " assert_equal(graph.nodes[3].adjacent[graph.nodes[5]], 6)\n",
" assert_equal(graph.nodes[4].connections[graph.nodes[0]], 7)\n", " assert_equal(graph.nodes[4].adjacent[graph.nodes[0]], 7)\n",
" assert_equal(graph.nodes[5].connections[graph.nodes[4]], 8)\n", " assert_equal(graph.nodes[5].adjacent[graph.nodes[4]], 8)\n",
" assert_equal(graph.nodes[5].connections[graph.nodes[2]], 9)\n", " assert_equal(graph.nodes[5].adjacent[graph.nodes[2]], 9)\n",
"\n", "\n",
" print('Success: test_graph')\n", " print('Success: test_graph')\n",
"\n", "\n",

View File

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Problem: Implement a basic graph.\n", "## Problem: Implement a graph.\n",
"\n", "\n",
"* [Constraints](#Constraints)\n", "* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n", "* [Test Cases](#Test-Cases)\n",
@ -61,7 +61,11 @@
"```\n", "```\n",
"\n", "\n",
"Result:\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", "Node will keep track of its:\n",
"* id\n", "* id\n",
"* visit state\n", "* visit state\n",
"* connections\n", "* adjacent\n",
" * key: node\n", " * key: node\n",
" * value: weight\n", " * value: weight\n",
"\n", "\n",
"### Graph\n", "### Graph\n",
"\n", "\n",
"Graph will keep track of its:\n", "Graph will keep track of its:\n",
"* Nodes\n", "* nodes\n",
" * key: node id\n", " * key: node id\n",
" * value: node\n", " * value: node\n",
"\n", "\n",
@ -117,32 +121,34 @@
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": true "collapsed": false
}, },
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting graph.py\n"
]
}
],
"source": [ "source": [
"# Python 2 users: Run pip install enum34\n", "%%writefile graph.py\n",
"from enum import Enum\n", "from collections import OrderedDict\n",
"\n",
"\n",
"class State(Enum):\n",
" unvisited = 1\n",
" visited = 2\n",
" visiting = 3\n",
"\n", "\n",
"\n", "\n",
"class Node:\n", "class Node:\n",
"\n", "\n",
" def __init__(self, id):\n", " def __init__(self, id):\n",
" self.id = id\n", " self.id = id\n",
" self.state = State.unvisited\n", " self.visited = False\n",
" self.connections = {} # key = node, val = weight\n", " self.adjacent = OrderedDict() # key = node, val = weight\n",
"\n", "\n",
"\n", "\n",
"class Graph:\n", "class Graph:\n",
"\n", "\n",
" def __init__(self):\n", " def __init__(self):\n",
" self.nodes = {} # key = node id, val = node\n", " self.nodes = OrderedDict() # key = node id, val = node\n",
"\n", "\n",
" def add_node(self, id):\n", " def add_node(self, id):\n",
" node = Node(id)\n", " node = Node(id)\n",
@ -154,7 +160,18 @@
" self.add_node(source)\n", " self.add_node(source)\n",
" if dest not in self.nodes:\n", " if dest not in self.nodes:\n",
" self.add_node(dest)\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", "cell_type": "code",
"execution_count": 2, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -188,8 +205,8 @@
"\n", "\n",
" def test_graph(self):\n", " def test_graph(self):\n",
" graph = Graph()\n", " graph = Graph()\n",
" for key in range(0, 6):\n", " for id in range(0, 6):\n",
" graph.add_node(key)\n", " graph.add_node(id)\n",
" graph.add_edge(0, 1, 5)\n", " graph.add_edge(0, 1, 5)\n",
" graph.add_edge(0, 5, 2)\n", " graph.add_edge(0, 5, 2)\n",
" graph.add_edge(1, 2, 3)\n", " graph.add_edge(1, 2, 3)\n",
@ -200,15 +217,15 @@
" graph.add_edge(5, 4, 8)\n", " graph.add_edge(5, 4, 8)\n",
" graph.add_edge(5, 2, 9)\n", " graph.add_edge(5, 2, 9)\n",
"\n", "\n",
" assert_equal(graph.nodes[0].connections[graph.nodes[1]], 5)\n", " assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n",
" assert_equal(graph.nodes[0].connections[graph.nodes[5]], 2)\n", " assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n",
" assert_equal(graph.nodes[1].connections[graph.nodes[2]], 3)\n", " assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)\n",
" assert_equal(graph.nodes[2].connections[graph.nodes[3]], 4)\n", " assert_equal(graph.nodes[2].adjacent[graph.nodes[3]], 4)\n",
" assert_equal(graph.nodes[3].connections[graph.nodes[4]], 5)\n", " assert_equal(graph.nodes[3].adjacent[graph.nodes[4]], 5)\n",
" assert_equal(graph.nodes[3].connections[graph.nodes[5]], 6)\n", " assert_equal(graph.nodes[3].adjacent[graph.nodes[5]], 6)\n",
" assert_equal(graph.nodes[4].connections[graph.nodes[0]], 7)\n", " assert_equal(graph.nodes[4].adjacent[graph.nodes[0]], 7)\n",
" assert_equal(graph.nodes[5].connections[graph.nodes[4]], 8)\n", " assert_equal(graph.nodes[5].adjacent[graph.nodes[4]], 8)\n",
" assert_equal(graph.nodes[5].connections[graph.nodes[2]], 9)\n", " assert_equal(graph.nodes[5].adjacent[graph.nodes[2]], 9)\n",
"\n", "\n",
" print('Success: test_graph')\n", " print('Success: test_graph')\n",
"\n", "\n",
@ -224,7 +241,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },

View File

@ -5,8 +5,8 @@ class TestGraph(object):
def test_graph(self): def test_graph(self):
graph = Graph() graph = Graph()
for key in range(0, 6): for id in range(0, 6):
graph.add_node(key) graph.add_node(id)
graph.add_edge(0, 1, 5) graph.add_edge(0, 1, 5)
graph.add_edge(0, 5, 2) graph.add_edge(0, 5, 2)
graph.add_edge(1, 2, 3) graph.add_edge(1, 2, 3)
@ -17,15 +17,15 @@ class TestGraph(object):
graph.add_edge(5, 4, 8) graph.add_edge(5, 4, 8)
graph.add_edge(5, 2, 9) graph.add_edge(5, 2, 9)
assert_equal(graph.nodes[0].connections[graph.nodes[1]], 5) assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)
assert_equal(graph.nodes[0].connections[graph.nodes[5]], 2) assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)
assert_equal(graph.nodes[1].connections[graph.nodes[2]], 3) assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)
assert_equal(graph.nodes[2].connections[graph.nodes[3]], 4) assert_equal(graph.nodes[2].adjacent[graph.nodes[3]], 4)
assert_equal(graph.nodes[3].connections[graph.nodes[4]], 5) assert_equal(graph.nodes[3].adjacent[graph.nodes[4]], 5)
assert_equal(graph.nodes[3].connections[graph.nodes[5]], 6) assert_equal(graph.nodes[3].adjacent[graph.nodes[5]], 6)
assert_equal(graph.nodes[4].connections[graph.nodes[0]], 7) assert_equal(graph.nodes[4].adjacent[graph.nodes[0]], 7)
assert_equal(graph.nodes[5].connections[graph.nodes[4]], 8) assert_equal(graph.nodes[5].adjacent[graph.nodes[4]], 8)
assert_equal(graph.nodes[5].connections[graph.nodes[2]], 9) assert_equal(graph.nodes[5].adjacent[graph.nodes[2]], 9)
print('Success: test_graph') print('Success: test_graph')