2
2
# https://en.wikipedia.org/wiki/Breadth-first_search
3
3
4
4
import queue
5
- from typing import Tuple , List , Dict
5
+ from typing import Dict , List , Tuple
6
6
7
7
8
8
def swap (a : int , b : int ) -> Tuple [int , int ]:
9
+ """
10
+ Return a tuple (b, a) when given two integers a and b
11
+ >>> swap(2,3)
12
+ (3,2)
13
+ >>> swap(3,4)
14
+ (4,3)
15
+ >>> swap(67, 12)
16
+ (12, 67)
17
+ """
9
18
a ^= b
10
19
b ^= a
11
20
a ^= b
12
21
return a , b
13
22
14
23
15
- # creating sparse table which saves each nodes 2^i-th parent
16
- def creatSparse (max_node : int , parent : List [List [int ]]) -> List [List [int ]]:
24
+ def create_sparse (max_node : int , parent : List [List [int ]]) -> List [List [int ]]:
25
+ """
26
+ creating sparse table which saves each nodes 2^i-th parent
27
+ """
17
28
j = 1
18
29
while (1 << j ) < max_node :
19
30
for i in range (1 , max_node + 1 ):
@@ -23,7 +34,9 @@ def creatSparse(max_node: int, parent: List[List[int]]) -> List[List[int]]:
23
34
24
35
25
36
# returns lca of node u,v
26
- def LCA (u : int , v : int , level : List [int ], parent : List [List [int ]]) -> List [List [int ]]:
37
+ def lowest_common_ancestor (
38
+ u : int , v : int , level : List [int ], parent : List [List [int ]]
39
+ ) -> List [List [int ]]:
27
40
# u must be deeper in the tree than v
28
41
if level [u ] < level [v ]:
29
42
u , v = swap (u , v )
@@ -43,16 +56,18 @@ def LCA(u: int, v: int, level: List[int], parent: List[List[int]]) -> List[List[
43
56
44
57
45
58
# runs a breadth first search from root node of the tree
46
- # sets every nodes direct parent
47
- # parent of root node is set to 0
48
- # calculates depth of each node from root node
49
- def bfs (
59
+ def breadth_first_search (
50
60
level : List [int ],
51
61
parent : List [List [int ]],
52
62
max_node : int ,
53
63
graph : Dict [int , int ],
54
64
root = 1 ,
55
65
) -> Tuple [List [int ], List [List [int ]]]:
66
+ """
67
+ sets every nodes direct parent
68
+ parent of root node is set to 0
69
+ calculates depth of each node from root node
70
+ """
56
71
level [root ] = 0
57
72
q = queue .Queue (maxsize = max_node )
58
73
q .put (root )
@@ -87,14 +102,14 @@ def main() -> None:
87
102
12 : [],
88
103
13 : [],
89
104
}
90
- level , parent = bfs (level , parent , max_node , graph , 1 )
91
- parent = creatSparse (max_node , parent )
92
- print ("LCA of node 1 and 3 is: " , LCA (1 , 3 , level , parent ))
93
- print ("LCA of node 5 and 6 is: " , LCA (5 , 6 , level , parent ))
94
- print ("LCA of node 7 and 11 is: " , LCA (7 , 11 , level , parent ))
95
- print ("LCA of node 6 and 7 is: " , LCA (6 , 7 , level , parent ))
96
- print ("LCA of node 4 and 12 is: " , LCA (4 , 12 , level , parent ))
97
- print ("LCA of node 8 and 8 is: " , LCA (8 , 8 , level , parent ))
105
+ level , parent = breadth_first_search (level , parent , max_node , graph , 1 )
106
+ parent = create_sparse (max_node , parent )
107
+ print ("LCA of node 1 and 3 is: " , lowest_common_ancestor (1 , 3 , level , parent ))
108
+ print ("LCA of node 5 and 6 is: " , lowest_common_ancestor (5 , 6 , level , parent ))
109
+ print ("LCA of node 7 and 11 is: " , lowest_common_ancestor (7 , 11 , level , parent ))
110
+ print ("LCA of node 6 and 7 is: " , lowest_common_ancestor (6 , 7 , level , parent ))
111
+ print ("LCA of node 4 and 12 is: " , lowest_common_ancestor (4 , 12 , level , parent ))
112
+ print ("LCA of node 8 and 8 is: " , lowest_common_ancestor (8 , 8 , level , parent ))
98
113
99
114
100
115
if __name__ == "__main__" :
0 commit comments