From cd50f73069ad7c842ee9378229ef95fa13b7fced Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 10 Sep 2016 07:42:21 -0400 Subject: [PATCH] Move graph path exists to a class --- .../path_exists_challenge.ipynb | 16 +++---- .../path_exists_solution.ipynb | 42 ++++++++++--------- .../graph_path_exists/test_path_exists.py | 8 ++-- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/graphs_trees/graph_path_exists/path_exists_challenge.ipynb b/graphs_trees/graph_path_exists/path_exists_challenge.ipynb index 51a3100..8ffdb97 100644 --- a/graphs_trees/graph_path_exists/path_exists_challenge.ipynb +++ b/graphs_trees/graph_path_exists/path_exists_challenge.ipynb @@ -106,9 +106,11 @@ }, "outputs": [], "source": [ - "def path_exists(start, end):\n", - " # TODO: Implement me\n", - " pass" + "class GraphPathExists(Graph):\n", + "\n", + " def path_exists(self, start, end):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -141,7 +143,7 @@ "\n", " def test_path_exists(self):\n", " nodes = []\n", - " graph = Graph()\n", + " graph = GraphPathExists()\n", " for id in range(0, 6):\n", " nodes.append(graph.add_node(id))\n", " graph.add_edge(0, 1, 5)\n", @@ -153,9 +155,9 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", "\n", - " assert_equal(path_exists(nodes[0], nodes[2]), True)\n", - " assert_equal(path_exists(nodes[0], nodes[0]), True)\n", - " assert_equal(path_exists(nodes[4], nodes[5]), False)\n", + " assert_equal(graph.path_exists(nodes[0], nodes[2]), True)\n", + " assert_equal(graph.path_exists(nodes[0], nodes[0]), True)\n", + " assert_equal(graph.path_exists(nodes[4], nodes[5]), False)\n", "\n", " print('Success: test_path_exists')\n", "\n", diff --git a/graphs_trees/graph_path_exists/path_exists_solution.ipynb b/graphs_trees/graph_path_exists/path_exists_solution.ipynb index 899ee08..da45b70 100644 --- a/graphs_trees/graph_path_exists/path_exists_solution.ipynb +++ b/graphs_trees/graph_path_exists/path_exists_solution.ipynb @@ -124,23 +124,25 @@ "from collections import deque\n", "\n", "\n", - "def path_exists(start, end):\n", - " if start is None or end is None:\n", - " return False\n", - " if start is end:\n", - " return True\n", - " queue = deque()\n", - " queue.append(start)\n", - " start.visit_state = State.visited\n", - " while queue:\n", - " node = queue.popleft()\n", - " if node is end:\n", + "class GraphPathExists(Graph):\n", + "\n", + " def path_exists(self, start, end):\n", + " if start is None or end is None:\n", + " return False\n", + " if start is end:\n", " return True\n", - " for adj_node in node.adjacent:\n", - " if adj_node.visit_state == State.unvisited:\n", - " queue.append(adj_node)\n", - " adj_node.visit_state = State.visited\n", - " return False" + " queue = deque()\n", + " queue.append(start)\n", + " start.visit_state = State.visited\n", + " while queue:\n", + " node = queue.popleft()\n", + " if node is end:\n", + " return True\n", + " for adj_node in node.adj_nodes.values():\n", + " if adj_node.visit_state == State.unvisited:\n", + " queue.append(adj_node)\n", + " adj_node.visit_state = State.visited\n", + " return False" ] }, { @@ -174,7 +176,7 @@ "\n", " def test_path_exists(self):\n", " nodes = []\n", - " graph = Graph()\n", + " graph = GraphPathExists()\n", " for id in range(0, 6):\n", " nodes.append(graph.add_node(id))\n", " graph.add_edge(0, 1, 5)\n", @@ -186,9 +188,9 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", "\n", - " assert_equal(path_exists(nodes[0], nodes[2]), True)\n", - " assert_equal(path_exists(nodes[0], nodes[0]), True)\n", - " assert_equal(path_exists(nodes[4], nodes[5]), False)\n", + " assert_equal(graph.path_exists(nodes[0], nodes[2]), True)\n", + " assert_equal(graph.path_exists(nodes[0], nodes[0]), True)\n", + " assert_equal(graph.path_exists(nodes[4], nodes[5]), False)\n", "\n", " print('Success: test_path_exists')\n", "\n", diff --git a/graphs_trees/graph_path_exists/test_path_exists.py b/graphs_trees/graph_path_exists/test_path_exists.py index a99bca0..a7374c7 100644 --- a/graphs_trees/graph_path_exists/test_path_exists.py +++ b/graphs_trees/graph_path_exists/test_path_exists.py @@ -5,7 +5,7 @@ class TestPathExists(object): def test_path_exists(self): nodes = [] - graph = Graph() + graph = GraphPathExists() for id in range(0, 6): nodes.append(graph.add_node(id)) graph.add_edge(0, 1, 5) @@ -17,9 +17,9 @@ class TestPathExists(object): graph.add_edge(3, 2, 7) graph.add_edge(3, 4, 8) - assert_equal(path_exists(nodes[0], nodes[2]), True) - assert_equal(path_exists(nodes[0], nodes[0]), True) - assert_equal(path_exists(nodes[4], nodes[5]), False) + assert_equal(graph.path_exists(nodes[0], nodes[2]), True) + assert_equal(graph.path_exists(nodes[0], nodes[0]), True) + assert_equal(graph.path_exists(nodes[4], nodes[5]), False) print('Success: test_path_exists')