Class DfsGraph<T>
java.lang.Object
com.thewinterframework.utils.graph.DfsGraph<T>
- Type Parameters:
T- the type of nodes in the graph
- All Implemented Interfaces:
Iterable<T>
A simple directed graph implementation using Depth-First Search (DFS) for topological sorting.
This class allows adding nodes and dependencies, and provides an ordered list of nodes
based on their dependencies.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a dependency indicating that 'after' should come after 'before'voidAdds a dependency indicating that 'before' should come before 'after'voidAdds a node to the graphiterator()Returns an iterator over the nodes in topological order.ordered()Returns a list of nodes ordered based on their dependencies using DFS.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
DfsGraph
public DfsGraph()
-
-
Method Details
-
addNode
Adds a node to the graph- Parameters:
node- the node to be added
-
addAfter
Adds a dependency indicating that 'after' should come after 'before'- Parameters:
after- - the node that comes afterbefore- - the node that comes before
-
addBefore
Adds a dependency indicating that 'before' should come before 'after'- Parameters:
before- - the node that comes beforeafter- - the node that comes after
-
ordered
Returns a list of nodes ordered based on their dependencies using DFS.- Returns:
- a list of nodes in topological order
- Throws:
IllegalStateException- if a cycle is detected in the graph
-
iterator
Returns an iterator over the nodes in topological order.
-