A* Algorithm: Dijkstra with a Compass

A* Algorithm: Dijkstra with a Compass

Kite Eugine

Kite Eugine • Oct 1, 2025

After writing about Dijkstra's algorithm, I thought I was done with shortest paths for a while. I mean, Dijkstra already felt like a genius move, systematically checking paths until it finds the best route. It's thorough, reliable, and mathematically proven to work. But then I stumbled upon the A* algorithm (pronounced "A-star"), and everything I thought I knew about pathfinding got a fresh perspective.

When I first heard about it, I was intrigued but skeptical. Another pathfinding algorithm? What could possibly improve on Dijkstra's already elegant approach?

At first glance, A* looks just like Dijkstra: same graph, same edges, same process of expanding nodes and tracking distances. But as I dug deeper into the algorithm's mechanics, I noticed one subtle yet profound difference: it wasn't just blindly exploring everything in all directions. It seemed to have a sense of direction, like it was saying: "I think the goal is over there, so let's lean that way."

And that's when it clicked for me. A is Dijkstra with a compass.*

Where Dijkstra explores methodically but without preference, treating all unexplored paths as equally worthy of investigation, A* adds a layer of informed decision-making. It's like giving a methodical explorer a rough map that says "you are here, and your destination is approximately in that direction." The explorer doesn't abandon their careful approach, but they can now make smarter choices about which paths to investigate first.

The Maze Walker's Dilemma

Let me paint you a clearer picture. Imagine Dijkstra as a person walking through a vast, complex maze. They've got unlimited patience and a meticulous note-taking system, carefully marking every corridor they've explored and making absolutely sure no path is overlooked. They don't know where the exit is, and they have no sense of direction, so they explore methodically in all directions: north, south, east, west—every possibility gets equal attention and consideration.

Eventually, through sheer thoroughness, they will find the shortest route to the exit. That's guaranteed. But here's the catch: they might waste considerable time and energy walking down dead ends that lead nowhere near the exit, exploring entire sections of the maze that, in hindsight, were never going to help them reach their goal.

Picture them spending half an hour carefully mapping out a corridor that actually heads away from the exit, simply because they had no way of knowing it was the wrong direction. It's frustrating to watch, even though you can't fault their logic.

A*, on the other hand, is like Dijkstra's clever cousin who not only has the same patience and methodical nature but also carries a compass that whispers, "The exit is probably in that direction." This cousin still checks paths carefully—they're not reckless or careless—but when deciding which corridor to explore next, they favor the ones that seem to point toward the exit.

They won't ignore a path entirely just because it doesn't point directly at the goal (that could cause them to miss the actual shortest route), but they'll naturally prioritize paths that look more promising. If they have to choose between exploring a corridor that leads north toward the exit or one that leads south away from it, they'll check the northward corridor first. It's common sense guided by information.

This "compass" is what we call the heuristic: an estimate, an educated guess about the distance to the goal. Think of a heuristic like the feeling you get when someone gives you directions by saying "head generally northwest for about two miles." It's not turn-by-turn precision, but it's incredibly useful for orienting yourself.

The beauty of A* is that it doesn't abandon Dijkstra's thoroughness or mathematical rigor. It just adds intuition and informed decision-making on top of that solid foundation. It's the best of both worlds: the reliability of systematic exploration combined with the efficiency of having a sense of direction.

How A* Thinks

The heart of A* is this elegant formula that guides every decision it makes:

f(n) = g(n) + h(n)

Let me break that down into digestible pieces:

  • g(n): The actual cost to reach this node from the start. This is exactly what Dijkstra tracks—the real, confirmed distance traveled so far. There's no guesswork here; it's the truth. If you've walked 100 meters to get to this intersection, g(n) = 100 meters. Simple, factual, reliable.

  • h(n): The estimated cost from this node to the goal. This is your heuristic—your educated guess about how much farther you have to go. For example, if you're standing at a street corner and you can see your destination two blocks away in a straight line, you might estimate you have about 200 meters left to walk. You don't know the exact path yet (there might be one-way streets or closed roads), but you can make a reasonable estimate based on the straight-line distance, like a bird would fly. This is often called the "Manhattan distance" or "Euclidean distance" depending on your situation.

  • f(n): The total estimated cost of the path through this node. It's your best guess at the full journey cost, answering the question: "How much have I already spent to get here, plus how much do I think I'll still need to spend to finish?" If you've walked 100 meters and estimate 200 meters remain, f(n) = 300 meters total. This becomes your priority score for this path.

At each step of the algorithm, A* looks at all the nodes it could explore next and picks the one with the smallest f(n) value. It's essentially asking itself: "Which unexplored node looks like it's on the most promising path to the goal? Which one appears to offer the best combination of 'not too far from the start' and 'seems close to the finish'?"

This simple prioritization makes all the difference. Instead of exploring nodes in a circular wave expanding equally in all directions (like Dijkstra does), A* explores in an elliptical or teardrop-shaped wave, stretched toward the goal. It still explores thoroughly enough to guarantee finding the shortest path, but it dramatically reduces wasted exploration in wrong directions.

If the heuristic is perfect—which rarely happens in real life but is theoretically possible—A* flies straight to the goal like it has GPS, barely exploring any unnecessary nodes. If the heuristic is merely reasonable, like using straight-line distance on a map, A* still tends to beat Dijkstra hands down, often exploring only a fraction of the nodes.

Here's the critical rule, though: the heuristic must never overestimate the actual distance. If it does, A* might get too confident, skip exploring important paths, and end up missing the truly shortest route. We call heuristics that obey this rule "admissible heuristics."

Think of it like this: it's okay to underestimate how far you have to walk (your compass can point you generally in the right direction even if the actual path is windier than you thought), but if you consistently think the goal is closer than it really is, you might take shortcuts that aren't actually shorter. An admissible heuristic is like being optimistic but realistic—never promising more than you can deliver.

Real-World Magic

The beautiful thing about A* is how ubiquitous it has become in our digital lives, often working invisibly in the background. Video game characters navigating around obstacles? Probably A*. GPS systems calculating routes through city streets? Often a variant of A*. Robot vacuum cleaners planning efficient cleaning paths? You guessed it—A* or something inspired by it.

The algorithm strikes such an elegant balance between optimality (finding the actual shortest path) and efficiency (not wasting time exploring obviously wrong directions) that it has become a cornerstone of artificial intelligence and pathfinding for decades.

What started as a research paper in 1968 by Peter Hart, Nils Nilsson, and Bertram Raphael has become one of the most widely deployed algorithms in computing history. Its elegance lies in its simplicity: take something that already works perfectly (Dijkstra), add one intuitive improvement (a heuristic), and suddenly you have something that works perfectly and efficiently.

Next time you watch a game character navigate a complex environment, or marvel at how quickly your GPS recalculates a route when you miss a turn, remember: there's probably an A* algorithm in there, quietly and efficiently doing its job, proving that sometimes the best improvements are the simplest ones.

Comments (0)

No comments yet. Be the first to comment!

Related Posts