I have written a function that calculates if a destination vertex is reachable from the source vertex using BFS. Now i need to keep track of the path that I am using to reach to the vertex. I guess it shall be a small tweak where we can add the node to my path arraylist. Someone please help me out.
public Boolean isReachable(Node destination) {
ArrayList<Node> visited = new ArrayList<>();
LinkedList<Node> queue = new LinkedList<>();
ArrayList<Node> path = new ArrayList<>();
queue.add(this);
while (queue.size() != 0) {
Node source = queue.poll();
for (Node node : source.adjacentNodes) {
if (node.equals(destination))
return true;
if (!visited.contains(node)) {
visited.add(node);
queue.add(node);
}
}
}
return false;
}