Skip to content

Bidirectional dijkstra optimization: from 1.1x to 25x faster - #8206

Merged
rossbar merged 5 commits into
networkx:mainfrom
amcandio:bidirectional_dijkstra_optimization
Aug 28, 2025
Merged

rossbar merged 5 commits into
networkx:mainfrom
amcandio:bidirectional_dijkstra_optimization

Conversation

@amcandio

@amcandio amcandio commented Aug 25, 2025 •

Copy link
Copy Markdown
Contributor

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_path method 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.

Change Before [50d0e03] After [55fb31d] <bidirectional_dijkstra_optimization> Ratio Benchmark (Parameter)
- 186±0.2μs 146±0.4μs 0.78 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('dijkstra_relaxation_worst_case(100)')
- 2.83±0.09ms 1.71±0.02ms 0.60 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('dijkstra_relaxation_worst_case(1000)')
- 192±4ms 21.4±0.6ms 0.11 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('dijkstra_relaxation_worst_case(10000)')
- 760±4ms 45.8±0.3ms 0.06 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('dijkstra_relaxation_worst_case(20000)')
4.25±0.01μs 4.02±0.01μs 0.95 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 10, 0.1, seed=42)')
- 10.8±0.09μs 9.31±0.3μs 0.86 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 10, 0.5, seed=42)')
- 10.9±0.01μs 9.31±0.03μs 0.85 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 10, 0.9, seed=42)')
- 129±0.5μs 106±2μs 0.83 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 100, 0.1, seed=42)')
- 403±4μs 335±6μs 0.83 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 100, 0.5, seed=42)')
- 373±0.4μs 306±1μs 0.82 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 100, 0.9, seed=42)')
- 3.07±0.01ms 2.40±0ms 0.78 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 1000, 0.1, seed=42)')
- 20.6±0.3ms 18.0±0.8ms 0.87 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 1000, 0.5, seed=42)')
17.2±1ms 15.2±0.3ms ~0.88 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 1000, 0.9, seed=42)')
- 69.1±2μs 58.9±1μs 0.85 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, path_graph, 100)')
- 965±10μs 537±4μs 0.56 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, path_graph, 1000)')
- 82.0±2ms 5.43±0.2ms 0.07 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, path_graph, 10000)')
- 297±6ms 11.5±0.5ms 0.04 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, path_graph, 20000)')

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_rev

Or 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!

@amcandio

Copy link
Copy Markdown
Contributor Author

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

@amcandio

Copy link
Copy Markdown
Contributor Author

@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

Change Before [d397c7b] <bidirectional_dijkstra_optimization~1> After [55fb31d] <bidirectional_dijkstra_optimization> Ratio Benchmark (Parameter)
153±0.5μs 146±0.4μs 0.95 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('dijkstra_relaxation_worst_case(100)')
1.77±0.01ms 1.71±0.02ms 0.96 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('dijkstra_relaxation_worst_case(1000)')
22.3±0.4ms 21.4±0.6ms 0.96 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('dijkstra_relaxation_worst_case(10000)')
48.9±0.9ms 45.8±0.3ms 0.94 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('dijkstra_relaxation_worst_case(20000)')
4.16±0.03μs 4.02±0.01μs 0.97 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 10, 0.1, seed=42)')
9.60±0.1μs 9.31±0.3μs 0.97 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 10, 0.5, seed=42)')
9.81±0.1μs 9.31±0.03μs 0.95 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 10, 0.9, seed=42)')
110±0.4μs 106±2μs 0.97 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 100, 0.1, seed=42)')
342±1μs 335±6μs 0.98 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 100, 0.5, seed=42)')
317±0.8μs 306±1μs 0.96 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 100, 0.9, seed=42)')
2.60±0.04ms 2.40±0ms 0.92 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 1000, 0.1, seed=42)')
17.8±1ms 18.0±0.8ms 1.01 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 1000, 0.5, seed=42)')
16.7±0.7ms 15.2±0.3ms ~0.91 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, gnp_random_graph, 1000, 0.9, seed=42)')
63.6±0.8μs 58.9±1μs 0.93 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, path_graph, 100)')
585±20μs 537±4μs 0.92 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, path_graph, 1000)')
6.02±0.3ms 5.43±0.2ms ~0.90 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, path_graph, 10000)')
12.3±0.1ms 11.5±0.5ms 0.94 benchmark_algorithms.WeightedGraphBenchmark.time_shortest_path('weighted_graph(42, path_graph, 20000)')

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for those timings!
I approve this PR

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, LGTM too thanks @amcandio !

@rossbar
rossbar merged commit 090cc09 into networkx:main Aug 28, 2025
@jarrodmillman jarrodmillman added this to the 3.6 milestone Aug 28, 2025
@amcandio
amcandio deleted the bidirectional_dijkstra_optimization branch August 28, 2025 19:08
alfred-mikhael pushed a commit to alfred-mikhael/networkx that referenced this pull request Sep 3, 2025
…x#8206)

Avoid constructing intermediate paths in bidirectional_dijkstra.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants