Is this a bug, if statement validation Djikstra

I’m coding a djiktsra search algorithm however I wanted to improve the last bit my programs algorithm and again run into some if statement issues, not sure if this is a bug or not.

The issue occurs arround line 2285, in the code commented out.

The jist of the issue is, the pathfinder is made to travel from node to node and find the next nearest node, whilst it does this it has to fill in the path to mark it as having been travelled. All this is fine until I get to the end, at this point the logic statements I’m using do not work.

Each node is aware of its children and the steps required to get to them, and each child is aware of its parents and its distance to them. Therefore I would have though you verify that the finish point lies on a path, retrieve the targets parents, and work from there. Pick the parent which is currently the pathfinder, from there look at the pathfinders children and only mark the ones which have matching parents with the finishing point. Alas this does not work.

The function is called this.parentNode, and called at this.nextNode.

var step;
    for(var i=0;i<this.child.length;i++){
      var a = this.child[i];
      if(a[1] ===target){
        step = a[0];

    var a;
    var b;
    var c;

    for(var i=0;i<2;i++){
      var x = target.node_info[i];
          b = x;
        else if(!a){
          a = x;

    var d1 = dist(a[1].x,a[1].y,target.x,target.y);
    var d2 = dist(b[1].x,b[1].y,target.x,target.y);

    var array = [];

    // for(var i=0;i<this.child.length;i++){
    //   var k = this.child[i];
    //   var d = k[1].node_info[0];
    //   var c = k[1].node_info[1];
    //   if(d[1]!==a[1]&&c!==b[1]&&c[1]!==a[1]){
    //     array.push(k)
    //   }}
    //   if(array.length>0){
    //     for(var i=0;i<array.length;i++){
    //       var k = array[i];
    //       var d = k[1].node_info[0];
    //       var c = k[1].node_info[1];
    //       // if(){
    //        k[1].pcol2 = color(0,0,0,150);
    //        k[1].depth = this.depth;
    //        k[1].visited2 = true;
    //     }}
        return target;
      for(var i=0;i<a[1].child.length;i++){
        var k = a[1].child[i];
// node info contains parent information, distance at index 0, and reference at index 1.
        var d = k[1].node_info[0];
        var c = k[1].node_info[1];
//stack array is the reference to the stack the function is currently using.
          k[1].pcol2 = color(0,0,0,150);
          k[1].depth = this.depth;
          k[1].visited2 = true;
      return target;
    else if(b[1]===this){
      for(var i=0;i<b[1].child.length;i++){
        var k = b[1].child[i];
        var d = k[1].node_info[0];
        var c = k[1].node_info[1];
          k[1].pcol2 = color(0,0,0,150);
          k[1].depth = this.depth;
          k[1].visited2 = true;
      return target;
1 Like

When this is true, then don’t think about the process as such but think about the end of it.

Is there a part of the condition that won’t work at the end? Eg it uses a variable that tries to look over the end?

Well its iterating through an array of values, containing the children, looking at their parents and comparing them to already established unchanging values. Then as long as both pf the parents match the reference in any order then it should work.


But at the end, maybe there is no parent?

1 Like

Ive checked and rechecked this function, parents are always available for the child entities, they are created prior to being able to run the search and I have been very thorough about it. In any case if there was no parent at the end, the points before this ought to yield a positive result.

Also if there was no parent p5.js would halt because it wouldnt be able to identify the index in the array im calling

all I am saying is, when it’s working throughout, but not at the end, it has to do with: the end. So I would check this.

but never mind, I don’t have time atm.

My Apologies.

1 Like

Thanks for your input, I ended up checking for no parents instead and realised that my assignment call in parent node, which checked the target parents was flawed. This is now working.

Thanks again Chris!


That’s great news!

Good to hear!