Repository navigation
Bidirectional dijkstra optimization: from 1.1x to 25x faster - #8206
Conversation
dschult
left a comment
There was a problem hiding this comment.
Thanks for this!
I think it will make a helpful difference, especially by not constructing intermediate paths.
I'm hoping we can avoid the idiom X.get(w) is not None without much time cost, but let's see whether it makes a dfiference. I think there are 3 places where that is used.
We could avoid many lookups by gathering/removing the [dir] lookups within the inner loops. I think just after dir = 1 - dir we could put something like:
fringe_dir = fringe[dir]
dists_dir = dists[dir]
seen_dir, seen_rev = seen[dir], seen[1-dir]
preds_dir = preds[dir]I think dists[1 - dir] and neighs[dir] are only used once each so can leave them, but could introduce names for them too if more readable.
Another option to remove list lookups would be to rearrange the info for the two directions as something like:
seen_fwd, seen_rev = {}, {}
nbrs = [G._succ, G._pred] if G.is_directed() else [G._adj, G._adj]
# info = (fringe, dists, preds, seen, seen_back, nbrs)
info_fwd = ([], {}, {source: None}, seen_fwd, seen_rev, nbrs[0])
info_rev = ([], {}, {target: None}, seen_rev, seen_fwd, nbrs[1])and then in the loop after changing dir unpack:
fringe, dists, preds, seen, seen_back, nbrs = fringe_fwd if dir else fringe_revOr maybe list lookups are not such a big deal. I haven't tried timing those lately.
:) ;/
Anyway, I like the name meetnode. But to avoid the walk-vs-path questions I'd suggest changing _walk to something like _path.
Thanks!
|
Thanks Dan! I will need more time to do these experiments. I think it will be easier to review+experiment if I send the two optimizations on separate PR. So will update this one to have just the predecessor tracking change. Ale |
|
@dschult I re-run the benchmarks after removing lookup optimization and I see better results. I think we should just leave the path-recontruction optimization
|
dschult
left a comment
There was a problem hiding this comment.
Thanks for those timings!
I approve this PR
…x#8206) Avoid constructing intermediate paths in bidirectional_dijkstra.
This PR improves the bi-directional Dijkstra algorithm used for point-to-point shortest-path queries in NetworkX. This algorithm sits at the core of NetworkX shortest-path simplified interface. Every calls to
nx.shortest_pathmethod specifying both source, target and weight relies on this algorithm.Currently, this implementation does not benefit from optimizations introduced in #8191 and #8023 for regular Dijkstra (unidirectional).
In this PR, I introduce a Path-construction optimization: which prevents the algorithm from constructing paths to all visited nodes, computing only the path to the target.
Results
Benchmarks from #8059 show improvements ranging from ~10% faster to up to 25× faster in corner cases where the shortest path spans multiple nodes.