4 Replies Latest reply: May 24, 2007 2:01 PM by 807606

# Iterators in recursive functions

I've been having some trouble in a function that apparently should work perfectly fine. The program is about dealing with graphs, detecting and showing the Euler path and circuit of a given graph

The following function is a depth first search , for each vertex it searches in depth the adjacent vertexes and checks each edge connecting each pair of vertexes. An iterator is used for each vertex, allowing to cover the list of adjacencies, that are on linked-list
``````/* edge[ ] is an array of edges, and the boolean visited defines if that edge already belongs to the circuit or not
circuit[ ] is an array where the edges of the path are saved
*/
public boolean Euler(int n, int pos, int numSteps) {

int ind;
//this section verifies the last step of the Euler circuit
if (pos==numSteps-2 && typeEuler==EULER_CIRCUIT) {

ind = findEdge(circuito[pos], circuit[pos+2]);
if(ind != NOT_FOUND) {
circuit[pos+1]=edge[ind].name();
return true;

} else {
edge[ind].set_visited(false);
return false;
}
}

else {
/* The error is here: when the recursive call returns false (which means that no unvisited edge was found) the iterator doesn't increase, not pointing to the next adjacent vertex to continue building the Euler Path. This happens to all the pending calls, ending the DFS*/

pos+=2;
while (it.hasNext()) {

tempS =(String)it.next();

ind=findEdge(verts[n].name(), tempS);

if( (ind != NOT_FOUND) && (!tempS.equals(circuit[0]))) {
edge[ind].set_visited(true);

circuit[pos] = tempS;
circuit[pos-1] = edge[ind].nome();
n = searchVert(circuito[pos]);
if(pos==numSteps)  // Ends Euler Path
return true;
if(Euler(n,pos,numSteps))
return true;
else
edge[ind].set_visited(false);
}
}
return false;
}
}``````
• ###### 1. Re: Iterators in recursive functions
I'm not entirely understanding what you are doing, but my gut feeling is that you don't realize that this: