-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind Path.py
More file actions
34 lines (27 loc) · 797 Bytes
/
Find Path.py
File metadata and controls
34 lines (27 loc) · 797 Bytes
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
def find_path(graph, start, end, path = []): # Not the shortest path
path += [start]
if start == end:
return path
if start not in graph:
return None
for vertex in graph[start]:
if vertex not in path:
extended_path = find_path(graph, vertex, end, path)
if extended_path: return extended_path
return None
if __name__ == '__main__':
g = {'a': ['s', 'b'],
'b': ['a'],
'c': ['d', 'e', 'f', 's'],
'd': ['c'],
'e': ['c', 'h'],
'f': ['c', 'g'],
'g': ['h', 'f', 's'],
'h': ['e', 'g'],
's': ['g', 'c', 'a']
}
path = find_path(g, 'a', 'c')
if path:
print("Path: ", end=" ")
for i in path:
print(i, end=" ")