-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrank.js
55 lines (53 loc) · 1.44 KB
/
rank.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
// PR(A) = (1-d) + d(PR(T1)/C(T1) + ... + PR(T(n)/C(Tn))
module.exports.rank = function(vertices) {
var d = .85;
var count = 0;
var endgame0 = true;
var endgame1 = true;
for (var i = 0; i < vertices.length; i++) {
for (var j = 0; j < vertices[i].outEdges.length; j++) {
if (vertices[i].outEdges[j] != -1) {
count++;
}
}
vertices[i].outDegree = count;
count = 0;
}
var update = [[]];
for (var i = 0; i < vertices.length; i++) {
if (vertices[i].owner == 0) {
vertices[i].setP1Rank(1 - d);
vertices[i].setP2Rank(.0);
} else {
vertices[i].setP1Rank(.0);
vertices[i].setP2Rank(1 - d);
}
for (var j = 0; j < vertices[i].inEdges.length; j++) {
if (vertices[i].inEdges[j] != -1) {
if (vertices[j].owner == 0) {
vertices[i].setP1Rank(vertices[i].p1rank +
(d * vertices[j].oldRank / vertices[j].outDegree));
} else {
vertices[i].setP2Rank(vertices[i].p2rank +
(d * vertices[j].oldRank / vertices[j].outDegree));
}
}
}
if (vertices[i].p1rank > vertices[i].p2rank) {
endgame1 = false;
vertices[i].owner = 0;
} else {
endgame0 = false;
vertices[i].owner = 1;
}
update[i] = [ vertices[i].p1rank, vertices[i].p2rank, vertices[i].totalRank, vertices[i].owner];
}
for (var i = 0; i < vertices.length; i++) {
vertices[i].nextState();
}
// console.log(' ');
// console.log(' ');
// console.log(update);
var state = [update, [endgame0, endgame1]];
return state;
}