edu.princeton.cs.algs4
Class CC
- java.lang.Object
-
- edu.princeton.cs.algs4.CC
-
public class CC extends java.lang.ObjectTheCCclass represents a data type for determining the connected components in an undirected graph. The id operation determines in which connected component a given vertex lies; the connected operation determines whether two vertices are in the same connected component; the count operation determines the number of connected components; and the size operation determines the number of vertices in the connect component containing a given vertex. The component identifier of a connected component is one of the vertices in the connected component: two vertices have the same component identifier if and only if they are in the same connected component.This implementation uses depth-first search. The constructor takes time proportional to V + E (in the worst case), where V is the number of vertices and E is the number of edges. Afterwards, the id, count, connected, and size operations take constant time.
For additional documentation, see Section 4.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
Constructor Summary
Constructors Constructor and Description CC(EdgeWeightedGraph G)Computes the connected components of the edge-weighted graphG.CC(Graph G)Computes the connected components of the undirected graphG.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description booleanareConnected(int v, int w)Deprecated.Replaced byconnected(int, int).booleanconnected(int v, int w)Returns true if verticesvandware in the same connected component.intcount()Returns the number of connected components in the graphG.intid(int v)Returns the component id of the connected component containing vertexv.static voidmain(java.lang.String[] args)Unit tests theCCdata type.intsize(int v)Returns the number of vertices in the connected component containing vertexv.
-
-
-
Constructor Detail
-
CC
public CC(Graph G)
Computes the connected components of the undirected graphG.- Parameters:
G- the undirected graph
-
CC
public CC(EdgeWeightedGraph G)
Computes the connected components of the edge-weighted graphG.- Parameters:
G- the edge-weighted graph
-
-
Method Detail
-
id
public int id(int v)
Returns the component id of the connected component containing vertexv.- Parameters:
v- the vertex- Returns:
- the component id of the connected component containing vertex
v - Throws:
java.lang.IllegalArgumentException- unless0 <= v < V
-
size
public int size(int v)
Returns the number of vertices in the connected component containing vertexv.- Parameters:
v- the vertex- Returns:
- the number of vertices in the connected component containing vertex
v - Throws:
java.lang.IllegalArgumentException- unless0 <= v < V
-
count
public int count()
Returns the number of connected components in the graphG.- Returns:
- the number of connected components in the graph
G
-
connected
public boolean connected(int v, int w)Returns true if verticesvandware in the same connected component.- Parameters:
v- one vertexw- the other vertex- Returns:
trueif verticesvandware in the same connected component;falseotherwise- Throws:
java.lang.IllegalArgumentException- unless0 <= v < Vjava.lang.IllegalArgumentException- unless0 <= w < V
-
areConnected
@Deprecated public boolean areConnected(int v, int w)Deprecated. Replaced byconnected(int, int).Returns true if verticesvandware in the same connected component.- Parameters:
v- one vertexw- the other vertex- Returns:
trueif verticesvandware in the same connected component;falseotherwise- Throws:
java.lang.IllegalArgumentException- unless0 <= v < Vjava.lang.IllegalArgumentException- unless0 <= w < V
-
main
public static void main(java.lang.String[] args)
Unit tests theCCdata type.- Parameters:
args- the command-line arguments
-
-
DataMelt 3.0 © DataMelt by jWork.ORG