-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph-traversal.py
150 lines (133 loc) · 7.6 KB
/
graph-traversal.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Iterative breadth-first and depth-first traversal of directed graph.
Each node of the graph represents a course. An edge from course A to course B indicates that A is a sufficient pre-requisite for B.
Using two graph traversal algorithms, this script finds a path from any given course to 'Brain Computer Interfacing'."""
class Graph:
def __init__(self, v_list =None):
self.gdict = {}
if v_list is not None:
for vertex in v_list:
self.addVertex(vertex)
def addVertex(self, vertex):
self.gdict[vertex] = []
def addEdge(self, start, end):
if start not in self.gdict.keys():
print("Start vertex not found : ", start)
if type(end) == list:
for vertex in end:
self.addEdge(start, vertex)
elif end not in self.gdict.keys():
print("End vertex not found : ", end)
else:
self.gdict[start].append(end)
def getVertices(self):
return self.gdict.keys()
def getNeighbours(self, vertex):
if vertex in self.gdict.keys():
return self.gdict[vertex]
else:
print("Vertex not found.")
def bfs(self, source, g):
"""BFS traversal to find shortest path between source and target
Input params: source - Start node
g - target node
Returns path list or None (if no path)"""
if source not in self.getVertices():
print('Source node invalid.')
return
travel = {}
queue = []
visited = []
queue.append(source)
while queue:
s = queue.pop(0)
visited.append(s)
child_nodes = self.getNeighbours(s)
for node in child_nodes:
if node == g:
print('Found goal')
travel[node] = s # store visited node information in child: parent format
path = self.printPath(travel, source, node)
return path
else:
if node not in visited:
travel[node] = s # store visited node information in child: parent format
queue.append(node)
def printPath(self, travelhistory, source, target):
'''Travel history: Dictionary containing details of all visited nodes in node: parent node format'''
temp = []
child = target
parent = travelhistory[child]
while parent != source:
temp.append(child)
child = parent
parent = travelhistory[child]
temp.append(source)
temp.reverse()
return temp
def wrapper_fn(self, start, target): #wrapper function calls DFS and stores path
path = []
def dfs(graph, start, target, visited=[]): # recursive DFS
visited.append(start)
if start == target:
return
child_nodes = [child for child in graph.getNeighbours(start) if child not in visited]
for node in child_nodes:
if node==target:
path.append(node)
path.append(start)
visited.append(node)
break
dfs(graph, node, target, visited)
if target in visited:
path.append(start)
break
return visited
dfs(self, start, target)
path = path[::-1] if path else "Path does not exist"
return path
courses = ["Biology", "Physics", "Chemistry", "Computer Science", "Cognitive Psychology", "Psychology", "Cognitive Neuroscience", "Anatomy", "Neurobiology", "Neuroscience",
"Logic and Cognitive Science", "Social Psychology", "Game Design", "Lego Robotics", "Robotics", "Mechanics", "Neural Interfacing", "Cognitive Science",
"Computational Tools in Cognitive Science", "Philosophy of Mind", "Anthropology", "Linguistics", "Machine Learning", "Artificial Intelligence",
"Natural Language Processing", "Embodied Cognition", "Electronics", "Haptic Systems", "Signal Processing", "Embedded Systems", "Brain Computer Interfacing"]
course_graph = Graph(courses)
course_graph.addEdge("Biology", ["Anatomy", "Neurobiology", "Neuroscience","Cognitive Science"])
course_graph.addEdge("Physics", ["Mechanics", "Electronics", "Cognitive Science", "Game Design"])
course_graph.addEdge("Chemistry", ["Neurobiology", "Neuroscience", "Cognitive Science"])
course_graph.addEdge("Linguistics", ["Anthropology","Artificial Intelligence", "Natural Language Processing", "Cognitive Science"])
course_graph.addEdge("Computer Science", ["Cognitive Science", "Computational Tools in Cognitive Science", "Artificial Intelligence", "Game Design"])
course_graph.addEdge("Psychology", ["Cognitive Psychology", "Social Psychology", "Anthropology", "Cognitive Science"])
course_graph.addEdge("Mechanics", ["Lego Robotics", "Robotics", "Electronics", "Cognitive Science", "Embedded Systems"])
course_graph.addEdge("Electronics", ["Mechanics", "Embedded Systems", "Lego Robotics", "Robotics", "Signal Processing", "Haptic Systems", "Cognitive Science"])
course_graph.addEdge("Cognitive Science", ["Cognitive Psychology", "Neurobiology", "Cognitive Neuroscience", "Logic and Cognitive Science","Cognitive Science", "Linguistics", "Computational Tools in Cognitive Science", "Philosophy of Mind", "Anthropology", "Embodied Cognition"])
course_graph.addEdge("Cognitive Psychology", ["Social Psychology", "Anthropology", "Artificial Intelligence"])
course_graph.addEdge("Philosophy of Mind", ["Anthropology", "Artificial Intelligence"])
course_graph.addEdge("Neurobiology", ["Neuroscience", "Neural Interfacing"])
course_graph.addEdge("Anatomy", ["Neurobiology", "Haptic Systems", "Embodied Cognition"])
course_graph.addEdge("Computational Tools in Cognitive Science", ["Artificial Intelligence", "Embedded Systems", "Game Design"])
course_graph.addEdge("Logic and Cognitive Science", ["Artificial Intelligence", "Signal Processing", "Game Design"])
course_graph.addEdge("Artificial Intelligence", ["Machine Learning", "Natural Language Processing","Game Design", "Embodied Cognition"])
course_graph.addEdge("Machine Learning", ["Lego Robotics", "Robotics", "Natural Language Processing"])
course_graph.addEdge("Game Design", ["Lego Robotics", "Haptic Systems"])
course_graph.addEdge("Lego Robotics", ["Robotics", "Signal Processing", "Embedded Systems"])
course_graph.addEdge("Robotics", ["Haptic Systems", "Signal Processing", "Embedded Systems"])
course_graph.addEdge("Signal Processing", ["Neural Interfacing", "Embedded Systems"])
course_graph.addEdge("Embedded Systems", ["Robotics"])
course_graph.addEdge("Neuroscience", ["Neural Interfacing", "Brain Computer Interfacing"])
course_graph.addEdge("Haptic Systems", ["Signal Processing"])
course_graph.addEdge("Neural Interfacing", ["Haptic Systems", "Signal Processing", "Embedded Systems", "Brain Computer Interfacing"])
# Uncomment for DFS traversal. To change goal course, change target_node
# source_node = input("Enter the course to be used as the start node : ")
# target_node = 'Brain Computer Interfacing'
# path = course_graph.wrapper_fn(source_node, target_node)
# if path == 'Path does not exist':
# print(path)
# else:
# print(' --> '.join(path))
# Uncomment for BFS traversal. To change goal course, change target_node
# source_node = input("Enter the course to be used as the start node : ")
# target_node = 'Brain Computer Interfacing'
# path = course_graph.bfs(source_node, g = target_node)
# if path == None:
# print('Path does not exist')
# else:
# print(' --> '.join(path))