The maze S is the start and the flag is the goal. Black blocks are walls, brown cells are mud: entering mud costs 3, entering a plain cell costs 1. BFS ignores the mud and only counts steps.
The wave spreads: 9 cells opened BFS takes the oldest cell from the queue and adds its neighbours to the back of the queue. The blue rings are the 2 cells waiting in the queue. The numbers show how many steps each cell is from the start.
The wave spreads: 17 cells opened BFS takes the oldest cell from the queue and adds its neighbours to the back of the queue. The blue rings are the 2 cells waiting in the queue. The numbers show how many steps each cell is from the start.
The wave spreads: 25 cells opened 1 2 3 1 4 2 6 5 3 7 13 4 8 9 10 11 12 5 12 6 7 8 9 10 11 S BFS takes the oldest cell from the queue and adds its neighbours to the back of the queue. The blue rings are the 1 cells waiting in the queue. The numbers show how many steps each cell is from the start.
The wave spreads: 34 cells opened 1 2 3 17 18 19 20 21 1 4 16 2 6 5 15 14 15 3 7 13 4 8 9 10 11 12 5 12 6 7 8 9 10 11 S BFS takes the oldest cell from the queue and adds its neighbours to the back of the queue. The blue rings are the 1 cells waiting in the queue. The numbers show how many steps each cell is from the start.
The wave spreads: 42 cells opened 1 2 3 17 18 19 20 21 22 23 1 4 16 22 24 2 6 5 15 14 15 23 25 3 7 13 24 26 4 8 9 10 11 12 25 5 12 6 7 8 9 10 11 S BFS takes the oldest cell from the queue and adds its neighbours to the back of the queue. The blue rings are the 2 cells waiting in the queue. The numbers show how many steps each cell is from the start.
The wave spreads: 50 cells opened 1 2 3 17 18 19 20 21 22 23 1 4 16 22 24 2 6 5 15 14 15 23 25 3 7 13 24 26 4 8 9 10 11 12 26 25 27 5 12 27 28 6 7 8 9 10 11 29 28 29 S BFS takes the oldest cell from the queue and adds its neighbours to the back of the queue. The blue rings are the 2 cells waiting in the queue. The numbers show how many steps each cell is from the start.
A path is found 1 2 3 17 18 19 20 21 22 23 1 4 16 22 24 2 6 5 15 14 15 23 25 3 7 13 24 26 4 8 9 10 11 12 26 25 27 5 12 27 28 6 7 8 9 10 11 29 28 29 S The goal is reached in 29 steps, the fewest possible. The cost of the path is 29. In total 50 cells were opened.
Why it matters 1 2 3 17 18 19 20 21 22 23 1 4 16 22 24 2 6 5 15 14 15 23 25 3 7 13 24 26 4 8 9 10 11 12 26 25 27 5 12 27 28 6 7 8 9 10 11 29 28 29 S BFS guarantees the shortest path when every step counts the same: the fewest links between two people in a social network, the solution of a puzzle with the fewest moves.