-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPowerlawDegreeSequence.cpp
148 lines (115 loc) · 4.31 KB
/
PowerlawDegreeSequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright (c) 2013 Christian Staudt, Henning Meyerhenke
MIT License (http://opensource.org/licenses/MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <cmath>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include "PowerlawDegreeSequence.h"
PowerlawDegreeSequence::PowerlawDegreeSequence(int minDeg, int maxDeg, double gamma) :
minDeg(minDeg), maxDeg(maxDeg), gamma(gamma), hasRun(false) {
/*if (minDeg > maxDeg) throw std::runtime_error("Error: minDeg must not be larger than maxDeg");
if (gamma > -1) throw std::runtime_error("Error: gamma must be lower than -1");*/
}
void PowerlawDegreeSequence::setMinimumDegree(int minDeg) {
this->minDeg = minDeg;
hasRun = false;
}
void PowerlawDegreeSequence::setGamma(double gamma) {
this->gamma = gamma;
hasRun = false;
}
void PowerlawDegreeSequence::setGammaFromAverageDegree(double avgDeg, double minGamma, double maxGamma) {
double gamma_l = maxGamma;
double gamma_r = minGamma;
setGamma(gamma_l); run();
double average_l = getExpectedAverageDegree();
setGamma(gamma_r); run();
double average_r = getExpectedAverageDegree();
// Note: r is the larger expected average degree!
if (avgDeg > average_r) {
setGamma(gamma_r);
return;
}
if (avgDeg < average_l) {
setGamma(gamma_l);
return;
}
while (gamma_l + 0.001 < gamma_r) {
setGamma((gamma_r + gamma_l) * 0.5); run();
double avg = getExpectedAverageDegree();
if (avg > avgDeg) {
average_r = avg;
gamma_r = gamma;
} else {
average_l = avg;
gamma_l = gamma;
}
}
if (avgDeg - average_l < average_r - avgDeg) {
setGamma(gamma_l);
} else {
setGamma(gamma_r);
}
}
int PowerlawDegreeSequence::getMinimumDegree() const {
return minDeg;
}
void PowerlawDegreeSequence::run() {
cumulativeProbability.clear();
cumulativeProbability.reserve(maxDeg - minDeg + 1);
double sum = 0;
for (double d = maxDeg; d >= minDeg; --d) {
sum += std::pow(d, gamma);
cumulativeProbability.push_back(sum);
}
for (int i=0;i<cumulativeProbability.size();i++){
cumulativeProbability[i] /= sum;
// for (double & prob : cumulativeProbability) {
//prob /= sum;
}
cumulativeProbability.back() = 1.0;
hasRun= true;
}
double PowerlawDegreeSequence::getExpectedAverageDegree() const {
//if (!hasRun) throw std::runtime_error("Error: run needs to be called first");
double average = cumulativeProbability[0] * maxDeg;
for (int i = 1; i < cumulativeProbability.size(); ++i) {
average += (cumulativeProbability[i] - cumulativeProbability[i-1]) * (maxDeg - i);
}
return average;
}
std::vector< int > PowerlawDegreeSequence::getDegreeSequence(int numNodes) const {
std::vector<int> degreeSequence;
//if (!hasRun) throw std::runtime_error("Error: run needs to be called first");
degreeSequence.reserve(numNodes);
int degreeSum = 0;
for (int i = 0; i < numNodes; ++i) {
degreeSequence.push_back(getDegree());
degreeSum += degreeSequence.back();
}
if (degreeSum % 2 != 0) {
(*std::max_element(degreeSequence.begin(), degreeSequence.end()))--;
}
return degreeSequence;
}
int PowerlawDegreeSequence::getDegree() const {
//if (!hasRun) throw std::runtime_error("Error: run needs to be called first");
double randomValue = ((double)rand())/((double)RAND_MAX);
return maxDeg - std::distance(cumulativeProbability.begin(), std::lower_bound(cumulativeProbability.begin(), cumulativeProbability.end(), randomValue));
}