-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch_graph.py
49 lines (38 loc) · 1.1 KB
/
search_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import sys
sys.path.append(
'/'.join(os.getcwd().split('/')[:-1] + [
'lesson-06-js-graph-data-structure'
])
)
sys.path.append(
'/'.join(os.getcwd().split('/')[:-1] + [
'lesson-02-queue-data-structure-in-js'
])
)
from graph import Graph
from custom_queue import CustomQueue
class SearchGraph(Graph):
def breadth_first_search(self, key, func):
head = self.get_node(key)
hist = { node.key: False for node in self.nodes }
custom_queue = CustomQueue()
custom_queue.enqueue(head)
while not custom_queue.is_empty():
curr = custom_queue.dequeue()
if not hist[curr.key]:
func(curr)
hist[curr.key] = True
for neighbor in curr.neighbors:
if not hist[neighbor.key]:
custom_queue.enqueue(neighbor)
def depth_first_search(self, key, func):
head = self.get_node(key)
hist = { node.key: False for node in self.nodes }
def traverse(node):
if hist[node.key]: return
func(node)
hist[node.key] = True
for neighbor in node.neighbors:
traverse(neighbor)
traverse(head)