forked from codebasics/data-structures-algorithms-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
84 lines (66 loc) · 2.29 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class Graph:
def __init__(self, edges):
self.edges = edges
self.graph_dict = {}
for start, end in edges:
if start in self.graph_dict:
self.graph_dict[start].append(end)
else:
self.graph_dict[start] = [end]
print("Graph Dict:", self.graph_dict)
def get_paths(self, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if start not in self.graph_dict:
return []
paths = []
for node in self.graph_dict[start]:
if node not in path:
new_paths = self.get_paths(node, end, path)
for p in new_paths:
paths.append(p)
return paths
def get_shortest_path(self, start, end, path=[]):
path = path + [start]
if start == end:
return path
if start not in self.graph_dict:
return None
shortest_path = None
for node in self.graph_dict[start]:
if node not in path:
sp = self.get_shortest_path(node, end, path)
if sp:
if shortest_path is None or len(sp) < len(shortest_path):
shortest_path = sp
return shortest_path
if __name__ == '__main__':
routes = [
("Mumbai","Pune"),
("Mumbai", "Surat"),
("Surat", "Bangaluru"),
("Pune","Hyderabad"),
("Pune","Mysuru"),
("Hyderabad","Bangaluru"),
("Hyderabad", "Chennai"),
("Mysuru", "Bangaluru"),
("Chennai", "Bangaluru")
]
routes = [
("Mumbai", "Paris"),
("Mumbai", "Dubai"),
("Paris", "Dubai"),
("Paris", "New York"),
("Dubai", "New York"),
("New York", "Toronto"),
]
route_graph = Graph(routes)
start = "Mumbai"
end = "New York"
print(f"All paths between: {start} and {end}: ",route_graph.get_paths(start,end))
print(f"Shortest path between {start} and {end}: ", route_graph.get_shortest_path(start,end))
start = "Dubai"
end = "New York"
print(f"All paths between: {start} and {end}: ",route_graph.get_paths(start,end))
print(f"Shortest path between {start} and {end}: ", route_graph.get_shortest_path(start,end))