Reputation: 1
#Initializing the Graph Class
class Graph:
def __init__(self, numberofVertices):
self.graph = defaultdict(list)
self.numberofVertices = numberofVertices
def addEdge(self, vertex, edge):
self.graph[vertex].append(edge)
#Implementing Topological Sort
def topogologicalSortUtil(self, v, visited, stack): visited.append(v)
for i in self.graph[v]:
if i not in visited:
self.topogologicalSortUtil(i, visited, stack)
stack.insert(0, v)
def topologicalSort(self): visited = [] stack = []
for k in list(self.graph):
if k not in visited:
self.topogologicalSortUtil(k, visited, stack)
print(stack)
tempGraph = Graph(8)
tempGraph.addEdge("A", "C")
tempGraph.addEdge("C", "E")
tempGraph.addEdge("E", "H")
tempGraph.addEdge("E", "F")
tempGraph.addEdge("F", "G")
tempGraph.addEdge("B", "D")
tempGraph.addEdge("B", "C")
tempGraph.addEdge("D", "F")
tempGraph.topologicalSort() # SHOWING ERROR IN THIS LINE WHEN I RUN. CAN ANY ONE HELP?
Upvotes: 0
Views: 71
Reputation: 21
unless "by showing an error" you mean syntactical error, there are no errors in the code.
I copied your code and removed all the errors related to syntax and indentation, the code is working fine. The errors are mentioned here.
from collections import defaultdict
These are the basic errors, but as you have not mentioned which error you are getting, this is the least one can do.
Upvotes: 2