def bellman_ford(g, w, s): graph.init_single_source(g, s) for i in range(1, len(g.vertices)): for u,v in g.edges: graph.relax(g.vertices[u], g.vertices[v], w) for u,v in g.edges: if g.vertices[v].d > g.vertices[u].d + w[(u,v)]: return False return True
def dijkstra(graph, weights, start_vertex): graph_utils.init_single_source(graph, start_vertex) queue = [vertex for vertex in graph.vertices.values()] # vertices to process heapq.heapify(queue) s = [] # set of vertices with shortest path already determined while queue: u = heapq.heappop(queue) # extract min s.append(u) for v in u.edges: graph_utils.relax(u, v, weights)