Skip to content

Commit 777ff6e

Browse files
committed
graph
1 parent a2faf3f commit 777ff6e

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

DSA-Python/DataStructures/graph.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
"""
2+
Graph Visualization
3+
4+
member1--member5
5+
|
6+
member3--member0--member2
7+
|
8+
member4
9+
10+
11+
|------city1----city3
12+
| | |
13+
city4----city2------|
14+
city5
15+
16+
17+
Graph Usage
18+
Facebook Friends Suggestion
19+
[m0 is friend of m1 & m5 is friend of m1; it is likely that m0 is friend of m5 too...
20+
FB will suggest m5 as friend suggestion to m0]
21+
Flight 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)]
27+
Google Maps
28+
Internet
29+
E-commerce shopping Recommendation
30+
31+
Difference between TREE and GRAPH:
32+
In 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+
class Graph:
37+
def __init__(self, edges):
38+
self.edges = edges
39+
# Transform route tuple to route dictionary
40+
self.graph_dict = {} # blank dictionary
41+
for start, end in self.edges:
42+
if start in self.graph_dict: # element1 is already in dictionary
43+
self.graph_dict[start].append(end) # add another element associated to element1 in graph_dictionary
44+
else:
45+
self.graph_dict[start] = [end] # if element1 is not present, add it to graph_dictionary
46+
print("Graph dictionary: ", self.graph_dict) # print route dictionary
47+
48+
# get paths between start-point and end-point
49+
def getpath(self, start, end, path=[]):
50+
path = path + [start]
51+
if start == end:
52+
return [path]
53+
54+
if start not in self.graph_dict: # if one-way between one node, say Chennai; return []
55+
# Chennai is not as a starting point, so it has no route associated
56+
return []
57+
58+
paths = []
59+
for node in self.graph_dict[start]:
60+
if node not in path:
61+
new_path = self.getpath(node, end, path)
62+
for p in new_path:
63+
paths.append(p)
64+
return paths
65+
66+
# method to find shortest path between start-point and end-point
67+
def getShortestPath(self, start, end, path=[]):
68+
path = path + [start]
69+
70+
# if starting-point and end-point are same
71+
if start == end:
72+
return path
73+
74+
# If no path available from a point, return None
75+
if start not in self.graph_dict:
76+
return None
77+
78+
# searching for shortest path
79+
shortest_path = None # shortest path initialised
80+
for node in self.graph_dict[start]:
81+
if node not in path:
82+
sp = self.getShortestPath(node, end, path)
83+
if sp:
84+
# if no shortest path is available; but in later iteration, we may have a path
85+
# so check if it is shorter than original path (array of routes) or not
86+
if shortest_path is None or len(sp) < len(shortest_path):
87+
shortest_path = sp # shortest path returned
88+
89+
return shortest_path
90+
91+
92+
93+
94+
if __name__ == '__main__':
95+
96+
routes = [
97+
("Mumbai","Pune"),
98+
("Mumbai", "Surat"),
99+
("Surat", "Bangaluru"),
100+
("Pune","Hyderabad"),
101+
("Pune","Mysuru"),
102+
("Hyderabad","Bangaluru"),
103+
("Hyderabad", "Chennai"),
104+
("Mysuru", "Bangaluru"),
105+
("Chennai", "Bangaluru")
106+
]
107+
108+
routes = [
109+
("Mumbai", "Paris"),
110+
("Mumbai", "Dubai"),
111+
("Paris", "Dubai"),
112+
("Paris", "New York"),
113+
("Dubai", "New York"),
114+
("New York", "Toronto"),
115+
]
116+
117+
route_graph = Graph(routes)
118+
119+
''' start == end
120+
start = "Mumbai"
121+
end = "Mumbai"
122+
[op]: [['Mumbai']]'''
123+
124+
''' start not in self.graph_dict
125+
start = "Chennai"
126+
end = "Mumbai"
127+
[op]: []'''
128+
129+
start = "Mumbai"
130+
end = "New York"
131+
132+
print(f"All paths between: {start} and {end}: ",route_graph.getpath(start,end))
133+
print(f"Shortest path between {start} and {end}: ", route_graph.getShortestPath(start,end))
134+
135+
start = "Dubai"
136+
end = "New York"
137+
138+
print(f"All paths between: {start} and {end}: ",route_graph.getpath(start,end))
139+
print(f"Shortest path between {start} and {end}: ", route_graph.getShortestPath(start,end))

0 commit comments

Comments
 (0)