# Is this a bug, if statement validation Djikstra

**URL:** https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184
**Category:** Coding Questions
**Created:** [June 19, 2019, 3:22pm UTC](https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184 "2019-06-19T15:22:50Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 19, 2019, 3:22pm UTC](https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184/1 "2019-06-19T15:22:50Z")

</div>

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](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.

```auto
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];
        if(a){
          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;
    if(a[0]<step&a[1]===this){
      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.
        if((d[1]===a[1]&&d[0]<=step-1||c[1]===a[1]&&c[0]<=step-1)){
          stack_array.push(k[1]);
          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];
    
        if(d[1]===b[1]&&d[0]<=step-1||c[1]===b[1]&&c[0]<=step-1){
          stack_array.push(k[1]);
          k[1].pcol2 = color(0,0,0,150);
          k[1].depth = this.depth;
          k[1].visited2 = true;
        }}
      return target;
    }

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 19, 2019, 3:41pm UTC](https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184/2 "2019-06-19T15:41:45Z")

</div>

> [@paulgoux](#):
>
> All this is fine until I get to the end

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?

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 19, 2019, 3:56pm UTC](https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184/3 "2019-06-19T15:56:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 19, 2019, 4:00pm UTC](https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184/4 "2019-06-19T16:00:52Z")

</div>

Understood

But at the end, maybe there is no parent?

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 19, 2019, 4:20pm UTC](https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184/5 "2019-06-19T16:20:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 19, 2019, 5:04pm UTC](https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184/6 "2019-06-19T17:04:42Z")

</div>

> [@paulgoux](#):
>
> checked this function, parents are always available for the child entities, they are created prior to being able to run

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.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 19, 2019, 5:21pm UTC](https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184/7 "2019-06-19T17:21:02Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 19, 2019, 9:21pm UTC](https://discourse.processing.org/t/is-this-a-bug-if-statement-validation-djikstra/12184/8 "2019-06-19T21:21:40Z")

</div>

That’s great news!

Good to hear!

Chrisir
