← comparison
tgen
Loading...
Searching...
No Matches

Labeled weighted graph generator. More...

Inheritance diagram for tgen::wgraph< VWeight, EWeight >:

Classes

struct  value
 Labeled graph value. More...

Public Member Functions

 wgraph (int n, int m, bool is_directed=false, bool has_self_loops=false)
 Creates a graph generator for a fixed number of vertices and edges.
wgraphadd_edge (int u, int v)
 Adds a preset edge that must appear in the generated graph.
wgraphadd_edges_from (const value &rhs)
 Adds all edges from another graph as preset edges.
value gen () const
 Generates a uniformly random graph satisfying the constraints.
value get_connected () const
 Random connected undirected graph extending preset edges.
value get_acyclic () const
 Random directed acyclic graph extending preset edges.
Public Member Functions inherited from tgen::gen_base< wgraph< VWeight, EWeight > >
auto gen_list (int size, Args &&...args) const
 Generates a list of several generation calls.
auto gen_until (Pred predicate, int max_tries, Args &&...args) const
 Generates a random value from the valid set until a condition is met.
auto distinct (Args &&...args) const
 Creates distinct generator for current generator.

Static Public Member Functions

static value gen_skewed (int n, int m, int elongation, int spread, bool is_directed=false)
 Random skewed connected graph (large diameter).
static value gen_bipartite (int n1, int n2, int m, bool connected=false)
 Generates a random bipartite graph.

Detailed Description

template<typename VWeight, typename EWeight>
struct tgen::wgraph< VWeight, EWeight >

Labeled weighted graph generator.

Defines graphs on n vertices with exactly m edges after generation, subject to preset edges from tgen::wgraph::add_edge. A uniformly random completion (respecting simple / directed / self-loop rules) is produced by tgen::wgraph::gen.

There are other static generation methods:

There are also structured, non-uniform operations:

Definition at line 5121 of file tgen.h.

Constructor & Destructor Documentation

◆ wgraph()

template<typename VWeight, typename EWeight>
tgen::wgraph< VWeight, EWeight >::wgraph ( int n,
int m,
bool is_directed = false,
bool has_self_loops = false )
inline

Creates a graph generator for a fixed number of vertices and edges.

Parameters
nNumber of vertices.
mTarget number of edges.
is_directedIf true, edges are directed and distinguish (u,v) from (v,u).
has_self_loopsIf true, pairs (i,i) may be generated.

Complexity

O(1).

Examples

// Undirected simple graph on 10 vertices with 15 edges.
auto g = tgen::graph(10, 15);
// Directed simple graph on 10 vertices with 20 edges.
auto directed_gen = tgen::graph(10, 20, true);
wgraph< int, int > graph
Unweighted labeled graphs.
Definition tgen.h:6331

Definition at line 5131 of file tgen.h.

Member Function Documentation

◆ add_edge()

template<typename VWeight, typename EWeight>
wgraph & tgen::wgraph< VWeight, EWeight >::add_edge ( int u,
int v )
inline

Adds a preset edge that must appear in the generated graph.

Parameters
uEndpoint (head if directed).
vEndpoint (tail if directed).

The number of preset edges must not exceed m.

Complexity

O(log m) amortized.

Examples

// Generator on 6 vertices with 8 edges and edge (0,5) forced.
auto gen = tgen::graph(6, 8).add_edge(0, 5);
value gen() const
Generates a uniformly random graph satisfying the constraints.
Definition tgen.h:5840
wgraph & add_edge(int u, int v)
Adds a preset edge that must appear in the generated graph.
Definition tgen.h:5139

Definition at line 5139 of file tgen.h.

◆ add_edges_from()

template<typename VWeight, typename EWeight>
wgraph & tgen::wgraph< VWeight, EWeight >::add_edges_from ( const value & rhs)
inline

Adds all edges from another graph as preset edges.

Parameters
rhsGraph to take edges from.

Complexity

O(rhs.m() log m).

Examples

// Generator whose output is a path on 4 vertices with an extra edge.
auto g = tgen::graph(4, 4).add_edges_from(tgen::P(4));
std::cout << g.gen().m() << std::endl; // Prints "4".
graph::value P(int n, bool is_directed=false)
Path graph.
Definition tgen.h:6345
int m() const
Number of edges.
Definition tgen.h:5261
wgraph & add_edges_from(const value &rhs)
Adds all edges from another graph as preset edges.
Definition tgen.h:5827

Definition at line 5827 of file tgen.h.

◆ gen()

template<typename VWeight, typename EWeight>
value tgen::wgraph< VWeight, EWeight >::gen ( ) const
inline

Generates a uniformly random graph satisfying the constraints.

Returns
A tgen::wgraph::value with m distinct edges, including all presets.

Preset edges are included first; remaining edges are sampled uniformly from admissible pairs until m distinct edges exist.

Exceptions
std::runtime_errorif there are not m valid edges to generate.

