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

com.googlecode.blaisemath.graph.modules.metrics.BetweenCentrality Maven / Gradle / Ivy

The newest version!
/*
 * BetweenCentrality.java
 * Created Jul 3, 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.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;

/**
 * 

Provides a metric describing the betweenness centrality of a vertex in a * CONNECTED graph. Returns infinity if the graph is not connected. May take a * long time for large graphs.

Computationally, the centrality measures * the probability that a given node lies on a randomly chosen geodesic.

* * @author Elisha Peterson */ public class BetweenCentrality implements GraphNodeMetric { public Double apply(Graph graph, V node) { return allValues(graph).get(node); } public Map allValues(Graph graph) { int id = GAInstrument.start("BetweenCentrality.allValues", graph.nodeCount()+" nodes", graph.edgeCount()+" edges"); HashMap between = new HashMap(); for (V v : graph.nodes()) { between.put(v, 0.0); } for (V start : graph.nodes()) { brandes(graph, start, between, graph.isDirected() ? 1.0 : 0.5); } GAInstrument.end(id); return between; } /** * Breadth-first search algorithm for an unweighted graph to generate * betweenness scores, with specified starting vertex. From Brandes, * "A Faster Algorithm for Betweenness Centrality" * * @param graph the graph * @param start the start vertex * @param between data structure storing existing betweenness centrality values * @param multiplier applied to all elements of resulting map * @return data structure encoding the result */ static HashMap brandes(Graph graph, V start, HashMap between, double multiplier) { Set nodes = graph.nodes(); if (!nodes.contains(start)) { return new HashMap(); } // number of shortest paths to each vertex HashMap numShortest = new HashMap(); // length of shortest paths to each vertex HashMap lengths = new HashMap(); // tracks elements in non-increasing order for later use Stack stack = new Stack(); // tracks vertex predecessors in resulting tree HashMap> pred = new HashMap>(); GraphUtils.breadthFirstSearch(graph, start, numShortest, lengths, stack, pred); // compute betweenness HashMap dependencies = new HashMap(); for (V v : nodes) { dependencies.put(v, 0.0); } while (!stack.isEmpty()) { V w = stack.pop(); for (V v : pred.get(w)) { dependencies.put(v, dependencies.get(v) + (double) numShortest.get(v) / numShortest.get(w) * (1 + dependencies.get(w))); } if (w != start) { between.put(w, between.get(w)+multiplier*dependencies.get(w)); } } return between; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy