forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_255.py
37 lines (30 loc) · 989 Bytes
/
problem_255.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
def update_transitive_closure(orig, node, adjacency_list, transitive_closure):
if len(adjacency_list[node]) == 1:
return
for adj_node in adjacency_list[node]:
if orig == adj_node or node == adj_node:
continue
transitive_closure[orig][adj_node] = 1
update_transitive_closure(
orig, adj_node, adjacency_list, transitive_closure)
def get_transitive_closure(adjacency_list):
transitive_closure = \
[[0 for _ in range(len(adjacency_list))]
for _ in range(len(adjacency_list))]
for i in range(len(adjacency_list)):
transitive_closure[i][i] = 1
for i in adjacency_list:
update_transitive_closure(i, i, adjacency_list, transitive_closure)
return transitive_closure
# Tests
adjacency_list = {
0: [0, 1, 3],
1: [1, 2],
2: [2],
3: [3]
}
assert get_transitive_closure(adjacency_list) == \
[[1, 1, 1, 1],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]