Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
randomGeographicalThresholdGraph.h
Go to the documentation of this file.
1
32#pragma once
33
34#include <ogdf/basic/Array.h>
35#include <ogdf/basic/Graph.h>
36#include <ogdf/basic/basic.h>
37
38#include <cmath>
39#include <functional>
40#include <random>
41
42namespace ogdf {
43
51
53
72template<typename D>
73void randomGeographicalThresholdGraph(Graph& G, Array<int>& weights, D& dist, double threshold,
74 std::function<double(double)> h, int dimension = 2) {
75 OGDF_ASSERT(dimension >= 1);
76 OGDF_ASSERT(threshold >= 0.0);
77
78 G.clear();
79 Array<node> nodes(weights.size());
80
81 // Randomly distribute nodes in a d-dimensional area
82 NodeArray<int> nodeWeights = NodeArray<int>(G);
83 NodeArray<Array<double>> cord(G, Array<double>(dimension));
84 std::minstd_rand rng(randomSeed());
85 for (int i = 0; i < weights.size(); i++) {
86 nodes[i] = G.newNode();
87 nodeWeights[nodes[i]] = weights[i];
88 for (int j = 0; j < dimension; j++) {
89 cord[nodes[i]][j] = dist(rng);
90 }
91 }
92
93 for (node v : nodes) {
94 for (node w = v->succ(); w; w = w->succ()) {
95 double distance = 0.0;
96 for (int i = 0; i < dimension; i++) {
97 distance += (cord[v][i] - cord[w][i]) * (cord[v][i] - cord[w][i]);
98 }
99 distance = sqrt(distance);
100
101 if ((nodeWeights[v] + nodeWeights[w]) * h(distance) > threshold) {
102 G.newEdge(v, w);
103 }
104 }
105 }
106}
107
125template<typename D>
126void randomGeographicalThresholdGraph(Graph& G, Array<int>& weights, D& dist, double threshold,
127 int alpha = 2, int dimension = 2) {
128 randomGeographicalThresholdGraph<D>(
129 G, weights, dist, threshold, [alpha](double d) { return 1 / pow(d, alpha); }, dimension);
130}
131
133
136}
Declaration and implementation of Array class and Array algorithms.
Includes declaration of graph class.
Basic declarations, included by all source files.
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:219
INDEX size() const
Returns the size (number of elements) of the array.
Definition Array.h:302
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
Class for the representation of nodes.
Definition Graph_d.h:241
node succ() const
Returns the successor in the list of all nodes.
Definition Graph_d.h:293
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
void randomGeographicalThresholdGraph(Graph &G, Array< int > &weights, D &dist, double threshold, std::function< double(double)> h, int dimension=2)
Creates a random geometric graph where edges are created based on their distance and the weight of no...
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
The namespace for all OGDF objects.