org.graphstream.algorithm
Class Toolkit

java.lang.Object
  extended by org.graphstream.algorithm.Toolkit

public class Toolkit
extends Object

Lots of small often used algorithms on graphs.

This class contains a lot of very small algorithms that could be often useful with a graph. Most methods take a graph as first argument.

Usage

Degrees

The degreeDistribution(Graph) method allows to obtain an array where each cell index represents the degree, and the value of the cell the number of nodes having this degree. Its complexity is O(n) with n the number of nodes.

The degreeMap(Graph) returns an array of nodes sorted by degree in descending order. The complexity is O(n log(n)) with n the number of nodes.

The averageDegree(Graph) returns the average degree. The complexity is O(1).

The degreeAverageDeviation(Graph) returns the deviation of the average degree. The complexity is O(n) with n the number of nodes.

*

Density

The density(Graph) method returns the number of links in the graph divided by the total number of possible links. The complexity is O(1).

Diameter

The diameter(Graph) method computes the diameter of the graph. The diameter of the graph is the largest of all the shortest paths from any node to any other node.

Note that this operation can be quite costly, the algorithm used to compute all shortest paths is the Floyd-Warshall algorithm whose complexity is at worst of O(n^3).

The returned diameter is not an integer since some graphs have non-integer weights on edges.

The diameter(Graph, String, boolean) method does the same thing, but considers that the graph is weighted. The second argument is the weight attribute name. The third argument indicates if the graph must be considered as directed or not.

Clustering coefficient

The clusteringCoefficient(Node) method return the clustering coefficient for the given node. The complexity if O(d^2) where d is the degree of the node.

The clusteringCoefficients(Graph) method return the clustering coefficient of each node of the graph as an array.

The averageClusteringCoefficient(Graph) method return the average clustering coefficient for the graph.

Random nodes and edges

The randomNode(Graph) returns a node chosen at random in the graph. You can alternatively pass a ``Random`` instance as parameter with randomNode(Graph, Random). The complexity depends on the kind of graph.

The randomEdge(Graph) returns an edge chosen at random in the graph. You can alternatively pass a ``Random`` instance as parameter with randomEdge(Graph, Random). The randomEdge(Node) returns an edge chosen at random within the edge set of the given node. You can also use randomEdge(Node, Random). To chose a random edge of a node inside the entering or leaving edge sets only, you can use randomInEdge(Node) or randomInEdge(Node, Random), or randomOutEdge(Node) or finally randomOutEdge(Node, Random).

Nodes position

Extracting nodes position from attributes can be tricky due to the face the positions can be stored either as separate ``x``, ``y`` and ``z`` attributes or inside ``xy`` or ``xyz`` attributes.

To simplify things you can use nodePosition(Node) which returns an array of three doubles, containing the position of the node. You can also use nodePosition(Graph, String) with a graph and a node identifier.

If you already have an array of doubles with at least three cells you can also use nodePosition(Node, double[]) that will store the position in the passed array. You can as well use nodePosition(Graph, String, double[]).

All these methods can also handle the ``org.graphstream.ui.geom.Point3`` class instead of arrays of doubles. Methods that use such an array as argument are the same. Methods that return a ``Point3`` instead of an array are nodePointPosition(Graph, String) and nodePointPosition(Node).

Example

You can use this class with a static import for example:

 import static org.graphstream.algorithm.Toolkit.*;
 


Constructor Summary
Toolkit()
           
 
Method Summary
static double averageClusteringCoefficient(org.graphstream.graph.Graph graph)
          Average clustering coefficient of the whole graph.
static double averageDegree(org.graphstream.graph.Graph graph)
          Returns the value of the average degree of the graph.
static double clusteringCoefficient(org.graphstream.graph.Node node)
          Clustering coefficient for one node of the graph.
static double[] clusteringCoefficients(org.graphstream.graph.Graph graph)
          Clustering coefficient for each node of the graph.
static HashMap<Object,HashSet<org.graphstream.graph.Node>> communities(org.graphstream.graph.Graph graph, String marker)
          Return set of nodes grouped by the value of one attribute (the marker).
static double degreeAverageDeviation(org.graphstream.graph.Graph graph)
          Returns the value of the degree average deviation of the graph.
static int[] degreeDistribution(org.graphstream.graph.Graph graph)
          Compute the degree distribution of this graph.
static ArrayList<org.graphstream.graph.Node> degreeMap(org.graphstream.graph.Graph graph)
          Return a list of nodes sorted by degree, the larger first.
static double density(org.graphstream.graph.Graph graph)
          The density is the number of links in the graph divided by the total number of possible links.
static double diameter(org.graphstream.graph.Graph graph)
          Compute the diameter of the graph.
static double diameter(org.graphstream.graph.Graph graph, String weightAttributeName, boolean directed)
          Compute the diameter of the graph.
static double edgeLength(org.graphstream.graph.Edge edge)
          Like edgeLength(Graph,String) but use an existing edge as argument.
static double edgeLength(org.graphstream.graph.Graph graph, String id)
          Compute the edge length of the given edge according to its two nodes positions.
static double modularity(double[][] E)
          Compute the modularity of the graph from the E matrix.
static double modularity(org.graphstream.graph.Graph graph, String marker)
          Computes the modularity as defined by Newman and Girvan in "Finding and evaluating community structure in networks".
static double modularity(org.graphstream.graph.Graph graph, String marker, String weightMarker)
          Computes the weighted modularity.
static double[][] modularityMatrix(org.graphstream.graph.Graph graph, HashMap<Object,HashSet<org.graphstream.graph.Node>> communities)
          Create the modularity matrix E from the communities.
static double[][] modularityMatrix(org.graphstream.graph.Graph graph, HashMap<Object,HashSet<org.graphstream.graph.Node>> communities, String weightMarker)
          Create the weighted modularity matrix E from the communities.
static org.graphstream.ui.geom.Point3 nodePointPosition(org.graphstream.graph.Graph graph, String id)
          Retrieve a node position from its attributes ("x", "y", "z", or "xy", or "xyz").
static org.graphstream.ui.geom.Point3 nodePointPosition(org.graphstream.graph.Node node)
          Like nodePointPosition(Graph,String) but use an existing node as argument.
static double[] nodePosition(org.graphstream.graph.Graph graph, String id)
          Retrieve a node position from its attributes ("x", "y", "z", or "xy", or "xyz").
static void nodePosition(org.graphstream.graph.Graph graph, String id, double[] xyz)
          Like nodePosition(Graph,String), but instead of returning a newly allocated array, fill up the array given as parameter.
static void nodePosition(org.graphstream.graph.Graph graph, String id, org.graphstream.ui.geom.Point3 pos)
          Like nodePointPosition(Graph,String), but instead of returning a newly allocated array, fill up the array given as parameter.
static double[] nodePosition(org.graphstream.graph.Node node)
          Like nodePosition(Graph,String) but use an existing node as argument.
static void nodePosition(org.graphstream.graph.Node node, double[] xyz)
          Like nodePosition(Graph,String,double[]) but use an existing node as argument.
static void nodePosition(org.graphstream.graph.Node node, org.graphstream.ui.geom.Point3 pos)
          Like nodePosition(Graph,String,Point3) but use an existing node as argument.
static org.graphstream.graph.Edge randomEdge(org.graphstream.graph.Graph graph)
          Choose an edge at random.
static org.graphstream.graph.Edge randomEdge(org.graphstream.graph.Graph graph, Random random)
          Choose an edge at random.
static org.graphstream.graph.Edge randomEdge(org.graphstream.graph.Node node)
          Choose an edge at random from the edges connected to the given node.
static org.graphstream.graph.Edge randomEdge(org.graphstream.graph.Node node, Random random)
          Choose an edge at random from the edges connected to the given node.
static org.graphstream.graph.Edge randomInEdge(org.graphstream.graph.Node node)
          Choose an edge at random from the entering edges connected to the given node.
static org.graphstream.graph.Edge randomInEdge(org.graphstream.graph.Node node, Random random)
          Choose an edge at random from the entering edges connected to the given node.
static org.graphstream.graph.Node randomNode(org.graphstream.graph.Graph graph)
          Choose a node at random.
static org.graphstream.graph.Node randomNode(org.graphstream.graph.Graph graph, Random random)
          Choose a node at random.
static org.graphstream.graph.Edge randomOutEdge(org.graphstream.graph.Node node)
          Choose an edge at random from the leaving edges connected to the given node.
static org.graphstream.graph.Edge randomOutEdge(org.graphstream.graph.Node node, Random random)
          Choose an edge at random from the leaving edges connected to the given node.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Toolkit

public Toolkit()
Method Detail

degreeDistribution

public static int[] degreeDistribution(org.graphstream.graph.Graph graph)
Compute the degree distribution of this graph. Each cell of the returned array contains the number of nodes having degree n where n is the index of the cell. For example cell 0 counts how many nodes have zero edges, cell 5 counts how many nodes have five edges. The last index indicates the maximum degree.

Computational Complexity :
O(n) where n is the number of nodes.

degreeMap

public static ArrayList<org.graphstream.graph.Node> degreeMap(org.graphstream.graph.Graph graph)
Return a list of nodes sorted by degree, the larger first.

Returns:
The degree map.
Computational Complexity :
O(n log(n)) where n is the number of nodes.

averageDegree

public static double averageDegree(org.graphstream.graph.Graph graph)
Returns the value of the average degree of the graph. A node with a loop edge has degree two.

Returns:
The average degree of the graph.
Computational Complexity :
O(1).

degreeAverageDeviation

public static double degreeAverageDeviation(org.graphstream.graph.Graph graph)
Returns the value of the degree average deviation of the graph.

Returns:
The degree average deviation.
Computational Complexity :
O(n) where n is the number of nodes.

density

public static double density(org.graphstream.graph.Graph graph)
The density is the number of links in the graph divided by the total number of possible links.

Returns:
The density of the graph.
Computational Complexity :
O(1)

clusteringCoefficients

public static double[] clusteringCoefficients(org.graphstream.graph.Graph graph)
Clustering coefficient for each node of the graph.

Returns:
An array whose size correspond to the number of nodes, where each element is the clustering coefficient of a node.
Computational Complexity :
at worse O(n d^2) where n is the number of nodes and d the average or maximum degree of nodes.

averageClusteringCoefficient

public static double averageClusteringCoefficient(org.graphstream.graph.Graph graph)
Average clustering coefficient of the whole graph. Average of each node individual clustering coefficient.

Returns:
The average clustering coefficient.
Computational Complexity :
at worse O(n d^2) where n is the number of nodes and d the average or maximum degree of nodes.

clusteringCoefficient

public static double clusteringCoefficient(org.graphstream.graph.Node node)
Clustering coefficient for one node of the graph.

Parameters:
node - The node to compute the clustering coefficient for.
Returns:
The clustering coefficient for this node.
Computational Complexity :
O(d^2) where d is the degree of the given node.

randomNode

public static org.graphstream.graph.Node randomNode(org.graphstream.graph.Graph graph)
Choose a node at random.

Returns:
A node chosen at random, null if the graph is empty.
Computational Complexity :
at worse O(n) where n is the number of nodes.

randomNode

public static org.graphstream.graph.Node randomNode(org.graphstream.graph.Graph graph,
                                                    Random random)
Choose a node at random.

Parameters:
random - The random number generator to use.
Returns:
A node chosen at random, null if the graph is empty.
Computational Complexity :
at worse O(n) where n is the number of nodes.

randomEdge

public static org.graphstream.graph.Edge randomEdge(org.graphstream.graph.Graph graph)
Choose an edge at random.

Returns:
An edge chosen at random. complexity at worse O(n) where n is the number of edges.

randomEdge

public static org.graphstream.graph.Edge randomEdge(org.graphstream.graph.Graph graph,
                                                    Random random)
Choose an edge at random.

Parameters:
random - The random number generator to use.
Returns:
An edge chosen at random, null if the graph has no edges. complexity at worse O(n) where n is the number of edges.

randomEdge

public static org.graphstream.graph.Edge randomEdge(org.graphstream.graph.Node node)
Choose an edge at random from the edges connected to the given node.

Returns:
An edge chosen at random, null if the node has no edges. complexity at worse O(n) where n is the number of edges.

randomInEdge

public static org.graphstream.graph.Edge randomInEdge(org.graphstream.graph.Node node)
Choose an edge at random from the entering edges connected to the given node.

Returns:
An edge chosen at random, null if the node has no entering edges. complexity at worse O(n) where n is the number of edges.

randomOutEdge

public static org.graphstream.graph.Edge randomOutEdge(org.graphstream.graph.Node node)
Choose an edge at random from the leaving edges connected to the given node.

Returns:
An edge chosen at random, null if the node has no leaving edges. complexity at worse O(n) where n is the number of edges.

randomEdge

public static org.graphstream.graph.Edge randomEdge(org.graphstream.graph.Node node,
                                                    Random random)
Choose an edge at random from the edges connected to the given node.

Parameters:
random - The random number generator to use.
Returns:
An edge chosen at random, null if the node has no edges. complexity at worse O(n) where n is the number of edges.

randomInEdge

public static org.graphstream.graph.Edge randomInEdge(org.graphstream.graph.Node node,
                                                      Random random)
Choose an edge at random from the entering edges connected to the given node.

Parameters:
random - The random number generator to use.
Returns:
An edge chosen at random, null if the node has no entering edges. complexity at worse O(n) where n is the number of edges.

randomOutEdge

public static org.graphstream.graph.Edge randomOutEdge(org.graphstream.graph.Node node,
                                                       Random random)
Choose an edge at random from the leaving edges connected to the given node.

Parameters:
random - The random number generator to use.
Returns:
An edge chosen at random, null if the node has no leaving edges. complexity at worse O(n) where n is the number of edges.

communities

public static HashMap<Object,HashSet<org.graphstream.graph.Node>> communities(org.graphstream.graph.Graph graph,
                                                                              String marker)
Return set of nodes grouped by the value of one attribute (the marker). For example, if the marker is "color" and in the graph there are nodes whose "color" attribute value is "red" and others with value "blue", this method will return two sets, one containing all nodes corresponding to the nodes whose "color" attribute is red, the other with blue nodes. If some nodes do not have the "color" attribute, a third set is returned. The returned sets are stored in a hash map whose keys are the values of the marker attribute (in our example, the keys would be "red" and "blue", and if there are nodes that do not have the "color" attribute, the third set will have key "NULL_COMMUNITY").

Parameters:
marker - The attribute that allows to group nodes.
Returns:
The communities indexed by the value of the marker.
Computational Complexity :
O(n) with n the number of nodes.

modularityMatrix

public static double[][] modularityMatrix(org.graphstream.graph.Graph graph,
                                          HashMap<Object,HashSet<org.graphstream.graph.Node>> communities)
Create the modularity matrix E from the communities. The given communities are set of nodes forming the communities as produced by the communities(Graph,String) method.

Parameters:
graph - Graph to which the computation will be applied
communities - Set of nodes.
Returns:
The E matrix as defined by Newman and Girvan.
Computational Complexity :
O(m!k) with m the number of communities and k the average number of nodes per community.

modularityMatrix

public static double[][] modularityMatrix(org.graphstream.graph.Graph graph,
                                          HashMap<Object,HashSet<org.graphstream.graph.Node>> communities,
                                          String weightMarker)
Create the weighted modularity matrix E from the communities. The given communities are set of nodes forming the communities as produced by the communities(Graph,String) method.

Parameters:
graph - Graph to which the computation will be applied
communities - Set of nodes.
weightMarker - The marker used to store the weight of each edge
Returns:
The E matrix as defined by Newman and Girvan.
Computational Complexity :
O(m!k) with m the number of communities and k the average number of nodes per community.

modularity

public static double modularity(double[][] E)
Compute the modularity of the graph from the E matrix.

Parameters:
E - The E matrix given by modularityMatrix(Graph,HashMap) .
Returns:
The modularity of the graph.
Computational Complexity :
O(m!) with m the number of communities.

modularity

public static double modularity(org.graphstream.graph.Graph graph,
                                String marker)
Computes the modularity as defined by Newman and Girvan in "Finding and evaluating community structure in networks". This algorithm traverses the graph to count nodes in communities. For this to work, there must exist an attribute on each node whose value define the community the node pertains to (see communities(Graph,String)). This method is an utility method that call: in order to produce the modularity value.

Parameters:
marker - The community attribute stored on nodes.
Returns:
The graph modularity.
See Also:
Modularity
Computational Complexity :
0(n + m! + m!k) with n the number of nodes, m the number of communities and k the average number of nodes per communities.

modularity

public static double modularity(org.graphstream.graph.Graph graph,
                                String marker,
                                String weightMarker)
Computes the weighted modularity. This algorithm traverses the graph to count nodes in communities. For this to work, there must exist an attribute on each node whose value define the community the node pertains to (see communities(Graph,String)) and a attribute on each edge storing their weight (all edges without this attribute will be ignored in the computation). This method is an utility method that call: in order to produce the modularity value.

Parameters:
marker - The community attribute stored on nodes.
weightMarker - The marker used to store the weight of each edge.
Returns:
The graph modularity.
See Also:
Modularity
Computational Complexity :
0(n + m! + m!k) with n the number of nodes, m the number of communities and k the average number of nodes per communities.

