-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq_bfs_algo.py
71 lines (57 loc) · 1.69 KB
/
seq_bfs_algo.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
__author__ = "Lui Sheng Jie"
__email__ = "[email protected]"
""" Sequential BFS implementation.
Sequential implementation of Algorithm 1 Parallel BFS algorithm: High-level overview [1].
Reference:
[1] https://www.researchgate.net/publication/220782745_Scalable_Graph_Exploration_on_Multicore_Processors
"""
import numpy as np
import time
from src.load_graph import get_graph, gen_balanced_tree
def get_adjacent_nodes(G, x):
idx_lst = []
adj_list = G[x]
for idx, val in enumerate(adj_list):
if val == 1:
idx_lst.append(idx)
return idx_lst
def bfs_seq(G, target):
r = 0
CQ = []
# Init all values in P to inf
P = [np.inf for i in range(G.shape[0])]
# Set root node
P[r] = 0
# Enqueue r
CQ.append(r)
while len(CQ) != 0:
# print(f"CQ: {CQ}")
NQ = []
for i in range(len(CQ)):
# Dequeue CQ
u = CQ.pop(0)
# For each v adjacent to u
for v in get_adjacent_nodes(G, u):
if v == target:
return True
if P[v] == np.inf:
P[v] = u
NQ.append(v)
# Swap CQ and NQ
tmp = NQ
NQ = CQ
CQ = tmp
return False
def main():
start_time = time.time()
G = gen_balanced_tree(4, 5, directed=True)
print(G.shape)
# G = get_graph()
find_node = bfs_seq(G, target=999999)
print("--- %s seconds ---" % (time.time() - start_time))
if find_node:
print(f"Node Found")
else:
print(f"Node not Found")
if __name__=='__main__':
main()