# what are the common data structures used for bfs and dfs in graph?

### Mohammed

Guys, does anyone know the answer?

get what are the common data structures used for bfs and dfs in graph? from screen.

## Difference between BFS and DFS

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Difference between BFS and DFS

Difficulty Level : Easy

Last Updated : 12 Sep, 2022

**Breadth-First Search:**

**BFS, Breadth-First Search,**is a vertex-based technique for finding the shortest path in the graph. It uses a Queue data structure that follows first in first out. In BFS, one vertex is selected at a time when it is visited and marked then its adjacent are visited and stored in the queue. It is slower than DFS.

**Example**:

**Input:**

A / \ B C / / \ D E F

**Output:**

A, B, C, D, E, F

**Depth First Search:**

**DFS,**

**Depth First Search**, is an edge-based technique. It uses the Stack data structure and performs two stages, first visited vertices are pushed into the stack, and second if there are no vertices then visited vertices are popped.

**Example:**

**Input:**

A / \ B D / / \ C E F

**Output:**

A, B, C, D, E, F

**BFS vs DFS**

S. No. Parameters BFS DFS

1. Stands for BFS stands for Breadth First Search. DFS stands for Depth First Search.

2. Data Structure BFS(Breadth First Search) uses Queue data structure for finding the shortest path. DFS(Depth First Search) uses Stack data structure.

3. Definition BFS is a traversal approach in which we first walk through all nodes on the same level before moving on to the next level. DFS is also a traversal approach in which the traverse begins at the root node and proceeds through the nodes as far as possible until we reach the node with no unvisited nearby nodes.

4. Technique BFS can be used to find a single source shortest path in an unweighted graph because, in BFS, we reach a vertex with a minimum number of edges from a source vertex. In DFS, we might traverse through more edges to reach a destination vertex from a source.

5. Conceptual Difference BFS builds the tree level by level. DFS builds the tree sub-tree by sub-tree.

6. Approach used It works on the concept of FIFO (First In First Out). It works on the concept of LIFO (Last In First Out).

7. Suitable for BFS is more suitable for searching vertices closer to the given source. DFS is more suitable when there are solutions away from source.

8. Suitable for Decision Treestheirwinning BFS considers all neighbors first and therefore not suitable for decision-making trees used in games or puzzles. DFS is more suitable for game or puzzle problems. We make a decision, and the then explore all paths through this decision. And if this decision leads to win situation, we stop.

9. Time Complexity The Time complexity of BFS is O(V + E) when Adjacency List is used and O(V^2) when Adjacency Matrix is used, where V stands for vertices and E stands for edges. The Time complexity of DFS is also O(V + E) when Adjacency List is used and O(V^2) when Adjacency Matrix is used, where V stands for vertices and E stands for edges.

10. Visiting of Siblings/ Children Here, siblings are visited before the children. Here, children are visited before the siblings.

11. Removal of Traversed Nodes Nodes that are traversed several times are deleted from the queue. The visited nodes are added to the stack and then removed when there are no more nodes to visit.

12. Backtracking In BFS there is no concept of backtracking. DFS algorithm is a recursive algorithm that uses the idea of backtracking

13. Applications BFS is used in various applications such as bipartite graphs, shortest paths, etc. DFS is used in various applications such as acyclic graphs and topological order etc.

14. Memory BFS requires more memory. DFS requires less memory.

15. Optimality BFS is optimal for finding the shortest path. DFS is not optimal for finding the shortest path.

16. Space complexity In BFS, the space complexity is more critical as compared to time complexity. DFS has lesser space complexity because at a time it needs to store only a single path from the root to the leaf node.

17. Speed BFS is slow as compared to DFS. DFS is fast as compared to BFS.

18. When to use? When the target is close to the source, BFS performs better. When the target is far from the source, DFS is preferable.

Please also see BFS vs DFS for Binary Tree for the differences for a Binary Tree Traversal.

Recommended

Solve DSA problems on GfG Practice.

Solve Problems

## Which data structures are used for BFS and DFS of a graph?

## Which data structures are used for BFS and DFS of a graph?

Data Structure / Graph / Graph in Data Structure Short

7150

### Answer:

Queue is used for BFS

Stack is used for DFS. DFS can also be implemented using recursion (Note that recursion also uses function call stack).

### Join Our telegram group to ask Questions

Click below to button to join our groups.

Ask Question - CSE

Programming Language - PDF

Go to Question List

This Particular section is dedicated to **Question & Answer** only. If you want learn more about **Data Structure**. Then you can visit below links to gets more depth on this subject.

Data Structure Syllabus

Data Structure Tutorial

Data Structure Programs

