1

I am trying to explore a graph here but I am not sure what is wrong with the explore function. The recursion doesn't seem to be working correctly; while exploring the neighbours of the node 0 it explored 0, 1, 2 and then never returns back to explore 3, 4, 5; why is that so?

explored=[]
//class definition 
function graph(){
    this.graph=new Array();
    this .graph[0] = [1,0,1,1,0,1]
    this .graph[1] = [0,1,1,1,0,0]
    this .graph[2] = [1,1,1,1,0,0]
    this .graph[3] = [1,1,1,1,1,0]
    this .graph[4] = [0,0,0,1,1,0]
    this .graph[5] = [1,0,0,0,0,0]

    this.explore    = explore

}

function explore(node,depth){

    explored[node]=1
    document.write('<br>')
    for(x=0;x<depth;x++)
        document.write('-')
    document.write(node+'<br>')
    neighbours=this.graph[node]

    document.write('exploring '+node +' neighbours' + neighbours +'explored = '+explored)

    for ( i=0;i<neighbours.length;i++){
        document.write('checking'+i+' node is ='+node )
        if(neighbours[i] ==1 && explored[i]!=1)
            this.explore(i,++depth)
    }

}

g = new graph()
g.explore(0,0)  
3
  • 1
    by not using var you've set both x and i as global variables. Commented Feb 8, 2011 at 8:49
  • 1
    @generalhenry You should make it an answer, so he can accept it.. if not, I will do it.. mohowhah..mwhahahaha! Commented Feb 8, 2011 at 9:15
  • thanks a lot , and yeah u can make it an anwswer Commented Feb 8, 2011 at 9:17

2 Answers 2

4

by leaving out var you're setting global variables in a recursive function and stepping on your toes, here's the corrected code

function explore(node,depth){

    explored[node]=1
    document.write('<br>')
    for(**var** x=0;x<depth;x++)
        document.write('-')
    document.write(node+'<br>')
    **var** neighbours=this.graph[node]

    document.write('exploring '+node +' neighbours' + neighbours +'explored = '+explored)

    for (**var** i=0;i<neighbours.length;i++){
        document.write('checking'+i+' node is ='+node )
        if(neighbours[i] ==1 && explored[i]!=1)
            this.explore(i,++depth)
    }

}
Sign up to request clarification or add additional context in comments.

Comments

2

The line this.explore(i,++depth) may also be causing you problems, as you are incrementing depth in the current scope as well as passing the incremented value to the recusive call,

better to use

this.explore(i, depth + 1);

When in doubt with javascript it is always good to check the code with jslint.

1 Comment

Hmm, wouldn't that be exactly equivalent? While he doesn't use the depth argument for the actual exploration, I think what he actually wants is this.explore(i, depth + 1) though - so that the depth isn't increased in the calling scope, and indentation will remain correct for that depth.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.