You are viewing a single comment's thread. Return to all comments →
shortest python solution
def bfs(n, m, edges, s): graph = {i: [] for i in range(1, n+1)} for i in edges: u, v = i graph[u].append(v) graph[v].append(u) q = deque() distance = [-1] * (n+1) distance[s] = 0 q.append(s) while q: node = q.popleft() for neighbor in graph[node]: if distance[neighbor] == -1: distance[neighbor] = distance[node] + 6 q.append(neighbor) distance.remove(0) return distance[1:]
Seems like cookies are disabled on this browser, please enable them to open this website
Breadth First Search: Shortest Reach
You are viewing a single comment's thread. Return to all comments →
shortest python solution