algorithm,shortest-path,floyd-warshall

The longest possible valid path can have length = (n - 1) * 1000. Thus, "infinity" must be strictly greater than this value. And there is no need to handle it specially as long as 2 * infinity fits into the type you are using to store distances.

c++,insert,segmentation-fault,floyd-warshall

chemin.insert(it,sequenceTest[v1-1][temp-1]); This invalidates the iterator it, but you keep reusing it in the iterations that follow. If you want to continue inserting at the same position, capture the return value. it = chemin.insert(it,sequenceTest[v1-1][temp-1]); ...

c++,algorithm,graph,shortest-path,floyd-warshall

Floyd-Warshall algorithm would be very inefficient for such a sparse graph. The graph is sparse because every vertex connected to no more than 4 other vertices. In a dense graph a vertex can be connected to up to N-1 other vertices, where N is the number of vertices in the...

python,dictionary,graph,nodes,floyd-warshall

Not all your graph nodes have an edge to all other graph nodes, so iterating through all nodes k on the entire graph with graph[n][k] will cause a KeyError. Perhaps you want something like: for n in nodes: distance[n] = {} for k in graph[n]: distance[n][k] = graph[n][k] Alternatively, if...

algorithm,shortest-path,directed-acyclic-graphs,floyd-warshall

Yes, Floyd Warshall can give a correct answer for your problems, can be proved like using Floyd Warshall to find the shortest path between all pairs in graph. Or you can multiply each edges with (-1), and solve your problem like finding the shortest path between all pairs, then multiply...

java,graph,shortest-path,floyd-warshall

The answer depends on the data type that you use to represent the weight. If it is double, you would be safe to use Double.POSITIVE_INFINITY. If it is an integer, pick a value that you do not use, e.g. negative one if your graph does not allow negative edges. An...

c++,algorithm,graph,shortest-path,floyd-warshall

The reason why you get very big negative values is integer overflow. If there is no edge, you set D[i][j]=INT_MAX. But then if (D[i][j] > D[i][k] + D[k][j]) { D[i][j] = D[i][k] + D[k][j]; if there is no edge from i to k and from k to j, then the...