nxpp
Header-only graph utilities on top of Boost Graph Library
Loading...
Searching...
No Matches
generators.hpp
Go to the documentation of this file.
1#pragma once
2
11#include "graph.hpp"
12
13namespace nxpp {
14
15// Generators
16
17template <typename GraphType = Graph<int>>
24GraphType complete_graph(size_t n) {
25 GraphType G;
26 using NodeID = typename GraphType::NodeType;
27 static_assert(
28 std::constructible_from<NodeID, std::size_t>,
29 "nxpp::complete_graph requires GraphType::NodeType to be constructible from std::size_t because it synthesizes node IDs 0..n-1."
30 );
31 for (size_t i = 0; i < n; ++i) {
32 for (size_t j = 0; j < n; ++j) {
33 if (i != j) {
34 G.add_edge(static_cast<NodeID>(i), static_cast<NodeID>(j));
35 }
36 }
37 }
38 return G;
39}
40
41template <typename GraphType = Graph<int>>
48GraphType path_graph(size_t n) {
49 GraphType G;
50 using NodeID = typename GraphType::NodeType;
51 static_assert(
52 std::constructible_from<NodeID, std::size_t>,
53 "nxpp::path_graph requires GraphType::NodeType to be constructible from std::size_t because it synthesizes node IDs 0..n-1."
54 );
55 for (size_t i = 0; i < n - 1; ++i) {
56 G.add_edge(static_cast<NodeID>(i), static_cast<NodeID>(i + 1));
57 }
58 return G;
59}
60
61template <typename GraphType = Graph<int>>
70GraphType erdos_renyi_graph(size_t n, double p, int seed = 42) {
71 GraphType G;
72 using NodeID = typename GraphType::NodeType;
73 static_assert(
74 std::constructible_from<NodeID, std::size_t>,
75 "nxpp::erdos_renyi_graph requires GraphType::NodeType to be constructible from std::size_t because it synthesizes node IDs 0..n-1."
76 );
77 std::mt19937 gen(seed);
78 std::uniform_real_distribution<> dis(0.0, 1.0);
79
80 for (size_t i = 0; i < n; ++i) {
81 for (size_t j = 0; j < (GraphType::is_directed ? n : i); ++j) {
82 if (i == j) continue;
83 if (dis(gen) < p) {
84 G.add_edge(static_cast<NodeID>(i), static_cast<NodeID>(j));
85 }
86 }
87 }
88 return G;
89}
90
91} // namespace nxpp
GraphType path_graph(size_t n)
Builds a simple path on node IDs 0 through n - 1.
Definition generators.hpp:48
GraphType erdos_renyi_graph(size_t n, double p, int seed=42)
Builds an Erdos-Renyi random graph on node IDs 0 through n - 1.
Definition generators.hpp:70
GraphType complete_graph(size_t n)
Builds the complete graph on node IDs 0 through n - 1.
Definition generators.hpp:24
Core graph wrapper, proxy surface, and public alias presets.