Created
September 13, 2025 14:32
-
-
Save evanthebouncy/2e589ca3b0acc61402732d3cbf31f954 to your computer and use it in GitHub Desktop.
q13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
E = { | |
"A": [], | |
"B": ["A", "E"], | |
"C": ["D", "H"], | |
"D": ["B"], | |
"E": ["F"], | |
"F": [], | |
"G": ["A"], | |
"H": ["B", "G"], | |
} | |
class Toposort: | |
def __init__(self): | |
self.visited = [] | |
def DFS(self, node, graph): | |
for x in graph.get(node, []): | |
if x not in self.visited: | |
self.DFS(x, graph) | |
self.visited = [node] + self.visited | |
def toposort(self, graph): | |
for node in graph: | |
if node not in self.visited: | |
self.DFS(node, graph) | |
return self.visited | |
if __name__ == "__main__": | |
ts = Toposort() | |
print(ts.toposort(E)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment