Dijkstra's algorithm
My version in python. The code is specific to how g, the graph matrix is defined. Here is a good explanation to why we need to get the vertex with min edge in each iteration. [code] def graphDistances(g, s): INFINITY = 9999999999 size = len(g) dist = [INFINITY] * size prev = [None] * size unvisited = set(range(size)) dist[s] = 0 while unvisited: u = findVertexWithMinDist(unvisited, dist) unvisited.remove(u) for v, edge_weight in enumerate(g[u]): if edge_weight == -1: continue my_dist = dist[u] + edge_weight if my_dist < dist[v]: dist[v] = my_dist prev[v] = u ...