- BFS — Amplitud
- DFS — Profundidad
BFS explores states level by level. Before diving deeper into any branch, it fully expands every state at the current depth. This guarantees that the first time you reach the goal, you have taken the shortest possible path.Internally, the
lista behaves as a queue (FIFO):- New children are appended to the back with
lista.push_back(hijo) - The next state to expand is always taken from the front with
lista.front()/lista.pop_front()
How the shared loop works
BothAmplitud() and Profundidad() follow the same outer loop. At each iteration they:
Peek at the front of the list
Check whether the goal has been reached
The algorithm compares every cell of
aux.matriz against meta.matriz. If all nine cells match, the search stops and the move history is printed.Avoiding repeated states with esta_historial()
Without cycle detection, both algorithms would loop forever on the 8-Puzzle (you can always undo the last move). The esta_historial() function prevents this by keeping a separate historial list and checking each candidate child before inserting it.
The
historial list is separate from lista. lista is the active frontier (states yet to be expanded); historial is the complete record of every state that has ever been enqueued, used solely for the cycle check.BFS vs DFS comparison
| Property | BFS (Amplitud) | DFS (Profundidad) |
|---|---|---|
| Data structure | Queue — push_back / front | Stack — push_front / front |
| Explores | Level by level | Depth-first along one branch |
| Finds shortest path? | Yes | Not guaranteed |
| Memory usage | High — keeps entire frontier | Low — keeps only current path |
| Risk of infinite loop | Low (with history) | Low (with history) |
| Best when | Path length matters | Memory is limited |
Where each is used in this project
8-Puzzle (8-PUZZLE.cpp)
Amplitud()— BFS over 8-Puzzle states; guarantees the minimum number of movesProfundidad()— DFS over 8-Puzzle states; may find a solution faster in memory but with more moves
Sudoku.cpp)
Amplitud()— BFS expanding one empty cell at a time; inserts candidate boards withlista.push_backProfundidad()— DFS; reads from the back of the list withlista.back()/lista.pop_back()and also inserts at the back, making thestd::listact as a stack