Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by OskarS on Saturday January 01, 2022 @07:15AM
(#29757693)
That assumes that the path only ever goes right and down. There’s no basis for that assumption in the general case, it’s entirely possible that the shortest path has to take a “detour” to avoid some high cost sections. This is a shortest-path problem, not a DP problem.
Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by Kwantuum on Saturday January 01, 2022 @08:45AM
(#29758209)
And in fact, there were detours in pretty much all the solutions I've seen posted online for part 2
Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by n4r9 on Saturday January 01, 2022 @07:44AM
(#29757850)
Ah, I see what you mean. The shortest path could snake around a lot. Not quite as easy as I thought!
Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by jacobmischka on Saturday January 01, 2022 @08:01AM
(#29757950)
Yep, I wasted a few hours trying to force the DP solution to work as well before giving up and just using Dijkstra.
Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by vitus on Saturday January 01, 2022 @07:49AM
(#29757877)
Yeah. Priority queue backing is the most common way of introducing the algorithm, although fib heap achieves better asymptotic performance in the general case.
Here the number of edges is linear in the number of vertices (at most 4x) so it doesn't make a difference (relative simplicity of priority queue probably wins out here in practice) but it's even more efficient to solve via memoized recursion as briefly mentioned in the article, assuming that you don't blow your stack.
Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by eutectic on Saturday January 01, 2022 @07:10AM
(#29757658)
The naive dynamic programming solution doesn't work because it's possible to 'backtrack'. But I used Bellman-Ford which is almost as simple and very fast.
Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by smcl on Saturday January 01, 2022 @07:31AM
(#29757779)
I worked on a lot of previous AoC problems in November and then did AoC 2021 in December. I should do a little write-up because there are definitely patterns that repeat, but one thing I noticed was that if you're doing them in Python then the "heapq" module is sometimes so useful it feels like cheating. There are many problems where as long as you have a simple brute force algorithm and a way to rank potential solutions, you're mostly done.
Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by EnderShadow8 on Saturday January 01, 2022 @07:51AM
(#29757889)
If it works, it works. Priority queues are an extremely powerful data structure if you leverage them right.
I find C++'s set and map and related data structures are also very useful for solving cp problems.
Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by smcl on Saturday January 01, 2022 @08:39AM
(#29758158)
oh yeah it goes without saying that Python dicts and sets are my AOC bread and butter :D
Re:Speeding up my Advent of Code solution with Dijkstra's Al...
by taneq on Saturday January 01, 2022 @08:37AM
(#29758147)
Yeah, that's how we were taught it anyway. Nodes are added to a priority queue as they're discovered while being popped off the queue in order. From the title I was expecting something like going from naive Dijkstra via A* to an optimized jump point search or something.
That said, the title aside, it's still a worthwhile post. It's sometimes easy to accidentally nuke your algorithm's performance with an innocent-looking function call or operation that's secretly turning it quadratic or worse.
I haven't yet attempted this year's AOC but I suspect this is one where working backwards from the bottom right and dynamically storing the best value at each cell would work much better than dijsktra.