Skip to content

Avoid re-exploring nodes in Kosaraju's SCC algorithm - #8056

Merged
MridulS merged 8 commits into
networkx:mainfrom
Peiffap:enh/speedup-kosaraju
Aug 7, 2025
Merged

MridulS merged 8 commits into
networkx:mainfrom
Peiffap:enh/speedup-kosaraju

Conversation

@Peiffap

@Peiffap Peiffap commented May 20, 2025 •

Copy link
Copy Markdown
Member

Fixes #4181.

EDIT

This PR speeds up the Kosaraju SCC computation by

  1. Avoiding re-exploring nodes multiple times in Kosaraju's SCC algorithm.
  2. Short-circuiting when all nodes in the graph have been seen, similarly to what was done in Speed up connected_components and weakly_connected_components #7971.
  3. Some minor docstring improvements, most notably adding an entry for the source argument and simplifying the examples.

Outdated description This PR does three things:
  1. Makes more intelligent use of the seen set in kosaraju_strongly_connected_components to avoid revisiting nodes that have already been visited—I'm 99% sure this is what the OP in kosaraju_strongly_connected_components is n^2 #4181 was referring to.
  2. Uses G._adj in the inner loops of strongly_connected_components to achieve a reasonably significant speed-up.
  3. Adds benchmarks for both algorithms. I'm not sure if these will run automatically, or if they need to be triggered (or if I even did this right). I'm not sure how many graphs or how many different densities are reasonable to explore here, so I've kept it fairly minimal for now.

It's always messy for me to try to run asv benchmarks locally, so I've included some ugly, manual testing code to get an idea for the speed-up. fast_kscc and tarjan_scc are the updated versions of the Kosaraju and Tarjan algorithms:

import networkx as nx
import time


def fast_kscc(G, source=None):
    post = list(nx.dfs_postorder_nodes(G.reverse(copy=False), source=source))

    seen = set()
    while post:
        r = post.pop()
        if r in seen:
            continue
        new = {r}
        seen.add(r)
        stack = [r]
        while stack:
            v = stack.pop()
            for w in G._adj[v]:
                if w not in seen:
                    new.add(w)
                    seen.add(w)
                    stack.append(w)
        yield new


def tarjan_scc(G):
    preorder = {}
    lowlink = {}
    scc_found = set()
    scc_queue = []
    i = 0  # Preorder counter
    for source in G:
        if source not in scc_found:
            queue = [source]
            while queue:
                v = queue[-1]
                if v not in preorder:
                    i = i + 1
                    preorder[v] = i
                done = True
                for w in G._adj[v]:
                    if w not in preorder:
                        queue.append(w)
                        done = False
                        break
                if done:
                    lowlink[v] = preorder[v]
                    for w in G._adj[v]:
                        if w not in scc_found:
                            if preorder[w] > preorder[v]:
                                lowlink[v] = min([lowlink[v], lowlink[w]])
                            else:
                                lowlink[v] = min([lowlink[v], preorder[w]])
                    queue.pop()
                    if lowlink[v] == preorder[v]:
                        scc = {v}
                        while scc_queue and preorder[scc_queue[-1]] > preorder[v]:
                            k = scc_queue.pop()
                            scc.add(k)
                        scc_found.update(scc)
                        yield scc
                    else:
                        scc_queue.append(v)


for n in [10, 100, 1000, 10000]:
    for p in [0.1, 0.01, 0.001, 0.0001]:
        G = nx.fast_gnp_random_graph(n, p, directed=True, seed=42)
        print(f"\nn={n}, p={p}")
        
        # Time fast_kscc
        start = time.time()
        kscc_result = list(fast_kscc(G))
        kscc_time = time.time() - start
        print(f"fast_kscc time: {kscc_time:.4f} seconds")

        # Time nx.kosaraju_strongly_connected_components
        start = time.time()
        nx_result = list(nx.kosaraju_strongly_connected_components(G))
        nx_time = time.time() - start
        print(f"networkx kosaraju time: {nx_time:.4f} seconds")

        # Time tarjan_scc
        start = time.time()
        tarjan_result = list(tarjan_scc(G))
        tarjan_time = time.time() - start
        print(f"tarjan scc time: {tarjan_time:.4f} seconds")

        # Time nx.strongly_connected_components
        start = time.time()
        scc_result = list(nx.strongly_connected_components(G))
        scc_time = time.time() - start
        print(f"networkx scc time: {scc_time:.4f} seconds")

        # Check that the results match (as sets of frozensets)
        assert set(map(frozenset, kscc_result)) == set(map(frozenset, nx_result)) == set(map(frozenset, scc_result)) == set(map(frozenset, tarjan_result)), "Results do not match!"

Running this on my machine gives the following speed-ups:

❯ python test_kosaraju.py

n=10, p=0.1
fast_kscc time: 0.0010 seconds
networkx kosaraju time: 0.0013 seconds
tarjan scc time: 0.0000 seconds
networkx scc time: 0.0010 seconds

n=10, p=0.01
fast_kscc time: 0.0001 seconds
networkx kosaraju time: 0.0002 seconds
tarjan scc time: 0.0000 seconds
networkx scc time: 0.0001 seconds

n=10, p=0.001
fast_kscc time: 0.0001 seconds
networkx kosaraju time: 0.0002 seconds
tarjan scc time: 0.0000 seconds
networkx scc time: 0.0001 seconds

n=10, p=0.0001
fast_kscc time: 0.0001 seconds
networkx kosaraju time: 0.0001 seconds
tarjan scc time: 0.0000 seconds
networkx scc time: 0.0000 seconds

n=100, p=0.1
fast_kscc time: 0.0006 seconds
networkx kosaraju time: 0.0007 seconds
tarjan scc time: 0.0005 seconds
networkx scc time: 0.0005 seconds

n=100, p=0.01
fast_kscc time: 0.0002 seconds
networkx kosaraju time: 0.0009 seconds
tarjan scc time: 0.0001 seconds
networkx scc time: 0.0002 seconds

n=100, p=0.001
fast_kscc time: 0.0002 seconds
networkx kosaraju time: 0.0005 seconds
tarjan scc time: 0.0001 seconds
networkx scc time: 0.0002 seconds

n=100, p=0.0001
fast_kscc time: 0.0003 seconds
networkx kosaraju time: 0.0006 seconds
tarjan scc time: 0.0001 seconds
networkx scc time: 0.0002 seconds

n=1000, p=0.1
fast_kscc time: 0.0188 seconds
networkx kosaraju time: 0.0250 seconds
tarjan scc time: 0.0455 seconds
networkx scc time: 0.0414 seconds

n=1000, p=0.01
fast_kscc time: 0.0036 seconds
networkx kosaraju time: 0.0040 seconds
tarjan scc time: 0.0054 seconds
networkx scc time: 0.0070 seconds

n=1000, p=0.001
fast_kscc time: 0.0013 seconds
networkx kosaraju time: 0.0111 seconds
tarjan scc time: 0.0019 seconds
networkx scc time: 0.0022 seconds

n=1000, p=0.0001
fast_kscc time: 0.0014 seconds
networkx kosaraju time: 0.0054 seconds
tarjan scc time: 0.0009 seconds
networkx scc time: 0.0029 seconds

n=10000, p=0.1
fast_kscc time: 3.3322 seconds
networkx kosaraju time: 6.8289 seconds
tarjan scc time: 4.6371 seconds
networkx scc time: 4.8860 seconds

n=10000, p=0.01
fast_kscc time: 0.4190 seconds
networkx kosaraju time: 0.3396 seconds
tarjan scc time: 0.4548 seconds
networkx scc time: 0.4941 seconds

n=10000, p=0.001
fast_kscc time: 0.0505 seconds
networkx kosaraju time: 0.0676 seconds
tarjan scc time: 0.0696 seconds
networkx scc time: 0.0988 seconds

n=10000, p=0.0001
fast_kscc time: 0.0184 seconds
networkx kosaraju time: 0.2674 seconds
tarjan scc time: 0.0215 seconds
networkx scc time: 0.0304 seconds


class DirectedAlgorithmBenchmarks:
timeout = 120
_graphs = [

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.

This is loaded every time you create the class. Would you do it lazily and load the graph on a setup call?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

+1 for doing this, but in a follow-up PR. We can do the same for UndirectedAlgorithmBenchmarks.

@Peiffap
Peiffap force-pushed the enh/speedup-kosaraju branch from aebee6a to dbfbd03 Compare May 20, 2025 17:21

@amcandio amcandio 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.

Looks good to me. You might want to update the benchmark results after the latest changes!

@Peiffap

Peiffap commented May 23, 2025

Copy link
Copy Markdown
Member Author

Hm... I've been playing around with timing for a few hours now and can't seem to get the same results anymore for Kosaraju. I'm not sure why that is. Right now, my timing experiments show that the NetworkX implementation is slightly faster (~1.25x) than in this PR when the number of edges is huge and there is only one SCC. This makes sense when you consider that the change only affects the scenario where a bunch of nodes have been explored in a previous SCC, and don't need to be re-explored). Something about dfs_labeled_edges makes it really fast—much faster than the naive exploration done in this PR. For situations with multiple SCCs, though, the PR is much faster, in line with previous timings. The annoying thing is that the "very large number of edges" scenario is by far the one that takes the longest, and so a 25% difference there is significant.

