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
13#include <random>
14
15namespace nxpp {
16
17// Generators
18
19template <typename GraphType = Graph<int>>
20 requires NumericNodeID<typename GraphType::NodeType>
27GraphType complete_graph(size_t n) {
28 GraphType G;
29 using NodeID = typename GraphType::NodeType;
30 for (size_t i = 0; i < n; ++i) {
31 if constexpr (GraphType::is_directed) {
32 for (size_t j = 0; j < n; ++j) {
33 if (i == j) continue;
34 G.add_edge(static_cast<NodeID>(i), static_cast<NodeID>(j));
35 }
36 } else {
37 for (size_t j = i + 1; j < n; ++j) {
38 G.add_edge(static_cast<NodeID>(i), static_cast<NodeID>(j));
39 }
40 }
41 }
42 return G;
43}
44
45template <typename GraphType = Graph<int>>
46 requires NumericNodeID<typename GraphType::NodeType>
53GraphType path_graph(size_t n) {
54 GraphType G;
55 using NodeID = typename GraphType::NodeType;
56 if (n == 0) {
57 return G;
58 }
59 for (size_t i = 0; i < n - 1; ++i) {
60 G.add_edge(static_cast<NodeID>(i), static_cast<NodeID>(i + 1));
61 }
62 return G;
63}
64
65template <typename GraphType = Graph<int>>
66 requires NumericNodeID<typename GraphType::NodeType>
75GraphType erdos_renyi_graph(size_t n, double p, int seed = 42) {
76 GraphType G;
77 using NodeID = typename GraphType::NodeType;
78 std::mt19937 gen(seed);
79 std::uniform_real_distribution<> dis(0.0, 1.0);
80
81 for (size_t i = 0; i < n; ++i) {
82 G.add_node(static_cast<NodeID>(i));
83 }
84
85 for (size_t i = 0; i < n; ++i) {
86 for (size_t j = 0; j < (GraphType::is_directed ? n : i); ++j) {
87 if (i == j) continue;
88 if (dis(gen) < p) {
89 G.add_edge(static_cast<NodeID>(i), static_cast<NodeID>(j));
90 }
91 }
92 }
93 return G;
94}
95
96} // namespace nxpp
Core graph wrapper, proxy surface, and public alias presets.