All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.blaisemath.graph.mod.metrics.ClosenessCentrality Maven / Gradle / Ivy

/*
 * ClosenessCentrality.java
 * Created Jul 23, 2010
 */
package com.googlecode.blaisemath.graph.mod.metrics;

/*
 * #%L
 * BlaiseGraphTheory
 * --
 * Copyright (C) 2009 - 2018 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.google.common.base.Function;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Maps;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.googlecode.blaisemath.util.GAInstrument;
import com.googlecode.blaisemath.graph.Graph;
import com.googlecode.blaisemath.graph.GraphMetrics;
import com.googlecode.blaisemath.graph.GraphUtils;
import java.util.ArrayDeque;

/**
 * 

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 extends AbstractGraphNodeMetric { public ClosenessCentrality() { super("Closeness centrality"); } @Override public Double apply(Graph graph, V node) { int n = graph.nodeCount(); Map lengths = new HashMap(); GraphUtils.breadthFirstSearch(graph, node, HashMultiset.create(), lengths, new ArrayDeque(), HashMultimap.create()); double cptSize = lengths.size(); double sum = 0.0; for (Integer i : lengths.values()) { sum += i; } return cptSize / n * (n - 1.0) / sum; } @Override public Map apply(Graph graph) { int id = GAInstrument.start("ClosenessCentrality.allValues", graph.nodeCount()+" nodes", graph.edgeCount()+" edges"); Map res = GraphMetrics.applyToComponents(graph, new ApplyConnected()); GAInstrument.end(id); return res; } /** Computes values for a connected portion of a graph */ private static class ApplyConnected implements Function, Map> { @Override public Map apply(Graph graph) { Map res = Maps.newHashMap(); Set nodes = graph.nodes(); int n = nodes.size(); double max = n - 1.0; for (V start : nodes) { Map lengths = new HashMap(); GraphUtils.breadthFirstSearch(graph, start, HashMultiset.create(), lengths, new ArrayDeque(), HashMultimap.create()); double sum1 = 0.0; for (Integer j : lengths.values()) { sum1 += j; } res.put(start, max / sum1); } return res; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy