Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary BFS and DFS in data structures

Rating
-
Sold
-
Pages
17
Uploaded on
26-02-2023
Written in
2022/2023

easy to understand and in short about the topics. last moment revision notes for exam

Institution
Course

Content preview

BFS vs. DFS
Before looking at the differences between BFS and DFS, we first should know about
BFS and DFS separately.

What is BFS?
BFS stands for Breadth First Search. It is also known as level order traversal. The
Queue data structure is used for the Breadth First Search traversal. When we use the
BFS algorithm for the traversal in a graph, we can consider any node as a root node.

Let's consider the below graph for the breadth first search traversal.




Suppose we consider node 0 as a root node. Therefore, the traversing would be started
from node 0.




Once node 0 is removed from the Queue, it gets printed and marked as a visited node.

Once node 0 gets removed from the Queue, then the adjacent nodes of node 0 would
be inserted in a Queue as shown below:

,Now the node 1 will be removed from the Queue; it gets printed and marked as a
visited node

Once node 1 gets removed from the Queue, then all the adjacent nodes of a node 1
will be added in a Queue. The adjacent nodes of node 1 are 0, 3, 2, 6, and 5. But we
have to insert only unvisited nodes in a Queue. Since nodes 3, 2, 6, and 5 are unvisited;
therefore, these nodes will be added in a Queue as shown below:




The next node is 3 in a Queue. So, node 3 will be removed from the Queue, it gets
printed and marked as visited as shown below:




Once node 3 gets removed from the Queue, then all the adjacent nodes of node 3
except the visited nodes will be added in a Queue. The adjacent nodes of node 3 are
0, 1, 2, and 4. Since nodes 0, 1 are already visited, and node 2 is present in a Queue;
therefore, we need to insert only node 4 in a Queue.

, Now, the next node in the Queue is 2. So, 2 would be deleted from the Queue. It gets
printed and marked as visited as shown below:




Once node 2 gets removed from the Queue, then all the adjacent nodes of node 2
except the visited nodes will be added in a Queue. The adjacent nodes of node 2 are
1, 3, 5, 6, and 4. Since the nodes 1 and 3 have already been visited, and 4, 5, 6 are
already added in the Queue; therefore, we do not need to insert any node in the
Queue.

The next element is 5. So, 5 would be deleted from the Queue. It gets printed and
marked as visited as shown below:




Once node 5 gets removed from the Queue, then all the adjacent nodes of node 5
except the visited nodes will be added in the Queue. The adjacent nodes of node 5 are
1 and 2. Since both the nodes have already been visited; therefore, there is no vertex
to be inserted in a Queue.

The next node is 6. So, 6 would be deleted from the Queue. It gets printed and marked
as visited as shown below:

Written for

Institution
Course

Document information

Uploaded on
February 26, 2023
Number of pages
17
Written in
2022/2023
Type
SUMMARY

Subjects

$7.89
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
yashonandan9480

Get to know the seller

Seller avatar
yashonandan9480 bengaluru city university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions