-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs_hops_from.py
More file actions
51 lines (46 loc) · 1.1 KB
/
bfs_hops_from.py
File metadata and controls
51 lines (46 loc) · 1.1 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
'''
BFS and Find Hops (Distance) from Node
'''
from collections import deque
from graph_utils import *
def bfs(node: Node):
visited = set()
q = deque([node])
while q:
node = q.pop()
print(node, end=', ')
visited.add(node)
node.visited = True
for link in node.links:
if not link.to_node.visited:
q.appendleft(link.to_node)
print()
for node in visited:
node.visited = False
g = make_graph(5, 6)
print(g)
bfs(g.nodes[0])
def hops_from(node: Node):
visited = set()
hops_nodes = {}
hops = 0
q = deque([node])
q_tmp = deque()
while q or q_tmp:
if not q:
q.extendleft(q_tmp)
q_tmp.clear()
hops += 1
node = q.pop()
hops_nodes[node] = hops
visited.add(node)
node.visited = True
for link in node.links:
if not link.to_node.visited:
q_tmp.appendleft(link.to_node)
for node in visited:
node.visited = False
return hops_nodes
# g = make_graph(5, 6)
# print(g)
print(hops_from(g.nodes[0]))