Maze Solving Exercise

Step-by-Step Search Algorithms Practice

Perfect for classroom instruction and student practice

Problem Statement

An agent must find a path from the start (S) to the goal (G) in the following maze:

S . . #
# . # .
. . . G
S
Start position
G
Goal position
.
Free cell
#
Wall (blocked)
Rules
  • The agent can move up, down, left, or right into free cells
  • Each move has a cost of 1
  • The agent cannot move through walls (#) or outside the maze boundaries
Learning Outcomes

By completing this exercise, you will be able to:

  • Execute BFS and DFS algorithms step-by-step on a maze problem
  • Compare search strategies and understand their different exploration patterns
  • Analyze algorithm performance in terms of path optimality and nodes explored
Coordinate Reference (Row, Column):
S
(1,1)
.
(1,2)
.
(1,3)
#
(1,4)
#
(2,1)
.
(2,2)
#
(2,3)
.
(2,4)
.
(3,1)
.
(3,2)
.
(3,3)
G
(3,4)
  • 1. What is the state space for this problem?
    (Hint: Think of the agent's position.)
  • 2. What is the initial state?
  • 3. What is the goal test?
  • 4. What are the possible actions from a given state?
  • 5. What is the path cost?
Sample Solution
  1. State space: The set of all possible positions (row,column coordinates) the agent can be in the maze. In this 3×4 maze, there are potentially 12 positions, but some are walls, so the actual state space consists of 9 positions where the agent can be located.
  2. Initial state: The agent's position at the start (S), which is at coordinates (1,1).
  3. Goal test: Check if the agent's current position is at the goal (G), which is at coordinates (3,4).
  4. Actions: From any given position, the agent can move up, down, left, or right, provided the destination cell is not a wall and is within the maze boundaries. Each position will have between 1 and 4 possible actions depending on walls and boundaries.
  5. Path cost: Each move (action) has a cost of 1, so the path cost is the total number of moves made from the start position to the current position.
Key Insight: Understanding these elements is crucial for properly formulating any search problem before applying algorithms to solve it.
  • 6. If you use BFS:
    • Which data structure should be used for the frontier?
    • Why does BFS always guarantee the shortest path?
  • 7. Simulate BFS on the maze:
    • List the order in which states are expanded.
    • What path does BFS find from S to G?
    • What is the total path cost?
BFS Solution
BFS Characteristics:
  • Data structure for frontier: Queue (FIFO - First In, First Out)
  • BFS guarantees shortest path because it explores all nodes at depth d before exploring any node at depth d+1. Since each action has the same cost (1), the first path found to the goal will have the minimum number of steps (minimum cost).
BFS Simulation:
Expansion Order:
1. (1,1) - Start
2. (1,2) - Right from S
3. (1,3) - Right from (1,2)
4. (2,2) - Down from (1,2)
5. (3,2) - Down from (2,2)
6. (3,3) - Right from (3,2)
7. (3,1) - Left from (3,2)
8. (3,4) - Goal reached!
Path Found:
S(1,1) → (1,2) → (2,2) → (3,2) → (3,3) → G(3,4)

Total Cost: 5 moves
Note: Actual expansion may vary slightly depending on the order in which neighbors are added to the queue (up, down, left, right), but BFS will always find a shortest path.
  • 8. If you use DFS:
    • Which data structure should be used for the frontier?
    • Why might DFS not find the shortest path?
  • 9. Simulate DFS on the maze:
    • List the order in which states are expanded.
    • What path does DFS find from S to G?
    • What is the total path cost?
DFS Solution
DFS Characteristics:
  • Data structure for frontier: Stack (LIFO - Last In, First Out)
  • DFS might not find the shortest path because it explores as far as possible along each branch before backtracking. It commits to a specific path and follows it to its end before trying alternatives, regardless of path length. Thus, it may find a longer path to the goal before discovering a shorter one.
DFS Simulation (exploring in order: right, down, left, up):
Expansion Order:
1. (1,1) - Start
2. (1,2) - Right from S
3. (1,3) - Right from (1,2)
4. (2,2) - Down from (1,2)
5. (3,2) - Down from (2,2)
6. (3,3) - Right from (3,2)
7. (3,4) - Goal reached!
Path Found:
S(1,1) → (1,2) → (2,2) → (3,2) → (3,3) → G(3,4)

Total Cost: 5 moves
Important: In this specific maze, DFS happens to find the same optimal path as BFS because the goal is not hidden deep in a branch. However, if we change the order of exploration, DFS might find a different, potentially longer path.
  • 10. Compare BFS and DFS in terms of:
    • Path optimality (shortest path vs. any path)
    • Completeness (will it always find a solution?)
    • Time and space complexity in this grid

BFS vs DFS Comparison

Aspect BFS DFS
Path Optimality ✓ Optimal
Always finds shortest path in unweighted graphs
⚠ Not Optimal
Finds any path, may be longer than optimal
Completeness ✓ Complete
Will find solution if one exists (finite state spaces)
⚠ Conditionally Complete
Complete with explored set, may get stuck in cycles without it
Time Complexity O(b^d)
Must explore all nodes at depth d
O(b^m)
May explore all paths to maximum depth
Space Complexity O(b^d)
Stores all nodes at current depth
O(m)
Only stores current path
Our Maze Results Path: 5 moves, 8 expansions Path: 5 moves, 7 expansions
In this specific small maze:
  • BFS uses more memory but guarantees the shortest path
  • DFS uses less memory but might find a longer path (though it found optimal here by chance)
  • The practical difference in performance is minimal due to the small size
  • 11. If we add an explored set (to avoid revisiting states), how does this change the performance of BFS and DFS?
  • 12. Which algorithm would you prefer in this maze and why?
Reflection Answers
Impact of Adding an Explored Set:
BFS with Explored Set:
  • Time Complexity: Improves from O(b^d) to O(|V|+|E|) where |V| is vertices and |E| is edges
  • Space Complexity: Still O(b^d) but practically reduced
  • Performance: Significant improvement in graphs with many paths to same state
DFS with Explored Set:
  • Time Complexity: Improves from O(b^m) to O(|V|+|E|)
  • Space Complexity: Increases from O(m) to O(|V|) but prevents infinite loops
  • Performance: Critical improvement - prevents cycles and makes DFS complete
Algorithm Preference for This Maze:
BFS is Preferable Because:
  • It guarantees finding the shortest path
  • The maze is small, so BFS's space complexity disadvantage is negligible
  • The branching factor is limited by walls, reducing BFS's memory requirements
  • Finding the optimal solution is typically more important than saving memory
  • The relatively shallow depth of the goal makes BFS efficient
However, if memory was extremely limited or if we only cared about finding any solution (not necessarily the shortest), then DFS could be preferred due to its lower space complexity.
Real-world Consideration: In practice, for maze solving, informed search algorithms like A* would be even better as they combine the benefits of both while using heuristics to guide the search toward the goal.
BFS Summary
  • Data Structure: Queue (FIFO)
  • Exploration: Level by level
  • Optimality: ✅ Shortest path guaranteed
  • Completeness: ✅ Always finds solution
  • Space: Higher (stores all frontier)
  • Best for: Finding optimal paths
DFS Summary
  • Data Structure: Stack (LIFO)
  • Exploration: Deep first
  • Optimality: ❌ May find longer paths
  • Completeness: ⚠️ With explored set
  • Space: Lower (stores current path)
  • Best for: Memory-constrained situations
Key Educational Takeaways
  • Different algorithms solve different problems
  • BFS guarantees shortest path in unweighted graphs
  • DFS is memory-efficient but not optimal
  • Explored sets prevent infinite loops
  • Choose algorithm based on your goal
  • Optimality often comes with computational cost
  • Small problems may not show differences clearly
  • Real applications vary by use case

Continue Learning