I am trying to write an algorithm that determines whether a graph is connected or not. I think my code is almost correct, although I keep getting StackOverFlowError. I personally think because there's a cycle in the graph I'm testing my algorithm with, my code doesn't understand that and comes in a loop. But I'm using an array to see if a node was already visited! So that should not happen! Anyways this is my code:
public int isConnected(String s)
{
int in = nodes.indexOf(s);
visited[in] = true;
counter++;
for(int i = 0; i < listOfChildren(s).size(); i++)
{
int ind = nodes.indexOf(listOfChildren(s).get(i));
if(visited[ind] == false)
{
isConnected(listOfChildren(s).get(i));
}
}
System.out.println(counter);
if(counter == nodes.size())
return 1;
return 0;
}
s is some node I begin with, nodes is an ArrayList of nodes and has the same size(5 in this case) as the array visited. In the beginning visited looks like this: [false false false false false], so none of the nodes was visited. listOfChildren() return an ArrayList of the children(not all of them, just the ones adjacent to the node) of a particular node. I am sure that listOfChildren() works, since I tested it 43545454 times.
Any help is appreciated(with minimum change of the code, if possible). Thanks.
UPDATE:
My graph is directed..
I define visited like this:
private boolean[] visited;
and I set everything in it to false in my constructor this code:
public void setUnvisited()
{
visited = new boolean[nodes.size()];
for(int i = 0; i < nodes.size(); i++)
{
visited[i] = false;
}
}
The nodes are strings! visited and nodes have the same length. That's why I can use nodes.indexOf(blabla) for the array visited.
UPDATE2:
This is how the graph looks like:

I'm sure the problem is after N3, the algorithm goes in a loop after N3, because it doesn't understand that N1 was already visited. I really don't understand why this happens!
UPDATE3
String have different names and there are no duplicates.. so for example indexOf(nodes.get(2)) gives 2 and not 0 or anything else..
The problem is that after visiting N3 the algorithm should stop and return 0 or 1, but I don't know how to do that :)
visited, maybe by callingsetUnvisited()? - rlibby