1

I'm trying to reconstruct the least cost path between two vertices from a Floyd-Warshall algorithm I implemented in Lua.

Here is what i've written so far:

function shallow_copy(g)
    local h = {}
    for k, v in pairs(g) do
        h[k] = v
    end
    return h
end

function fw(g) -- g is an adjacency matrix representing the graph
    local d = shaloow_copy(g)
    for k = 1, #d do
        for i = 1, #d do
            for j = 1, #d do
                d[i][j] = math.min(d[i][j], d[i][k] + d[k][j])
            end
        end
    end
    return d
end

These functions give me a matrix containing the length (number of edges) separating each pair of vertices in my graph g, which is great.

I now need to know the actual path between any given vertices. Wikipedia conveniently provides pseudo code, but -being a newbie- I'm confuse about how to implement it.

Here is the pseudocode provided in the wikipedia article (https://en.wikipedia.org/wiki/Floyd–Warshall_algorithm#Path_reconstruction):

let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity)
let next be a |V| × |V| array of vertex indices initialized to null

procedure FloydWarshallWithPathReconstruction ()
   for each vertex v
      dist[v][v] ← 0
   for each edge (u,v)
      dist[u][v] ← w(u,v)  // the weight of the edge (u,v)
   for k from 1 to |V|
      for i from 1 to |V|
         for j from 1 to |V|
            if dist[i][k] + dist[k][j] < dist[i][j] then
               dist[i][j] ← dist[i][k] + dist[k][j]
               next[i][j] ← k

function Path (i, j)
   if dist[i][j] = ∞ then
     return "no path"
   var intermediate ← next[i][j]
   if intermediate = null then
     return " "   // the direct edge from i to j gives the shortest path
   else
     return Path(i, intermediate) + intermediate + Path(intermediate, j)

Would anyone know how to implement this pseudo code in lua? Do I need to re-write my short piece of lua code or can this pseudocode be somehow merged into what I've got?

Cheers!

Lucien S.
  • 5,123
  • 10
  • 52
  • 88
  • Using a separate matrix to keep the paths is going to be much easier than trying to deduce the info from the final distance table. What part of the algorithm are you confused about? What is making it hard to convert the code you read into Lua? – hugomg Feb 02 '14 at 05:17
  • Oh no, sorry, it might not be clear, i'll edit. I actually wrote this code (with some help) which works great to produce a matrix of distances between each pair of nodes in my graph. Now I nead to know the exact path between each pairs of nodes. I'll try and make this clearer in the question. Cheers! – Lucien S. Feb 03 '14 at 03:59
  • You don't need to start all your code from scratch. AS you can see, the basic structure of the algorithm is still the same and the only difference is that you update both a path and a distance matrix instead of only updating the path matrix. – hugomg Feb 03 '14 at 04:42
  • 1
    It might help if you think about why the algorithm is doing things like this. The basic idea is that we maintain an invariant that dist[i][j] is the shortest path between i and j using only nodes from the set [1..k] as intermediary nodes. Then, for each of the k steps we compare the current best path that only uses nodes up to [1..k-1] as intermetiary steps with the paths that use k as an intermediary (go from i to k using [1..k-1] as stepping stones and then go from k to j using [1..k-1] as stepping stones) – hugomg Feb 03 '14 at 04:46

0 Answers0