0
$\begingroup$

I have a LxL square lattice. I randomly chose some points on this lattice and take them as my points. I want to find a connected path from left(0) to right(L). Below is my code:

    foo[points : {{_?NumericQ, _?NumericQ} ..}] := 
  Module[{leftEdgeNodes = Select[points, #[[1]] == 0 &],
    rightEdgeNodes = Select[points, #[[1]] == L &]},
   If[leftEdgeNodes != {} && rightEdgeNodes != {}, 
    FindShortestPath[
     EdgeAdd[NearestNeighborGraph[points, {All, 1}], 
      Join[UndirectedEdge @@@ Tuples[leftEdgeNodes, {2}], 
       UndirectedEdge @@@ Tuples[rightEdgeNodes, {2}]]], 
     First[leftEdgeNodes], First[rightEdgeNodes]], {}]
   ];
_foo := {};

However, there are cases where there is a warning:

A graph object is expected at position 1 in FindShortestPath[EdgeAdd[ ,{{...}} ],{...},{...}].

I suspect the problem is due to the first argument in EdgeAdd, meaning NearestNeighborGraph[points, {All, 1}] might not be able to form connected graph. How should I fix this problem?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.