edu.princeton.cs.algs4
Class KosarajuSharirSCC
- java.lang.Object
-
- edu.princeton.cs.algs4.KosarajuSharirSCC
-
public class KosarajuSharirSCC extends java.lang.ObjectTheKosarajuSharirSCCclass represents a data type for determining the strong components in a digraph. The id operation determines in which strong component a given vertex lies; the areStronglyConnected operation determines whether two vertices are in the same strong component; and the count operation determines the number of strong components. The component identifier of a component is one of the vertices in the strong component: two vertices have the same component identifier if and only if they are in the same strong component.This implementation uses the Kosaraju-Sharir algorithm. 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, and areStronglyConnected operations take constant time. For alternate implementations of the same API, see
TarjanSCCandGabowSCC.For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
Constructor Summary
Constructors Constructor and Description KosarajuSharirSCC(Digraph G)Computes the strong components of the digraphG.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description intcount()Returns the number of strong components.intid(int v)Returns the component id of the strong component containing vertexv.static voidmain(java.lang.String[] args)Unit tests theKosarajuSharirSCCdata type.booleanstronglyConnected(int v, int w)Are verticesvandwin the same strong component?
-
-
-
Constructor Detail
-
KosarajuSharirSCC
public KosarajuSharirSCC(Digraph G)
Computes the strong components of the digraphG.- Parameters:
G- the digraph
-
-
Method Detail
-
count
public int count()
Returns the number of strong components.- Returns:
- the number of strong components
-
stronglyConnected
public boolean stronglyConnected(int v, int w)Are verticesvandwin the same strong component?- Parameters:
v- one vertexw- the other vertex- Returns:
trueif verticesvandware in the same strong component, andfalseotherwise- Throws:
java.lang.IllegalArgumentException- unless0 <= v < Vjava.lang.IllegalArgumentException- unless0 <= w < V
-
id
public int id(int v)
Returns the component id of the strong component containing vertexv.- Parameters:
v- the vertex- Returns:
- the component id of the strong component containing vertex
v - Throws:
java.lang.IllegalArgumentException- unless0 <= s < V
-
main
public static void main(java.lang.String[] args)
Unit tests theKosarajuSharirSCCdata type.- Parameters:
args- the command-line arguments
-
-
DataMelt 3.0 © DataMelt by jWork.ORG