-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreadth_first_search.py
More file actions
72 lines (49 loc) · 1.46 KB
/
Copy pathbreadth_first_search.py
File metadata and controls
72 lines (49 loc) · 1.46 KB
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
72
import sys
from queue import Queue
def readGraph(input_file):
with open(input_file, 'r') as f:
raw = [line.split(',') for line in f.read().splitlines()]
N = int(raw[0][0])
s = int(raw[1][0])
adj_list = []
for line in raw[2:]:
if line == ['-']:
adj_list.append([])
else:
adj_list.append([int(index) for index in line])
return N, s, adj_list
def writeOutput(output_file, level):
with open(output_file, 'w') as f:
for i in level:
f.write(str(i) + '\n')
def Run(input_file, output_file):
N, s, adj_list = readGraph(input_file)
level = BFS(N, s, adj_list)
writeOutput(output_file, level)
def BFS(N, s, adj_list):
level = ['x']*N
discovered = [False]*N # initialize a boolean array
Q = Queue(maxsize = N) # initliaze a Queue
# single source case
if type(s) == list:
for source in s:
level[source] = 0
discovered[source] = True
Q.put(source)
else:
level[s] = 0
discovered[s] = True
Q.put(s)
# BFS
while not Q.empty():
node = Q.get()
for neighbor in adj_list[node]:
if discovered[neighbor] == False:
discovered[neighbor] = True
Q.put(neighbor)
level[neighbor] = level[node] + 1
return level
def main(args=[]):
Run('input', 'output')
if __name__ == "__main__":
main(sys.argv[1:])