-
Notifications
You must be signed in to change notification settings - Fork 0
/
218.py
45 lines (32 loc) · 1.09 KB
/
218.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
"""
Problem:
Write an algorithm that computes the reversal of a directed graph. For example, if a
graph consists of A -> B -> C, it should become A <- B <- C.
"""
from DataStructures.Graph import GraphDirectedUnweighted
def reverse_direction(graph: GraphDirectedUnweighted) -> None:
visited = set()
for node in graph.connections:
# storing the nodes that require updation in to change as for loop doesn't
# support simultaneous updation
visited.add(node)
to_change = []
for neighbour in graph.connections[node]:
if neighbour not in visited:
if node not in graph.connections[neighbour]:
to_change.append(neighbour)
for neighbour in to_change:
graph.connections[neighbour].add(node)
graph.connections[node].remove(neighbour)
if __name__ == "__main__":
graph = GraphDirectedUnweighted()
graph.add_edge("A", "B")
graph.add_edge("B", "C")
print(graph)
reverse_direction(graph)
print(graph)
"""
SPECS:
TIME COMPLEXITY: O(v + e)
SPACE COMPLEXITY: O(v)
"""