E
E
Eugene2015-05-27 19:33:02
C++ / C#
Eugene, 2015-05-27 19:33:02

How to find the shortest path between two vertices in a graph?

Task: find the shortest path between two vertices of a weighted undirected graph and output it as a sequence of vertices. The graph is given by the adjacency matrix.
I use the Floyd-Warshall algorithm for finding the shortest paths between all pairs of vertices
( e-maxx.ru/algo/floyd_warshall_algorithm).

for (int k=0; k<n; ++k)
  for (int i=0; i<n; ++i)
    for (int j=0; j<n; ++j)
      d[i][j] = min (d[i][j], d[i][k] + d[k][j]);

Question: how to restore paths? The link says about "a simple recursive algorithm for restoring the shortest path". But I can't write it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2015-05-27
@evlevin

www.youtube.com/watch?v=DmrFwLcDl9c

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question