@NotThreadSafe public class DirectedGraph<VertexT> extends Object
This class is not a general-purpose graph implementation: it is only intended for
clients that will build a graph, sort it, and then immediately throw it away. In particular, once
topologicalSort() has been invoked, it is not possible to add vertices or sort again.
| Constructor and Description |
|---|
DirectedGraph(Collection<VertexT> vertices) |
| Modifier and Type | Method and Description |
|---|---|
void |
addEdge(VertexT from,
VertexT to) |
List<VertexT> |
topologicalSort()
Performs a topological sort of the graph: for every directed edge
from->to, from will come before to in the resulting list. |
public DirectedGraph(Collection<VertexT> vertices)
public void addEdge(VertexT from, VertexT to)
IllegalStateException - if topologicalSort() was invoked beforeIllegalArgumentException - if from or to is not part of the vertices
passed to the constructorpublic List<VertexT> topologicalSort()
from->to, from will come before to in the resulting list.
Implementation note: this is based on Kahn's algorithm, the running time is linear in the number of nodes plus the number of edges.
IllegalStateException - if this method was already invoked on this graph beforeIllegalArgumentException - if the graph has a cycleCopyright © 2021. All rights reserved.