I have to use DFS algorithm for my university project. I saw this link DFS but i face this problem. The Depth First Search algorithm is Traversal it goes to every node in the graph but i want to define a target node when i reach to it i want to stop the algorithm i tried the following code but it still go to all node
void DFSUtil(int v, int goal, boolean visited[]) {
visited[v] = true;
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n]) {
if (n == goal) {
System.err.print(infoLinkedList.get(1).nameStation+":");
System.err.print(" |"+goal+ "| -> ");
return;
}
}
}
}
Dequeue<...>of work to do. If we read and write from the same side, we have a DFS algorithm. If we read form one side, but write to anoteher, we have BFS. I have a DFS implementation here if you want to take a look.