com.googlecode.blaisemath.graph.modules.metrics.ClosenessCentrality 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!
/*
* ClosenessCentrality.java
* Created Jul 23, 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.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import com.googlecode.blaisemath.graph.GAInstrument;
import com.googlecode.blaisemath.graph.Graph;
import com.googlecode.blaisemath.graph.GraphUtils;
/**
* Implements closeness centrality (Sabidussi 1966), the inverse sum of
* distances from one node to other nodes. The same calculation can be used to
* compute the "eccentricity" of the node, the max distance from this node to
* any other node, termed graph centrality by Hage/Harary 1995. Instances
* of both metrics are provided.
*
* @author elisha
*/
public class ClosenessCentrality implements GraphNodeMetric {
final boolean useSum = true;
public Double apply(Graph graph, V node) {
int n = graph.nodeCount();
HashMap lengths = new HashMap();
GraphUtils.breadthFirstSearch(graph, node, new HashMap(), lengths, new Stack(), new HashMap>());
double cptSize = lengths.size();
if (useSum) {
double sum = 0.0;
for (Integer i : lengths.values()) {
sum += i;
}
return cptSize / n * (n - 1.0) / sum;
} else {
double max = 0.0;
for (Integer i : lengths.values()) {
max = Math.max(max, i);
}
return cptSize / n * 1.0 / max;
}
}
public Map allValues(Graph graph) {
int id = GAInstrument.start("ClosenessCentrality.allValues", graph.nodeCount()+" nodes", graph.edgeCount()+" edges");
if (graph.nodeCount() == 0) {
return Collections.emptyMap();
} else if (graph.nodeCount() == 1) {
return Collections.singletonMap((V) graph.nodes().toArray()[0], 0.0);
}
int n = graph.nodeCount();
Set> components = GraphUtils.componentGraphs(graph);
HashMap values = new HashMap();
for (Graph cg : components) {
if (cg.nodeCount() == 1) {
values.put(cg.nodes().iterator().next(), 0.0);
} else {
computeAllValuesConnected(cg, values);
}
}
for (Graph cg : components) {
double multiplier = cg.nodeCount() / (double) n;
for (V v : cg.nodes()) {
values.put(v, multiplier * values.get(v));
}
}
GAInstrument.end(id);
return values;
}
/**
* Computes values for a connected portion of a graph
*/
private void computeAllValuesConnected(Graph graph, Map values) {
Set nodes = graph.nodes();
int n = nodes.size();
double max = (n - 1.0);
for (V start : nodes) {
HashMap lengths = new HashMap();
GraphUtils.breadthFirstSearch(graph, start, new HashMap(), lengths, new Stack(), new HashMap>());
if (useSum) {
double sum1 = 0.0;
for (Integer j : lengths.values()) {
sum1 += j;
}
values.put(start, max / sum1);
} else {
double max1 = 0.0;
for (Integer j : lengths.values()) {
max1 = Math.max(max1, j);
}
values.put(start, 1.0 / max1);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy