com.googlecode.blaisemath.graph.modules.metrics.EigenCentrality Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blaise-graphtheory Show documentation
Show all versions of blaise-graphtheory Show documentation
Link graph definitions, algorithms, and visualization.
The newest version!
/*
* EigenCentrality.java
* Created Jul 12, 2010
*/
package com.googlecode.blaisemath.graph.modules.metrics;
/*
* #%L
* BlaiseGraphTheory
* --
* Copyright (C) 2009 - 2016 Elisha Peterson
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.googlecode.blaisemath.graph.GraphNodeMetric;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.googlecode.blaisemath.graph.GAInstrument;
import com.googlecode.blaisemath.graph.Graph;
import com.googlecode.blaisemath.graph.GraphUtils;
import com.googlecode.blaisemath.linear.Matrices;
/**
* Implementation of the eigenvalue centrality calculation.
*
* @author Elisha Peterson
*/
public class EigenCentrality implements GraphNodeMetric {
public Double apply(Graph graph, V node) {
return allValues(graph).get(node);
}
public Map allValues(Graph graph) {
int id = GAInstrument.start("EigenCentrality.allValues", graph.nodeCount()+" nodes", graph.edgeCount()+" edges");
// computes eigenvalue centrality via repeated powers of the adjacency matrix
// (this finds the largest-magnitude eigenvector)
List nodes = new ArrayList();
boolean[][] adj0 = GraphUtils.adjacencyMatrix(graph, nodes);
int n = nodes.size();
int[][] mx = new int[adj0.length][adj0.length];
for (int i = 0; i < mx.length; i++) {
for (int j = 0; j < mx.length; j++) {
mx[i][j] = adj0[i][j] ? 1 : 0;
}
}
double[][] mx2 = new double[n][n];
for(int i=0;i0)) {
// should not happen
Logger.getLogger(EigenCentrality.class.getName()).log(Level.SEVERE,
"WARNING -- eigenvector has inconsistent signs");
break;
}
}
double sign = Math.signum(vecf2[0]);
Map result = new HashMap(n);
for (int i = 0; i < div.length; i++) {
result.put(nodes.get(i), sign*vecf2[i]);
}
GAInstrument.end(id);
return result;
}
/** Normalize a matrix by dividing by max value */
private static void normalize(double[][] mx) {
double max = -Double.MAX_VALUE;
for (double[] mx1 : mx) {
for (int j = 0; j < mx.length; j++) {
max = Math.max(max, mx1[j]);
}
}
for (double[] mx1 : mx) {
for (int j = 0; j < mx.length; j++) {
mx1[j] /= max;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy