-
Notifications
You must be signed in to change notification settings - Fork 17
/
22.1-3.py
40 lines (27 loc) · 967 Bytes
/
22.1-3.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
# this is a pseudocode, may not work
class AdjListNode:
def __init__(self, val):
self.val = val
self.next = None
def inverse_edges_from_adj_list(adj_list):
new_adj_list = {}
for vertex_src in adj_list.keys():
node_dst = adj_list[vertex_src]
while node_dst:
vertex_dst = node_dst.val
if vertex_dst in new_adj_list:
node = new_adj_list[vertex_dst]
while node.next:
node = node.next
node.next = AdjListNode(vertex_src)
else:
new_adj_list[vertex_dst] = AdjListNode(vertex_src)
node_dst.next
return new_adj_list
def inverse_edges_from_adj_matrix(adj_matrix, size):
for row in range(size):
for col in range(row, size):
adj_matrix[col][row], adj_matrix[row][col] = (
adj_matrix[row][col],
adj_matrix[col][row],
)