Professional Documents
Culture Documents
Sumanmondal 14871022056
Sumanmondal 14871022056
Sumanmondal 14871022056
CONTINUOUS ASSESSMENT #1
SUBJECT NAME :- Artificial Intelligence
SUBJECT CODE :- MCAN-302
2023-24
Introduction of BFS
Breadth-first search (BFS) is an algorithm for traversing or searching Tree or Graph data structures.
BFS finds a path between two nodes by taking one step down all paths and then immediately
backtracking.
It starts at the tree root or some arbitrary node of a graph, sometimes referred to as a 'search key' and
explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next
depth level. Often implemented by maintaining a queue of vertices to visit.
• BFS always returns the shortest path (the one with the fewest edges) between the start and the
end vertices.
Algorithm of BFS
1. In the various levels of the data, you can mark any node as the starting or initial node to begin
traversing. The BFS will visit the node and mark it as visited and places it in the queue.
2. Now the BFS will visit the nearest and un-visited nodes and marks them. These values are also added
to the queue. The queue works on the FIFO model.
3. In a similar manner, the remaining nearest and un-visited nodes on the graph are analyzed marked
and added to the queue. These items are deleted from the queue as receive and printed as the result.
● Visiting a node: Just like the name suggests, visiting a node means to visit or select a node
● Exploring a node: Exploring the adjacent nodes (child nodes) of a selected node
Advantages of Breadth First Search:
1. BFS will never get trapped exploring the useful path forever.
2. If there is a solution, BFS will definitely find it.
3. If there is more than one solution then BFS can find the minimal one that requires less number
of steps.
4. Low storage requirement – linear with depth.
5. Easily programmable.
● Time complexity: Equivalent to the number of nodes traversed in BFS until the shallowest
solution. T(n) = 1 + n^2 + n^3 + ... + n^s = O(n^s)
● Space complexity: Equivalent to how large can the fringe get. S(n) = O(n^s)
● Completeness: BFS is complete, meaning for a given search tree, BFS will come up with a
solution if it exists.
● Optimality: BFS is optimal as long as the costs of all edges are equal.