mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-07 18:08:01 +00:00
Move graph bfs to a class
This commit is contained in:
@@ -103,9 +103,11 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def bfs(root, visit_func):\n",
|
"class GraphBfs(Graph):\n",
|
||||||
" # TODO: Implement me\n",
|
"\n",
|
||||||
" pass"
|
" def bfs(self, root, visit_func):\n",
|
||||||
|
" # TODO: Implement me\n",
|
||||||
|
" pass"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -152,7 +154,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" def test_bfs(self):\n",
|
" def test_bfs(self):\n",
|
||||||
" nodes = []\n",
|
" nodes = []\n",
|
||||||
" graph = Graph()\n",
|
" graph = GraphBfs()\n",
|
||||||
" for id in range(0, 6):\n",
|
" for id in range(0, 6):\n",
|
||||||
" nodes.append(graph.add_node(id))\n",
|
" nodes.append(graph.add_node(id))\n",
|
||||||
" graph.add_edge(0, 1, 5)\n",
|
" graph.add_edge(0, 1, 5)\n",
|
||||||
@@ -163,7 +165,7 @@
|
|||||||
" graph.add_edge(2, 1, 6)\n",
|
" graph.add_edge(2, 1, 6)\n",
|
||||||
" graph.add_edge(3, 2, 7)\n",
|
" graph.add_edge(3, 2, 7)\n",
|
||||||
" graph.add_edge(3, 4, 8)\n",
|
" graph.add_edge(3, 4, 8)\n",
|
||||||
" bfs(nodes[0], self.results.add_result)\n",
|
" graph.bfs(nodes[0], self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
|
" assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bfs')\n",
|
" print('Success: test_bfs')\n",
|
||||||
|
|||||||
@@ -115,19 +115,21 @@
|
|||||||
"from collections import deque\n",
|
"from collections import deque\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def bfs(root, visit_func):\n",
|
"class GraphBfs(Graph):\n",
|
||||||
" if root is None:\n",
|
"\n",
|
||||||
" return\n",
|
" def bfs(self, root, visit_func):\n",
|
||||||
" queue = deque()\n",
|
" if root is None:\n",
|
||||||
" queue.append(root)\n",
|
" return\n",
|
||||||
" root.visit_state = State.visited\n",
|
" queue = deque()\n",
|
||||||
" while queue:\n",
|
" queue.append(root)\n",
|
||||||
" node = queue.popleft()\n",
|
" root.visit_state = State.visited\n",
|
||||||
" visit_func(node)\n",
|
" while queue:\n",
|
||||||
" for adjacent_node in node.adjacent:\n",
|
" node = queue.popleft()\n",
|
||||||
" if adjacent_node.visit_state == State.unvisited:\n",
|
" visit_func(node)\n",
|
||||||
" queue.append(adjacent_node)\n",
|
" for adjacent_node in node.adj_nodes.values():\n",
|
||||||
" adjacent_node.visit_state = State.visited"
|
" if adjacent_node.visit_state == State.unvisited:\n",
|
||||||
|
" queue.append(adjacent_node)\n",
|
||||||
|
" adjacent_node.visit_state = State.visited"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -175,7 +177,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" def test_bfs(self):\n",
|
" def test_bfs(self):\n",
|
||||||
" nodes = []\n",
|
" nodes = []\n",
|
||||||
" graph = Graph()\n",
|
" graph = GraphBfs()\n",
|
||||||
" for id in range(0, 6):\n",
|
" for id in range(0, 6):\n",
|
||||||
" nodes.append(graph.add_node(id))\n",
|
" nodes.append(graph.add_node(id))\n",
|
||||||
" graph.add_edge(0, 1, 5)\n",
|
" graph.add_edge(0, 1, 5)\n",
|
||||||
@@ -186,7 +188,7 @@
|
|||||||
" graph.add_edge(2, 1, 6)\n",
|
" graph.add_edge(2, 1, 6)\n",
|
||||||
" graph.add_edge(3, 2, 7)\n",
|
" graph.add_edge(3, 2, 7)\n",
|
||||||
" graph.add_edge(3, 4, 8)\n",
|
" graph.add_edge(3, 4, 8)\n",
|
||||||
" bfs(nodes[0], self.results.add_result)\n",
|
" graph.bfs(nodes[0], self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
|
" assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bfs')\n",
|
" print('Success: test_bfs')\n",
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ class TestBfs(object):
|
|||||||
|
|
||||||
def test_bfs(self):
|
def test_bfs(self):
|
||||||
nodes = []
|
nodes = []
|
||||||
graph = Graph()
|
graph = GraphBfs()
|
||||||
for id in range(0, 6):
|
for id in range(0, 6):
|
||||||
nodes.append(graph.add_node(id))
|
nodes.append(graph.add_node(id))
|
||||||
graph.add_edge(0, 1, 5)
|
graph.add_edge(0, 1, 5)
|
||||||
@@ -19,7 +19,7 @@ class TestBfs(object):
|
|||||||
graph.add_edge(2, 1, 6)
|
graph.add_edge(2, 1, 6)
|
||||||
graph.add_edge(3, 2, 7)
|
graph.add_edge(3, 2, 7)
|
||||||
graph.add_edge(3, 4, 8)
|
graph.add_edge(3, 4, 8)
|
||||||
bfs(nodes[0], self.results.add_result)
|
graph.bfs(nodes[0], self.results.add_result)
|
||||||
assert_equal(str(self.results), "[0, 1, 4, 5, 3, 2]")
|
assert_equal(str(self.results), "[0, 1, 4, 5, 3, 2]")
|
||||||
|
|
||||||
print('Success: test_bfs')
|
print('Success: test_bfs')
|
||||||
|
|||||||
Reference in New Issue
Block a user