org.graphstream.algorithm
Class Toolkit

java.lang.Object
  extended by org.graphstream.ui.graphicGraph.GraphPosLengthUtils
      extended by org.graphstream.algorithm.Toolkit

public class Toolkit
extends org.graphstream.ui.graphicGraph.GraphPosLengthUtils

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.

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 if the second argument is non-null. The second argument is the weight attribute name. The third argument indicates if the graph must be considered as directed or not.

Note that this operation can be quite costly, the algorithm used depends on the fact the graph is weighted or not. If unweighted, the algorithm is in O(n*(n+m)). If weighted the algorithm is the Floyd-Warshall algorithm whose complexity is at worst of O(n^3).

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 GraphPosLengthUtils.nodePosition(Node) which returns an array of three doubles, containing the position of the node. You can also use GraphPosLengthUtils.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 GraphPosLengthUtils.nodePosition(Node, double[]) that will store the position in the passed array. You can as well use GraphPosLengthUtils.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 GraphPosLengthUtils.nodePointPosition(Graph, String) and GraphPosLengthUtils.nodePointPosition(Node).

Cliques

A clique C is a subset of the node set of a graph, such that there exists an edge between each pair of nodes in C. In other words, the subgraph induced by C is complete. A maximal clique is a clique that cannot be extended by adding more nodes, that is, there is no node outside the clique connected to all the clique nodes.

This class provides several methods for dealing with cliques. Use isClique(Collection) or isMaximalClique(Collection, Graph) to check if a set of nodes is a clique or a maximal clique.

The methods getMaximalCliqueIterator(Graph) and getMaximalCliques(Graph) enumerate all the maximal cliques in a graph. Iterating on all the maximal cliques of a graph can take much time, because their number can grow exponentially with the size of the graph. For example, the following naive method to find the maximum clique (that is, the largest possible clique) in a graph, is practical only for small and sparse graphs.

 List<Node> maximumClique = new ArrayList<Node>();
 for (List<Node> clique : Toolkit.getMaximalCliques(g))
        if (clique.size() > maximumClique.size())
                maximumClique = clique;
 

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
<T extends org.graphstream.graph.Node>
int
getDegeneracy(org.graphstream.graph.Graph graph, List<T> ordering)
           This method computes the gedeneracy and the degeneracy ordering of a graph.
static
<T extends org.graphstream.graph.Node>
Iterator<List<T>>
getMaximalCliqueIterator(org.graphstream.graph.Graph graph)
          This iterator traverses all the maximal cliques in a graph.
static
<T extends org.graphstream.graph.Node>
Iterable<List<T>>
getMaximalCliques(org.graphstream.graph.Graph graph)
          An iterable view of the set of all the maximal cliques in a graph.
static boolean isClique(Collection<? extends org.graphstream.graph.Node> nodes)
          Checks if a set of nodes is a clique.
static boolean isMaximalClique(Collection<? extends org.graphstream.graph.Node> nodes, org.graphstream.graph.Graph graph)
          Checks if a set of nodes is a maximal clique.
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.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.
static int unweightedEccentricity(org.graphstream.graph.Node node, boolean directed)
          Eccentricity of a node not considering edge weights.
 
Methods inherited from class org.graphstream.ui.graphicGraph.GraphPosLengthUtils
edgeLength, edgeLength, nodePointPosition, nodePointPosition, nodePosition, nodePosition, nodePosition, nodePosition, nodePosition, nodePosition
 
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 :
O(1).

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 :
O(1).

randomEdge

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

Returns:
An edge chosen at random.
Computational Complexity :
O(1).

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:
O(1).

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:
O(1).

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:
O(1).

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.
Computational Complexity :
O(1).

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.
Computational Complexity :
O(1).

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.
Computational Complexity :
O(1).

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.
Computational Complexity :
O(1).

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.

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. The graph is considered non weighted.

Note that this operation can be quite costly, O(n*(n+m)).

The returned diameter is not an integer since some graphs have non-integer weights on edges. Although this version of the diameter algorithm will return an integer.

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. Two algorithms are used here. If the graph is not weighted (the weightAttributeName parameter is null), the algorithm use breath first search from all the nodes to find the max depth (or eccentricity) of each node. The diameter is then the maximum of these maximum depths. The complexity of this algorithm is O(n*(n+m)), with n the number of nodes and m the number of edges.

If the graph is weighted, 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 weighted 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.

unweightedEccentricity

public static int unweightedEccentricity(org.graphstream.graph.Node node,
                                         boolean directed)
Eccentricity of a node not considering edge weights.

The eccentricity is the largest shortest path between the given node and any other. It is here computed on number of edges crossed, not considering the eventual weights of edges.

This is computed using a breath first search and looking at the maximum depth of the search.

Parameters:
node - The node for which the eccentricity is to be computed.
directed - If true, the computation will respect edges direction, if any.
Returns:
The eccentricity.
Computational Complexity :
O(n+m) with n the number of nodes and m the number of edges.

isClique

public static boolean isClique(Collection<? extends org.graphstream.graph.Node> nodes)
Checks if a set of nodes is a clique.

Parameters:
nodes - a set of nodes
Returns:
true if nodes form a clique
Computational Complexity :
O(k), where k is the size of nodes

isMaximalClique

public static boolean isMaximalClique(Collection<? extends org.graphstream.graph.Node> nodes,
                                      org.graphstream.graph.Graph graph)
Checks if a set of nodes is a maximal clique.

Parameters:
nodes - a set of nodes
Returns:
true if form a maximal clique
Computational Complexity :
O(kn), where n is the number of nodes in the graph and k is the size of nodes

getMaximalCliqueIterator

public static <T extends org.graphstream.graph.Node> Iterator<List<T>> getMaximalCliqueIterator(org.graphstream.graph.Graph graph)
This iterator traverses all the maximal cliques in a graph. Each call to Iterator.next() returns a maximal clique in the form of list of nodes. This iterator does not support remove.

Parameters:
graph - a graph, must not have loop edges
Returns:
an iterator on the maximal cliques of graph
Throws:
IllegalArgumentException - if graph has loop edges
Computational Complexity :
This iterator implements the Bron–Kerbosch algorithm. There is no guarantee that each call to Iterator.next() will run in polynomial time. However, iterating over all the maximal cliques is efficient in worst case sense. The whole iteration takes O(3n/3) time in the worst case and it is known that a n-node graph has at most 3n/3 maximal cliques.

getMaximalCliques

public static <T extends org.graphstream.graph.Node> Iterable<List<T>> getMaximalCliques(org.graphstream.graph.Graph graph)
An iterable view of the set of all the maximal cliques in a graph. Uses getMaximalCliqueIterator(Graph).

Parameters:
graph - a graph
Returns:
An iterable view of the maximal cliques in graph.

getDegeneracy

public static <T extends org.graphstream.graph.Node> int getDegeneracy(org.graphstream.graph.Graph graph,
                                                                       List<T> ordering)

This method computes the gedeneracy and the degeneracy ordering of a graph.

The degeneracy of a graph is the smallest number d such that every subgraph has a node with degree d or less. The degeneracy is a measure of sparseness of graphs. A degeneracy ordering is an ordering of the nodes such that each node has at most d neighbors following it in the ordering. The degeneracy ordering is used, among others, in greedy coloring algorithms.

Parameters:
graph - a graph
ordering - a list of nodes. If not null, this list is first cleared and then filled with the nodes of the graph in degeneracy order.
Returns:
the degeneracy of graph
Computational Complexity :
O(m) where m is the number of edges in the graph


Copyright © 2011. All Rights Reserved.