I am attempting to write a module that prints an optimal path from a starting node to a destination node. The first node is 1 and the final node is 100 (though when module prints these they should be 0 and 99 respectively). The module takes as inputs a distance matrix, Q, and a list, J, whose elements are the shortest-path weight from node i to destination node. Below is my best attempt. ``` findPathandTotalCost3[Q_, J_] := Module[ {costs, node, v, w, i}, i = 1; costs[i_] := costs[i] = Table[Q[[node[i], w]] + J[[w]], {w, Length[J]}]; node[1] = 1; node[2] = Position[costs[1], Min[costs[1]]]; node[i] = Position[costs[i - 1], Min[costs[i - 1]]; While[node[i] < 100, (Print[node[i] - 1]; ++i; costs[i]; node[i];)];];] ``` All my attempts at implementing this module seem to have problems with either correctly iterating or specifying which parts are recursive. Below is a typical error output. ``` findPathandTotalCost3[Q, J] Part::pkspec1: The expression node$45807[0] cannot be used as a part specification. Part::pkspec1: The expression node$45807[0] cannot be used as a part specification. Part::pkspec1: The expression node$45807[0] cannot be used as a part specification. General::stop: Further output of Part::pkspec1 will be suppressed during this calculation. 0 ``` As can be seen, the first node prints correctly ("0") but the module subsequently doesn't evaluate properly. Does anyone have any suggestions as to how to make this work? I can post more of my code attempts if helpful. Thanks