|
| | value (const std::vector< std::set< int > > &adj, bool is_directed=false) |
| | Builds a graph from an adjacency list.
|
| | value (int n, const std::vector< std::pair< int, int > > &edges={}, bool is_directed=false) |
| | Builds a graph from number of vertices and edge list.
|
| | value (const typename wtree< VWeight, EWeight >::value &t) |
| | Builds an undirected graph from a tree.
|
| int | n () const |
| | Number of vertices.
|
| int | m () const |
| | Number of edges.
|
| bool | is_directed () const |
| | If the graph is directed.
|
| const std::vector< std::set< int > > & | adj () const |
| | Adjacency list.
|
| const std::vector< std::pair< int, int > > & | edges () const |
| | Edge list.
|
| const std::optional< std::vector< VWeight > > & | vertex_weights () const |
| | Optional vertex weights.
|
| const std::optional< std::vector< EWeight > > & | edge_weights () const |
| | Optional edge weights.
|
| template<typename NewVWeight = VWeight> |
| wgraph< NewVWeight, EWeight >::value | set_vertex_weights (const std::vector< NewVWeight > &vertex_weights) const |
| | Attaches vertex weights.
|
| template<typename NewEWeight = EWeight> |
| wgraph< VWeight, NewEWeight >::value | set_edge_weights (const std::vector< NewEWeight > &edge_weights) const |
| | Attaches edge weights.
|
| value & | edge_weighted () |
| | Enables edge-weighted mode on an edgeless graph.
|
| value & | add_1 () |
| | Adds 1 for printing.
|
| value & | print_nm () |
| | Prints number of vertices and edges before edge list.
|
| value & | shuffle_except (std::set< int > indices) |
| | Shuffles vertices except given vertices, and edge order.
|
| value & | shuffle () |
| | Shuffles all vertices and edge order.
|
| value & | add_vertices (int k, std::optional< std::vector< VWeight > > new_vertex_weights=std::nullopt) |
| | Adds new isolated vertices.
|
| value & | add_edge (int u, int v, std::optional< EWeight > w=std::nullopt) |
| | Adds an edge between two vertices.
|
| value & | link (const value &rhs, int new_u, int new_v, std::optional< EWeight > new_w=std::nullopt) |
| | Links two graphs by an new edge.
|
| value & | glue (const value &rhs, std::set< std::pair< int, int > > index_pairs) |
| | Glues another graph at given vertex pairs.
|
| value & | glue (const value &rhs, std::set< int > indices) |
| | Glues another graph at given vertices.
|
| value & | disjoint_union (const value &rhs) |
| | Disjoint union with another graph.
|
| value & | random_subgraph (int num_edges) |
| | Random subgraph with a fixed number of edges.
|
| value & | random_connected_subgraph (int num_edges) |
| | Random subgraph with a fixed number of edges that keeps components connected.
|
| value | operator! () const |
| | Graph complement of unweighted graph.
|
| value | operator+ (const value &rhs) const |
| | Concatenates two graphs (disjoint union).
|
| std::tuple< int, int, std::vector< std::set< int > > > | to_std () const |
| | Converts the graph to std types.
|
| bool | operator< (const value &rhs) const |
template<typename VWeight, typename EWeight>
struct tgen::wgraph< VWeight, EWeight >::value
Labeled graph value.
Adjacency lists are symmetric for undirected graphs; the edge list tgen::wgraph::value::edges lists each undirected edge once as (u, v) with u <= v. Directed graphs list each directed edge once.
It can be printed through std::cout.
Definition at line 5157 of file tgen.h.
template<typename VWeight, typename EWeight>
| tgen::wgraph< VWeight, EWeight >::value::value |
( |
int | n, |
|
|
const std::vector< std::pair< int, int > > & | edges = {}, |
|
|
bool | is_directed = false ) |
|
inline |
Builds a graph from number of vertices and edge list.
- Parameters
-
| n | Number of vertices. |
| edges | List of edges. |
| is_directed | If the graph is directed. |
For undirected graphs, each edge (u, v) should appear once with u <= v.
Complexity
O(n + m log n).
Examples
tgen::graph::value g(4, {{0, 1}, {2, 3}});
Definition at line 5197 of file tgen.h.
template<typename VWeight, typename EWeight>
Disjoint union with another graph.
Complexity
O(rhs.n + rhs.m log n) amortized.
That is, the complexity depends mostly on the rhs graph.
Examples
tgen::graph::value a(2, {{0, 1}});
tgen::graph::value b(2, {{0, 1}});
a.disjoint_union(b);
std::cout << a.n() << std::endl;
Definition at line 5579 of file tgen.h.
template<typename VWeight, typename EWeight>
Enables edge-weighted mode on an edgeless graph.
After this call, add_edge(u, v, w) accepts a weight for each new edge.
- Precondition
- The graph has no edges (m() == 0).
Complexity
O(1).
Examples
tgen::egraph<int>::value g(3, {}, true);
g.edge_weighted();
g.add_edge(0, 1, 10);
g.add_edge(1, 2, 20);
std::cout << g;
Definition at line 5313 of file tgen.h.
template<typename VWeight, typename EWeight>
Glues another graph at given vertices.
- Parameters
-
| rhs | Other graph to be glued. |
| indices | Set of vertex labels: for each i in indices, vertex i is glued to vertex i in rhs. |
Gluing one vertex to another means they are considered to be the same vertex, maintaining the label it had in this graph. Other vertices are shifted accordingly.
Complexity
O(rhs.n + rhs.m log n) amortized.
That is, the complexity depends mostly on the rhs graph.
Examples
graph::value C(int n, bool is_directed=false)
Cycle graph.
int n() const
Number of vertices.
Definition at line 5566 of file tgen.h.
template<typename VWeight, typename EWeight>
| value & tgen::wgraph< VWeight, EWeight >::value::glue |
( |
const value & | rhs, |
|
|
std::set< std::pair< int, int > > | index_pairs ) |
|
inline |
Glues another graph at given vertex pairs.
- Parameters
-
| rhs | Other graph to be glued. |
| index_pairs | Set of (l, r) pairs: vertex l is glued to vertex r in rhs. |
Gluing one vertex to another means they are considered to be the same vertex, maintaining the label it had in this graph. Other vertices are shifted accordingly.
Complexity
O(rhs.n + rhs.m log n) amortized.
That is, the complexity depends mostly on the rhs graph.
Examples
graph::value P(int n, bool is_directed=false)
Path graph.
Definition at line 5502 of file tgen.h.
template<typename VWeight, typename EWeight>
| value & tgen::wgraph< VWeight, EWeight >::value::link |
( |
const value & | rhs, |
|
|
int | new_u, |
|
|
int | new_v, |
|
|
std::optional< EWeight > | new_w = std::nullopt ) |
|
inline |
Links two graphs by an new edge.
- Parameters
-
| rhs | Second graph. |
| new_u | Endpoint in this graph. |
| new_v | Endpoint in rhs before relabeling. |
| new_w | Optional weight of the connecting edge. |
Vertices on rhs are shifted by the current n. Weight usage must be consistent.
Complexity
O(rhs.n + rhs.m log n) amortized.
That is, the complexity depends mostly on the rhs graph.
Examples
tgen::graph::value a(2, {{0, 1}});
tgen::graph::value b(2, {{0, 1}});
a.link(b, 0, 0);
std::cout << a.n() << std::endl;
Definition at line 5475 of file tgen.h.
template<typename VWeight, typename EWeight>
Graph complement of unweighted graph.
- Returns
- Complement graph.
Immutable style: returns a copy with the complement with the same n, is_directed, and printing flags; does not modify *this.
Self loops are maintained.
Complexity
O(n^2).
Examples
std::cout << (!p).
m() << std::endl;
int m() const
Number of edges.
Definition at line 5708 of file tgen.h.
template<typename VWeight, typename EWeight>
Concatenates two graphs (disjoint union).
Immutable style: returns a copy with the disjoint union; does not modify *this.
Complexity
O(n + m log n) for the combined size.
Examples
tgen::graph::value a(2, {{0, 1}});
tgen::graph::value b(2, {{0, 1}});
std::cout << (a + b).
n() << std::endl;
Definition at line 5742 of file tgen.h.
template<typename VWeight, typename EWeight>
| value & tgen::wgraph< VWeight, EWeight >::value::random_connected_subgraph |
( |
int | num_edges | ) |
|
|
inline |
Random subgraph with a fixed number of edges that keeps components connected.
- Parameters
-
| num_edges | Number of edges to keep (subset of current edges). |
First, picks a random spanning forest via randomized Prim on each connected component (so the number of connected components does not increase). Then, extra edges are chosen uniformly at random.
Requires at least n - c edges, where c is the number of connected components in the current graph.
- Note
- This operation is not uniformly random.
Complexity
O(n + m).
Examples
std::cout << g.random_connected_subgraph(8).m() << std::endl;
graph::value K(int n)
Complete undirected graph.
Definition at line 5618 of file tgen.h.