Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Go to Problems

# Example implementation of BFS and DFS

###### Algorithmic Steps

Step 1: Push the root node in the Stack.
Step 2: Loop until stack is empty.
Step 3: Peek the node of the stack.
Step 4: If the node has unvisited child nodes, get the unvisited child node, mark it as traversed and push it on stack.
Step 5: If the node does not have any unvisited child nodes, pop the node from the stack.

Based upon the above steps, the following Java code shows the implementation of the DFS algorithm:

``````
public void dfs() {
//DFS uses Stack data structure
Stack s = new Stack();
s.push(this.rootNode);
rootNode.visited = true;
printNode(rootNode);
while(!s.isEmpty()) {
Node n = (Node)s.peek();
Node child = getUnvisitedChildNode(n);  // Essentially this function goes through the neighbors of n, and finds the one with node.visited = false
if(child != null) {
child.visited = true;
printNode(child); // print the node as you like.
s.push(child);
}
else {
s.pop();
}
}
//Clear visited property of nodes
clearNodes();
}
``````
###### Algorithmic Steps

Step 1: Push the root node in the Queue.
Step 2: Loop until the queue is empty.
Step 3: Remove the node from the Queue.
Step 4: If the removed node has unvisited child nodes, mark them as visited and insert the unvisited children in the queue.

Based upon the above steps, the following Java code shows the implementation of the BFS algorithm:

``````        public void bfs() {
//BFS uses Queue data structure
Queue q = new LinkedList();
printNode(this.rootNode);
rootNode.visited = true;
while(!q.isEmpty()) {
Node n = (Node)q.remove();
Node child = null;
while((child = getUnvisitedChildNode(n)) != null) {
child.visited = true;
printNode(child);
}
}
//Clear visited property of nodes
clearNodes();
}
``````

## Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

## Graph Data Structure & Algorithms Problems

Graph traversal
Problem Score Companies Time Status
Path in Directed Graph 150 40:56
Water Flow 200
71:00
Stepping Numbers 300 60:28
Capture Regions on Board 500
66:16
Word Search Board 500 59:35
Depth first search
Problem Score Companies Time Status
Path with good nodes! 150 59:12
Largest Distance between nodes of a Tree 200
78:20
Good Graph 200
62:30
Cycle in Directed Graph 200 55:58
Delete Edge! 200 66:03
Two teams? 300 53:53
Bfs
Problem Score Companies Time Status
Valid Path 200 84:23
Snake Ladder Problem! 200 65:42
Region in BinaryMatrix 200 45:04
Path in Matrix 200 28:38
Level Order 300 29:55
Smallest Multiple With 0 and 1 300 75:15
Min Cost Path 300 67:34
Permutation Swaps! 300 57:01
Graph connectivity
Problem Score Companies Time Status
Commutable Islands 200 77:45
Possibility of finishing all courses given pre-requisites 200 64:40
Cycle in Undirected Graph 200 53:10
Mother Vertex 200 52:58
File Search 200 21:40
Black Shapes 300 48:55
Problem Score Companies Time Status
Convert Sorted List to Binary Search Tree 300
44:40
Shortest path
Problem Score Companies Time Status
Sum Of Fibonacci Numbers 300
53:01
Knight On Chess Board 300 67:43
Useful Extra Edges 400 63:12
Word Ladder I 600 69:34
Word Ladder II 800 68:26
Graph hashing
Problem Score Companies Time Status
Clone Graph 500 57:06

Problem Score Companies Time Status
Connected Components 200 22:29
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *