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
EDIT: Q and J below. Q is too large to paste in full, but is a list of 100 lists, each similar to:
{Infinity, 0.04, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, 11.11, Infinity, Infinity, Infinity, Infinity, Infinity, 72.21, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity},
{Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, 20.59, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, 64.94, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, 1247.25, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity}
where, e.g. 0.04 is the edgeweight 1->2.
J:
{160.55, 162.26, 88.52, 143.73, 145.12, 147.43, 141.67, 144.1, \
149.44, 140.95, 150.8, 141.99, 148.93, 303.77, 130.85, 107.01, \
128.15, 114.66, 104.44, 124.66, 124.42, 168.62, 200.27, 88.21, \
114.61, 102.74, 112.81, 112.8, 131.97, 70.38, 71.45, 176.51, 66.16, \
65.84, 110.18, 64.7, 156.07, 67.8, 67.44, 63.95, 77.15, 62.61, 58.66, \
149.25, 50.72, 52.26, 67.53, 48.58, 65.21, 46.27, 45.76, 54.36, \
135.03, 44.38, 54.99, 42.16, 40.05, 40.03, 62.47, 30.69, 33.02, 37.5, \
35.56, 38.77, 32.62, 34.98, 34.34, 31.39, 31.68, 30.47, 30.41, 30.02, \
35.96, 22.04, 21.16, 21.45, 20.64, 42.31, 79.71, 8.91, 33.37, 77.12, \
15.27, 10.37, 33.5, 7.46, 85.72, 4.8, 4.59, 37.6, 13.56, 22.8, 11.87, \
3.28, 3.09, 0.27, 1.06, 0.63, 0.33, 0}
V13
I have not been able to reproduce your errors. I am attaching a screenshot so you can check for yourself $\endgroup$Q
andJ
? But see partpkspec1-the-expression-j-cannot-be-used-as-a-part-specification $\endgroup$