Data Structure Question Answers

Data Structure MCQ

# C Tutorials # JAVA Tutorials # HTML Tutorials # Computer Fundamental # Data Structure # DBMS Tutorials SQL # C# Language # R Language # PHP # Python # Vue JS

## Which data structures are used for BFS and DFS?

Hello Everyone, I am new here and I want to know which data structures are used for BFS and DFS? In my last interview, I have faced this question but I didn’t know those answers. Can anyone knows and help me out or sugge…

**We're hiring!**— Learn More

Make sure to pick the right category for your post:

Do you need help using OpenZeppelin products? #support

Do you want to discuss or get help with broader topics? #smart-contracts

Win POAPs for helping the forum community!

Which data structures are used for BFS and DFS?

Support ankitdixit Jan '20

Hello Everyone, I am new here and I want to know which data structures are used for BFS and DFS? In my last interview, I have faced this question but I didn’t know those answers. Can anyone knows and help me out or suggest some data structure based interview questions as a beginner point.

Solved by Rohan_Joshi in post #4

I Totally, Agree with your points, Yes this question is valid for those data science forums where you can get the perfect answers, I am a developer as well, but as you asked, So here sharing with your answer of the difference between BFS and DFS. [BFS and DFS] For Data Science Interview Questions…

### created

Jan '20

### last reply

Jan '20 5

### replies

7.3k

### views

4

### users

6

### likes

3

### links

2 2 ylv-io Great contributor 10 Jan '20

Hey @ankitdixit! Welcome to the community!

This forum is dedicated to blockchain topics. You’d be better of looking at websites like stackoverflow 24 for general CS help.

Going back to your question BFS uses Queue there DFS uses Stack. Hope it helps!

abcoathup Great contributor 731 Jan '20 Hi @ankitdixit,

Welcome to the community

Was this for a blockchain developer role?

I didn’t know what the terms were: Breadth first search (BFS) and depth first search (DFS)

https://www.ics.uci.edu/~eppstein/161/960215.html 18

Were you looking for data structures in Solidity?

Rohan_Joshi 1 Jan '20

I Totally, Agree with your points, Yes this question is valid for those data science forums where you can get the perfect answers, I am a developer as well, but as you asked, So here sharing with your answer of the difference between BFS and DFS.

BFS and DFS 638×733 49.3 KB

For Data Science Interview Questions, I am going to list out questions which are asked in the interviews:

Question: What do you understand by a data structure?

Question: How does a linear data structure differ from a non-linear data structure?

Question: Please enumerate the various operations that can be performed on a data structure.

Question: Can you tell which data structures are used for BFS and DFS of a graph?

Question: Please explain stack and also mention some of its important applications.

Question: What is a queue? How is it different from a stack?

Question: What do you understand by a binary search? What is the best scenario of using it?

Question: Could you explain how to reference all the elements in a one-dimension array?

Question: Please explain what do you understand by FIFO and LIFO?

Question: Do you know how does dynamic memory allocation help in managing data?

Question: What is the difference between NULL and VOID?

Question: How does a POP operation differ from a PUSH operation?

Question: Could you explain how does variable declaration affect memory allocation?

Question: Please explain the concept of data abstraction.

Question: How will you insert a new item in a binary search tree?

Question: Could you explain how does the selection sort work on an array?

Question: Do you know how the memory is affected by signed and unsigned numbers?

Question: Does all declaration statements result in a fixed memory reservation?

Question: How does an array differ from a stack?

Question: What do you understand by an AVL tree?

Question: Please explain how does an Array differ from a Linked List?

Question: What do you understand by Infix, Prefix, and Postfix notations?

Question: Please explain the Linked List and its various types.

Question: How will you implement a stack using queue and vice-versa?

Question: Which data structures are used for implementing LRU cache?

Question: Could you give a brief explanation of the various approaches for developing algorithms?

Question: Please enumerate some examples of greedy and divide and conquer algorithms.

Question: How does insertion sort differ from selection sort?

Question: What do you understand by shell sort?

Question: Can you explain tree traversal?

Question: Please explain a spanning tree. What is the maximum number of spanning trees a graph can have?

Question: How does the Kruskal’s Algorithm work?

Question: What do you understand by Heap in data structure?

Question: Please explain recursion.

Question: Can you explain the Tower of Hanoi problem?

Question: How do the BFS (Breadth-First Search) and DFS (Depth First Search) algorithms work?

Question: What do you understand by hashing?

Question: Please explain an MST (Minimum Spanning Tree). Also, explain how does Prim’s algorithm find a minimum spanning tree.

Question: Can you explain the interpolation search technique?

Guys, does anyone know the answer?