Can you find any bugs in this code? import heapq from collections import defaultdict def…
- User
Can you find any bugs in this code?
import heapq from collections import defaultdict
def shortestReach(n, edges, s): """Find shortest paths from s to all nodes other than s. Nodes are integers 1, 2, ..., n. """ graph = defaultdict(dict) for u, v, w in edges: if u in graph and v in graph[u]: w = min(graph[u][v], w) graph[u][v] = w graph[v][u] = w distances = {v: float('inf') for v in range(1, n+1)} distances[s] = 0 todo = set(range(1, n+1)) while todo: print(distances) print(todo) new = min(todo, key=lambda k: distances[k]) todo.discard(new) for v, w in graph[new].items(): distances[v] = min(distances[v], distances[new] + w) for v, d in distances.items(): if d == float('inf'): distances[v] = -1 return [distances[i] for i in range(1, n+1) if i != s]
- Assistant