Package edu.princeton.cs.algs4
Class KosarajuSharirSCC
- Object
- 
- edu.princeton.cs.algs4.KosarajuSharirSCC
 
- 
 public class KosarajuSharirSCC extends Object TheKosarajuSharirSCCclass 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 vertex is an integer between 0 and k–1, where k is the number of strong components. 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 Θ(V + E) time, where V is the number of vertices and E is the number of edges. Each instance method takes Θ(1) time. It uses Θ(V) extra space (not including the digraph). For alternative implementations of the same API, see TarjanSCCandGabowSCC.For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. - Author:
- Robert Sedgewick, Kevin Wayne
 
- 
- 
Constructor SummaryConstructors Constructor Description KosarajuSharirSCC(Digraph digraph)Computes the strong components of a digraph.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcount()Returns the number of strong components.intid(int v)Returns the component id of the strong component containing vertexv.static voidmain(String[] args)Unit tests theKosarajuSharirSCCdata type.booleanstronglyConnected(int v, int w)Are verticesvandwin the same strong component?
 
- 
- 
- 
Constructor Detail- 
KosarajuSharirSCCpublic KosarajuSharirSCC(Digraph digraph) Computes the strong components of a digraph.- Parameters:
- digraph- the digraph
 
 
- 
 - 
Method Detail- 
countpublic int count() Returns the number of strong components.- Returns:
- the number of strong components
 
 - 
stronglyConnectedpublic boolean stronglyConnected(int v, int w)Are verticesvandwin the same strong component?- Parameters:
- v- one vertex
- w- the other vertex
- Returns:
- trueif vertices- vand- ware in the same strong component, and- falseotherwise
- Throws:
- IllegalArgumentException- unless- 0 <= v < V
- IllegalArgumentException- unless- 0 <= w < V
 
 - 
idpublic 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:
- IllegalArgumentException- unless- 0 <= s < V
 
 - 
mainpublic static void main(String[] args) Unit tests theKosarajuSharirSCCdata type.- Parameters:
- args- the command-line arguments
 
 
- 
 
-