NelworksNelworks

ML & AI / Season 1 / EP05

THE BLIND SPIDER (DIJKSTRA VS A*)

Shez

It's hanging! The routing engine is hanging!

Kurumi

...the drivers are going in circles. Another driver just delivered pizza to himself.

Shez

I don't understand! I used the standard algorithm! `Dijkstra.py`! It's mathematically proven to find the shortest path!

Kurumi

It's proven to work. It's not proven to be *fast* when you run it on a graph with 2 million intersections.

Kurumi

Show me how you select the next node to visit.

Shez

See? I'm using a Min-Heap. Complexity is $O(\log V)$. It's efficient!

Kurumi

You are prioritizing by `distance_from_start`.

Shez

Yeah. $g(n)$. The cost so far. That's how Dijkstra works.

Kurumi

Dijkstra is like water. It flows equally in every direction. It doesn't care where the Pizza is. It just wants to find the closest dry spot to wet.

Kurumi

See? You explored the North, South, and West suburbs... just to go East.

Shez

But... it found the path!

Kurumi

It found the path after checking 500,000 intersections. That's why your server is melting. You are exploring the entire city for every single delivery.

Shez

But I can't just guess! I need the *guaranteed* shortest path. If I skip nodes, I might miss a shortcut!

Kurumi

Do you think there is a magical wormhole in the opposite direction of the destination?

Shez

...No?

Kurumi

Then stop looking there. You need a **Heuristic**.

Kurumi

This is **A* (A-Star)**.

Shez

The grades I'll be getting?!

Kurumi

$g(n)$ is the cost you've already paid (distance traveled).

Kurumi

$h(n)$ is the estimated cost to go (distance to target).

Kurumi

Usually, we use **Euclidean Distance** (straight line) or **Manhattan Distance** (grid) as the heuristic.

Kurumi

The algorithm now prioritizes nodes that effectively reduce the distance to the goal. It ignores the nodes behind you.

Shez

So... `priority = cost_so_far + euclidean_dist(current, target)`.

Kurumi

Correct. You are biasing the queue. You are giving the algorithm a sense of "smell."

Shez

It's much faster! But wait... is it accurate? What if the straight line goes through a lake?

Kurumi

A* is smart. It tries the straight line first. It hits the "wall" (high cost/blocked).

Kurumi

Then, as the $g(n)$ increases for those blocked nodes, the priority shifts to the nodes going *around* the lake.

Kurumi

As long as your heuristic is **Admissible**, then it is mathematically guaranteed to find the shortest path.

Shez

It's working! CPU usage dropped to 10%!

Shez

A* is the savior!

Kurumi

Don't celebrate yet. Try routing from New York to Los Angeles.

Shez

W-Why? It's lagging again!

Kurumi

A* is better than Dijkstra. But for a continental scale? It's still too many nodes.

Kurumi

When you drive to LA, do you check every driveway in Ohio?

Shez

No. I get on the Interstate.

Kurumi

Your graph treats every residential street as equal to a highway. A* is still checking too many local roads.

Shez

Contraction... what?

Kurumi

It's a pre-processing step. You take the graph and "contract" the unimportant nodes.

Kurumi

You create "shortcuts." If you enter a highway segment, you skip all the exits until the one you need.

Kurumi

We bake the long paths into single edges. At runtime, the algorithm only looks at the "Highway Graph."

Shez

That sounds hard to implement from scratch.

Kurumi

Fool, that's why you don't write your own routing engine in Python.

Kurumi

Use a library like OSRM (Open Source Routing Machine) or GraphHopper. They use Contraction Hierarchies by default.

Kurumi

Stop reinventing the wheel with a square `while` loop.

Shez

Goodbye, my beautiful code...

Kurumi

It wasn't beautiful. It was $O(V^2)$ garbage.

Shez

So... Dijkstra is useless?

Kurumi

No. Dijkstra is for **dense, unguided exploration**.

Kurumi

Like network packet routing (OSPF). The packet doesn't know where the destination is geographically. It just flows.

Kurumi

But for physical space? Where coordinates exist? You always use the coordinates and a Heuristic.

Kurumi

A* got my coffee here while it's still hot.

Shez

(Smiling) And Dijkstra would have delivered it cold?

Kurumi

Dijkstra would have delivered it to the building next door, realized it was a wall, backtracked, checked the store room, then delivered it cold.