You are viewing a single comment's thread. Return to all comments →
My Python3 solution
def bfs(n, m, edges, s): from collections import deque # Build graph graph = {} for num in range(1, n+1): graph[num] = set() for l, r in edges: graph[l].add(r) graph[r].add(l) reached = {} # Explore graph once frontier = deque([(s, 0)]) seen = {s} while frontier: curr_node, curr_cost = frontier.popleft() for nbour in graph[curr_node]: if nbour not in seen: seen.add(nbour) reached[nbour] = curr_cost+6 frontier.append((nbour, curr_cost+6)) result = [] for node in range(1, n+1): if s != node: result.append(reached.get(node, -1)) return result
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 →
My Python3 solution