Package edu.princeton.cs.algs4
Class TopologicalX
- Object
-
- edu.princeton.cs.algs4.TopologicalX
-
public class TopologicalX extends Object
TheTopologicalXclass represents a data type for determining a topological order of a directed acyclic graph (DAG). A digraph has a topological order if and only if it is a DAG. The hasOrder operation determines whether the digraph has a topological order, and if so, the order operation returns one.This implementation uses a nonrecursive, queue-based algorithm. The constructor takes Θ(V + E) time in the worst case, 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).
See
DirectedCycle,DirectedCycleX, andEdgeWeightedDirectedCycleto compute a directed cycle if the digraph is not a DAG. SeeTopologicalfor a recursive version that uses depth-first search.For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description TopologicalX(Digraph digraph)Determines whetherdigraphhas a topological order and, if so, finds such a topological order.TopologicalX(EdgeWeightedDigraph digraph)Determines whether the edge-weighteddigraphhas a topological order and, if so, finds such a topological order.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanhasOrder()Does the digraph have a topological order?static voidmain(String[] args)Unit tests theTopologicalXdata type.Iterable<Integer>order()Returns a topological order if the digraph has a topological order, andnullotherwise.intrank(int v)The rank of vertexvin the topological order; -1 if the digraph is not a DAG
-
-
-
Constructor Detail
-
TopologicalX
public TopologicalX(Digraph digraph)
Determines whetherdigraphhas a topological order and, if so, finds such a topological order.- Parameters:
digraph- the digraph
-
TopologicalX
public TopologicalX(EdgeWeightedDigraph digraph)
Determines whether the edge-weighteddigraphhas a topological order and, if so, finds such a topological order.- Parameters:
digraph- the digraph
-
-
Method Detail
-
order
public Iterable<Integer> order()
Returns a topological order if the digraph has a topological order, andnullotherwise.- Returns:
- a topological order of the vertices (as an iterable) if the
digraph has a topological order (or equivalently, if the digraph is a DAG),
and
nullotherwise
-
hasOrder
public boolean hasOrder()
Does the digraph have a topological order?- Returns:
trueif the digraph has a topological order (or equivalently, if the digraph is a DAG), andfalseotherwise
-
rank
public int rank(int v)
The rank of vertexvin the topological order; -1 if the digraph is not a DAG- Parameters:
v- vertex- Returns:
- the position of vertex
vin a topological order of the digraph; -1 if the digraph is not a DAG - Throws:
IllegalArgumentException- unless0 <= v < V
-
main
public static void main(String[] args)
Unit tests theTopologicalXdata type.- Parameters:
args- the command-line arguments
-
-