0

I am trying to do a graph traversal. Since all vertices are not connected. I need to initiate the traversal from every node. Nodes are in a 2d array. I am getting the input from a large text file and here is how I do it:

lr.on('end', function(){
 //Callback called when file reading is complete
 initialize(); //initialize stuff
 startTraversal();
});

The startTraversal() method is defined as:

 function startTraversal(){
   for(i=0;i<x;i++){
    for(j=0;j<y;j++){
        console.log(i+', '+j);
        traverse(i,j); //Call Traverse i,j every time once
    }
   }
 }

traverse(i,j) is a recursive function. So I look at all possible paths from the node i,j and initiate traverse on them. A broad structure of the traverse() method is below:

function traverse(i,j){
var possible = getAdjacent(i,j); //getPossible Routes
if(possible.length == 0){
    //do stuff
    return; //Tried adding return statement here
}

else{
    for(x=0;x<possible.length;x++){
        if(!data[a][b].visited) //if node is not visited
            traverse(a,b);

        //Do further stuff when this ^ call returns, finding max etc
        ...
       } //End of for
    }
}

Now in the startTraversal() function call, the inner loop is executed only for the first value of i which I confirmed from the console.log. I am not able to understand why is the loop not getting executed further.

PS: When I manually put the nested loops outside any callbacks, the loops and traversal gets done as expected. However, I need to initiate the startTraversal() method only when the file is read completely. I guess it has something to do with the traverse() function not returning a value so that the loop is not continued. I tried adding a return in the base case of traverse method but to no success.

Any insights on this problem is deeply appreciated. Would want to know how to handle recursive calls in nested loops, atleast in javascript.

5
  • 2
    You really must declare the variables in the functions ... Currently all loops share the same counters. Commented Sep 5, 2016 at 10:18
  • Please, getAdjacent() method, does it has a callback? And when you said that the inner loop is executed only for the first value of i, you means the value 0? In real I want to know if code you didn't write contains callbacks methods Commented Sep 5, 2016 at 10:32
  • I don't see how all the loops use the same counter. I am passing variables i and j as paramters to the traverse function. They will be passed by value right. Commented Sep 5, 2016 at 10:36
  • @Ediruth, getAdjacent() method doesn't have a callback. It just returns an array. Commented Sep 5, 2016 at 10:37
  • x in for(x=0;x<possible.length;x++) is shared with all recursive function calls. Commented Sep 5, 2016 at 10:44

1 Answer 1

1

You should never, ever (*) use global variables. It seems that your x, y, i, j (and possibly other) variables are global, which is probably the cause of your problems.

x & y look like some kind of "dimensionality" vars of your array/graph/whatever, so it's better to pass them as arguments of startTraversal. i & j are local variables, so they should be declared inside the function they are used in (eg. for(var i = 0; i < x; i++)). It also might be a good idea to pass the graph itself as an argument of all your graph-related functions.

You should also consider using Strict mode. One of the restriction is you are not allowed to accidentally create global vars.


(*) - of course there are some cases where global variables are actually desired, but it mostly concerns people creating some kind of global, reusable modules or constant, shared values. You should never use globals for your "local" operations.

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

1 Comment

Thanks @mdziekon for pointing out about Strict mode. Indeed I was reusing x in the function which was causing the error.

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.