Breadth First Search (BFS)
Bishal Bhandari
12
Introduction
 Breadth-first search (BFS) is an algorithm for searching tree or data structures.
 BFS was invented in the late 1950s by E. F. Moore.
 Breadth First Search (BFS) algorithm looks a graph in a breadth wise motion and
uses a queue to remember to get the next node to start a search, when a dead end
occurs in any iteration.
Algorithm
 Rule 1 Visit the adjacent unvisited node. Mark it as visited. Display it. Insert it in a
queue.
 Rule 2 If no adjacent node is found, remove the first node from the queue.
 Rule 3 Repeat Rule 1 and Rule 2 until the queue is empty.
Example
1. Visit S and take it to the queue.
2. Then visit adjacent node from S, lets take A and enqueue it.
3. Move to another node which is adjacent to S, which is B and enqueue it.
4. Then C and enqueue it.
5. We have ABC in queue.
6. Now there is no any adjacent node to S so, we move to next node A.
7. Now we have BCD in queue.
8. Then for B, we have CDE.
9. At C node we have DEF in queue.
10. Now it turn for D and we have EFG in queue.
11. Checking in E and F as there are no adjacent node so we need to dequeue
them.
12. At last G is remain in the queue.
13. As there is no adjacent node G is dequeue from the queue.
Our final output will be SABCDEFG.
ADVANTAGES OF BREADTH-FIRST SEARCH
 BFS will never get trapped checking the useless path forever.
 If there is more than one solution then BFS can find the minimum one that
less number of steps.
DISADVANTAGES OF BREADTH-FIRST SEARCH
 The main drawback of BFS is its memory requirement.
 If the solution is farther away from the root, BFS will consume lot of time.
Thank you!!!!
Any Question???

Breadth first search (bfs)

  • 1.
    Breadth First Search(BFS) Bishal Bhandari 12
  • 2.
    Introduction  Breadth-first search(BFS) is an algorithm for searching tree or data structures.  BFS was invented in the late 1950s by E. F. Moore.  Breadth First Search (BFS) algorithm looks a graph in a breadth wise motion and uses a queue to remember to get the next node to start a search, when a dead end occurs in any iteration.
  • 3.
    Algorithm  Rule 1Visit the adjacent unvisited node. Mark it as visited. Display it. Insert it in a queue.  Rule 2 If no adjacent node is found, remove the first node from the queue.  Rule 3 Repeat Rule 1 and Rule 2 until the queue is empty.
  • 4.
    Example 1. Visit Sand take it to the queue. 2. Then visit adjacent node from S, lets take A and enqueue it. 3. Move to another node which is adjacent to S, which is B and enqueue it. 4. Then C and enqueue it. 5. We have ABC in queue. 6. Now there is no any adjacent node to S so, we move to next node A. 7. Now we have BCD in queue. 8. Then for B, we have CDE. 9. At C node we have DEF in queue. 10. Now it turn for D and we have EFG in queue. 11. Checking in E and F as there are no adjacent node so we need to dequeue them. 12. At last G is remain in the queue. 13. As there is no adjacent node G is dequeue from the queue. Our final output will be SABCDEFG.
  • 5.
    ADVANTAGES OF BREADTH-FIRSTSEARCH  BFS will never get trapped checking the useless path forever.  If there is more than one solution then BFS can find the minimum one that less number of steps. DISADVANTAGES OF BREADTH-FIRST SEARCH  The main drawback of BFS is its memory requirement.  If the solution is farther away from the root, BFS will consume lot of time.
  • 6.