-
Notifications
You must be signed in to change notification settings - Fork 0
/
harmonic-centrality.js
92 lines (86 loc) · 2.03 KB
/
harmonic-centrality.js
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
const DB = require('./database');
function GetDistance(d, i, j) {
if (i === j) {
return 0;
}
if (i > j) {
const tmp = i;
i = j;
j = tmp;
}
if (!(i in d)) {
return Infinity;
}
if (!(j in d[i])) {
return Infinity;
}
return d[i][j];
}
function SetDistance(d, i, j, value) {
if (i === j) {
return;
}
if (i > j) {
const tmp = i;
i = j;
j = tmp;
}
if (!(i in d)) {
d[i] = {};
}
if (value === Infinity) {
return;
}
d[i][j] = value;
}
function FloydWarshall(relationships, candidates) {
const d = {};
console.log('Number of candidates', candidates.length);
console.log('Initializing matrix');
const minCoplayTimeToBotherWith = 1;
const maxDistanceToCalculate = 1 / minCoplayTimeToBotherWith;
relationships.forEach((r) => {
const initialDistance = 1 / r.discounted_diluted_seconds;
if (initialDistance < maxDistanceToCalculate) {
SetDistance(d, r.lo_user_id, r.hi_user_id, initialDistance);
}
});
console.log('Starting cubic loop');
const n = candidates.length;
let complexity = 0;
for (let k = 0; k < n; k++) {
for (const i in d) {
for (const j in d[i]) {
complexity++;
const a = GetDistance(d, i, j);
const b = GetDistance(d, i, k);
const c = GetDistance(d, k, j);
if (b + c !== Infinity && b + c < a && b + c < maxDistanceToCalculate) {
SetDistance(d, i, j, b + c);
}
}
}
}
console.log('Finished cubic loop with', complexity, 'iterations.');
return d;
}
function ConvertDistanceMatrixToHarmonicCentrality(d, candidates) {
const h = {};
candidates.forEach((i) => {
let c = 0;
candidates.forEach((j) => {
if (i !== j) {
c += 1 / GetDistance(d, i, j);
}
});
h[i] = c * 1.00;
});
return h;
}
async function HarmonicCentrality(candidates) {
const relationships = await DB.GetTimeMatrix();
const d = FloydWarshall(relationships, candidates);
const h = ConvertDistanceMatrixToHarmonicCentrality(d, candidates);
return h;
}
module.exports = HarmonicCentrality;