2015-08-03 18:24:38 +08:00
{
" cells " : [
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2016-07-31 20:11:18 +08:00
" 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). "
2015-08-03 18:24:38 +08:00
]
} ,
{
" 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 " ,
2016-11-13 20:32:33 +08:00
" * Can the graph have cycles? \n " ,
" * Yes \n " ,
2016-07-04 19:16:15 +08:00
" * If we try to add a node that already exists, do we just do nothing? \n " ,
" * Yes \n " ,
" * If we try to delete a node that doesn ' t exist, do we just do nothing? \n " ,
" * Yes \n " ,
2017-03-13 17:24:32 +08:00
" * Can we assume this is a connected graph? \n " ,
" * Yes \n " ,
2016-11-13 20:32:33 +08:00
" * Can we assume the inputs are valid? \n " ,
" * Yes \n " ,
2016-07-04 19:16:15 +08:00
" * Can we assume this fits memory? \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 " ,
2016-07-04 19:16:15 +08:00
" * incoming edge count (useful for algorithms such as topological sort) \n " ,
" * adjacent nodes and edge weights \n " ,
2015-08-04 20:09:16 +08:00
" \n " ,
2017-05-16 12:30:12 +08:00
" #### add_neighbor \n " ,
2016-06-27 05:48:32 +08:00
" \n " ,
2016-07-04 19:16:15 +08:00
" * If the neighbor doesn ' t already exist as an adjacent node \n " ,
2017-05-16 12:30:12 +08:00
" * Update the adjacent nodes and edge weights \n " ,
2016-07-04 19:16:15 +08:00
" * Increment the neighbor ' s incoming edge count \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) \n " ,
" \n " ,
2017-05-16 12:30:12 +08:00
" #### remove_neighbor \n " ,
2016-07-04 19:16:15 +08:00
" \n " ,
" * If the neighbor exists as an adjacent node \n " ,
" * Decrement the neighbor ' s incoming edge count \n " ,
" * Remove the neighbor as an adjacent node \n " ,
2016-06-27 05:48:32 +08:00
" \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
" \n " ,
" #### add_node \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
2016-07-04 19:16:15 +08:00
" * If node already exists, return it \n " ,
" * Create a node with the given id \n " ,
" * Add the newly created node to the collection of nodes \n " ,
2015-08-03 18:24:38 +08:00
" \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 " ,
2016-07-04 19:16:15 +08:00
" * If the source node is not in the collection of nodes, add it \n " ,
" * If the dest node is not in the collection of nodes, add it \n " ,
2015-08-03 18:24:38 +08:00
" * Add a connection from the source node to the dest node with the given edge weight \n " ,
" \n " ,
2016-07-04 19:16:15 +08:00
" #### add_undirected_edge \n " ,
" \n " ,
" * Call add_edge \n " ,
" * Also add a connection from the dest node to the source node with the given edge weight \n " ,
" \n " ,
2015-08-03 18:24:38 +08:00
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 1 ,
2020-07-11 09:02:32 +08:00
" metadata " : { } ,
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 " ,
2017-01-26 17:55:45 +08:00
" \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 " ,
2016-07-04 19:16:15 +08:00
" def __init__(self, key): \n " ,
" self.key = key \n " ,
2015-08-23 20:08:46 +08:00
" self.visit_state = State.unvisited \n " ,
2016-07-04 19:16:15 +08:00
" self.incoming_edges = 0 \n " ,
" self.adj_nodes = {} # Key = key, val = Node \n " ,
" self.adj_weights = {} # Key = key, val = weight \n " ,
" \n " ,
" def __repr__(self): \n " ,
" return str(self.key) \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
2016-07-04 19:16:15 +08:00
" def __lt__(self, other): \n " ,
" return self.key < other.key \n " ,
2015-08-05 18:14:44 +08:00
" \n " ,
2016-06-27 05:48:32 +08:00
" def add_neighbor(self, neighbor, weight=0): \n " ,
2016-11-13 20:32:33 +08:00
" if neighbor is None or weight is None: \n " ,
" raise TypeError( ' neighbor or weight cannot be None ' ) \n " ,
2016-07-04 19:16:15 +08:00
" neighbor.incoming_edges += 1 \n " ,
" self.adj_weights[neighbor.key] = weight \n " ,
" self.adj_nodes[neighbor.key] = neighbor \n " ,
" \n " ,
" def remove_neighbor(self, neighbor): \n " ,
2016-11-13 20:32:33 +08:00
" if neighbor is None: \n " ,
" raise TypeError( ' neighbor cannot be None ' ) \n " ,
" if neighbor.key not in self.adj_nodes: \n " ,
" raise KeyError( ' neighbor not found ' ) \n " ,
2016-07-20 19:29:48 +08:00
" neighbor.incoming_edges -= 1 \n " ,
" del self.adj_weights[neighbor.key] \n " ,
" del self.adj_nodes[neighbor.key] \n " ,
2016-06-27 05:48:32 +08:00
" \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" class Graph: \n " ,
" \n " ,
" def __init__(self): \n " ,
2016-07-04 19:16:15 +08:00
" self.nodes = {} # Key = key, val = Node \n " ,
" \n " ,
" def add_node(self, key): \n " ,
" if key is None: \n " ,
2016-11-13 20:32:33 +08:00
" raise TypeError( ' key cannot be None ' ) \n " ,
2016-11-24 10:21:38 +08:00
" if key not in self.nodes: \n " ,
" self.nodes[key] = Node(key) \n " ,
2016-07-04 19:16:15 +08:00
" return self.nodes[key] \n " ,
" \n " ,
" def add_edge(self, source_key, dest_key, weight=0): \n " ,
" if source_key is None or dest_key is None: \n " ,
2016-11-24 10:21:38 +08:00
" raise KeyError( ' Invalid key ' ) \n " ,
2016-07-04 19:16:15 +08:00
" if source_key not in self.nodes: \n " ,
" self.add_node(source_key) \n " ,
" if dest_key not in self.nodes: \n " ,
" self.add_node(dest_key) \n " ,
2016-11-24 10:21:38 +08:00
" self.nodes[source_key].add_neighbor(self.nodes[dest_key], weight) \n " ,
2016-07-04 19:16:15 +08:00
" \n " ,
2016-11-13 20:32:33 +08:00
" def add_undirected_edge(self, src_key, dst_key, weight=0): \n " ,
" if src_key is None or dst_key is None: \n " ,
" raise TypeError( ' key cannot be None ' ) \n " ,
" self.add_edge(src_key, dst_key, weight) \n " ,
" self.add_edge(dst_key, src_key, weight) "
2015-08-05 07:37:05 +08:00
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 2 ,
2020-07-11 09:02:32 +08:00
" metadata " : { } ,
2015-08-05 07:37:05 +08:00
" 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 ,
2020-07-11 09:02:32 +08:00
" metadata " : { } ,
2015-08-03 18:24:38 +08:00
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Overwriting test_graph.py \n "
]
}
] ,
" source " : [
" %% writefile test_graph.py \n " ,
2020-07-11 09:02:32 +08:00
" import unittest \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" \n " ,
2020-07-11 09:02:32 +08:00
" class TestGraph(unittest.TestCase): \n " ,
2015-08-03 18:24:38 +08:00
" \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 " ,
2016-07-04 19:16:15 +08:00
" for key in range(0, 6): \n " ,
" graph.add_node(key) \n " ,
2016-06-27 05:48:32 +08:00
" return graph \n " ,
" \n " ,
" def test_graph(self): \n " ,
" graph = self.create_graph() \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 " ,
2020-07-11 09:02:32 +08:00
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[1].key], 5) \n " ,
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2) \n " ,
" self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3) \n " ,
" self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[3].key], 4) \n " ,
" self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[4].key], 5) \n " ,
" self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[5].key], 6) \n " ,
" self.assertEqual(graph.nodes[4].adj_weights[graph.nodes[0].key], 7) \n " ,
" self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[4].key], 8) \n " ,
" self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[2].key], 9) \n " ,
" \n " ,
" self.assertEqual(graph.nodes[0].incoming_edges, 1) \n " ,
" self.assertEqual(graph.nodes[1].incoming_edges, 1) \n " ,
" self.assertEqual(graph.nodes[2].incoming_edges, 2) \n " ,
" self.assertEqual(graph.nodes[3].incoming_edges, 1) \n " ,
" self.assertEqual(graph.nodes[4].incoming_edges, 2) \n " ,
" self.assertEqual(graph.nodes[5].incoming_edges, 2) \n " ,
2016-07-04 19:16:15 +08:00
" \n " ,
" graph.nodes[0].remove_neighbor(graph.nodes[1]) \n " ,
2020-07-11 09:02:32 +08:00
" self.assertEqual(graph.nodes[1].incoming_edges, 0) \n " ,
2016-07-04 19:16:15 +08:00
" graph.nodes[3].remove_neighbor(graph.nodes[4]) \n " ,
2020-07-11 09:02:32 +08:00
" self.assertEqual(graph.nodes[4].incoming_edges, 1) \n " ,
2016-07-04 19:16:15 +08:00
" \n " ,
2020-07-11 09:02:32 +08:00
" self.assertEqual(graph.nodes[0] < graph.nodes[1], True) \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 " ,
" 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 " ,
2020-07-11 09:02:32 +08:00
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[1].key], 5) \n " ,
" self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[0].key], 5) \n " ,
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2) \n " ,
" self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[0].key], 2) \n " ,
" self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3) \n " ,
" self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[1].key], 3) \n " ,
2016-06-27 05:48:32 +08:00
" \n " ,
2016-07-04 19:16:15 +08:00
" print( ' Success: test_graph_undirected ' ) \n " ,
2016-06-27 05:48:32 +08:00
" \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 ,
2020-07-11 09:02:32 +08:00
" metadata " : { } ,
2015-08-03 18:24:38 +08:00
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
2016-06-27 05:48:32 +08:00
" Success: test_graph \n " ,
2016-07-04 19:16:15 +08:00
" Success: test_graph_undirected \n "
2015-08-03 18:24:38 +08:00
]
}
] ,
" 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 " ,
2020-07-11 09:02:32 +08:00
" version " : " 3.7.2 "
2015-08-03 18:24:38 +08:00
}
} ,
" nbformat " : 4 ,
2020-07-11 09:02:32 +08:00
" nbformat_minor " : 1
2015-08-03 18:24:38 +08:00
}