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 " : [
" # Challenge 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) \n " ,
" * [Solution Notebook](#Solution-Notebook) "
]
} ,
{
" 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 " ,
2016-07-04 19:16:15 +08:00
" * Yes \n " ,
" * 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 " ,
2018-04-09 08:22:14 +08:00
" Refer to the [Solution Notebook](http://nbviewer.jupyter.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph/graph_solution.ipynb). If you are stuck and need a hint, the solution notebook ' s algorithm discussion might be a good place to start. "
2015-08-03 18:24:38 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : null ,
2018-04-09 08:22:14 +08:00
" metadata " : { } ,
2015-08-03 18:24:38 +08:00
" outputs " : [ ] ,
" source " : [
2016-07-04 19:16:15 +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-07-04 19:16:15 +08:00
" unvisited = 0 \n " ,
" visiting = 1 \n " ,
" visited = 2 \n " ,
" \n " ,
" \n " ,
2015-08-03 18:24:38 +08:00
" class Node: \n " ,
" \n " ,
2016-07-04 19:16:15 +08:00
" def __init__(self, key): \n " ,
" self.key = key \n " ,
" self.visit_state = State.unvisited \n " ,
" self.incoming_edges = 0 \n " ,
" self.adj_nodes = {} # Key = key, val = Node \n " ,
2018-08-20 21:48:34 +08:00
" self.adj_weights = {} # Key = key, val = weight \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
2016-07-04 19:16:15 +08:00
" def __repr__(self): \n " ,
2017-04-10 18:35:58 +08:00
" return str(self.key) \n " ,
2015-08-05 18:14:44 +08:00
" \n " ,
2017-04-10 18:35:58 +08:00
" def __lt__(self, other): \n " ,
" return self.key < other.key \n " ,
2016-07-04 19:16:15 +08:00
" \n " ,
2016-06-27 05:48:32 +08:00
" def add_neighbor(self, neighbor, weight=0): \n " ,
" # TODO: Implement me \n " ,
" pass \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
2016-07-04 19:16:15 +08:00
" def remove_neighbor(self, neighbor): \n " ,
" # TODO: Implement me \n " ,
" pass \n " ,
" \n " ,
" \n " ,
2015-08-03 18:24:38 +08:00
" class Graph: \n " ,
" \n " ,
" def __init__(self): \n " ,
2016-07-04 19:16:15 +08:00
" self.nodes = {} # Key = key, val = Node \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" def add_node(self, id): \n " ,
" # TODO: Implement me \n " ,
2015-08-05 07:37:05 +08:00
" pass \n " ,
2015-08-03 18:24:38 +08:00
" \n " ,
" def add_edge(self, source, dest, weight=0): \n " ,
2015-08-05 07:37:05 +08:00
" # TODO: Implement me \n " ,
2016-06-27 05:48:32 +08:00
" pass \n " ,
" \n " ,
" def add_undirected_edge(self, source, dest, weight=0): \n " ,
" # TODO: Implement me \n " ,
2015-08-05 07:37:05 +08:00
" pass "
2015-08-03 18:24:38 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Unit Test "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" **The following unit test is expected to fail until you solve the challenge.** "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : null ,
2018-04-09 08:22:14 +08:00
" metadata " : { } ,
2015-08-03 18:24:38 +08:00
" outputs " : [ ] ,
" source " : [
" # %lo ad 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 " ,
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 " ,
2016-07-04 19:16:15 +08:00
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5) \n " ,
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2) \n " ,
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3) \n " ,
" assert_equal(graph.nodes[2].adj_weights[graph.nodes[3].key], 4) \n " ,
" assert_equal(graph.nodes[3].adj_weights[graph.nodes[4].key], 5) \n " ,
" assert_equal(graph.nodes[3].adj_weights[graph.nodes[5].key], 6) \n " ,
" assert_equal(graph.nodes[4].adj_weights[graph.nodes[0].key], 7) \n " ,
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[4].key], 8) \n " ,
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[2].key], 9) \n " ,
" \n " ,
" assert_equal(graph.nodes[0].incoming_edges, 1) \n " ,
" assert_equal(graph.nodes[1].incoming_edges, 1) \n " ,
" assert_equal(graph.nodes[2].incoming_edges, 2) \n " ,
" assert_equal(graph.nodes[3].incoming_edges, 1) \n " ,
" assert_equal(graph.nodes[4].incoming_edges, 2) \n " ,
" assert_equal(graph.nodes[5].incoming_edges, 2) \n " ,
" \n " ,
" graph.nodes[0].remove_neighbor(graph.nodes[1]) \n " ,
" assert_equal(graph.nodes[1].incoming_edges, 0) \n " ,
" graph.nodes[3].remove_neighbor(graph.nodes[4]) \n " ,
" assert_equal(graph.nodes[4].incoming_edges, 1) \n " ,
" \n " ,
" assert_equal(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 " ,
2016-07-04 19:16:15 +08:00
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5) \n " ,
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[0].key], 5) \n " ,
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2) \n " ,
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[0].key], 2) \n " ,
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3) \n " ,
" assert_equal(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 " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Solution Notebook \n " ,
" \n " ,
" Review the [Solution Notebook](https://github.com/donnemartin/interactive-coding-challenges/graphs_trees/graphs/graph_solution.ipynb) for a discussion on algorithms and code solutions. "
]
}
] ,
" 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 " ,
2018-04-09 08:22:14 +08:00
" version " : " 3.6.4 "
2015-08-03 18:24:38 +08:00
}
} ,
" nbformat " : 4 ,
2018-04-09 08:22:14 +08:00
" nbformat_minor " : 1
2015-08-03 18:24:38 +08:00
}