-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search algorithms
42 lines (36 loc) · 1.15 KB
/
Search algorithms
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
#depth first search
def dfs(graph, current_vertex, target_value, visited = None):
if visited is None:
visited = []
visited.append(current_vertex)
if current_vertex is target_value:
return visited
for neighbor in graph[current_vertex]:
if neighbor not in visited:
path = dfs(graph, neighbor, target_value, visited)
if path:
return path
#breadth first search
def bfs(graph, start_vertex, target_value):
path = [start_vertex]
vertex_and_path = [start_vertex, path]
bfs_queue = [vertex_and_path]
visited = set()
while bfs_queue:
current_vertex, path = bfs_queue.pop(0)
visited.add(current_vertex)
for neighbor in graph[current_vertex]:
if neighbor not in visited:
if neighbor is target_value:
return path + [neighbor]
else:
bfs_queue.append([neighbor, path + [neighbor]])
some_hazardous_graph = {
'lava': set(['sharks', 'piranhas']),
'sharks': set(['piranhas', 'bees']),
'piranhas': set(['bees']),
'bees': set(['lasers']),
'lasers': set([])
}
print(bfs(some_hazardous_graph, 'sharks', 'bees'))
print(dfs(some_hazardous_graph, 'sharks', 'bees'))