Search With Open and Closed List
Breadth-First Search (BFS) is a systematic graph traversal algorithm that explores the search space level by level. BFS examines all nodes at the current depth before moving to nodes at the next depth. This ensures that the first solution found is the shallowest or shortest path in an unweighted graph.
To implement BFS efficiently, we use two main data structures: the open list and the closed list:
Open List:
- Stores nodes or states that have been generated but whose children have not yet been explored.
- Operates as a First-In-First-Out (FIFO) queue, ensuring level-wise exploration.
Closed List:
- Keeps track of nodes that have already been explored.
- Prevents revisiting nodes, avoiding infinite loops and redundant computation.
Key Features of BFS:
- Completeness: BFS will find a solution if one exists.
- Optimality: BFS guarantees the shortest path in unweighted graphs.
- Deterministic Behavior: Given the same graph and start node, BFS produces the same traversal order.
Algorithm: BFS with Open and Closed Lists
Initialization:
- Begin at the initial state (start node).
- Initialize an empty open list (FIFO queue) and closed list.
Add Initial State:
- Insert the initial state into the open list.
Explore States:
- While the open list is not empty:
a. Remove the first state from the open list (FIFO behavior).
b. Add this state to the closed list to mark it as explored.
c. Generate all possible successor states from the current state.
d. For each successor state:
- If it has not been explored (i.e., not present in the closed list):
- Add it to the open list.
- Mark it as a child of the current state (for path reconstruction).
- If it has not been explored (i.e., not present in the closed list):
- While the open list is not empty:
a. Remove the first state from the open list (FIFO behavior).
b. Add this state to the closed list to mark it as explored.
c. Generate all possible successor states from the current state.
d. For each successor state:
Termination:
- Continue this process until either:
- The goal state is found, or
- The open list becomes empty (indicating no solution exists).
- Continue this process until either:
Example:
Suppose we want to find the shortest path from node A to node D in a graph.
- Open List starts with: [A]
- Remove A → Generate successors [B, C] → Add to open list → Open List: [B, C]
- Remove B → Generate successors [D, E] → Add unvisited nodes to open list → Open List: [C, D, E]
- Remove C → Generate successors → Add unvisited nodes → Open List updated
- Remove D → Goal found → Terminate traversal
BFS with open and closed lists ensures systematic exploration of the search space. By maintaining visited and unvisited nodes separately, BFS avoids loops and guarantees finding the shortest path in unweighted graphs. This approach is widely used in pathfinding, network analysis, AI problem-solving, and situations where optimality and completeness are required.