Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RandomGraphFactory.hpp
Go to the documentation of this file.
1
6// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
7
8#pragma once
9
10#include <alib/pair>
11#include <alib/vector>
12#include <alib/tuple>
13#include <random>
14
15namespace graph {
16
17namespace generate {
18
20// ---------------------------------------------------------------------------------------------------------------------
21 public:
22 template<typename TGraph>
23 static
25 randomGraph(unsigned long max_nodes, unsigned long branching_factor);
26
27// ---------------------------------------------------------------------------------------------------------------------
28
29 template<typename TGraph>
30 static
32 randomWeightedGraph(unsigned long max_nodes,
33 unsigned long branching_factor,
34 long max_weight);
35
36// ---------------------------------------------------------------------------------------------------------------------
37
38};
39
40// =====================================================================================================================
41
42template<typename TGraph>
44RandomGraphFactory::randomGraph(unsigned long max_nodes,
45 unsigned long branching_factor) {
46 using node_type = typename TGraph::node_type;
47
48 // Seed with a real random value, if available
49 std::random_device r;
50 std::default_random_engine e1(r());
51
52 // Create distribution
53 std::uniform_int_distribution<unsigned long> number_of_nodes(1, max_nodes); // Generate number of nodes
54 std::uniform_int_distribution<unsigned long>
55 number_of_edges(1, branching_factor); // Generate number of edges for each node
56
57 unsigned long node_cnt = number_of_nodes(e1);
58
59 std::uniform_int_distribution<node_type> node_id(0, node_cnt * 10); // Generate indexes of nodes
60 std::uniform_int_distribution<long> random_node(0, node_cnt - 1); // Generate random index in vector of nodes
61
62 TGraph graph;
63
64 // Add nodes
66
67 // Generate start and goal node
68 node_type start(node_id(e1));
69 node_type goal(node_id(e1));
70 nodes.push_back(start);
71 graph.addNode(start);
72 nodes.push_back(goal);
73 graph.addNode(goal);
74
75 // Generate nodes
76 for (unsigned long i = 0; i < node_cnt; ++i) {
77 node_type node(node_id(e1));
78 graph.addNode(node);
79 nodes.push_back(node);
80 }
81
82 // Generate edges
83 for (const auto &v : nodes) {
84 unsigned long cnt = number_of_edges(e1); // Generate number of edges for current v
85 for (unsigned long i = 0; i < cnt; ++i) {
86 long w = random_node(e1); // Get target v
87 graph.addEdge(v, nodes.at(w));
88 }
89 }
90
91 return ext::make_tuple(graph, start, goal);
92}
93
94// ---------------------------------------------------------------------------------------------------------------------
95
96template<typename TGraph>
99 unsigned long max_nodes,
100 unsigned long branching_factor,
101 long max_weight) {
102 using node_type = typename TGraph::node_type;
103 using weight_type = typename TGraph::edge_type::weight_type;
104
105 // Seed with a real random value, if available
106 std::random_device r;
107 std::default_random_engine e1(r());
108
109 // Create distribution
110 std::uniform_int_distribution<unsigned long> number_of_nodes(1, max_nodes); // Generate number of nodes
111 std::uniform_int_distribution<unsigned long>
112 number_of_edges(1, branching_factor); // Generate number of edges for each node
113 std::uniform_real_distribution<weight_type> edge_weight(1, max_weight); // Generate edge weight
114
115 unsigned long node_cnt = number_of_nodes(e1);
116
117 std::uniform_int_distribution<node_type> node_id(0, node_cnt * 10); // Generate indexes of nodes
118 std::uniform_int_distribution<long> random_node(0, node_cnt - 1); // Generate random index in vector of nodes
119
120 TGraph graph;
121
122 // Add nodes
124
125 // Generate start and goal node
126 node_type start(node_id(e1));
127 node_type goal(node_id(e1));
128 nodes.push_back(start);
129 graph.addNode(start);
130 nodes.push_back(goal);
131 graph.addNode(goal);
132
133 // Generate nodes
134 for (unsigned long i = 0; i < node_cnt; ++i) {
135 node_type node(node_id(e1));
136 graph.addNode(node);
137 nodes.push_back(node);
138 }
139
140 // Generate edges
141 for (const auto &v : nodes) {
142 unsigned long cnt = number_of_edges(e1); // Generate number of edges for current v
143 for (unsigned long i = 0; i < cnt; ++i) {
144 long w = random_node(e1); // Get target v
145 graph.addEdge(v, nodes.at(w), edge_weight(e1));
146 }
147 }
148
149 return ext::make_tuple(graph, start, goal);
150}
151
152// ---------------------------------------------------------------------------------------------------------------------
153
154} // namespace generate
155
156} // namespace graph
157
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Definition: RandomGraphFactory.hpp:19
static ext::tuple< TGraph, typename TGraph::node_type, typename TGraph::node_type > randomGraph(unsigned long max_nodes, unsigned long branching_factor)
Definition: RandomGraphFactory.hpp:44
static ext::tuple< TGraph, typename TGraph::node_type, typename TGraph::node_type > randomWeightedGraph(unsigned long max_nodes, unsigned long branching_factor, long max_weight)
Definition: RandomGraphFactory.hpp:98
int i
Definition: AllEpsilonClosure.h:118
constexpr auto make_tuple(Elements &&... args)
Helper of extended tuple construction. The tuple is constructed from values pack, types are deduced.
Definition: tuple.hpp:203
Definition: ReconstructPath.hpp:14
void start(measurements::stealth_string name, measurements::Type type)
Definition: measurements.cpp:14
Definition: Node.cpp:11