-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath_find.py
More file actions
51 lines (42 loc) · 1.47 KB
/
path_find.py
File metadata and controls
51 lines (42 loc) · 1.47 KB
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
50
51
graph = {'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']}
def find_path(graph, start, end, path=[]):
path = path + [start]
#base case
if start == end:
return path
#check if start is in the graph
if not graph.has_key(start):
return None
#for each child
for node in graph[start]:
if node not in path:
#find the child's path
newpath = find_path(graph, node, end, path)
if newpath: return newpath
return None
def find_shortest_path(graph, start, end, path=[]):
path = path + [start]
#base case
if start == end:
return path
#check if start is in graph
if not graph.has_key(start):
return None
shortest = None
for node in graph[start]:
if node not in path:
#find the child's path until base case
newpath = find_shortest_path(graph, node, end, path)
if newpath:
#extra check for making sure shortest path
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
# print list(bfs_paths(graph, 'A', 'F')) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]
print find_path(graph, 'A', 'F')
print find_shortest_path(graph, 'A', 'F')