2015-08-03 18:24:38 +08:00
{
" cells " : [
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" <small><i>This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small> "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Solution Notebook "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-08-05 07:37:05 +08:00
" ## Problem: Implement a graph. \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" * [Constraints](#Constraints) \n " ,
" * [Test Cases](#Test-Cases) \n " ,
" * [Algorithm](#Algorithm) \n " ,
" * [Code](#Code) \n " ,
" * [Unit Test](#Unit-Test) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Constraints \n " ,
" \n " ,
2015-08-04 20:09:16 +08:00
" * Is the graph directed? \n " ,
2016-06-27 05:48:32 +08:00
" * Implement both \n " ,
2015-08-04 20:09:16 +08:00
" * Do the edges have weights? \n " ,
2016-06-27 05:48:32 +08:00
" * Yes \n " ,
" * Can we assume the inputs are valid? \n " ,
2015-08-04 20:09:16 +08:00
" * Yes "
2015-08-03 18:24:38 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Test Cases \n " ,
" \n " ,
" Input: \n " ,
" * `add_edge(source, destination, weight)` \n " ,
" \n " ,
" ``` \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 " ,
" ``` \n " ,
" \n " ,
" Result: \n " ,
2015-08-05 07:37:05 +08:00
" * `source` and `destination` nodes within `graph` are connected with specified `weight`. \n " ,
" \n " ,
" Note: \n " ,
2016-06-27 05:48:32 +08:00
" * The Graph class will be used as a building block for more complex graph challenges. "
2015-08-03 18:24:38 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Algorithm \n " ,
" \n " ,
2015-08-04 20:09:16 +08:00
" ### Node \n " ,
" \n " ,
" Node will keep track of its: \n " ,
" * id \n " ,
" * visit state \n " ,
2015-08-05 07:37:05 +08:00
" * adjacent \n " ,
2015-08-04 20:09:16 +08:00
" * key: node \n " ,
" * value: weight \n " ,
" \n " ,
2016-06-27 05:48:32 +08:00
" #### 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 " ,
2015-08-04 20:09:16 +08:00
" ### Graph \n " ,
" \n " ,
" Graph will keep track of its: \n " ,
2015-08-05 07:37:05 +08:00
" * nodes \n " ,
2015-08-04 20:09:16 +08:00
" * key: node id \n " ,
" * value: node \n " ,
" \n " ,
" #### add_node \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" * Create a node with the input id \n " ,
" * Add the newly created node to the list of nodes \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) \n " ,
" \n " ,
2015-08-04 20:09:16 +08:00
" #### add_edge \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" * If the source node is not in the list of nodes, add it \n " ,
" * If the dest node is not in the list of nodes, add it \n " ,
" * Add a connection from the source node to the dest node with the given edge weight \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 1 ,
" metadata " : {
2015-08-05 07:37:05 +08:00
" collapsed " : false
2015-08-03 18:24:38 +08:00
} ,
2015-08-05 07:37:05 +08:00
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Overwriting graph.py \n "
]
}
] ,
2015-08-03 18:24:38 +08:00
" source " : [
2015-08-05 07:37:05 +08:00
" %% writefile graph.py \n " ,
2015-08-23 20:08:46 +08:00
" from enum import Enum # Python 2 users: Run pip install enum34 \n " ,
" \n " ,
" \n " ,
" class State(Enum): \n " ,
2016-06-27 05:48:32 +08:00
" unvisited = 0 \n " ,
" visiting = 1 \n " ,
2015-08-23 20:08:46 +08:00
" visited = 2 \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" \n " ,
" class Node: \n " ,
" \n " ,
" def __init__(self, id): \n " ,
" self.id = id \n " ,
2015-08-23 20:08:46 +08:00
" self.visit_state = State.unvisited \n " ,
2016-06-27 05:48:32 +08:00
" self.adjacent = {} # key = node, val = weight \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
2015-08-05 18:14:44 +08:00
" def __str__(self): \n " ,
" return str(self.id) \n " ,
" \n " ,
2016-06-27 05:48:32 +08:00
" def add_neighbor(self, neighbor, weight=0): \n " ,
" self.adjacent[neighbor] = weight \n " ,
" \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" class Graph: \n " ,
" \n " ,
" def __init__(self): \n " ,
2016-06-27 05:48:32 +08:00
" self.nodes = {} # key = node id, val = node \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" def add_node(self, id): \n " ,
" node = Node(id) \n " ,
" self.nodes[id] = node \n " ,
" return node \n " ,
" \n " ,
2016-06-27 05:48:32 +08:00
" 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) "
2015-08-05 07:37:05 +08:00
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 2 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
" %r un graph.py "
2015-08-03 18:24:38 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Unit Test "
]
} ,
{
" cell_type " : " code " ,
2015-08-05 07:37:05 +08:00
" execution_count " : 3 ,
2015-08-03 18:24:38 +08:00
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Overwriting test_graph.py \n "
]
}
] ,
" source " : [
" %% writefile test_graph.py \n " ,
" from nose.tools import assert_equal \n " ,
" \n " ,
" \n " ,
" class TestGraph(object): \n " ,
" \n " ,
2016-06-27 05:48:32 +08:00
" def create_graph(self): \n " ,
2015-08-03 18:24:38 +08:00
" graph = Graph() \n " ,
2015-08-05 07:37:05 +08:00
" for id in range(0, 6): \n " ,
" graph.add_node(id) \n " ,
2016-06-27 05:48:32 +08:00
" 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 " ,
2015-08-03 18:24:38 +08:00
" \n " ,
2015-08-05 07:37:05 +08:00
" 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 " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" print( ' Success: test_graph ' ) \n " ,
" \n " ,
2016-06-27 05:48:32 +08:00
" 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 " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" def main(): \n " ,
" test = TestGraph() \n " ,
" test.test_graph() \n " ,
2016-06-27 05:48:32 +08:00
" test.test_graph_undirected() \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" \n " ,
" if __name__ == ' __main__ ' : \n " ,
" main() "
]
} ,
{
" cell_type " : " code " ,
2015-08-05 07:37:05 +08:00
" execution_count " : 4 ,
2015-08-03 18:24:38 +08:00
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
2016-06-27 05:48:32 +08:00
" Success: test_graph \n " ,
2015-08-03 18:24:38 +08:00
" Success: test_graph \n "
]
}
] ,
" source " : [
" %r un -i test_graph.py "
]
}
] ,
" metadata " : {
" kernelspec " : {
2016-06-27 05:48:32 +08:00
" display_name " : " Python 3 " ,
2015-08-03 18:24:38 +08:00
" language " : " python " ,
2016-06-27 05:48:32 +08:00
" name " : " python3 "
2015-08-03 18:24:38 +08:00
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
2016-06-27 05:48:32 +08:00
" version " : 3
2015-08-03 18:24:38 +08:00
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
2016-06-27 05:48:32 +08:00
" pygments_lexer " : " ipython3 " ,
" version " : " 3.5.0 "
2015-08-03 18:24:38 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}