forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Topological_Sort.py
123 lines (104 loc) · 3.18 KB
/
Topological_Sort.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
# SOLUTION #1
class JobNode:
def __init__(self, job):
self.job = job
self.prereqs = []
self.visited = False
self.visiting = False
class JobGraph:
def __init__(self, jobs):
self.nodes = []
self.graph = {}
for job in jobs:
self.addNode(job)
def addPrereq(self, job, prereq):
jobNode = self.getNode(job)
prereqNode = self.getNode(prereq)
jobNode.prereqs.append(prereqNode)
def addNode(self, job):
self.graph[job] = JobNode(job)
self.nodes.append(self.graph[job])
def getNode(self, job):
if job not in self.graph:
self.addNode(job)
return self.graph[job]
# O(v + e) time | O(v + e) space
def topologicalSort(jobs, deps):
jobGraph = createJobGraph(jobs, deps)
return getOrderedJobs(jobGraph)
def createJobGraph(jobs, deps):
graph = JobGraph(jobs)
for prereq, job in deps:
graph.addPrereq(job, prereq)
return graph
def getOrderedJobs(graph):
orderedJobs = []
nodes = graph.nodes
while len(nodes):
node = nodes.pop()
containsCycle = depthFirstTraverse(node, orderedJobs)
if containsCycle:
return []
return orderedJobs
def depthFirstTraverse(node, orderedJobs):
if node.visited:
return False
if node.visiting:
return True
node.visiting = True
for prereqNode in node.prereqs:
containsCycle = depthFirstTraverse(prereqNode, orderedJobs)
if containsCycle:
return True
node.visited = True
node.visiting = False
orderedJobs.append(node.job)
return False
# SOLUTION #2
class JobNode:
def __init__(self, job):
self.job = job
self.deps = []
self.numOfPrereqs = 0
class JobGraph:
def __init__(self, jobs):
self.nodes = []
self.graph = {}
for job in jobs:
self.addNode(job)
def addDep(self, job, dep):
jobNode = self.getNode(job)
depNode = self.getNode(dep)
jobNode.dep.append(depNode)
depNode.numOfPrereqs += 1
def addNode(self, job):
self.graph[job] = JobNode(job)
self.nodes.append(self.graph[job])
def getNode(self, job):
if job not in self.graph:
self.addNode(job)
return self.graph[job]
# O(v + e) time | O(v + e) space
def topologicalSort(jobs, deps):
jobGraph = createJobGraph(jobs, deps)
return getOrderedJobs(jobGraph)
def createJobGraph(jobs, deps):
graph = JobGraph(jobs)
for job, dep in deps:
graph.addDep(job, dep)
return graph
def getOrderedJobs(graph):
orderedJobs = []
nodesWithNoPrereqs = list(filter(lambda node: node.numOfPrereqs == 0, graph.nodes))
while len(nodesWithNoPrereqs):
node = nodesWithNoPrereqs.pop()
orderedJobs.append(node.job)
removeDeps(node, nodesWithNoPrereqs)
graphHasEdges = any(node.numOfPrereqs for node in graph.nodes)
return [] if graphHasEdges else orderedJobs
def removeDeps(node, nodesWithNoPrereqs):
while len(node.deps):
dep = node.deps.pop()
dep.numOfPrereqs -= 1
if dep.numOfPrereqs == 0:
nodesWithNoPrereqs.append(dep)