Bron-Kerbosch

Performance comparison of many implementations of Bron–Kerbosch algorithms.

This project is maintained by ssomers

What is this?

Performance comparison of many implementations to solve one particular computational problem, to compare the effects of algorithm complexity, programming languages, library choices, and parallelism.

The algorithms implemented are three variants of the Bron-Kerbosch algorithm to find all maximal cliques in a graph. Some algorithm variants (IK_*) are described in the 2008 paper by F. Cazals & C. Karande, “A note on the problem of reporting maximal cliques”, Theoretical Computer Science, 407 (1): 564–568, doi:10.1016/j.tcs.2008.05.010.

It originated as a fork of cornchz/Bron-Kerbosch. Compared to the original project, the code is:

All charts below show the amount of time spent on the same particular Windows machine with a 6 core CPU, all on the same predetermined random graph, with error bars showing the minimum and maximum over 5 or 3 samples. Order of a graph = number of vertices, size of a graph = number of edges.

A random graph is easy to generate and objective, but not ideal to test the performance of the algorithm itself, because when you’re doing something useful looking for maximal cliques, the actual data likely comes in cliques, some of which are near-maximal and cause the heartaches described in the paper.

Executive summary

Report of results

Local optimization

Let’s first get one thing out of the way: what does some local optimization yield in the simplest, naive Bron-Kerbosch algorithm, in Python and Rust. Is this premature optimization or low hanging fruit?

Results

We gain as much as through switching to the best performing programming language Time spent on graphs of order 100

Therefore, all the other implementations will contain similar tweaks.

Comparing algorithms

As announced in the previous paragraph, we mostly implement locally optimized ½ versions of these. In particular, we write out the first iteration separately, because in that first iteration the set of candidate vertices starts off being huge, with every connected vertex in the graph, but that set doesn’t have to be represented at all because every reachable vertex is a candidate until excluded.

These are all single-threaded implementations (using only one CPU core).

Results

Introducing parallelism

Let’s implement Ver3-GP exploiting parallellism (using all CPU cores). How does Ver3 operate?

Ver3 structure

We already specialized the first iteration in Ver2, and Ver3 changes the order in the first iteration to the graph’s degeneracy order. So we definitely write the first iteration separately. Thus an obvious way to parallelize is to run 2 + N tasks in parallel:

Ways to implement parallelism varies per language:

Results

Comparing languages

Comparing versions of languages

Comparing implementations of the set data structure

All algorithms work heavily with sets. Some languages allow picking at compile time among various generic set implementations.

Rust

Results

In very sparse graphs, only BTreeSet allows Ver1 to scale up.

C++

Results

Time spent on graphs of order 100 Time spent on graphs of order 10k

C#

Results

Time spent on graphs of order 100 Time spent on graphs of order 10k

How to run & test

Python 3

To obtain these results:

Perform:

cd python3
(once) python -m venv venv
venv\Scripts\activate.bat
(once or twice) pip install --upgrade mypy ruff pytest hypothesis matplotlib
ruff check . --exclude "venv*"
mypy .
pytest
python -O test_maximal_cliques.py

Rust

To obtain these results:

Perform:

cd rust
(sometimes) rustup update
(sometimes) cargo upgrades && cargo update
cargo clippy --workspace
cargo test --workspace
cargo run --release

Go

To obtain these results:

Perform:

cd go
go vet ./...
go test ./...
go test ./Stats -fuzz=Stats1 -fuzztime=1s
go test ./Stats -fuzz=Stats2 -fuzztime=2s
go test ./Stats -fuzz=StatsN -fuzztime=5s
go test ./BronKerbosch -fuzz=DegeneracyOrder -fuzztime=20s
go run main.go

Optionally, on MSYS2:

PATH=$PATH:$PROGRAMFILES/go/bin
go test -race ./BronKerbosch

C#

To obtain these results:

Perform:

C++ 20

To obtain these results:

Perform:

Java

To obtain these results:

Perform:

Finally

Python and Rust publish results to detail_* files automatically, the others need a push:

python python3\publish.py go 100 10k 1M
python python3\publish.py csharp 100 10k 1M
python python3\publish.py cpp 100 10k 1M
python python3\publish.py java 100 10k 1M

And finally, generate report images:

python python3\publish.py
dot doc\Ver3.dot -Tsvg -O

License

BSD License