-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
55 lines (45 loc) · 1.32 KB
/
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
50
51
52
53
from collections import deque
class graph:
def __init__(self,edges):
self.edges = edges
self.dic = {}
self.q = deque()
for start,end in self.edges:
if start in self.dic:
self.dic[start].append(end)
else:
self.dic[start] = [end]
def get_path(self,start,end,path=[]):
path = path + [start]
if start == end:
return [path]
if start not in self.dic:
return []
paths = []
for place in self.dic[start]:
if place not in path:
new_paths = self.get_path(place,end,path)
for i in new_paths:
paths.append(i)
return paths
def bfs(self,start,end,path=[]):
path = path + [start]
if start == end:
return [path]
if not self.q:
self.q.append(start)
while self.q:
node =self.q.popleft()
if node not in path:
if __name__ == '__main__':
routes = [
("Mumbai", "Paris"),
("Mumbai", "Dubai"),
("Paris", "Dubai"),
("Paris", "New York"),
("Dubai", "New York"),
("New York", "Toronto"),
]
eg = graph(routes)
print(eg.get_path('Mumbai', 'New York'))
print(eg.bfs('Mumbai', 'New York'))