22#include "absl/container/flat_hash_set.h"
23#include "absl/memory/memory.h"
24#include "absl/random/bit_gen_ref.h"
25#include "absl/random/random.h"
35std::unique_ptr<StaticGraph<>> CreateGraphMaybeReserved(
int num_nodes,
38 absl::BitGenRef gen) {
39 std::unique_ptr<StaticGraph<>>
graph;
44 if (finalized && absl::Bernoulli(gen, 1.0 / 2)) {
45 graph = std::make_unique<StaticGraph<>>(num_nodes, num_arcs);
47 graph = std::make_unique<StaticGraph<>>();
48 graph->AddNode(num_nodes - 1);
57 absl::BitGenRef gen) {
58 std::unique_ptr<StaticGraph<>>
graph =
59 CreateGraphMaybeReserved(num_nodes, num_arcs, finalized, gen);
61 CHECK_GT(num_nodes, 0);
62 CHECK_GE(num_arcs, 0);
63 graph->AddNode(num_nodes - 1);
64 for (
int a = 0;
a < num_arcs; ++
a) {
65 graph->AddArc(absl::Uniform(gen, 0, num_nodes),
66 absl::Uniform(gen, 0, num_nodes));
69 CHECK_EQ(num_arcs, 0);
71 if (finalized)
graph->Build();
77std::unique_ptr<StaticGraph<>> GenerateRandomSimpleGraph(
int num_nodes,
81 absl::BitGenRef gen) {
82 CHECK_GE(num_nodes, 0);
84 CHECK(directed || (num_arcs % 2 == 0));
85 const int64_t max_num_arcs =
86 static_cast<int64_t
>(num_nodes) * (num_nodes - 1);
87 CHECK_LE(num_arcs, max_num_arcs);
88 std::unique_ptr<StaticGraph<>>
graph =
89 CreateGraphMaybeReserved(num_nodes, num_arcs, finalized, gen);
93 if (num_arcs > max_num_arcs / 2) {
94 std::unique_ptr<StaticGraph<>> inverse_graph =
95 GenerateRandomSimpleGraph(num_nodes, max_num_arcs - num_arcs,
97 std::vector<bool> node_mask(num_nodes,
false);
98 for (
int from = 0; from < num_nodes; ++from) {
99 for (
const int to : (*inverse_graph)[from]) {
100 node_mask[
to] =
true;
102 for (
int to = 0;
to < num_nodes; ++
to) {
104 node_mask[
to] =
false;
105 }
else if (
to != from) {
110 if (finalized)
graph->Build();
130 absl::flat_hash_set<std::pair<int, int>> arc_set;
134 int64_t num_iterations = 0;
135 const int64_t max_num_iterations = 1000 + max_num_arcs;
136 while (
graph->num_arcs() < num_arcs) {
138 CHECK_LE(num_iterations, max_num_iterations)
139 <<
"The random number generator supplied to GenerateRandomSimpleGraph()"
140 <<
" is likely biased or broken.";
141 const int tail = absl::Uniform(gen, 0, num_nodes);
142 const int head = absl::Uniform(gen, 0, num_nodes);
145 if (!arc_set.insert({tail, head}).second)
continue;
148 const std::pair<int, int>
arc = {
151 if (!arc_set.insert(
arc).second)
continue;
156 if (finalized)
graph->Build();
162 int num_nodes,
int num_arcs,
bool finalized, absl::BitGenRef gen) {
163 return GenerateRandomSimpleGraph(num_nodes, num_arcs, finalized,
168 int num_nodes,
int num_edges,
bool finalized, absl::BitGenRef gen) {
169 return GenerateRandomSimpleGraph(num_nodes, 2 * num_edges, finalized,
A collections of i/o utilities for the Graph classes in ./graph.h.
std::unique_ptr< StaticGraph<> > GenerateRandomMultiGraph(int num_nodes, int num_arcs, bool finalized, absl::BitGenRef gen)
std::unique_ptr< StaticGraph<> > GenerateRandomDirectedSimpleGraph(int num_nodes, int num_arcs, bool finalized, absl::BitGenRef gen)
std::unique_ptr< StaticGraph<> > GenerateRandomUndirectedSimpleGraph(int num_nodes, int num_edges, bool finalized, absl::BitGenRef gen)
trees with all degrees equal to