Posts

Showing posts with the label depth-first-search

Difference between DFS vs BFS in this example?

Image
Difference between DFS vs BFS in this example? I am bit confused after reading examples at DFS where output is printed in different fashion for both DFS approaches though both are said to be DFS. In fact DFS recursion approach print the output just in same fashion as BFS. Then what's the difference ? Is recursion approach given here not an example of DFS ? Using Stack // Iterative DFS using stack public void dfsUsingStack(Node node) { Stack<Node> stack=new Stack<Node>(); stack.add(node); node.visited=true; while (!stack.isEmpty()) { Node element=stack.pop(); System.out.print(element.data + " "); List<Node> neighbours=element.getNeighbours(); for (int i = 0; i < neighbours.size(); i++) { Node n=neighbours.get(i); if(n!=null && !n.visited) { stack.add(n); n.visi...