-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimrank.js
177 lines (167 loc) · 4.31 KB
/
simrank.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Created by Antonio Altamura on 14/10/2017.
*/
"use strict";
let g = {
nodes: [],
edges: []
};
let leftPartition = new Set(),
rightPartition = new Set();
let G2leftPartition, G2rightPartition;
/**
Attach 3 methods to sigma graph API for getting the outbounds, inbounds and setting the simRank of a node
*/
sigma.classes.graph.addMethod('O', function (nodeId) {
return this.edgesArray.reduce((arr, e) => {
return (e.source === nodeId) ? [...arr, e.target] : arr;
}, []);
});
sigma.classes.graph.addMethod('I', function (nodeId) {
return this.edgesArray.reduce((arr, e) => {
return (e.target === nodeId) ? [...arr, e.source] : arr;
}, []);
});
sigma.classes.graph.addMethod('setRank', function (nodeId, value) {
let n = this.nodesIndex.get(nodeId);
n.rank = value;
n.label = n.id + " " + (Math.round(n.rank * 1000) / 1000)
});
/**
Instanciate the sigma object
*/
let s = new sigma({
graph: g,
container: 'graph-container',
settings: {
labelThreshold: 0
}
});
let [O, I] = [s.graph.O, s.graph.I];
$.get("data.txt", function (data, status) {
let edges = [];
data.split(/\r?\n/).map((row, i) => {
let e = row.split(',');
leftPartition.add(e[0])
rightPartition.add(e[1])
edges.push({
id: i,
source: e[0],
target: e[1],
color: '#ccc',
type: "arrow"
})
});
//converting Set to Array (Set.prototype.forEach doesn't provide the numeric index I need for y coords)
[...leftPartition].forEach((n, i) => {
s.graph.addNode({id: n, label: n, y: i, x: 0, text: n,})
});
[...rightPartition].forEach((n, i) => {
s.graph.addNode({id: n, label: n, y: i, x: 2, text: n})
});
edges.forEach(e => s.graph.addEdge(e));
/**
Utility function, takes a 'coupledNode', e.g. "(A,B)", returns sigma graph node references to its single nodes
*/
let getNodeByPair = function (coupledNode) {
return {
l: s.graph.nodes(coupledNode.couple.l),
r: s.graph.nodes(coupledNode.couple.r)
}
};
/**
* Generates G^2 nodes
*/
let generateG2partition = function (set, partition) {
let pairs = [];
let y = 0;
let xCoord = (partition === "LEFT") ? 10 : 20;
set.forEach(i => {
set.forEach(j => {
pairs.push({
id: `${i},${j}`,
label: `${i},${j}`,
text: `${i},${j}`,
x: xCoord,
y: y++,
couple: {
l: `${i}`,
r: `${j}`
}
});
})
});
return pairs;
};
/**
* adding nodes of G^2
*/
G2leftPartition = generateG2partition(leftPartition, "LEFT");
G2rightPartition = generateG2partition(rightPartition, "RIGHT");
G2leftPartition.concat(G2rightPartition).forEach(n => s.graph.addNode(n));
/**
* adding edges of G^2
*/
//iterates right Partition of G^2
G2rightPartition.forEach(n => {
let couple = getNodeByPair(n);
let inBoundsL = I(couple.l.id);
let inBoundsR = I(couple.r.id);
//iterates left Partition of G^2
G2leftPartition.forEach((x, i) => {
//-1 is the only case a bitwise NOT returns 0
if (!!~inBoundsL.indexOf(x.couple.l) && !!~inBoundsR.indexOf(x.couple.r)) {
s.graph.addEdge({id: `${x.id},${n.id}`, color: '#ccc', source: x.id, target: n.id})
}
});
});
s.refresh();
});
/**
* The SimRank computation
* */
let C = 0.8;
let numIter;
let simRank = {
init: function () {
numIter = 0;
$('#numIter').css('display', 'inline-block');
$('#numIter span').html(numIter);
s.graph.nodes().forEach(n => n.couple && s.graph.setRank(n.id, +( n.couple.l === n.couple.r)));
s.refresh();
},
iteration: function () {
let it = $('#inputNumIter').val() || 1;
let i = 0;
while (++i <= it) {
G2leftPartition.forEach(n => {
simRank.s(n.couple.l, n.couple.r, "LEFT")
});
G2rightPartition.forEach(n => {
simRank.s(n.couple.l, n.couple.r, "RIGHT")
});
$('#numIter span').html(++numIter)
}
s.refresh();
},
s: function (Aid, Bid, partition) {
let links = (partition === "LEFT") ? O : I; //define if I'm using outbounds or inbounds
let [linksA, linksB] = [links(Aid), links(Bid)];
let sum = 0;
let coeff = (linksA.length === 0 && linksB.length === 0) ? 0 : C / (linksA.length * linksB.length);
if (Aid === Bid) {
s.graph.setRank(`${Aid},${Bid}`, 1);
}
else {
links(Aid).forEach(A => {
links(Bid).forEach(B => {
sum += s.graph.nodes(`${A},${B}`).rank;
});
});
s.graph.setRank(`${Aid},${Bid}`, coeff * sum);
}
}
};
$("p[data-action]").click(function () {
simRank[$(this).data('action')]()
});