nodePosition

public static double[] nodePosition(org.graphstream.graph.Graph graph,
                                    String id)
Retrieve a node position from its attributes ("x", "y", "z", or "xy", or "xyz").

Parameters:
id - The node identifier.
Returns:
A newly allocated array of three floats containing the (x,y,z) position of the node, or null if the node is not part of the graph.

nodePointPosition

public static org.graphstream.ui.geom.Point3 nodePointPosition(org.graphstream.graph.Graph graph,
                                                               String id)
Retrieve a node position from its attributes ("x", "y", "z", or "xy", or "xyz").

Parameters:
id - The node identifier.
Returns:
A newly allocated point containing the (x,y,z) position of the node, or null if the node is not part of the graph.

nodePosition

public static double[] nodePosition(org.graphstream.graph.Node node)
Like nodePosition(Graph,String) but use an existing node as argument.

Parameters:
node - The node to consider.
Returns:
A newly allocated array of three floats containing the (x,y,z) position of the node.

nodePointPosition

public static org.graphstream.ui.geom.Point3 nodePointPosition(org.graphstream.graph.Node node)
Like nodePointPosition(Graph,String) but use an existing node as argument.

Parameters:
node - The node to consider.
Returns:
A newly allocated point containing the (x,y,z) position of the node.

nodePosition

public static void nodePosition(org.graphstream.graph.Graph graph,
                                String id,
                                double[] xyz)
Like nodePosition(Graph,String), but instead of returning a newly allocated array, fill up the array given as parameter. This array must have at least three cells.

Parameters:
id - The node identifier.
xyz - An array of at least three cells.
Throws:
RuntimeException - If the node with the given identifier does not exist.

nodePosition

public static void nodePosition(org.graphstream.graph.Graph graph,
                                String id,
                                org.graphstream.ui.geom.Point3 pos)
Like nodePointPosition(Graph,String), but instead of returning a newly allocated array, fill up the array given as parameter. This array must have at least three cells.

Parameters:
id - The node identifier.
pos - A point that will receive the node position.
Throws:
RuntimeException - If the node with the given identifier does not exist.

nodePosition

public static void nodePosition(org.graphstream.graph.Node node,
                                double[] xyz)
Like nodePosition(Graph,String,double[]) but use an existing node as argument.

Parameters:
node - The node to consider.
xyz - An array of at least three cells.

nodePosition

public static void nodePosition(org.graphstream.graph.Node node,
                                org.graphstream.ui.geom.Point3 pos)
Like nodePosition(Graph,String,Point3) but use an existing node as argument.

Parameters:
node - The node to consider.
pos - A point that will receive the node position.

edgeLength

public static double edgeLength(org.graphstream.graph.Graph graph,
                                String id)
Compute the edge length of the given edge according to its two nodes positions.

Parameters:
id - The identifier of the edge.
Returns:
The edge length or -1 if the nodes of the edge have no positions.
Throws:
RuntimeException - If the edge cannot be found.

edgeLength

public static double edgeLength(org.graphstream.graph.Edge edge)
Like edgeLength(Graph,String) but use an existing edge as argument.

Parameters:
edge -
Returns:
The edge length or -1 if the nodes of the edge have no positions.

diameter

public static double diameter(org.graphstream.graph.Graph graph)
Compute the diameter of the graph.

The diameter of the graph is the largest of all the shortest paths from any node to any other node.

Note that this operation can be quite costly, the algorithm used to compute all shortest paths is the Floyd-Warshall algorithm whose complexity is at worst of O(n^3).

The returned diameter is not an integer since some graphs have non-integer weights on edges.

Parameters:
graph - The graph to use.
Returns:
The diameter.

diameter

public static double diameter(org.graphstream.graph.Graph graph,
                              String weightAttributeName,
                              boolean directed)
Compute the diameter of the graph.

The diameter of the graph is the largest of all the shortest paths from any node to any other node.

Note that this operation can be quite costly, the algorithm used to compute all shortest paths is the Floyd-Warshall algorithm whose complexity is at worst of O(n^3).

The returned diameter is not an integer since some graphs have non-integer weights on edges.

Parameters:
graph - The graph to use.
weightAttributeName - The name used to store weights on the edges (must be a Number).
directed - Does The edge direction should be considered ?.
Returns:
The diameter.


Copyright © 2011. All Rights Reserved.