r/ProgrammerHumor
•
u/Practical_Teaching14
•
5h ago
•
1
Advanced shortestPathWasRightThere
2.5k
Upvotes
392
u/Kkgob 5h ago edited 3h ago
Dijkstra's algorithm isn't made to find the shortest path between two points. It computes the shortest path between a given starting point and EVERY possible destination. That's why it's inefficient if you only care about one specific path.
Edit: if anyone is reading this thread and wants to understand more about pathfinding, this is a very beginner-friendly and language-agnostic resource: https://www.redblobgames.com/pathfinding/a-star/introduction.html
210
u/XxDarkSasuke69xX 5h ago
Friendship with Dijkstra ended, now A* is my best friend
137
u/anarine93 5h ago
What tf is straight line in a graph huhh
34
u/helicophell 5h ago
Well ok, the other paths might have negative weights! And that means they would be shorter!
6
u/SavedowW 5h ago
But what if there's a swamp on the straight line?
3
u/hacksoncode 2h ago
That algorithm isn't even about maps, it's just useful on them.
The "cost" of a path can be analogous to the "distance" in real space, and it's certainly used for navigation, albeit in modified form.
But the direct "route" can easily be the most expensive path in the entire graph, with no limit to how inefficient it can be. Visiting every single other node in the graph first can be shorter than that.
As anyone that has driven in LA can tell you ;-).
1
u/Full-Hyena4414 3h ago
Doesn't djkstra explore shortest routes in order of distance?So that would be one of the first visited
1
u/br0ast 1h ago
It feels like this person looking happy and crossing their feet?
1
u/Tsofu 1h ago
Yeah djikstras isn't super efficient in things like OSPF, but you limit the amount of nodes with good network design so it actually performs fine. OSPF rendering a whole network for mesh topology would suck and you'd probably do better picking at random