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

Welcome to Interviewbit, help us create the best experience for you!

Currently, You are a:

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your career...

Current Company *
Enter company name
Experience *

You're all set!

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Graph Data Structure & Algorithms

Go to Problems

Level 1

Jump to Level 2

Level 2

Jump to Level 3

Level 3

Jump to Level 4

Level 4

Jump to Level 5

Level 5

Jump to Level 6

Level 6

Jump to Level 7

Level 7

Jump to Level 8

Level 8

Be a Code Ninja!

Example implementation of BFS and DFS

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();
        }
BFS
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();
            q.add(this.rootNode);
            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);
                    q.add(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 adhoc
Problem Score Companies Time Status
Convert Sorted List to Binary Search Tree 300
44:30
Graph hashing
Problem Score Companies Time Status
Clone Graph 500 56:55

Additional Practice

Problem Score Companies Time Status
Connected Components 200
IBM
19:55
File Search 200 20:42
Mother Vertex 200 50:15
Path in Matrix 200 26:20
Free Mock Assessment
Help us know you better for the best experience
Current Employer *
Enter company name
College you graduated from *
Enter university name
Phone Number *
OTP will be sent to this number for verification
+1
+1
Change Number
Edit
Resend OTP
By Continuing I agree to be contacted by Scaler in the future.
Already have an account? Log in