NHacker Next
login
▲Introduction to the A* Algorithmredblobgames.com
119 points by auraham 1 days ago | 51 comments
Loading comments...
apnorton 2 hours ago [-]
The article doesn't explicitly state it in this manner in one concise place, but the way I would always think about A* from a "practical/easy-to-remember" perspective back when I was doing competitive programming is that they're all the same algorithm, but with different priorities on the priority queue:

Breadth-first Search: Priority is order of discovery of edges (that is, no priority queue/just a regular queue)

Dijkstra: Priority is distance so far + next edge distance

A*: Priority is distance so far + next edge distance + estimate of distance to target node.

This also helps me remember whether the estimate must over- or under-estimate: Since Dijkstra is making the estimate "0", clearly the "admissible heuristic" criteria must be an under-estimation.

breckognize 2 hours ago [-]
And depth first search is just a stack!
tonyhart7 59 minutes ago [-]
well they all just loop???
bcrosby95 43 minutes ago [-]
Simplifications help people memorize things but if you get too reductive it becomes useless.
JohnKemeny 4 hours ago [-]
It's that time of year again. I like A* as much as the next one, but it seems a bit excessive a times.

Title should have a (2014) in it: Introduction to the A* Algorithm (2014).

1 points, 8 months ago, 1 comments: Introduction to the a* Algorithm (https://news.ycombinator.com/item?id=41897736)

202 points, 3 years ago, 30 comments: Introduction to the A* Algorithm (2014) (https://news.ycombinator.com/item?id=30287733)

4 points, 5 years ago, 1 comments: Introduction to the a* Algorithm (https://news.ycombinator.com/item?id=24146045)

201 points, 7 years ago, 14 comments: Introduction to A* (2014) (https://news.ycombinator.com/item?id=18642462)

5 points, 7 years ago, 0 comments: Introduction to A* (https://news.ycombinator.com/item?id=16190604)

bandoti 3 hours ago [-]
Please consider some folks might be new to A*, and perhaps even HN, so maybe this is the first time they’ve seen it! :)

Also, I have ten books on perspective drawing, and my understanding isn’t complete without all ten of them

Or, if I’m teaching a subject on A*, perhaps ONE of those articles conveys the materials best for my students.

Thank you for providing links to the others though! I’m sure it will be helpful for someone.

schneems 2 hours ago [-]
I wish there was a “evergreen” feature for social sites where it tracked resubmissions and would auto suggest them to people who haven’t seen them and periodically surface them to those who have and ask “is this still relevant” That way really good content keeps being recommended to those who need it and you get fewer complaints from old timers who don’t have to see it N times.
dspillett 3 hours ago [-]
I agree, though to be a pedant:

> perhaps ONE of those articles

It is the same article each time, though the comments coming off the different postings of it might have unique nuggets of useful information to dig for.

> Thank you for providing links to the others though! I’m sure it will be helpful for someone.

It isn't as prominent as on other sites, so it isn't difficult to miss sat right at the bottom of the main page, but HN does have a working search function. I find searching for older posts this way can be quite useful for the above reason, when something comes up that has existed for a few years.

bandoti 2 hours ago [-]
Personally, I don’t bother searching because I only consume the headlines, on other news sites too, come to think of it. There’s lots of interesting things people post but frankly I’d rather pay for a good book on any subject.

hides from the dreaded downvoters

I used to spend more time browsing when reading an actual newspaper or magazine. The discourse on opinion pieces and such is more thought out too—many people, myself included, post too quickly before thinking because we’re always on the go.

Something about the online experience consuming news is less satisfying. Perhaps a hacker out there can set up a print version of HN archives, and print it on a Gutenberg Press. :)

add-sub-mul-div 3 hours ago [-]
Yeah people live by this leaky abstraction that an article having been posted before means everyone was online that day and saw it and now it has expired. And for some reason they chase these hall monitor points for pointing it out. Let's see what a discussion would be like from today's point of view.
dspillett 3 hours ago [-]
Also: some people seem to get an amount of pleasure from pointing out repeats, as if remembering that something was posted before is knowledge enough to make them a better person than the poster, us all, or just the person they thought they were themselves. This is fine when something is posted far too often, or is reposted by a point-farming bot (presumably the users running such bots hope to use the reputation of the account somehow in future), but is often done overzealously.
tialaramex 3 hours ago [-]
The cheapest available model once you have Theory of Mind (the idea that the other things in the environment might be thinking like you do) is that they're you again.

The Smarties test (What's in this Smarties tube - look it's not Smarties, ok now what does somebody else think is in the tube?) shows that humans need a further step to discover that model isn't enough.

But it's still cheaper and it's pretty good. It will correctly predict that this person you've never met before probably wants cake not death, just like you. It won't reliably predict whether they prefer lemon cake or coffee cake. But it's a good first guess.

dunham 1 hours ago [-]
I was surprised to see this article because it's not that time of year. Typically A* shows up around December, because people discover it via Advent of Code. (And that's the only place I've used it.)
devrandoom 2 hours ago [-]
There are a lot of people on HN that aren't you.
pmichaud 3 hours ago [-]
I think it's just the first, most obvious thing to teach people just starting in pathfinding. It works in real life, it's easy to visualize and compute. Therefore all the tutorials are about it :)
msk-lywenn 3 hours ago [-]
Please think of all the lucky few. (xkcd 1053)
ecshafer 3 hours ago [-]
Red Blob Games is a great blog if you are interested in game development. The explanations are solid, they have at least pseudo code or an implementation for most of their posts, and they have great animations on a lot of their bigger posts to help build intuition.
Barrin92 2 hours ago [-]
I remember one of the Advent of Code challenges had a hex grid puzzle on it, and Red Blob Games hex grid guide was so good the site got hugged to death because of it for a while. Used that later when I built a civ clone too, it's a fantastic resource.

https://www.redblobgames.com/grids/hexagons/

cryptomic22 35 minutes ago [-]
Path finding visualization, highlighting A*: https://youtube.com/shorts/L8t0tt1Vrsw
deadbabe 5 minutes ago [-]
A* is simple enough, but how do you handle pathfinding when the environment isn’t known to the entity?
0manrho 46 minutes ago [-]
I have a perhaps overly simplistic question, but how is this meant to be pronounced? A-star? Ah-sterisk?
Ao7bei3s 21 minutes ago [-]
Ay-star.
upghost 3 hours ago [-]
Interesting that this used to be called "AI". I'm still trying to figure out what to call the umbrella field of Artificial Intelligence now that "AI" has come to mean the genAI subset of DL which is a subset of ML which is a subset of what used to be called "AI".
dspillett 3 hours ago [-]
> Interesting that this used to be called "AI".

What has been called AI in gaming in the past is rich and varied, and goes all the way down to a computer control opponent “seeing” a player and opening fire, moving towards, or moving away. Any code controlling NPC was referred to as “the AI of the game” even if all the code was doing was applying a few simple rote rules rather than following an exactly pre-specified sequence.

“AI” in gaming means (or has previously meant) a lot less than “AI” has meant in other fields, but with the increasing use of “AI” in all contexts this will soon no longer be the case.

bonoboTP 1 hours ago [-]
Not just computer game AI. Literally university courses called "Artificial Intelligence" would teach A*, formal logic, planning, knowledge representation, etc. See for example the famous Russell-Norvig textbook. Since deep learning became dominant around 2012-2014, that conception of AI is now (somewhat deprecatingly) called GOFAI, or "good old-fashioned AI".
kccqzy 1 hours ago [-]
There is also certainly more sophisticated AI in gaming. Remember the AI used by Deep Blue in chess? Or the AI used by DeepMind in Go against Lee Sedol? Classic games like chess or Go receive more attention from the AI community than contemporary video games.
yeasku 1 hours ago [-]
Open AI tried it with Dota 2 and had to limit the gameplay a lot to be competetive against humans.
diggan 3 hours ago [-]
The definition of "AI" has for a long time now been basically "We know it works somehow, but only few people really understand it", which is a moving target. At one point in the future, the LLMs we use today won't even be called AI anymore.
Al-Khwarizmi 2 hours ago [-]
https://en.m.wikipedia.org/wiki/AI_effect

PS: the wiki article needs updating with more confirmation from the LLM era, if anyone's up for it... :)

throwawayoldie 1 hours ago [-]
"Artificial intelligence" has always been a marketing term, not a technical one. John McCarthy coined the phrase when he was applying for a DARPA grant, and he picked it because he thought it would sound cool to the grant reviewers.
ecshafer 3 hours ago [-]
I took a course in grad school on "Game AI" that put different path finding approaches and methods of making decisions into a useful bucket away from ML and AI.
a-dub 1 hours ago [-]
a lot of early ai was simply applied classical data structures and algorithms.

although perceptrons go back decades and decades.

k2xl 2 hours ago [-]
As a game developer for a grid based puzzle game (https://thinky.gg - one of the games Pathology is a game where you have to go from Point A to Point B in shortest amount of steps).

I have found A* fascinating not because of the optimization but also from the various heuristics that can be built on top of it make it more generalized for other types of pathfinding.

Some devs have built solvers that use techniques like bidirectional search, precomputed pattern databases, and dead locking detection.

aiuniverse 3 hours ago [-]
[flagged]
hoseja 4 hours ago [-]
I don't like A*

It's a performance hack, not how entities trying to get somewhere behave.

JohnKemeny 3 hours ago [-]
It's neither a hack nor trying to "behave" like anything.

It is complete and optimal, with provable properties.

Whenever there exists an admissible heuristic, you should use A* over Dijkstra's algorithm.

HelloNurse 2 hours ago [-]
Even inadmissible heuristics can have their place, and it is easy to reason about how suboptimal they can be: you might want to trade off optimal results for performance (i.e. ignoring some part of the search space that should be searched) or to make an agent in a game a little stupid or stylized (e.g. prone to zig-zagging).
kccqzy 2 hours ago [-]
OP's point, I believe, is that inadmissible heuristics sometimes give behaviors that seem more natural, even if not optimal.
ecshafer 3 hours ago [-]
It works really wells for many situations. If I am making a top down strategy game (Think Civilization) then A* is exactly what I need, a fast performance hack that gives me the shortest path without anything weird going on. For different kind of environments, then yes it doesn't work. A* isn't very useful in a racing game.
smallstepforman 3 hours ago [-]
It took me 3 hours to implement A* with hex tiles, got it working on first attempt (land tiles only), specifically for Civ type game. It gets complex when you want to group units so that they travel together. Adding water crossings with cargo ships and war ships is a different challenge.
seivan 2 hours ago [-]
If you want cohesion between entities pathfinding, adjust the cost when you do the pathfinding for tiles that has friendlies on them to be lower than their base cost.

The way to think about water crossing with naval transports, is to consider those things to be conditions. You already have a set of condition when pathfinding. Just add another case for water. Make it so the requirement is that you’re either on a ship or there is a ship on the adjacent tile you checked previously, e.g N-1. If valid, set a flag and now every tile check that is water should be appropriate.

herman_toothrot 4 hours ago [-]
What is your preference?
FrustratedMonky 3 hours ago [-]
just take all right turns?
hoseja 3 hours ago [-]
See the target/know which direction it is? Go that direction unless you see an obstacle, in that case go around the obstacle, eventually even backtracking if it turns out the obstacle was worse than you could see. Don't see/know the target? Brownian motion until you do or get tired. Have pathfinded to the target previously? The shortest path you saw while walking there.

Al these require deep and complicated simulation of the entity though instead of solving a graph problem from omniscient perspective. Many topdown games really break my immersion when I see things just blatantly a-staring places.

Basically, things usually have limited information and it's weird to see them behave as if they don't. Plus on grids there's the cringe "diagonal and then straight" movement pattern.

reitzensteinm 3 hours ago [-]
But humans do quite intelligently pathfind around objects they're aware of, and update their mental models with new information. The front door is locked? I'll go around the back.

You can achieve exactly what you're describing by hiding information entities do not have from their pathfinding.

Graphs aren't the problem, and thinking along those lines won't get you where you're trying to go.

dweekly 3 hours ago [-]
I'm not sure your complaint is actually that A* is bad, it's that the heuristic function is unfair (to the player, by giving the mob data they shouldn't have). A more sophisticated game could use a more interesting function like an estimate as to what direction the player's movement sound would be heard from.
HelloNurse 1 hours ago [-]
Optimal pathfinding isn't always the right objective: for a good looking game agent reasonable pathfinding can often be enough, or even better.

For example, in a RTS making units follow a leader in formation can be more natural and less expensive than performing independent optimal pathfinding for all of them, while in environments with many obstacles staying away from walls (on a simplified graph of good positions) is usually more elegant than hugging corners (on a much more complex graph).

marcosdumay 2 hours ago [-]
> See the target/know which direction it is? Go that direction unless you see an obstacle, in that case go around the obstacle, eventually even backtracking if it turns out the obstacle was worse than you could see. Don't see/know the target? Brownian motion until you do or get tired.

What a long and convoluted way to try to reinvent the A* algorithm...

ecshafer 3 hours ago [-]
This isn't an actual solution. Video Games have to do things fast. You can't just sit there and wait for a minute as you try brownian motion on hundreds of units. There are plenty of different solutions to get more realistic and natural pathfinding, typically navmeshes and then local node based movement. But you still need to go from a to b, somehow.

Your example also fails in several really obvious ways. What if there is a pool of mud or water in the middle of the path, it IS traversable but doing so is not ideal? A* you give this a high traversal cost and you'll naturally path around it. Your solution will have the object going through high cost but traversable areas in strange ways. This is going to be worse, as players will just kite and exploit this fact.

cornstalks 2 hours ago [-]
I encourage you to build a game with the mechanics you describe, especially something like an RTS, and see if it's any fun to play...
Barrin92 2 hours ago [-]
> Brownian motion until you do or get tired

The point of movement for npcs in a videogame isn't to behave realistically (or to be simulated fully), it's to produce engaging and challenging behavior to the player. In 99% of cases giving characters, like enemies in an action game, some extra information to enable them moving towards the player is the correct choice, people are fine with suspending their disbelief if the alternative is npcs giving up or running around like brownian particles, which does not make for a good experience.

Almost all the time the correct choice in game design is that it's fun and interesting, unless for the exception where you're literally building a real world simulator.

diggan 3 hours ago [-]
> It's a performance hack, not how entities trying to get somewhere behave.

Welcome to game development, where fun and performance tends to be more important than realism :)

seivan 2 hours ago [-]
[dead]