-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathBasicGraphs.py
More file actions
54 lines (49 loc) · 1.67 KB
/
Copy pathBasicGraphs.py
File metadata and controls
54 lines (49 loc) · 1.67 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
52
53
54
# the program below implements the above graph using two functions namely add_edge to add edges to the graph and show_graph to show all the edges in the graph.
class Graph:
graph_dict={}
def addEdge(self,node,neighbour):
if node not in self.graph_dict:
self.graph_dict[node]=[neighbour]
else:
self.graph_dict[node].append(neighbour)
def show_edges(self):
for node in self.graph_dict:
for neighbour in self.graph_dict[node]:
print("(",node,", ",neighbour,")")
# paths between two nodes.
def find_path(self,start,end,path=[]):
path = path + [start]
if start==end:
return path
'''Loop for transversing all neighbouring nodes of the start node and then recursively calls itself again untill finds a path from one node to the end node. '''
for node in self.graph_dict[start]:
if node not in path:
newPath=self.find_path(node,end,path)
if newPath:
return newPath
return None
g= Graph()
#inputs for the graphs nodes. i took pre defined nodes for easy observation of adjacent nodes.
g.addEdge('1', '2')
g.addEdge('1', '3')
g.addEdge('2', '3')
g.addEdge('2', '1')
g.addEdge('3', '1')
g.addEdge('3', '2')
g.addEdge('3', '4')
g.addEdge('4', '3')
g.show_edges()
print(g.find_path('4', '1'))
'''
The output of the graph will be the same as the input we provided it -
( 1 , 2 )
( 1 , 3 )
( 2 , 3 )
( 2 , 1 )
( 3 , 1 )
( 3 , 2 )
( 3 , 4 )
( 4 , 3 )
#path of the nodes transversal
[‘4’, ‘3’, ‘1’]
'''