Iteration through 2d array in lua -


i'm starting lua scripting , seem stuck @ simple problem.

i'm trying implement floyd-warschall algorithm compute shortest paths between each vertices of graph ( http://en.wikipedia.org/wiki/floyd–warshall_algorithm short explanation of algorithm). written in python (here's code https://gist.github.com/davidcain/4032399 ). version little different, in order make fit main code, same thing.

here's commented code. every time run it, "attempt index field '?' (a nil value)" (i feel solution simple enough, can't seem put finger on it. appreciated):

function adj(bodies) --returns, 1d array of vertices called "bodies", square adjacency matrix (a matrix of size number_of_vertices x number_of_vertices tells, ones, if vertices connected, or 'infs' if not connected)     n = table.getn(bodies)     dist = {}     _,i in pairs(bodies)         dist[i] = {}         _,j in pairs(bodies)             if == j                 dist[i][j] = 0             end             if areconnected(i,j) == true --areconnected function wrote see if, well, 2 particular vertices connected. if are, distance 1, if not, distance inf.                 dist[i][j] = 1             else dist[i][j] = math.huge             end         end     end     return adjmatrix end  function physicsdebugdraw:fw(adjmatrix) --i pass adjmatrix function  d = adjmatrix       _,k in pairs(d)         _,i in pairs(d)             _,j in pairs(d)                 d[i][j] = math.min(d[i][j], d[i][k] + d[k][j]) -- problem here suspect...             end         end     end     return d end 

you did not show structure of adjmatrix or layout looks looking @ triple nested loop, usage incorrect.

note variables k, i , j here:

for _,k in pairs(d)   _,i in pairs(d)     _,j in pairs(d)       d[i][j] = math.min(d[i][j], d[i][k] + d[k][j])     end   end end 

are not keys of adjmatrix value part of pair. remember pairs returns key follow value on each iteration. inner loop you're accessing adjmatrix's content using the value key.

without seeing actual structure of adjmatrix it'll hard recommend solution correctly iterates on it. making k, i , j hold key part reasonable start.

for k in pairs(d)   in pairs(d)     j in pairs(d)       d[i][j] = math.min(d[i][j], d[i][k] + d[k][j])     end   end end 

note if adjmatrix uses numbers keys , it's continuous(no numbers skipped) can use #adjmatrix instead of pairs(adjmatrix).

edit: after looking @ python version, made following observations:

  • adj returns square-like matrix. width == height
  • "empty" cells represented infinity
  • after conversion adj table's (or python dict) rows , columns continuous
  • fw makes "shallow" copy of g

assuming above invariants true(let me know if they're not) following more faithful translation in lua:

function shallow_copy(g)   local h = {}   k, v in pairs(g)     h[k] = v     end   return h   end  --[[ eg. g = {         {0, 3, 8, math.huge, -4},         {math.huge, 0, math.huge, 1, 7},         {math.huge, 4, 0, math.huge, math.huge},         {2, math.huge, -5, 0, math.huge},         {math.huge, math.huge, math.huge, 6, 0},     } --]] function fw(g)   local d = shallow_copy(g)   k = 1, #d     = 1, #d       j = 1, #d         d[i][j] = math.min(d[i][j], d[i][k] + d[k][j])         end         end         end    return d   end 

you may pretend end keyword invisible. :p


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -