public class ConnectedComponents extends org.graphstream.stream.SinkAdapter implements DynamicAlgorithm, Iterable<ConnectedComponents.ConnectedComponent>
This algorithm computes the connected components for a given graph. Connected components are the set of its connected subgraphs. Two nodes belong to the same connected component when there exists a path (without considering the direction of the edges) between them. Therefore, the algorithm does not consider the direction of the edges. The number of connected components of an undirected graph is equal to the number of connected components of the same directed graph. See wikipedia for details.
This algorithm tries to handle the dynamics of the graph, trying not to recompute all from scratch at each change (kind of re-optimization). In this way, each instance of the algorithm is registered as a graph sink. Each change in the graph topology may affect the algorithm.
To start using the algorithm, you first need an instance of
Graph
, then you only have to instantiate the
algorithm class. Whether you specify a reference to the graph in the
constructor or you set it with the init(Graph)
method.
The computation of the algorithm starts only when the graph is specified with
the init(Graph)
method or with the appropriated constructor. In case
of a static graph, you may call the compute()
method. In case of a
dynamic graph, the algorithm will compute itself automatically when an event
(node or edge added or removed) occurs.
Finally you may ask the algorithm for the number of connected components at
any moment with a call to the getConnectedComponentsCount()
method.
import org.graphstream.algorithm.ConnectedComponents; import org.graphstream.graph.Graph; import org.graphstream.graph.implementations.DefaultGraph; public class CCTest { public static void main(String[] args) { Graph graph = new DefaultGraph("CC Test"); graph.addNode("A"); graph.addNode("B"); graph.addNode("C"); graph.addEdge("AB", "A", "B"); graph.addEdge("AC", "A", "C"); ConnectedComponents cc = new ConnectedComponents(); cc.init(graph); System.out.printf("%d connected component(s) in this graph, so far.%n", cc.getConnectedComponentsCount()); graph.removeEdge("AC"); System.out.printf("Eventually, there are %d.%n", cc .getConnectedComponentsCount()); } }
It is possible to get rid of connected components belong a size threshold
when counting the overall number of connected components. It is also possible
to define a ceiling size for the connected component. Above that size
ceiling, connected components will not be counted. Use the
getConnectedComponentsCount(int)
or
getConnectedComponentsCount(int, int)
methods.
You can tag each node with an integer that identifies the component it
pertains to using setCountAttribute(String)
. The argument of this
method is an arbitrary name that will be used as attribute on each node of
the graph. The value of this attribute will be an integer (counting from
zero) that is different for each connected component.
The getGiantComponent()
method gives you a list of nodes belonging
to the biggest connected component of the graph.
The cut attribute is a feature that can optionally simulate a given edge to
be invisible (as if the edge did not exist). In other words if an edge is
given such a cut attribute, it will be ignored by the algorithm when
counting. You can enable (or disable by passing null) the cut attribute by
specifying it with the setCutAttribute(String)
method, and by giving
the special edges the same attribute.
What is it useful for? Well you may want to simulate the removal of a given edge and see if it increases the number of connected components. You may not want to really remove and then re-add that edge in the graph, because such removal event may have consequences on other algorithms, viewer, writers...
Note that setting the cut attribute will trigger a new computation of the algorithm.
Modifier and Type | Class and Description |
---|---|
class |
ConnectedComponents.ConnectedComponent |
Constructor and Description |
---|
ConnectedComponents()
Construction of an instance with no parameter.
|
ConnectedComponents(org.graphstream.graph.Graph graph)
Constructor with the given graph.
|
Modifier and Type | Method and Description |
---|---|
void |
compute()
Run the algorithm.
|
void |
edgeAdded(String graphId,
long timeId,
String edgeId,
String fromNodeId,
String toNodeId,
boolean directed) |
void |
edgeAttributeAdded(String graphId,
long timeId,
String edgeId,
String attribute,
Object value) |
void |
edgeAttributeRemoved(String graphId,
long timeId,
String edgeId,
String attribute) |
void |
edgeRemoved(String graphId,
long timeId,
String edgeId) |
int |
getConnectedComponentsCount()
Ask the algorithm for the number of connected components.
|
int |
getConnectedComponentsCount(int sizeThreshold)
Ask the algorithm for the number of connected components whose size is
equal to or greater than the specified threshold.
|
int |
getConnectedComponentsCount(int sizeThreshold,
int sizeCeiling)
Ask the algorithm for the number of connected components whose size is
equal to or greater than the specified threshold and lesser than the
specified ceiling.
|
List<org.graphstream.graph.Node> |
getGiantComponent()
Computes a list of nodes that belong to the biggest connected component.
|
void |
graphCleared(String graphId,
long timeId) |
void |
init(org.graphstream.graph.Graph graph)
Initialization of the algorithm.
|
Iterator<ConnectedComponents.ConnectedComponent> |
iterator() |
void |
nodeAdded(String graphId,
long timeId,
String nodeId) |
void |
nodeRemoved(String graphId,
long timeId,
String nodeId) |
void |
setCountAttribute(String countAttribute)
Enable (or disable by passing null for countAttribute) an optional
attribute that will be assigned to each node.
|
void |
setCutAttribute(String cutAttribute)
Enable (or disable by passing null) an optional attribute that makes
edges that have it invisible (as if the edge did not existed).
|
void |
terminate()
Terminate the dynamic algorithm.
|
public ConnectedComponents()
init(Graph)
method with a reference to
a graph so that the computation is able to start.
After the init(Graph)
method is invoked, the computation starts
as soon as and event is received or if the compute()
method is
invoked.public ConnectedComponents(org.graphstream.graph.Graph graph)
init(Graph)
method is invoked. This Constructor
will call the init(Graph)
method anyway.graph
- The graph who's connected components will be computed.public List<org.graphstream.graph.Node> getGiantComponent()
public int getConnectedComponentsCount()
public int getConnectedComponentsCount(int sizeThreshold)
sizeThreshold
- Minimum size for the connected component to be consideredpublic int getConnectedComponentsCount(int sizeThreshold, int sizeCeiling)
sizeThreshold
- Minimum size for the connected component to be consideredsizeCeiling
- Maximum size for the connected component to be considered (use
0 or lower values to ignore the ceiling)public Iterator<ConnectedComponents.ConnectedComponent> iterator()
iterator
in interface Iterable<ConnectedComponents.ConnectedComponent>
public void setCutAttribute(String cutAttribute)
cutAttribute
- The name for the cut attribute or null if the cut attribute
option must be disabled.public void setCountAttribute(String countAttribute)
countAttribute
- The name of the attribute to put on each node (pass null to
disable this feature).public void init(org.graphstream.graph.Graph graph)
Algorithm
Algorithm.compute()
method to initialize or reset the algorithm according
to the new given graph.public void compute()
Algorithm
Algorithm.init(Graph)
method has to be called
before computing.compute
in interface Algorithm
Algorithm.init(Graph)
public void terminate()
DynamicAlgorithm
terminate
in interface DynamicAlgorithm
Algorithm.init(org.graphstream.graph.Graph)
public void edgeAdded(String graphId, long timeId, String edgeId, String fromNodeId, String toNodeId, boolean directed)
edgeAdded
in interface org.graphstream.stream.ElementSink
edgeAdded
in class org.graphstream.stream.SinkAdapter
public void nodeAdded(String graphId, long timeId, String nodeId)
nodeAdded
in interface org.graphstream.stream.ElementSink
nodeAdded
in class org.graphstream.stream.SinkAdapter
public void edgeRemoved(String graphId, long timeId, String edgeId)
edgeRemoved
in interface org.graphstream.stream.ElementSink
edgeRemoved
in class org.graphstream.stream.SinkAdapter
public void nodeRemoved(String graphId, long timeId, String nodeId)
nodeRemoved
in interface org.graphstream.stream.ElementSink
nodeRemoved
in class org.graphstream.stream.SinkAdapter
public void graphCleared(String graphId, long timeId)
graphCleared
in interface org.graphstream.stream.ElementSink
graphCleared
in class org.graphstream.stream.SinkAdapter
public void edgeAttributeAdded(String graphId, long timeId, String edgeId, String attribute, Object value)
edgeAttributeAdded
in interface org.graphstream.stream.AttributeSink
edgeAttributeAdded
in class org.graphstream.stream.SinkAdapter
Copyright © 2015. All rights reserved.