Amazing-Python-Scripts
144 строки · 4.5 Кб
1"""
2Graph Visualization
3
4member1--member5
5|
6member3--member0--member2
7|
8member4
9
10
11|------city1----city3
12| | |
13city4----city2------|
14city5
15
16
17Graph Usage
18Facebook Friends Suggestion
19[m0 is friend of m1 & m5 is friend of m1; it is likely that m0 is friend of m5 too...
20FB will suggest m5 as friend suggestion to m0]
21Flight Routes : To find shortest path between two nodes
22[(city1,city2),(city1,city3),(city1,city4),
23(city2,city1),(city2,city4),(city2,city5),
24(city3,city1),(city3,city5),
25(city4,city1),(city4,city2),
26(city5,city2),(city5,city3)]
27Google Maps
28Internet
29E-commerce shopping Recommendation
30
31Difference between TREE and GRAPH:
32In TREE, there is only one path between two nodes. GRAPH is a complex DS where two nodes can randomly be connected
33
34"""
35
36
37class Graph:38def __init__(self, edges):39self.edges = edges40# Transform route tuple to route dictionary41self.graph_dict = {} # blank dictionary42for start, end in self.edges:43if start in self.graph_dict: # element1 is already in dictionary44# add another element associated to element1 in graph_dictionary45self.graph_dict[start].append(end)46else:47# if element1 is not present, add it to graph_dictionary48self.graph_dict[start] = [end]49print("Graph dictionary: ", self.graph_dict) # print route dictionary50
51# get paths between start-point and end-point52def getpath(self, start, end, path=[]):53path = path + [start]54if start == end:55return [path]56
57if start not in self.graph_dict: # if one-way between one node, say Chennai; return []58# Chennai is not as a starting point, so it has no route associated59return []60
61paths = []62for node in self.graph_dict[start]:63if node not in path:64new_path = self.getpath(node, end, path)65for p in new_path:66paths.append(p)67return paths68
69# method to find shortest path between start-point and end-point70def getShortestPath(self, start, end, path=[]):71path = path + [start]72
73# if starting-point and end-point are same74if start == end:75return path76
77# If no path available from a point, return None78if start not in self.graph_dict:79return None80
81# searching for shortest path82shortest_path = None # shortest path initialised83for node in self.graph_dict[start]:84if node not in path:85sp = self.getShortestPath(node, end, path)86if sp:87# if no shortest path is available; but in later iteration, we may have a path88# so check if it is shorter than original path (array of routes) or not89if shortest_path is None or len(sp) < len(shortest_path):90shortest_path = sp # shortest path returned91
92return shortest_path93
94
95if __name__ == '__main__':96
97routes = [98("Mumbai", "Pune"),99("Mumbai", "Surat"),100("Surat", "Bangaluru"),101("Pune", "Hyderabad"),102("Pune", "Mysuru"),103("Hyderabad", "Bangaluru"),104("Hyderabad", "Chennai"),105("Mysuru", "Bangaluru"),106("Chennai", "Bangaluru")107]108
109routes = [110("Mumbai", "Paris"),111("Mumbai", "Dubai"),112("Paris", "Dubai"),113("Paris", "New York"),114("Dubai", "New York"),115("New York", "Toronto"),116]117
118route_graph = Graph(routes)119
120''' start == end121start = "Mumbai"
122end = "Mumbai"
123[op]: [['Mumbai']]'''
124
125''' start not in self.graph_dict126start = "Chennai"
127end = "Mumbai"
128[op]: []'''
129
130start = "Mumbai"131end = "New York"132
133print(f"All paths between: {start} and {end}: ",134route_graph.getpath(start, end))135print(f"Shortest path between {start} and {end}: ",136route_graph.getShortestPath(start, end))137
138start = "Dubai"139end = "New York"140
141print(f"All paths between: {start} and {end}: ",142route_graph.getpath(start, end))143print(f"Shortest path between {start} and {end}: ",144route_graph.getShortestPath(start, end))145