BFS Project
BFS Project
▰ 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 thegraph, 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?
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