Commit ed42f522 authored by gsavin's avatar gsavin

Fix eigenvector centrality

parent ecc9ad4f
...@@ -31,32 +31,76 @@ ...@@ -31,32 +31,76 @@
*/ */
package org.graphstream.algorithm.measure; package org.graphstream.algorithm.measure;
import org.graphstream.algorithm.Spectrum; import org.graphstream.graph.Edge;
import org.graphstream.graph.Node;
import scala.actors.threadpool.Arrays;
public class EigenvectorCentrality extends AbstractCentrality { public class EigenvectorCentrality extends AbstractCentrality {
public static final int DEFAULT_MAX_ITER = 100;
protected int maxIter;
protected String weightAttribute;
public EigenvectorCentrality(String attribute, boolean normalize) { public EigenvectorCentrality(String attribute, boolean normalize) {
this(attribute, normalize, DEFAULT_MAX_ITER, "weight");
}
public EigenvectorCentrality(String attribute, boolean normalize,
int maxIter, String weightAttribute) {
super(attribute, normalize); super(attribute, normalize);
this.maxIter = maxIter;
this.weightAttribute = weightAttribute;
} }
@Override @Override
protected void computeCentrality() { protected void computeCentrality() {
Spectrum spectrum = new Spectrum(); int n = graph.getNodeCount();
spectrum.init(graph); double[] x1 = new double[n];
spectrum.compute(); double[] x2 = new double[n];
double[] t;
double f, s;
int iter = maxIter;
Node node;
Edge edge;
double[] eigenvalues = spectrum.getEigenvalues(); Arrays.fill(x2, 1.0 / n);
double k1 = Double.MIN_VALUE;
int k1idx = -1; while (iter-- > 0) {
//
for (int i = 0; i < eigenvalues.length; i++) { // Swap x1 and x2
if (eigenvalues[i] > k1) { //
k1idx = i; t = x1;
k1 = eigenvalues [i]; x1 = x2;
x2 = t;
Arrays.fill(x2, 0);
s = 0;
for (int idx = 0; idx < n; idx++) {
node = graph.getNode(idx);
for (int i = 0; i < node.getDegree(); i++) {
edge = node.getEdge(i);
f = 1;
if (edge.hasNumber(weightAttribute))
f = edge.getNumber(weightAttribute);
x2[idx] += x1[edge.getOpposite(node).getIndex()] * f;
}
s += x2[idx] * x2[idx];
} }
s = s == 0 ? 1.0 : 1.0 / Math.sqrt(s);
for (int idx = 0; idx < n; idx++)
x2[idx] *= s;
} }
data = spectrum.getEigenvector(k1idx); data = x2;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment