Skip to content

Slight improvements to _find_chordality_breaker and related functions - #8408

Closed
alabarre wants to merge 2 commits into
networkx:mainfrom
alabarre:faster_chordality_verification
Closed

alabarre wants to merge 2 commits into
networkx:mainfrom
alabarre:faster_chordality_verification

Conversation

@alabarre

Copy link
Copy Markdown
Contributor

I made the following changes to chordal.py:

  • _find_chordality_breaker:
    • clique_wanna_be = numbered.intersection(G[v]) avoids useless conversion set(G[v]), since set.intersection allows any iterable;
    • try / except statement avoids call to _is_complete_graph;
  • _is_complete_graph:
    • removed function since its only call in the whole networkx package was in _find_chordality_breaker (checked with grep _is_complete_graph -R .);
  • _max_cardinality_node:
    • fixed typo in docstring;
    • number = len(wanna_connect.intersection(G[x])) avoids useless list comprehension;

A few words about my choice for the try / except bit (the rest is straightforward): I figured that the worst case for the algorithm would be the one where we had to repeatedly check whether sg is complete, so I decided instead to assume that it wasn't by trying to select an arbitrary missing edge from it, which if it exists can be obtained with non_edges. If this fails, then non_edges raises a StopIteration error, and then we know that sg is complete.

What happens in practice is not spectacular, but it's better than nothing (and I've only tested this on rather small graphs, because I don't have access to large ones). I tried recognising the first 20,000 chordal graphs on 11 vertices (there are too many to recognise them all), and this is the difference in running time that my benchmark yields:

$ python3 bench_chordal.py 
Using networkx version 3.5
processing graph 20000...
Total time taken by is_chordal on chordal-connected-11.g6: 6.01463186510955
Total time taken by my_is_chordal on chordal-connected-11.g6: 4.480613493404235

(benchmark is attached, as well as the zipped dataset; if you don't trust this file, you can generate the dataset
chordal-connected-11.g6.zip
yourself using nauty-geng -cT 11 > chordal-connected-11.g6).

Note: it is possible that some of these improvements can be applied to other functions in chordal.py. I haven't checked yet.
bench_chordal.py

- _find_chordality_breaker:
	- clique_wanna_be = numbered.intersection(G[v]) avoids useless conversion set(G[v]);
	- try / except statement avoids call to _is_complete_graph;
- _is_complete_graph:
	- removed function since its only call in the whole networkx package was in _find_chordality_breaker (checked with grep _is_complete_graph -R .);
- _max_cardinality_node:
	- fixed typo in docstring;
	- number = len(wanna_connect.intersection(G[x])) avoids useless list comprehension;
@dschult

dschult commented Dec 20, 2025

Copy link
Copy Markdown
Member

It looks like the change to remove _find_missing_edge will not speed things up much because non_edge does just about the same thing. So I'm guessing all your speed-up is avoiding creating extra set objects when we don't need them.

Can you try removing even more set creation e.g. for len(wanna_connect.intersection(G[x])) we could use sum(1 for nbr in G[x] if nbr in wanna_connect). Then we won't be creating any list or set. For clique_wanna_be we need a collection in the end, so it might as well be a set I think.

@alabarre

alabarre commented Dec 20, 2025 •

Copy link
Copy Markdown
Contributor Author

Using sum(1 ...) doesn't speed things up for my datasets, although I imagine it might benefit large graphs (the resulting function is my_is_chordal_with_sum below):

$ python3 bench_chordal.py 
Using networkx version 3.5
processing graph 20000...
Total time taken by is_chordal on chordal-connected-11.g6: 6.288034760029404
Total time taken by my_is_chordal on chordal-connected-11.g6: 4.627837447784259
Total time taken by my_is_chordal_with_sum on chordal-connected-11.g6: 4.685196637845365

I hadn't had a look at the source code of non_edges, but if their behaviour is the same I don't think it is useful to have a separate find_missing_edge function.

(Side note: non_edges could also avoid a set conversion by replacing for v in nodes - set(graph[u]) with for v in nodes.difference(graph[u]).)

@rossbar

rossbar commented Dec 21, 2025

Copy link
Copy Markdown
Contributor

Using sum(1 ...) doesn't speed things up for my datasets

It may not improve computation time, but preventing the creation of another Collection can improve the overall memory footprint; even if it's not relevant except for very large, very dense graphs.

In practice the best way to motivate performance-related enhancements1 is to add benchmarks so that others can formally and reliably reproduce timing results across various machines.

Footnotes

  1. Though the removal of extra private functions in this case is a nice cleanup! ↩

@amcandio

Copy link
Copy Markdown
Contributor

Thanks for this! +1 on adding a benchmark

Using sum(1 ...) doesn't speed things up for my datasets

I recently learned that len(s1 & s2) tends to be faster in Python because it mostly runs in C. But yeah, @rossbar point on memory is right.

In [9]: import timeit
   ...: 
   ...: setup = """
   ...: s1 = set(range(200_000))
   ...: s2 = set(range(100_000, 300_000))
   ...: """
   ...: 
   ...: print("len(s1 & s2):",
   ...:       timeit.timeit("len(s1 & s2)", setup=setup, number=100))
   ...: 
   ...: print("sum(gen):",
   ...:       timeit.timeit("sum(1 for x in s1 if x in s2)", setup=setup, number=100))
   ...: 
len(s1 & s2): 0.2336165418382734
sum(gen): 0.6165004170034081

@alabarre

Copy link
Copy Markdown
Contributor Author

Duly noted, thanks. I made the change and will have a look at the benchmarks.

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

I've added some benchmarks in #8462 to help evaluate the performance impacts of these changes. The proposed changes increase computation time for larger graphs.

Benchmarking results
| Change   | Before [c49a4359]  main    | After [bf1cd2c8]  faster_chordality_verification    |   Ratio | Benchmark (Parameter)                                                |
|----------|----------------------------|-----------------------------------------------------|---------|----------------------------------------------------------------------|
| +        | 1.90±0.01ms                | 3.35±0.02ms                                         |    1.76 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_star(100)        |
| +        | 612±1μs                    | 975±2μs                                             |    1.59 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_star(50)         |
| +        | 1.98±0.01ms                | 2.48±0.03ms                                         |    1.26 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_cycle_graph(100) |
| +        | 1.97±0.01ms                | 2.47±0.01ms                                         |    1.25 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_path(100)        |
| +        | 621±5μs                    | 751±7μs                                             |    1.21 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_path(50)         |
| +        | 71.0±0.6μs                 | 85.9±0.2μs                                          |    1.21 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_star(10)         |
| +        | 630±3μs                    | 754±4μs                                             |    1.2  | benchmark_chordal.ChordalBenchmarks.time_is_chordal_cycle_graph(50)  |
| +        | 32.6±0.1μs                 | 36.3±0.3μs                                          |    1.11 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_star(5)          |
| -        | 207±0.8μs                  | 174±2μs                                             |    0.84 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_complete(10)     |
| -        | 49.9±0.06μs                | 40.4±0.3μs                                          |    0.81 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_complete(5)      |
| -        | 35.0±0.1μs                 | 28.0±0.2μs                                          |    0.8  | benchmark_chordal.ChordalBenchmarks.time_is_chordal_cycle_graph(5)   |
| -        | 118±0.8μs                  | 90.9±0.3μs                                          |    0.77 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_wheel(10)        |
| -        | 51.4±0.2μs                 | 34.0±0.2μs                                          |    0.66 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_wheel(5)         |

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE DECREASED.

The benchmarks can be run with asv continuous --bench ChordalBenchmarks main faster_chordality_verification

@rossbar

rossbar commented Jan 12, 2026

Copy link
Copy Markdown
Contributor

Given how this scales for graphs with larger cliques, I don't think this should go in as-is. At face value at least, it makes sense why the set-based approach works better than an iterative one as the number of nodes increases.

However, there are at least two things I think are worth pursuing here:

  1. Replacing the private "helper functions" where possible, which generally improves readability, and
  2. The algorithm used to generate the benchmarking examples in the OP seems interesting! If there exists an algorithm for efficiently producing1 all non-isomorphic connected chordal graphs of n nodes (similar to WROM for all non-isomorphic connected trees) it would be of interest (but a separate PR!)

Footnotes

  1. Specifically, algorithms that improve upon the naive baselines! ↩

@dschult

dschult commented Jan 17, 2026

Copy link
Copy Markdown
Member

Here is a potential source of ideas for how to generate all chordal graphs with n-nodes.

@alabarre

Copy link
Copy Markdown
Contributor Author

You're right, this is disappointing. I can't think of ways to make this PR worth your time at the moment, I'll get back to it should fruitful ideas occur to me.

@rossbar

rossbar commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

You're right, this is disappointing. I can't think of ways to make this PR worth your time at the moment, I'll get back to it should fruitful ideas occur to me.

No need to be disappointed - this was a very productive discussion and there are certainly a lot of opportunities to add/improve here! IMO this is an excellent example of a strong PR with a nice, clear motivation --- very helpful even if it doesn't go in as-is!

I will go ahead and close this, if for no other reason than some of the changes inspired by this PR will create merge conflicts anyways. Please don't hesitate to open new PRs/issues!

@rossbar rossbar closed this Jan 27, 2026
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