-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serial_BFS.py
47 lines (30 loc) · 988 Bytes
/
Serial_BFS.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
from collections import defaultdict
from collections import deque
import multiprocessing as mp
import multiprocessing
graph = defaultdict(list)
q = deque()
def addEdge(u, v):
graph[u].append(v)
graph[v].append(u)
def bfs(source):
q = deque([source])
visited = [0] * numberOfVertices
distance[source] = 0
while q:
front = q.popleft()
for node in graph[front]:
if not visited[node]:
visited[node] = 1
distance[node] = distance[front] + 1
q.append(node)
if __name__ == '__main__':
numberOfVertices, numberOfEdges = map(int, input().split())
distance = [0] * numberOfVertices
for _ in range(numberOfEdges):
x, y = map(int, input().split())
addEdge(x, y)
bfs(0)
distance[0] = 0
for i in range(numberOfVertices):
print( f"Shortest distance of node {i} from source is {distance[i]}")