-
Notifications
You must be signed in to change notification settings - Fork 0
/
Centrality.cpp
131 lines (113 loc) · 3.78 KB
/
Centrality.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
#include "Centrality.h"
//constructor for test case
Centrality::Centrality(vector< vector <double> > copyMatrix) {
adj.resize(copyMatrix.size());
for(unsigned int i = 0; i < adj.size(); i++) {
adj[i].resize(copyMatrix[i].size());
}
for(unsigned int j = 0; j < adj.size(); j++) {
for(unsigned int k = 0; k < adj[j].size(); k++) {
adj[j][k] = copyMatrix[j][k];
}
}
centrality.resize(copyMatrix.size(),0);
parent.resize(copyMatrix.size(),-1);
distance.resize(copyMatrix.size());
Tset.resize(copyMatrix.size());
}
//constructor for open flight data
Centrality::Centrality(vector< vector <double> > copyMatrix, vector <int> start) {
adj.resize(copyMatrix.size());
for(unsigned int i = 0; i < adj.size(); i++) {
adj[i].resize(copyMatrix[i].size());
}
for(unsigned int j = 0; j < adj.size(); j++) {
for(unsigned int k = 0; k < adj[j].size(); k++) {
adj[j][k] = copyMatrix[j][k];
}
}
centrality.resize(copyMatrix.size(),0);
parent.resize(copyMatrix.size(),-1);
distance.resize(copyMatrix.size());
Tset.resize(copyMatrix.size());
}
//helper function for dijkstra that calculates min distance
int Centrality::miniDist(vector<double> distance, vector<bool> Tset) {
int minimum = 1000000, ind;
for(unsigned int i = 0; i < distance.size(); i++) {
if(!Tset[i] && distance[i] <= minimum) {
minimum = distance[i];
ind = i;
}
}
return ind;
}
//betweenness Centrality Algorithm applied to the whole graph
int Centrality::betweenCentral(int id){
for (size_t i = 0; i< centrality.size(); i++){
cout<< "Starting BCT iteration on vertex" << i << endl;
bctHelper(i);
}
return centrality[id];
}
//Helper Function for BCA
void Centrality::bctHelper(int start){
for(unsigned int k = 0; k < distance.size(); k++) {
distance[k] = 1000000;
Tset[k] = false;
}
for(size_t i = 0; i < parent.size(); i++){
parent[i] = -1;
}
int r;
distance[start] = 0;
parent[start] = -2;
for(unsigned int k = 0; k < Tset.size(); k++) {
int m = miniDist(distance, Tset);
Tset[m] = true;
for(unsigned int k = 0; k < distance.size(); k++) {
if(!Tset[k] && adj[m][k] && distance[m] != 1000000 && distance[m] +adj[m][k] < distance[k]) {
if(parent[k] == -1){
parent[k] = m;
centrality[k]++;
r = m;
while(parent[r] != -2){
centrality[r] ++;
r = parent[r];
}
centrality[r]++;
} else{
//delete original path
centrality[k]--;
r = parent[k];
while(parent[r] != -2){
centrality[r]--;
r = parent[r];
}
centrality[r] --;
//update parent
parent[k]=m;
//update new shortest path
centrality[k] ++;
r = m;
while(parent[r] != -2){
centrality[r]++;
r = parent[r];
}
centrality[r]++;
}
distance[k] = distance[m] + adj[m][k];
}
}
}
}
//print out weights
void Centrality::central(){
for(unsigned int i = 0; i < centrality.size(); i++) {
cout << centrality[i] << endl;
}
}
//return weights of BCA
vector<int> Centrality::returnCentral(){
return centrality;
}