Commit 110cbf93 authored by gsavin's avatar gsavin

Update Closeness centrality documentation. Add demo.

parent 345188eb
/*
* Copyright 2006 - 2012
* Stefan Balev <stefan.balev@graphstream-project.org>
* Julien Baudry <julien.baudry@graphstream-project.org>
* Antoine Dutot <antoine.dutot@graphstream-project.org>
* Yoann Pigné <yoann.pigne@graphstream-project.org>
* Guilhelm Savin <guilhelm.savin@graphstream-project.org>
*
* This file is part of GraphStream <http://graphstream-project.org>.
*
* GraphStream is a library whose purpose is to handle static or dynamic
* graph, create them from scratch, file or any source and display them.
*
* This program is free software distributed under the terms of two licenses, the
* CeCILL-C license that fits European law, and the GNU Lesser General Public
* License. You can use, modify and/ or redistribute the software under the terms
* of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
* URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
*/
package org.graphstream.algorithm.measure.demo;
import org.graphstream.algorithm.generator.BarabasiAlbertGenerator;
import org.graphstream.algorithm.measure.ClosenessCentrality;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.AdjacencyListGraph;
public class ClosenessCentralityDemo {
public static final String STYLE = "node {" + "fill-mode: dyn-plain;"
+ "fill-color: blue,yellow;" + "size-mode: dyn-size;"
+ "stroke-color: black;" + "stroke-width: 1px;"
+ "stroke-mode: plain;" + "}";
public static void main(String... args) {
System.setProperty("org.graphstream.ui.renderer",
"org.graphstream.ui.j2dviewer.J2DGraphRenderer");
Graph g = new AdjacencyListGraph("g");
g.addAttribute("ui.quality");
g.addAttribute("ui.antialias");
g.addAttribute("ui.stylesheet", STYLE);
BarabasiAlbertGenerator gen = new BarabasiAlbertGenerator();
gen.addSink(g);
gen.begin();
for (int i = 0; i < 200; i++)
gen.nextEvents();
gen.end();
ClosenessCentrality cc = new ClosenessCentrality("ui.color", true,
true, true);
cc.init(g);
cc.compute();
for (int i = 0; i < g.getNodeCount(); i++)
g.getNode(i).addAttribute("ui.size",
g.getNode(i).getNumber("ui.color") * 25 + 5);
g.display();
}
}
/*
* Copyright 2006 - 2012
* Stefan Balev <stefan.balev@graphstream-project.org>
* Julien Baudry <julien.baudry@graphstream-project.org>
* Antoine Dutot <antoine.dutot@graphstream-project.org>
* Yoann Pigné <yoann.pigne@graphstream-project.org>
* Guilhelm Savin <guilhelm.savin@graphstream-project.org>
*
* This file is part of GraphStream <http://graphstream-project.org>.
*
* GraphStream is a library whose purpose is to handle static or dynamic
* graph, create them from scratch, file or any source and display them.
*
* This program is free software distributed under the terms of two licenses, the
* CeCILL-C license that fits European law, and the GNU Lesser General Public
* License. You can use, modify and/ or redistribute the software under the terms
* of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
* URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
*/
package org.graphstream.algorithm;
public class NotInitializedException extends RuntimeException {
private static final long serialVersionUID = 7561076615165578902L;
public NotInitializedException(Algorithm algo) {
super(String.format("Algorithm %s has not been initialized", algo
.getClass().getName()));
}
}
......@@ -33,31 +33,92 @@ package org.graphstream.algorithm.measure;
import org.graphstream.algorithm.APSP;
import org.graphstream.algorithm.APSP.APSPInfo;
import org.graphstream.algorithm.generator.BarabasiAlbertGenerator;
import org.graphstream.algorithm.NotInitializedException;
import org.graphstream.algorithm.Algorithm;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.AdjacencyListGraph;
/**
* Compute closeness centrality.
*
*
*/
public class ClosenessCentrality implements Algorithm {
/**
* Flag indicating if APSP should be computed in this algorithm. If false,
* user needs to compute APSP himself to provide {@link APSPInfo} object in
* nodes attribute {@link APSPInfo#ATTRIBUTE_NAME}.
*/
protected boolean computeAPSP;
/**
* Flag indicating if centrality results should be normalized between 0 and
* 1.
*/
protected boolean normalize;
/**
* Attribute name where centrality value will be stored.
*/
protected String centralityAttribute;
protected Graph graph;
/**
* Flag indicating if computation should use Dangalchev method rather than
* the classical method. This method is more adapted for disconnected graph.
*/
protected boolean useDangalchevMethod = false;
// Allow reusing same array.
private double[] data;
protected boolean useDangalchevMethod = false;
// Graph being used by this algorithm.
private Graph graph;
// APSP algorithm if computed in this algorithm.
private APSP apsp;
protected APSP apsp;
/**
* Default construtor. Same as calling `ClosenessCentrality("closeness")`.
*/
public ClosenessCentrality() {
this("closeness");
}
/**
* Construtor allowing to configure centrality attribute. Same as calling
* `ClosenessCentrality(attribute, false)`.
*
* @param attribute
* attribute where centrality will be stored
*/
public ClosenessCentrality(String attribute) {
this(attribute, false);
}
/**
* Constructor allowing to configure attribute and normalize flag. Same as
* calling `ClosenessCentrality(attribute, normalize, true, false)`.
*
* @param attribute
* attribute where centrality will be stored
* @param normalize
* if true, values will be normalized between 0 and 1
*/
public ClosenessCentrality(String attribute, boolean normalize) {
this(attribute, normalize, true, false);
}
/**
* Fully configurable construtor.
*
* @param centralityAttribute
* attribute where centrality will be stored
* @param normalize
* if true, centrality values will be normalized between 0 and 1
* @param computeAPSP
* if true, apsp will be computed in this algorithm
* @param useDangalchevMethod
* if true, Dangelchev method will be used in this algorithm
*/
public ClosenessCentrality(String centralityAttribute, boolean normalize,
boolean computeAPSP, boolean useDangalchevMethod) {
this.computeAPSP = computeAPSP;
......@@ -65,7 +126,16 @@ public class ClosenessCentrality implements Algorithm {
this.normalize = normalize;
}
/*
* (non-Javadoc)
*
* @see
* org.graphstream.algorithm.Algorithm#init(org.graphstream.graph.Graph)
*/
public void init(Graph graph) {
if (graph == null)
throw new NullPointerException();
this.graph = graph;
if (computeAPSP) {
......@@ -74,7 +144,15 @@ public class ClosenessCentrality implements Algorithm {
}
}
/*
* (non-Javadoc)
*
* @see org.graphstream.algorithm.Algorithm#compute()
*/
public void compute() {
if (graph == null)
throw new NotInitializedException(this);
int count = graph.getNodeCount();
Node node, other;
......@@ -129,22 +207,4 @@ public class ClosenessCentrality implements Algorithm {
for (int idx = 0; idx < count; idx++)
graph.getNode(idx).setAttribute(centralityAttribute, data[idx]);
}
public static void main(String... args) {
Graph g = new AdjacencyListGraph("g");
g.addAttribute("ui.stylesheet",
"node {fill-mode: dyn-plain; fill-color: blue,yellow;}");
BarabasiAlbertGenerator gen = new BarabasiAlbertGenerator();
gen.addSink(g);
gen.begin();
for (int i = 0; i < 1000; i++)
gen.nextEvents();
gen.end();
ClosenessCentrality cc = new ClosenessCentrality("ui.color", true, true, true);
cc.init(g);
cc.compute();
g.display();
}
}
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