ad
ad
Topview AI logo

Breadth first search algorithm Example | BFS | uninformed | AI | Lec-13 | Bhanu Priya

Education


Introduction

In this article, we will explore the breadth-first search (BFS) algorithm through a practical example. BFS is a fundamental method used in graph traversal. For illustration, we will work with a sample graph where we have to find a path from the root node to a goal node.

Setup of the Example Graph

Consider the following graph structure:

  • Root Node: S
  • Successors of S: A, B, C
  • Connections:
    • B is connected to D and G
    • C is connected to G

The edges of the graph are weighted as follows:

  • S to A (weight: 2)
  • S to B (weight: 1)
  • S to C (weight: 5)
  • B to D (weight: 3)
  • B to G (weight: 3)
  • C to G (weight: 4)

Our aim is to find a path from the root node S to the goal node G using the breadth-first search algorithm.

Step 1: Visit the Root Node

We start by visiting the root node, which is S.

Step 2: Visit All Successor Nodes

Next, we identify and visit all direct successors of S. These successors are A, B, and C.

Step 3: Expand the Graph

We continue by expanding the graph in the form of a tree structure. The first level contains S, followed by A, B, and C on the next level.

Step 4: Expand Further Connections

Continuing with the expansions:

  • A does not connect to anything further in our example.
  • B connects to D and G.
  • C connects to G directly as well.

Step 5: Complete Tree Representation

The graph can be visualized as follows:

        S
       /|\
      A B C
         |\
         D G
         |
         G

Here, we observe that there are possible paths from S to G.

Finding the Desired Path

To find the path from S to G using BFS, we note that we need to traverse all nodes at each level before moving to the next. In our scenario, G appears at the second depth level:

  • Possible paths are:
    • S → B → G
    • S → C → G

Thus, we conclude that there are two viable paths from S to G: S → B → G or S → C → G.

Conclusion

Through this example, we have successfully demonstrated how to utilize the breadth-first search algorithm to identify routes in a graph. The BFS approach ensures that all nodes at a given depth are explored before moving deeper, which aids in finding the shortest path in an unweighted graph scenario effectively.


Keyword

  • Breadth-First Search (BFS)
  • Graph
  • Nodes
  • Uninformed Search
  • Pathfinding
  • Tree Structure

FAQ

Q1: What is Breadth-First Search (BFS)?
A1: BFS is an algorithm for traversing or searching tree or graph data structures. It starts at the root node and explores all its neighbors at the present depth before moving on to nodes at the next depth level.

Q2: How does BFS ensure that it finds the shortest path?
A2: BFS explores all nodes at the present depth level before moving on to nodes at the next depth level, making it suitable for finding the shortest path in unweighted graphs.

Q3: What kind of data structures can BFS be applied to?
A3: BFS can be applied to both tree structures and graphs.

Q4: Can BFS be used for weighted graphs?
A4: While BFS can technically traverse weighted graphs, it is not guaranteed to find the shortest path if weights vary, as it does not account for path weights. Other algorithms such as Dijkstra's may be more appropriate in such cases.

Q5: What are the advantages of using BFS?
A5: BFS is easy to implement, guarantees the shortest path in unweighted graphs, and is useful for finding connected components in graphs.

ad

Share

linkedin icon
twitter icon
facebook icon
email icon
ad