-
Notifications
You must be signed in to change notification settings - Fork 0
/
art.py
50 lines (38 loc) · 1.21 KB
/
art.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
def tarjan(G):
n = len(G)
visited = [False for v in range(n)]
parent = [False for v in range(n)]
time_arr = [0 for v in range(n)]
low = [0 for v in range(n)]
arts = []
time = 0
def tarjan_dfs(u, is_root):
nonlocal time
children = 0
visited[u] = True
time_arr[u] = time
low[u] = time
time += 1
is_art = False
for v in G[u]:
if not visited[v]:
children += 1
parent[v] = u
tarjan_dfs(v, False)
low[u] = min(low[u], low[v])
if is_root and not is_art and children >= 2:
is_art = True
arts.append(u)
if not is_root and not is_art and low[v] >= time_arr[u]:
is_art = True
arts.append(u)
elif v != parent[u]:
low[u] = min(low[u], time_arr[v])
# for v in range(num_vertices):
# if not visited[v]:
tarjan_dfs(0, True)
return arts
# Przykładowy graf jako lista sąsiedztwa
G = [[1, 2], [0, 4], [0, 3, 4], [2], [1, 2, 5], [4, 6, 7], [5, 7], [5, 6]]
arts = tarjan(G)
print("Punkty artykulacji:", arts)