← comparison · doc/graph.md

Graph generation

This page is about Graph class and graph generators. To see the list of generic graphs methods please visit this page.

The Graph class has several static methods to generate random and special graphs, like random(n, m) or complete(n). The source of randomness is rnd.

After calling a method you can add modifiers to allow or disallow loops, make graph connected etc. As you can see from the following example, chaining semantics is used. To support this semantics generation methods return not Graph itself but a special proxy class. To get a Graph itself, you may do one of the following:

See the example for further clarifications.

auto g = Graph::random(10, 20).connected().allowMulti().g().shuffled();
Graph g2 = Graph::randomStretched(100, 200, 2, 5);
cout << Graph::complete(5).allowLoops() << endl;

All graph generators return graph with sorted edges to make tests more human-readable. If you want to have your graph shuffled, use .shuffle() method, as in the example.

Generators

random(int n, int m)

complete(int n)

cycle(int n)

empty(int n)

randomStretched(int n, int m, int elongation, int spread)

randomBipartite(int n1, int n2, int m)

completeBipartite(int n1, int n2)

Modifiers

All options are unset by default. If the generator contradicts some option (like randomStretched, which always produces a connected graph), it is ignored.

connected(bool value = true)

allowMulti(bool value = true)

allowLoops(bool value = true)

directed(bool value = true)

allowAntiparallel(bool value = true)

acyclic(bool value = true)

Graph methods

Graph(int n)

void setN(int n)

Graph& shuffle()

Graph shuffled() const

Graph& shuffleAllBut(const Array& except)

Graph shuffledAllBut(const Array& except)

g = Graph::random(n, m)...;
g.shuffleAllBut({0, n-1});