# 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.

https://editor.p5js.org/paulgoux/sketches/eiY8BkMUm

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 ===target){
step = a;
}}

var a;
var b;
var c;

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

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

var array = [];

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

if(d===b&&d<=step-1||c===b&&c<=step-1){
stack_array.push(k);
k.pcol2 = color(0,0,0,150);
k.depth = this.depth;
k.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.

Understood

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!

2 Likes

That’s great news!

Good to hear!

Chrisir