Introduction to Shortest Path Variants

Beyond basic BFS and Dijkstra

You know Dijkstra. You know BFS. But the interview problem has a twist: you can remove kk walls, or edges cost 00 or 11, or you need the shortest path that visits specific nodes in order.

So, what do you do?

The idea is state expansion. Instead of dist[node]dist[node], you track dist[node][extraState]dist[node][extraState] where the extra state tracks whatever constraint matters. I'll show you how to recognize these variants and modify your state space.

The algorithm stays the same. We just expand it with additional dimensions.

unnamed (57).jpg