/
Stat.cpp
148 lines (136 loc) · 3.89 KB
/
Stat.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
#include "stdafx.h"
#include <map>
// get degrees from current and add it to degrees
void AddDegreeStat(const TFltPrV& current, TFltPrV& degrees, TIntPrV& samples){
for (int j = 0; j < current.Len(); j++){
const TFltPr& elem = current[j];
const double& deg = elem.Val1.Val, &nodesCount = elem.Val2.Val;
bool wasFound = false;
// silly search
for (int k = 0; k < degrees.Len(); k++){
if (degrees[k].Val1.Val == deg){
degrees[k].Val2.Val += nodesCount;
samples[k].Val2.Val++;
wasFound = true; break;
}
}
if (!wasFound){
TFlt d(deg), n(nodesCount);
TFltPr val(d,n);
degrees.Add(val);
TInt di(static_cast<int>(deg));
TIntPr valI(di, 1);
samples.Add(valI);
}
}
}
// get graph and add in and out degrees to cumulative vectors
void AddDegreesStat(TFltPrV& deg, TIntPrV& samples, const PNGraph& G, bool isIn){
TFltPrV current;
if (isIn)
TSnap::GetInDegCnt(G, current);
else
TSnap::GetOutDegCnt(G, current);
AddDegreeStat(current, deg, samples);
}
void GetAvgDegreeStat (TFltPrV& deg, const TIntPrV& samples){
for (int i = 0; i < deg.Len(); i++)
deg[i].Val2.Val /= static_cast<double>(samples[i].Val2.Val);
}
void GetAvgDegreeStat (TFltPrV& deg, const TInt& NKron){
for (int i = 0; i < deg.Len(); i++)
deg[i].Val2.Val /= NKron;
}
void GetPoints(const TFlt& maxDegLog, const TFlt& minDegLog, const int& NInt, const TFltPrV& base, TFltPrV& points){
int beginIndex = 0;
// ignore nodes with zero degree (for Kronecker graphs)
/*if (base[0].Val1.Val != 0)
points.Add(base[beginIndex]);
else {
points.Add(base[++beginIndex]);
}*/
points.Add(base[beginIndex]);
TFlt baseMaxDeg = base[base.Len()-1].Val1.Val,
baseMinDeg = base[beginIndex].Val1.Val;
for (int i = beginIndex + 1; i < NInt; i++){
// deg - degree to be found in base
TFlt degRound (pow (10, minDegLog.Val + i * (maxDegLog.Val - minDegLog.Val) / NInt));
TInt degInt(static_cast<int>(degRound.Val));
TFlt deg(degInt);
// if deg < baseMinDeg (for cases when baseMinDeg > minDeg)
if (deg.Val <= baseMinDeg)
continue;
// if deg > baseMaxDeg, add last point and finish
if (deg.Val >= baseMaxDeg){
points.Add(base[base.Len()-1]);
break;
}
// we have two cases: when we can find an exact value of deg, or when we have not this value
bool isExact = false;
int index = FindVal1Elem(base, deg, isExact);
if (isExact){
points.Add(base[index]);
}
else
{
TFltPr x;
x.Val1.Val = deg;
x.Val2.Val = ( base[index].Val2.Val + base [index + 1].Val2.Val ) / 2;
points.Add(x);
}
}
}
int GetMaxMinDeg(const PNGraph& G, const TStr& IsDir, const TStr& IsIn, const TStr& IsMax){
TIntPrV DegCnt;
if (IsDir == "false"){
PUNGraph U = TSnap::ConvertGraph<PUNGraph>(G);
if (IsIn == "true")
TSnap::GetInDegCnt(U, DegCnt);
else
TSnap::GetOutDegCnt(U, DegCnt);
}
else{
if (IsIn == "true")
TSnap::GetInDegCnt(G, DegCnt);
else
TSnap::GetOutDegCnt(G, DegCnt);
}
// sort in descending order
DegCnt.Sort(false);
if (IsMax == "true") return DegCnt[0].Val1;
else return DegCnt[DegCnt.Len()-1].Val1;
}
void CompareDeg(const int i, const int MaxDeg, int& MinMaxDeg, int& MaxMaxDeg, int& AvgMaxDeg){
if (i == 0) {
MinMaxDeg = MaxDeg;
}
else if (MaxDeg < MinMaxDeg){
MinMaxDeg = MaxDeg;
}
if (MaxDeg > MaxMaxDeg) {
MaxMaxDeg = MaxDeg;
}
AvgMaxDeg += MaxDeg;
}
int GetExpectedModelEdges(const PNGraph& G, const int k, const TStr& order){
int expectedEdges = 0;
map<int, int> DegAcc;
TIntV DegSeq;
TSnap::GetDegSeqV(G, DegSeq);
for (int i = 0; i < DegSeq.Len(); i++){
if (order == "linear")
expectedEdges += DegSeq[i] * k;
else if (order == "square")
expectedEdges += DegSeq[i] * k * k;
}
// each edge is considered twice for both vertices
expectedEdges /= 2;
return expectedEdges;
}
bool CheckReciprocity(const PNGraph& G){
for (int i = 0; i < G->GetNodes(); i++){
if (G->GetNI(i).GetInDeg() != G->GetNI(i).GetOutDeg())
return false;
}
return true;
}