Pathfinding Algorithms [2D simulation : A*, Dijkstra, GBFS]

Published: (May 9, 2026 at 05:36 AM EDT)
3 min read
Source: Dev.to

Source: Dev.to

What is the most significant trade‑off in selecting a pathfinding algorithm for an automated navigation system?

The Three Algorithms I Tested

I implemented three classic algorithms in Python using Pygame:

1. Dijkstra’s Algorithm

  • Explores uniformly in all directions
  • Guarantees the absolute shortest path
  • Very slow; explores thousands of nodes
  • Best for: Offline planning

2. A*

  • Uses a heuristic to guide search toward the goal
  • Finds the shortest path, 80‑90 % faster than Dijkstra
  • Requires a good heuristic for optimal performance
  • Best for: Real‑world navigation (Google Maps, Waze), video games, robotics
  • Rushes directly toward the goal using only the heuristic
  • Extremely fast calculation
  • Often finds longer, suboptimal paths
  • Best for: Simple video games, rapid prototyping, when “good enough” is sufficient

The Speed‑Optimality Trade‑Off

The data shows a clear pattern across the tests:

AlgorithmPath QualityComputational Cost
DijkstraPerfectVery high (3000+ nodes explored)
A*PerfectMedium
GreedyOften longerLow

You cannot have an algorithm that is both lightning‑fast and always perfect. Choose what matters more for your specific use case.

How They Fare in Different Environments

I tested all three algorithms in two simulated environments.

Environment 1: Classic Grid

  • GBFS path length: 114
  • A* path length: 94 (Greedy’s path was only 21 % longer)

Environment 2: Irregular Grid

  • GBFS path length: 196
  • A* path length: 122 (Greedy’s path was 57 % longer)

In the first environment, GBFS’s performance was acceptable; in the second, it collapsed. An algorithm that works reasonably well in one environment can completely fail in another.

What Users Actually Want

A survey of 64 respondents revealed GPS priorities:

  • 50 % want the fastest route, even if it’s complicated.
  • 25 % want the simplest route, even if it’s slower.

A maze‑finding question showed:

  • 32.8 % would guess the exit direction (mirroring heuristic algorithms like A* and Greedy).
  • 29.7 % would methodically explore and remember where they’ve been (mirroring Dijkstra).

People naturally adopt the same strategies as the algorithms: speed vs. certainty.

Which Algorithm Is Best?

  • Dijkstra: Choose when you need the guaranteed shortest path and time doesn’t matter.
  • A*: Choose for fast, reliable paths.
  • Greedy: Choose when speed matters more than path quality.

Conclusion

A* is the most robust algorithm overall. It consistently finds optimal paths in all environments while keeping computational costs manageable. Greedy can be fast, but its reliability collapses in complex environments. Dijkstra is reliable but too slow for real‑time applications.

0 views
Back to Blog

Related posts

Read more »

str() vs repr() vs print() in Python

Overview When learning Python you encounter three built‑in utilities that often look similar: - str - repr - print At first they may seem to do the same thing—...