You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is the complete BFS implementation so we can have a better idea what we are working with:
203
+
Here is the complete BFS implementation (since all of the 4 algorithms share the most of the same base) so we can have a better idea what we are working with:
204
204
205
205
```ts title="src/solvers/maze-solver-bfs.ts"
206
206
exportclassMazeSolverBFSextendsMazeSolver {
@@ -277,7 +277,7 @@ Not all graphs have edges with uniform weights. In such cases we must use algori
277
277
278
278
Dijkstra is aware of the cost between two nodes (edge weight) and it will take it into account when selecting the next node. It uses a priority queue to keep the cost history.
279
279
280
-
```ts
280
+
```ts title="src/solvers/maze-solver-dijkstra.ts"
281
281
// Take the first element from the priority queue.
282
282
// Choose the node that ads minimal cost.
283
283
queue.sort((a, b) =>a.cost-b.cost);
@@ -297,7 +297,7 @@ Dijkstra keeps a history of the cost of the current path and when selecting the
297
297
298
298
A\* is same as Dijkstra but beside the keeping the history it uses the heuristics function to predict the future - the direction in which the end node could be.
299
299
300
-
```ts
300
+
```ts title="src/solvers/maze-solver-a-star.ts"
301
301
protectedheuristic(a: Coordinate, b: Coordinate): number {
302
302
// Manhattan distance as the heuristic.
303
303
// Can only move horizontally or vertically, not diagonally. In "rectangles".
@@ -324,6 +324,12 @@ If the heuristic function is well chosen it will make A\* more efficient than th
324
324
325
325
## Conclusion
326
326
327
+
On this example we could see how algorithm analysis and design is a very sensitive and subtle discipline, that leaves no room for a low focus or lack of understanding of the domain. Although BFS, DFS, Dijkstra, and A* share a majority of the implementation, just a subtle change in the code can cause a dramatic change in the behavior.
328
+
329
+
In the demo app you can tweak the predefined mazes in the `tests/fixtures/*.txt` files and try to make your own observations. You can also check the resources and the interactive playground listed in the [References](#references) section.
330
+
331
+
Did you experiment with maze solving and pathfinding algorithms yourself before? Let me now in the comments.
332
+
327
333
## References
328
334
329
335
- Some visualized algorithms behavior https://www.youtube.com/watch?v=GC-nBgi9r0U
0 commit comments