However, we can easily get the best of both worlds if we adapt the dfs_labeled_edges function to take an optional seen argument (default: None) that specifies nodes that have already been seen outside of the DFS. That way, we make use of the speed of dfs_labeled_edges, while also avoiding re-exploration of nodes. It also has the added benefit of being a minimal code change, and seems like it would be a useful pattern in some other places too). I'm beginning to feel like it'd be best to put the changes to the Tarjan algorithm and the benchmarking suite out of this PR (because they aren't affected by this change), and to make a separate PR for the augmented dfs_labeled_edges approach which can then be compared to this one. What do you think?

@Peiffap
Peiffap force-pushed the enh/speedup-kosaraju branch from dbfbd03 to e6a4cc6 Compare May 23, 2025 15:39
@Peiffap Peiffap changed the title Speed up SCC implementations Avoid re-exploring nodes in Kosaraju's SCC algorithm May 23, 2025
@amcandio

Copy link
Copy Markdown
Contributor

If you want to optimize for dense cases you can do the same trick as in #7971. What do you think?

    post = list(nx.dfs_postorder_nodes(G.reverse(copy=False), source=source))
    n = len(post)
    seen = set()
    while post and len(seen) < n:
        r = post.pop()
        if r in seen:
            continue
        new = {r}
        seen.add(r)
        stack = [r]
        while stack and len(seen) < n:
            v = stack.pop()
            for w in G._adj[v]:
                if w not in seen:
                    new.add(w)
                    seen.add(w)
                    stack.append(w)
        yield new

@amcandio

Copy link
Copy Markdown
Contributor

Seems like it's much better now!

n=10, p=1.0
fast_kscc time: 0.0076 seconds
networkx kosaraju time: 0.0008 seconds
tarjan scc time: 0.0006 seconds
ale scc time: 0.0002 seconds
networkx scc time: 0.0017 seconds

n=100, p=1.0
fast_kscc time: 0.0039 seconds
networkx kosaraju time: 0.0065 seconds
tarjan scc time: 0.0043 seconds
ale scc time: 0.0020 seconds
networkx scc time: 0.0091 seconds

n=10, p=0.1
fast_kscc time: 0.0001 seconds
networkx kosaraju time: 0.0011 seconds
tarjan scc time: 0.0001 seconds
ale scc time: 0.0004 seconds
networkx scc time: 0.0005 seconds

n=10, p=0.01
fast_kscc time: 0.0001 seconds
networkx kosaraju time: 0.0001 seconds
tarjan scc time: 0.0000 seconds
ale scc time: 0.0000 seconds
networkx scc time: 0.0000 seconds

n=10, p=0.001
fast_kscc time: 0.0001 seconds
networkx kosaraju time: 0.0001 seconds
tarjan scc time: 0.0063 seconds
ale scc time: 0.0006 seconds
networkx scc time: 0.0001 seconds

n=10, p=0.0001
fast_kscc time: 0.0001 seconds
networkx kosaraju time: 0.0009 seconds
tarjan scc time: 0.0000 seconds
ale scc time: 0.0001 seconds
networkx scc time: 0.0000 seconds

n=100, p=0.1
fast_kscc time: 0.0012 seconds
networkx kosaraju time: 0.0010 seconds
tarjan scc time: 0.0017 seconds
ale scc time: 0.0010 seconds
networkx scc time: 0.0019 seconds

n=100, p=0.01
fast_kscc time: 0.0002 seconds
networkx kosaraju time: 0.0044 seconds
tarjan scc time: 0.0002 seconds
ale scc time: 0.0009 seconds
networkx scc time: 0.0007 seconds

n=100, p=0.001
fast_kscc time: 0.0002 seconds
networkx kosaraju time: 0.0013 seconds
tarjan scc time: 0.0001 seconds
ale scc time: 0.0002 seconds
networkx scc time: 0.0002 seconds

n=100, p=0.0001
fast_kscc time: 0.0001 seconds
networkx kosaraju time: 0.0005 seconds
tarjan scc time: 0.0001 seconds
ale scc time: 0.0001 seconds
networkx scc time: 0.0002 seconds

n=1000, p=0.1
fast_kscc time: 0.0362 seconds
networkx kosaraju time: 0.0337 seconds
tarjan scc time: 0.0443 seconds
ale scc time: 0.0176 seconds
networkx scc time: 0.0438 seconds

n=1000, p=0.01
fast_kscc time: 0.0052 seconds
networkx kosaraju time: 0.0110 seconds
tarjan scc time: 0.0073 seconds
ale scc time: 0.0076 seconds
networkx scc time: 0.0083 seconds

n=1000, p=0.001
fast_kscc time: 0.0053 seconds
networkx kosaraju time: 0.0161 seconds
tarjan scc time: 0.0012 seconds
ale scc time: 0.0016 seconds
networkx scc time: 0.0041 seconds

n=1000, p=0.0001
fast_kscc time: 0.0042 seconds
networkx kosaraju time: 0.0084 seconds
tarjan scc time: 0.0027 seconds
ale scc time: 0.0029 seconds
networkx scc time: 0.0037 seconds


n=10000, p=0.1
fast_kscc time: 8.3025 seconds
networkx kosaraju time: 7.5332 seconds
tarjan scc time: 10.6029 seconds
ale scc time: 4.5603 seconds
networkx scc time: 11.3966 seconds

n=10000, p=0.01
fast_kscc time: 1.0399 seconds
networkx kosaraju time: 0.4223 seconds
tarjan scc time: 0.5747 seconds
ale scc time: 0.2562 seconds
networkx scc time: 0.7033 seconds

n=10000, p=0.001
fast_kscc time: 0.0841 seconds
networkx kosaraju time: 0.2050 seconds
tarjan scc time: 0.2217 seconds
ale scc time: 0.1874 seconds
networkx scc time: 0.1199 seconds

n=10000, p=0.0001
fast_kscc time: 0.0548 seconds
networkx kosaraju time: 0.4363 seconds
tarjan scc time: 0.0277 seconds
ale scc time: 0.0332 seconds
networkx scc time: 0.0450 seconds

@Peiffap

Peiffap commented May 26, 2025

Copy link
Copy Markdown
Member Author

Interesting that the speed-up you get from that is so significant... Thanks for trying it out!

@Peiffap

Peiffap commented Jul 9, 2025

Copy link
Copy Markdown
Member Author

I've merged in the changes to the benchmarking suite for SCC algorithms from #8059. That should make the effect of this change easier to validate.

@Peiffap

Peiffap commented Jul 14, 2025

Copy link
Copy Markdown
Member Author

For reference, running benchmarks on my machine (with a virtualenv instead of conda) gives a significant speed-up:

networkx/benchmarks on  HEAD (171095c) [!] via  v3.13.5 (networkx)
❯ asv continuous --bench DirectedAlgorithmBenchmarks main @
Couldn't load asv.plugins._mamba_helpers because
No module named 'libmambapy'
· No executable found for python 3.11
· No executable found for python 3.12
· Creating environments
· Discovering benchmarks
·· Uninstalling from virtualenv-py3.13-numpy-pandas-scipy.
·· Building 171095cd <enh/speedup-kosaraju> for virtualenv-py3.13-numpy-pandas-scipy........
·· Installing 171095cd <enh/speedup-kosaraju> into virtualenv-py3.13-numpy-pandas-scipy.
· Running 4 total benchmarks (2 commits * 1 environments * 2 benchmarks)
[ 0.00%] · For networkx commit 4410959e <main> (round 1/2):
[ 0.00%] ·· Building for virtualenv-py3.13-numpy-pandas-scipy..
[ 0.00%] ·· Benchmarking virtualenv-py3.13-numpy-pandas-scipy
[12.50%] ··· Running (benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc--)..
[25.00%] · For networkx commit 171095cd <enh/speedup-kosaraju> (round 1/2):
[25.00%] ·· Building for virtualenv-py3.13-numpy-pandas-scipy..
[25.00%] ·· Benchmarking virtualenv-py3.13-numpy-pandas-scipy
[37.50%] ··· Running (benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc--)..
[50.00%] · For networkx commit 171095cd <enh/speedup-kosaraju> (round 2/2):
[50.00%] ·· Benchmarking virtualenv-py3.13-numpy-pandas-scipy
[62.50%] ··· ...thms.DirectedAlgorithmBenchmarks.time_kosaraju_scc                 ok
[62.50%] ··· ========================== =============
                       graph
             -------------------------- -------------
               Erdos Renyi (100, 0.1)      169±5μs
               Erdos Renyi (100, 0.5)      448±8μs
               Erdos Renyi (100, 0.9)      726±10μs
              Erdos Renyi (1000, 0.01)   1.72±0.02ms
              Erdos Renyi (1000, 0.05)    4.53±0.2ms
              Erdos Renyi (1000, 0.09)    7.42±0.1ms
             ========================== =============

[75.00%] ··· ...rithms.DirectedAlgorithmBenchmarks.time_tarjan_scc                 ok
[75.00%] ··· ========================== =============
                       graph
             -------------------------- -------------
               Erdos Renyi (100, 0.1)      330±40μs
               Erdos Renyi (100, 0.5)    1.25±0.02ms
               Erdos Renyi (100, 0.9)    2.16±0.06ms
              Erdos Renyi (1000, 0.01)   3.54±0.09ms
              Erdos Renyi (1000, 0.05)    13.1±0.3ms
              Erdos Renyi (1000, 0.09)    23.2±0.5ms
             ========================== =============

[75.00%] · For networkx commit 4410959e <main> (round 2/2):
[75.00%] ·· Building for virtualenv-py3.13-numpy-pandas-scipy..
[75.00%] ·· Benchmarking virtualenv-py3.13-numpy-pandas-scipy
[87.50%] ··· ...thms.DirectedAlgorithmBenchmarks.time_kosaraju_scc                 ok
[87.50%] ··· ========================== =============
                       graph
             -------------------------- -------------
               Erdos Renyi (100, 0.1)      304±20μs
               Erdos Renyi (100, 0.5)      897±20μs
               Erdos Renyi (100, 0.9)    1.44±0.03ms
              Erdos Renyi (1000, 0.01)   2.75±0.05ms
              Erdos Renyi (1000, 0.05)    8.97±0.1ms
              Erdos Renyi (1000, 0.09)    14.9±0.3ms
             ========================== =============

[100.00%] ··· ...rithms.DirectedAlgorithmBenchmarks.time_tarjan_scc                 ok
[100.00%] ··· ========================== =============
                        graph
              -------------------------- -------------
                Erdos Renyi (100, 0.1)      323±6μs
                Erdos Renyi (100, 0.5)    1.27±0.02ms
                Erdos Renyi (100, 0.9)    2.13±0.02ms
               Erdos Renyi (1000, 0.01)   3.55±0.06ms
               Erdos Renyi (1000, 0.05)    13.4±0.2ms
               Erdos Renyi (1000, 0.09)    23.6±0.6ms
              ========================== =============

| Change   | Before [4410959e] <main>   | After [171095cd] <enh/speedup-kosaraju>   |   Ratio | Benchmark (Parameter)                                                                          |
|----------|----------------------------|-------------------------------------------|---------|------------------------------------------------------------------------------------------------|
| -        | 2.75±0.05ms                | 1.72±0.02ms                               |    0.63 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (1000, 0.01)') |
| -        | 304±20μs                   | 169±5μs                                   |    0.55 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (100, 0.1)')   |
| -        | 897±20μs                   | 448±8μs                                   |    0.5  | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (100, 0.5)')   |
| -        | 1.44±0.03ms                | 726±10μs                                  |    0.5  | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (100, 0.9)')   |
| -        | 8.97±0.1ms                 | 4.53±0.2ms                                |    0.5  | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (1000, 0.05)') |
| -        | 14.9±0.3ms                 | 7.42±0.1ms                                |    0.5  | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (1000, 0.09)') |

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.

@amcandio

Copy link
Copy Markdown
Contributor

Is Erdos Renyi really representative here? How many SCCs do these random graphs usually have?

Do you have any benchmark suite covering the worst case?

@Peiffap

Peiffap commented Jul 16, 2025

Copy link
Copy Markdown
Member Author

ER is representative as a model for random graphs, but the parameters aren't chosen very intelligently for SCC purposes—p should probably be a lot smaller. I believe the worst case here would be something like an empty graph? It's worth looking at your suggestion for building graphs with specific properties, or coming up with an alternative. It could even be as simple as finding some parameters and seeds that give a diverse set of benchmark graphs.

@Peiffap

Peiffap commented Jul 31, 2025

Copy link
Copy Markdown
Member Author

I've updated the benchmarks to have a more representative set of graphs for SCC algorithms to compare against. Here are the numbers (for both the Tarjan and Kosaraju algorithms) comparing both on the last commit before the changes to the Tarjan implementation vs the last change on this branch. cc @amcandio

| Change   | Before [9001c9e7] <refactor/is_aperiodic~12>   | After [e5a010ef] <enh/speedup-kosaraju>   |   Ratio | Benchmark (Parameter)                                                                              |
|----------|------------------------------------------------|-------------------------------------------|---------|----------------------------------------------------------------------------------------------------|
| -        | 308±2μs                                        | 264±9μs                                   |    0.85 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (100, 0.1)')         |
| -        | 3.43±0.02ms                                    | 2.88±0.02ms                               |    0.84 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (1000, 0.01)')       |
| -        | 199±6μs                                        | 165±7μs                                   |    0.83 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (100, 0.05)')        |
| -        | 2.35±0.05ms                                    | 1.93±0.01ms                               |    0.82 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (1000, 0.005)')      |
| -        | 25.1±0.2ms                                     | 20.2±0.06ms                               |    0.81 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (10000, 0.0005)')    |
| -        | 2.00±0.03ms                                    | 1.33±0.01ms                               |    0.67 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (1000, 0.01)')     |
| -        | 12.4±0.08ms                                    | 8.09±0.2ms                                |    0.65 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (10000, 0.0001)')    |
| -        | 193±1μs                                        | 119±2μs                                   |    0.62 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (100, 0.1)')       |
| -        | 1.14±0ms                                       | 710±10μs                                  |    0.62 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (1000, 0.001)')      |
| -        | 105±0.8μs                                      | 64.0±0.9μs                                |    0.61 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (100, 0.01)')        |
| -        | 11.5±0.2ms                                     | 7.05±0.2ms                                |    0.61 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (10000, 0.00005)')   |
| -        | 99.7±2μs                                       | 60.0±2μs                                  |    0.6  | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (100, 0.005)')       |
| -        | 1.09±0.01ms                                    | 626±10μs                                  |    0.57 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Erdos Renyi (1000, 0.0005)')     |
| -        | 9.91±0.3ms                                     | 5.58±0.2ms                                |    0.56 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Empty (10000)')                  |
| -        | 6.17±0.09ms                                    | 3.37±0.03ms                               |    0.55 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (1000, 0.05)')     |
| -        | 1.07±0.01ms                                    | 569±10μs                                  |    0.53 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Complete (100)')               |
| -        | 592±10μs                                       | 314±4μs                                   |    0.53 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (100, 0.5)')       |
| -        | 91.1±0.9μs                                     | 48.6±0.1μs                                |    0.53 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Empty (100)')                    |
| -        | 918±10μs                                       | 489±4μs                                   |    0.53 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_tarjan_scc('Empty (1000)')                   |
| -        | 108±2ms                                        | 55.6±0.5ms                                |    0.52 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Complete (1000)')              |
| -        | 235±90μs                                       | 99.7±1μs                                  |    0.42 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (100, 0.05)')      |
| -        | 238±1μs                                        | 67.2±2μs                                  |    0.28 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Empty (100)')                  |
| -        | 275±6μs                                        | 74.6±1μs                                  |    0.27 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (100, 0.005)')     |
| -        | 2.31±0.04ms                                    | 599±7μs                                   |    0.26 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Empty (1000)')                 |
| -        | 23.9±0.3ms                                     | 6.32±0.1ms                                |    0.26 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Empty (10000)')                |
| -        | 29.9±0.3ms                                     | 7.68±0.2ms                                |    0.26 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (10000, 0.00005)') |
| -        | 2.97±0.01ms                                    | 739±4μs                                   |    0.25 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (1000, 0.0005)')   |
| -        | 385±30μs                                       | 80.9±2μs                                  |    0.21 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (100, 0.01)')      |
| -        | 6.94±0.4ms                                     | 1.12±0.01ms                               |    0.16 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (1000, 0.005)')    |
| -        | 8.08±3ms                                       | 848±10μs                                  |    0.11 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (1000, 0.001)')    |
| -        | 168±70ms                                       | 8.93±0.1ms                                |    0.05 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (10000, 0.0001)')  |
| -        | 613±60ms                                       | 12.6±0.5ms                                |    0.02 | benchmark_algorithms.DirectedAlgorithmBenchmarks.time_kosaraju_scc('Erdos Renyi (10000, 0.0005)')  |

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.

@amcandio amcandio 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.

Sweet. Thank you so much Gilles!

@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.

This looks good to me!
Thanks @Peiffap for implementing this long-time Issue/suggestion.
It is well worth doing and you've done it well. :)

Thanks also to @JohnyXia001 for the suggestion!

@MridulS MridulS added this to the 3.6 milestone Aug 7, 2025

@MridulS MridulS 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 all!!

Looks good.

@MridulS
MridulS merged commit 722e472 into networkx:main Aug 7, 2025
@Peiffap
Peiffap deleted the enh/speedup-kosaraju branch August 7, 2025 21:30
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.

kosaraju_strongly_connected_components is n^2

5 participants