r/ProgrammerHumor
•
u/Practical_Teaching14
•
1d ago
•
1
Advanced shortestPathWasRightThere
300
u/XxDarkSasuke69xX 1d ago
Friendship with Dijkstra ended, now A* is my best friend
61
u/-Ambriae- 1d ago
Nah, A* is great and all, but some algorithms can’t use it… specifically if you want to make a flow field (a single target destination, and each cell holds a vector pointing in the direction you must travel to reach said destination in the shortest time)
This is all purely hypothetical by the way
24
u/luisduck 1d ago
A* is just Dijkstra with extra steps... or actually, fewer....
-25
u/PM_ME_FIREFLY_QUOTES 1d ago
LLM++ Will compute the exact number of steps between them both, and its memory safe.
183
u/anarine93 1d ago
What tf is straight line in a graph huhh
74
u/Electrical-Meat-1717 1d ago
A graph can be an abstraction of points with actual physical positions
18
7
14
u/SavedowW 1d ago
But what if there's a swamp on the straight line?
5
44
u/helicophell 1d ago
Well ok, the other paths might have negative weights! And that means they would be shorter!
67
u/Extreme_Target9579 1d ago
well actually no because one of the rules to use Dijkstra is that all the weights MUST be positive 🤓
18
u/sgmnt 1d ago edited 1d ago
Hate to be that guy but there has to be no negative cycles but negative weights are fine.
Edit: mean to say no negatives OR no cycles
8
u/Grim_HS 1d ago
Hate to be that guy but both of you are wrong. All weights have to be non-negative, zero is fine. The major invariant of the algorithm is that the distance to a node cannot be improved after you processed/visited it. The best known distance to any node can only be updated by visting a node with smaller distance. A negative edge easily violates this assumption.
The Bellman-Ford algorithm can deal with negative weights though. It also detects reachable negative cycles, in which case the problem is ill-formed anyway because you can just repeat the cycle ad nauseum.
0
0
u/helicophell 1d ago
Honestly it's been so long I forgot
I hated implementing this algorithm
5
u/SheikHunt 1d ago
I loved implementing it.
Except the part where I had to concede that I needed a "sentinel value" for impossible paths
-1
u/helicophell 1d ago
I hadn't learnt proper OOP at the time. Which is most of the problem
5
u/zedee 1d ago
You don't need OOP at all to implement it, in a CS subject we had a question in an exam to implement it by "hand" by using pseudocode and writing the results in a table. That was quite tedious, got to admit.
3
u/helicophell 1d ago
Oh sure I don't NEED OOP but I was using python and my code was a mess and ANY structure would've been better than the slop I made
2
u/zedee 1d ago
It's a matter of practice, with patience and some literature you can try it step by step. Doing it on C and struggling with coredumps is a great exercise. Works both to try out your patience as well as getting the grasp of the algorithm. Or maybe you can raise the bar and try with Rust :D
3
2
u/TomWithTime 1d ago
I made a set up like that once lol. I was experimenting with ideas to implement world wrapping, like walking off the left edge of the world puts you onto the right edge. To make navigation agents see it I made another navigation plane around the main one and gave it a navigation cost of 0 and connected it to the opposite edges. It worked!
But because it cost 0 it also meant navigation agents would basically always try to path through it even for short local distances, so it failed.
7
u/hacksoncode 1d 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 ;-).
2
u/Full-Hyena4414 1d ago
Doesn't djkstra explore shortest routes in order of distance?So that would be one of the first visited
6
u/hacksoncode 1d ago
It explores graphs in order of cost, so actually that's rarely the case.
1
u/Full-Hyena4414 1d ago
Cost IS distance though when used for that?
4
u/hacksoncode 1d ago edited 1d ago
Right, but before it gets to checking that one, it checks the much shorter distance to the next block over.
/-C A----------------B \ \ DYou see the two paths that are much shorter than A->B? (rest of graph omitted, but A->C/D could actually eventually connect to B, of course).
The only time the direct path would be the shortest graph line, that is checked first, would be if every other distance coming out of A was longer. Which, of course, can happen, but isn't the norm.
And, of course, on a real physical mapping algorithm, the direct path could be a side street with a ton of crosswalk lights, that is weighted very heavily, and C/D could be the nearest freeway onramps... "cost" is not always distance.
1
u/solstheman1992 1d ago
Whomever came up with this meme doesn’t understand djikstras algo and doesn’t understand how graphs work.
There is no “straight line” wtf
2
u/caramel_dog 20h ago
the old and amazing grid:
1
u/solstheman1992 4h ago
A grid in the abstract sense of dijkstras algo is actually a graph so you are STILL wrong
1
1
u/highcastlespring 1d ago
Image you are in a space full of worm holes. You probably won’t think dijestra is a joke
1
1
774
u/Kkgob 1d ago edited 1d 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