You are viewing a single comment's thread. Return to all comments →
def bfs(n, m, edges, s): graph = [[0] * n for i in range(n)] for edge in edges: graph[edge[0] - 1][edge[1] - 1] = 6 # make 0 based index graph[edge[1] - 1][edge[0] - 1] = 6 # make 0 based index dist = [-1] * n visited = [False] * n dist[s - 1] = 0 q = Queue() q.put(s - 1) while not q.empty(): cur = q.get() visited[cur] = True for i in range(n): if graph[cur][i] > 0 and not visited[i]: dist[i] = dist[cur] + graph[cur][i] q.put(i) visited[i] = True del dist[s - 1] return dist
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 →