Complexity

O(n + m log n).

Examples

// Prints a random labeled graph on 4 vertices with 5 edges.
std::cout << tgen::graph(4, 5).gen();

Definition at line 5840 of file tgen.h.

◆ gen_bipartite()

template<typename VWeight, typename EWeight>
value tgen::wgraph< VWeight, EWeight >::gen_bipartite ( int n1,
int n2,
int m,
bool connected = false )
inlinestatic

Generates a random bipartite graph.

Parameters
n1Size of the left side.
n2Size of the right side.
mTarget edge count.
connectedIf true, the graph is connected (requires m >= n1 + n2 - 1).
Returns
A bipartite tgen::wgraph::value with n1 + n2 vertices and m edges.

The vertices on the left side are indexed in [0, n1), and those on right side in [n1, n1 + n2).

When connected is false, m cross edges are chosen uniformly without replacement from K_{n1, n2}.

When connected is true, a spanning tree is built first via a bipartite Prüfer sequence, then remaining cross edges are added by rejection sampling.

Note
This function is not always uniformly random: it is uniform only when connected is false.

Complexity

O(n1 + n2 + m log(n1 * n2)) expected.

Examples

// Bipartite graph with 5 vertices and 5 edges.
std::cout << tgen::graph::gen_bipartite(2, 3, 5);
static value gen_bipartite(int n1, int n2, int m, bool connected=false)
Definition tgen.h:6144

Definition at line 6144 of file tgen.h.

◆ gen_skewed()

template<typename VWeight, typename EWeight>
value tgen::wgraph< VWeight, EWeight >::gen_skewed ( int n,
int m,
int elongation,
int spread,
bool is_directed = false )
inlinestatic

Random skewed connected graph (large diameter).

Parameters
nVertex count.
mEdge count.
elongationPassed to tgen::wnext; controls how star-like or path-like the spanning tree is.
spreadMaximum walk length toward the root when sampling an extra edge.
is_directedIf true, orients every edge down spanning tree.
Precondition
spread >= 2.
Returns
A connected skewed tgen::wgraph::value with exactly m edges.

First, builds a skewed spanning tree with tgen::wtree::gen_skewed(n, elongation). Then, the remaining edges (v, u) are generated with the following procedure. Vertex u is random, and vertex v is the result of walking tgen::next(2, spread) parent edges in the spanning tree.

If elongation is small (negative), the spanning tree tends to be star-like and the graph tends to have small diameter. If elongation is large (positive), the spanning tree tends to be path-like and vertices 0 and n - 1 tend to be far apart.

When is_directed is true, the result is a directed acyclic graph weakly connected with root 0.

Note
This operation is not uniformly random.

Complexity

O(n + m log n) if spread is O(1);
O(n log n + m log^2 n) otherwise.

Examples

// Random skewed connected undirected graph.
std::cout << tgen::graph::gen_skewed(20, 25, 10, 3);
// Random skewed directed acyclic graph (edges oriented away from the root).
std::cout << tgen::graph::gen_skewed(20, 25, 10, 3, true);
static value gen_skewed(int n, int m, int elongation, int spread, bool is_directed=false)
Definition tgen.h:6041

Definition at line 6041 of file tgen.h.

◆ get_acyclic()

template<typename VWeight, typename EWeight>
value tgen::wgraph< VWeight, EWeight >::get_acyclic ( ) const
inline

Random directed acyclic graph extending preset edges.

Returns
A DAG tgen::wgraph::value with exactly m edges.

Preset edges must be acyclic. Remaining edges respect a random topological order (randomized Kahn).

Exceptions
std::runtime_errorpreset edges form a cycle.
Note
This operation is not uniformly random.

Complexity

O(n + m log n).

Examples

// Random DAG on 10 vertices with 15 edges.
std::cout << tgen::graph(10, 15, true).get_acyclic();
value get_acyclic() const
Random directed acyclic graph extending preset edges.
Definition tgen.h:5938

Definition at line 5938 of file tgen.h.

◆ get_connected()

template<typename VWeight, typename EWeight>
value tgen::wgraph< VWeight, EWeight >::get_connected ( ) const
inline

Random connected undirected graph extending preset edges.

Returns
A connected tgen::wgraph::value with exactly m edges.

First, edges are chosen to connect the connected components induced by the preset edges, uniformly at random. Then, remaining edges are chosen uniformly at random.

Note
This operation is not uniformly random.

Complexity

O(n + m log n).

Examples

// Random connected graph on 12 vertices with 14 edges.
std::cout << tgen::graph(12, 14).get_connected();
value get_connected() const
Random connected undirected graph extending preset edges.
Definition tgen.h:5864

Definition at line 5864 of file tgen.h.


The documentation for this struct was generated from the following files:
  • /home/runner/work/tgen_vs_jngen/tgen_vs_jngen/vendor/tgen/single_include/tgen.h
  • graph.dox