Members - Reputation: 102
Posted 24 January 2012 - 02:25 PM
Such an example can be very easily used to describe Dijkstra's pathfinding, and expanded to A*. Those of you that wrote tutorials of your own, feel free to either correct my metaphor, or use it if you find it correct enough. Those of you that haven't quite grasped how those algorithms work, and are just struggling to make their pathfinder work at all, I hope this helps a bit. It's Dijkstra and A* for coders at kindergarten level. If you're an old pro, don't say it's obvious, it wasn't obvious for me until I pictured it like this.
So, here goes.
Consider a volcano, all teeming with lava, and a village near its base, pretty unlucky, if you ask me. The crater, full of white-hot lava, is the starting point; the village is the destination. We need to find out how fast can lava bring fiery armageddon to the village.
Now, in this virtual volcano, lava spreads in turns. At any moment in time, lava will pour from exactly one place onto spots around and below it - and the hotter the lava is, the easier it flows, so at any moment the hottest spot flows out to spots below... and the original spot cools completely, while the spots lava flowed onto retain some temperature: the closest, steepest spots were reached faster, so lava is hotter there than in far, slightly sloped areas. Of course, initially the crater is the only hot spot. In subsequent turns, lava from the hottest spots flows out... and out... cooling off, while the expanding edge is still pretty warm... until it reaches the village somewhere out there and reduces it to cinder. As soon as it does, we can see where lava flowed from - look at photos of lava flows, the flow direction is often easily seen! - so we can backtrack from the village onto the crater. Presto, the lava always took the easiest route, so it found the easiest path for us.
This is Dijkstra. Spots with hot lava = open nodes. Spots cooled = closed nodes. Terrain slope = cost. Lava temperature = cost of current path.
How do we burn this damn hamlet faster? Mind-bogglingly easy: we temporarily tilt the volcano towards the village, and push the village into a valley. Yes, the village is really out of luck this time. Yes, the lava can now flow differently, which may result in a longer path once we straighten the volcano and the village again (the terrain-bending was only temporary). But it should take it less turns to make crispy peasants.
A* right there. We have the volcano and the lava and everything, we just add a tilt, so that the lava has extra incentive to flow towards the village, rather than away from it. That's the heuristics magic.
Good luck burning those huts!