0% found this document useful (0 votes)
5 views

BFS Project

Breath first presentation

Uploaded by

mirzadsami2116
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

BFS Project

Breath first presentation

Uploaded by

mirzadsami2116
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

BFS (Breadth-first search)

▰ What is BFS?
▰ How to implement a ​breadth-first search in Python?
▰ Advantages of BFS Algorithm
▰ Disadvantages of BFS Algorithm
▰ Applications of Breadth First Search Algorithm

1
Prepared By:
▰ Ajmal Zaland ▰ Zekrullah Nazari
▰ Ansarullah Quraishi ▰ Rafiullah Rahimi
▰ Elyas Salemi ▰ Zahid Yasin
▰ Basir Ahmad Yasin ▰ Sulaiman Payindah
▰ Junaidullah Burhan ▰ Samiullah Wardak
▰ Hafizullah Rasa ▰ Sayed Mahdi Hashimi
▰ Khan Muhammad Hasani▰ Shahabuddin Safi
▰ Zaker Hussain Kazemi ▰ Abdul Musawer Din Zad
▰ Zabiullah Noori 2
• What is BFS?
• Breadth-first search is a graph
traversal algorithm that starts
traversing the graph from the root
node and explores all the
neighboring nodes. Then, it
selects the nearest node and
explores all the unexplored
nodes. While using BFS for
traversal, any node in the graph
can be considered as the root
node. 3
The Algorithm
1. Pick any node, visit the adjacent unvisited vertex,
mark it as visited, display it, and insert it in a queue.
2. If there are no remaining adjacent vertices left,
remove the first vertex from the queue.
3. Repeat step 1 and step 2 until the queue is empty or
the desired node is found.

4
Implementation:
Consider the​graph, which ​is implemented in the code below:

5
Explanation
• Lines 3-10: The illustrated graph is represented using
an adjacency list. An easy way to do this in Python is
to use a dictionary data structure, where each vertex
has a stored list of its adjacent nodes.
• Line 12: visited is a list that is used to keep track of
visited nodes.
• Line 13: queue is a list that is used to keep track of
nodes currently in the queue.

6
• Line 29: The arguments of the bfs function are the visited list,
the graph in the form of a dictionary, and the starting node A.
• Lines 15-26: bfs follows the algorithm described above:
1. It checks and appends the starting node to the visited list and
the queue.
2. Then, while the queue contains elements, it keeps taking out
nodes from the queue, appends the neighbors of that node to the
queue if they are unvisited, and marks them as visited.
3. This continues until the queue is empty.

7
• Where BFS is used in real life?
• Using GPS navigation system BFS is used to
find neighboring places. In networking, when
we want to broadcast some packets, we use
the BFS algorithm. Path finding algorithm is
based on BFS or DFS. BFS is used in Ford-
Fulkerson algorithm to find maximum flow in
a network
8
• When Should I use BFS vs DFS?

• BFS and DFS have distinct properties and


behaviors, depending on the structure and size
of the graph. BFS is more suitable for finding
the shortest path or the closest node to the
starting node, while DFS is more suitable for
finding the longest path or the farthest node
from the starting node.
9
Advantages of Breadth First Search Algorithm
There are many advantages to using the Breadth First Search algorithm:
• The main advantage is that it is very simple to implement and understand.
• It is guaranteed to find the shortest path from the starting point to the goal.
• Breadth First Search tends to find paths with fewer steps than other
algorithms, such as Depth First Search.
• Breadth First Search can be easily parallelized, which means that it can take
advantage of multiple processors to speed up the search.

10
Disadvantages of Breadth First Search Algorithm
There are a few disadvantages to the Breadth First Search
algorithm:
• It can be very memory intensive since it needs to keep track of
all the nodes in the search tree.
• It can be slow since it expands all the nodes at each level
before moving on to the next level.
• It can sometimes find sub-optimal solutions since it doesn’t
explore all possible paths through the search tree.

11
Applications of Breadth First Search Algorithm
The breadth first search algorithm is a powerful tool that can be used to solve various problems.
In this article, we will explore some of the potential applications of this algorithm.
1. Graph traversal: Breadth first search can be used to traverse a graph. This can be useful for
tasks such as finding the shortest path between two nodes or determining if a graph is connected.
2. Network routing: Breadth first search can be used to find the shortest path between two nodes
in a network. This can be useful for tasks such as routing traffic or finding the quickest route
between two points.
3. Pattern matching: Breadth first search can be used to match patterns in data. This can be useful
for tasks such as searching for a specific string in a text file or finding all instances of a particular
word in a document.
4. Data mining: Breadth first search can be used to mine data for insights. This can be useful for
tasks such as finding trends in social media data or uncovering hidden relationships in large
datasets.

12

You might also like