Class ConnectedComponents

java.lang.Object
  |
  +--ConnectedComponents

public class ConnectedComponents
extends java.lang.Object

A ConnectedComponents maintains information about the connected components of an undirected graph. As such it is essentially the same as a general purpose union-find algorithm, with the added feature that it can return the least element in a set (i.e., in a connected component).

The algorithm uses a union by size method combined with path compression.

It supports the following operations.

ConnectedComponents( int N ) starts a new graph on the vertices 0, 1, 2, ..., N-1 with no edges.

addEdge( int i, int j ) adds a new edge.

areConnected(int i, int j) returns true if the vertices i and j are connected by a path of edges in the graph.

minConnected( int i ) finds the smallest numbered vertex which is connected to vertex i.

getNumPointerTraversals() is used to gather statistics about the traversal pattern.


Field Summary
protected  int numPointerTraversals
          The total number of pointer traversals
 
Constructor Summary
ConnectedComponents(int N)
          Constructor for a ConnectedComponents object which is initially for a total disconnected graph on N vertices.
 
Method Summary
 int addEdge(int i, int j)
          Modifies the graph by adding an (undirected) edge between vertices i and j.
 boolean areConnected(int i, int j)
          Tests whether two vertices are in the same connected component.
 int getNumPointerTraversals()
          Returns the total number of pointer traversals.
 int minConnected(int i)
          Find the minumum numbered vertex which is in the same connected component as i.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numPointerTraversals

protected int numPointerTraversals
The total number of pointer traversals
Constructor Detail

ConnectedComponents

public ConnectedComponents(int N)
Constructor for a ConnectedComponents object which is initially for a total disconnected graph on N vertices. The vertices in the graph are numbered from 0 to N-1.
Parameters:
N - the number of vertices in the graph.
Method Detail

addEdge

public int addEdge(int i,
                   int j)
Modifies the graph by adding an (undirected) edge between vertices i and j. The connected components are updated accordingly.
Parameters:
i - the first vertex
j - the second vertex
Returns:
a nonnegative number if the two vertices were not already in the same connected component, and a negative number if they were already in the same connected component. The nonnegative number will be the minumum numbered vertex in the new combined component. The negative number will equal -(N+1) where N is the minimum vertex number in the connected component.

minConnected

public int minConnected(int i)
Find the minumum numbered vertex which is in the same connected component as i.
Parameters:
i - a vertex number
Returns:
the minumum number vertex connected to i.

areConnected

public boolean areConnected(int i,
                            int j)
Tests whether two vertices are in the same connected component.
Parameters:
i - a vertex number
j - a vertex number
Returns:
true iff the vertices are in the same connected component.

getNumPointerTraversals

public int getNumPointerTraversals()
Returns the total number of pointer traversals. This value is updated by calls to minConnected, calls to areConnected and calls to addEdge.
Returns:
the total number of pointer traversals.