В чём ошибка? Алгоритм посещает только ближайших соседей.
public boolean path(int from, int to) {
//массив посещений графов
boolean[] visited = new boolean[getAllNodes().size()];
dfs(from, visited);
return visited[to];
}
public void dfs(int index, boolean[] visited) {
Stack<Integer> stack = new Stack<>();
stack.push(index);
visited[index] = true;
while(!stack.isEmpty()) {
Integer nodeIndex = stack.pop();
visited[nodeIndex] = true;
//Заполняем список потомков
List<Integer> neighboursList = new ArrayList<>(getChildrenOf(index));
for(Integer neighbour: neighboursList) {
if(!visited[neighbour]) {
stack.push(neighbour);
}
}
}
for (int i = 0; i < visited.length; i++) {
}
}
Граф

И можно ли с помошью данного алгоритма запоминать путь, а не только его наличие true/false