-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarjan
More file actions
34 lines (26 loc) · 721 Bytes
/
Tarjan
File metadata and controls
34 lines (26 loc) · 721 Bytes
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
class DiGraph:
def __init__(self, graph_size):
self.graph_size = graph_size
self.adj = [[] for i in range(graph_size)]
def add_edge(self, i, j):
self.adj[i - 1].append(j)
def remove_edge(self, i, j):
self.adj[i-1].remove(j)
T = []
def tarjan(graph):
global mark
mark = [None]*graph.graph_size
unmarked = [i for i in range(graph.graph_size)]
while unmarked:
n = unmarked.pop()
dfs(graph, n)
def dfs(g, n):
global mark
if mark[n] == "active":
raise Exception("Not a DAG")
if not mark[n]:
mark[n] = "active"
for m in graph.adj[n]:
dfs(g, m - 1)
mark[n] = "done"
T.append(n+1)