I've been trying to implement a DFS search of a Graph as part of my project, but I've been having trouble with figuring exactly how the recursion is meant to work..., I think my code is correct, but the results show otherwise.
public void RecursionHelper(HashSet seen2, int startVertex2, LinkedStack components2, boolean[] alert2){
seen2.add(startVertex2);
for(int i = 0; i < matrix.get(startVertex2).size() ; i++){
boolean b = helper(matrix.get(startVertex2), i);
if(matrix.get(startVertex2).get(i) !=0 && !seen2.contains(i)){
int p = i;
RecursionHelper(seen2, i , components2, alert2);
if(b = true && alert2[startVertex2] == false){
components2.push(startVertex2);
alert2[startVertex2]= true;
}
}
else if(b = true && alert2[startVertex2] == false){
components2.push(startVertex2);
alert2[startVertex2]= true;
}
}
}
Here is my helper method for telling if the current index is the one with the last 1 in the ArrayList
public boolean helper(ArrayList<Integer> k, int o){
boolean last = false;
int capture = 0; //the index of the last connection in the arraylist
if(k.size() == 1){
return true;
}
for(int i = k.size() -1 ; i > 0; i--){
if(k.get(i) == 1){
capture = i;
}
}
if
( o == capture){
last = true;
}
return last;
}
matrix is a 2D arraylist that I'm using as an adjacency matrix to display the graph.