Pathfinding algorithms visualized [OC]

Pathfinding is what it sounds like. You're looking for a path between two points.

These 4 algorithms are search algorithms being applied to a maze with the goal of finding an exit. It's actually basic AI.

The 4 algorithms are pretty closely related with some key differences.

Depth-first (DFS) searches "down" through child nodes of a graph. So it'll keep checking a child node until there's no more children and then backtrack to the last node with unsearched children.

Breadth-first (BFS) is the same except it goes "side to side". It'll look at all child nodes of a given node before moving on to the next set of children.

Dijkstra is the same as BFS except it accounts for what are known as "weights" between nodes. This could represent any number of things but for simple purposes we can just say they're the "cost" of traveling between nodes. TBH, showing both BFS and Dijkstra's on a simple maze problem is kinda boring: they should return the same thing. Some reason, OP's doesn't, so I suspect there's something up. You typically use Dijkstra when traveling from node 1 to node 2 could be more expensive than traveling from node 1 to node 4 to node 2. Lots of ways to conceptualize this. Imagine you're trying to fly from Los Angeles to New York. Flying from LA to NY might cost $1000 and take 10 hours but flying from LA to SLC to NY might cost $800 and take 15 hours. So you can have Dijkstra return LA -> NY if you're looking for the shortest flight, or LA -> SLC -> NY if you're looking for cheapest. Or you can have it return LA -> NY if you're looking for most expensive, etc. BFS wouldn't be able to do that.

A* is similar to Dijkstra but it uses a heuristic to determine when to go deeper into the graph. So it'll prioritize nodes that it thinks are more likely to reach a goal.

/r/dataisbeautiful Thread Parent Link - v.redd.it