Package edu.uky.ai.util
Class DirectedAcyclicGraph<T>
java.lang.Object
edu.uky.ai.util.DirectedAcyclicGraph<T>
- Type Parameters:
T- the type of object used as nodes in this DAG
- All Implemented Interfaces:
Iterable<T>
A directed acyclic graph (or DAG) whose nodes are of a given type. This
data structure is immutable, meaning that methods which would modify it
instead return a copy of the object with the changes reflected in that copy.
For the sake of efficiency, modifications which would return an invalid DAG
(e.g. adding a cycle) return null instead of throwing an exception.
- Author:
- Stephen G. Ware
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns a new DAG with a new edge from 'before' to 'after.' If either node does not exist, they will exist in the new DAG.edges()Returns anIterableof all the edges in this graph.iterator()booleanChecks whether or not a path exists from one node to another in this DAG.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
DirectedAcyclicGraph
public DirectedAcyclicGraph()Constructs a DAG with no nodes.
-
-
Method Details
-
toString
-
edges
Returns anIterableof all the edges in this graph.- Returns:
- all edges
-
add
Returns a new DAG with a new edge from 'before' to 'after.' If either node does not exist, they will exist in the new DAG.- Parameters:
before- the tail of the new directed edgeafter- the head of the new directed edge- Returns:
- a new DAG with this edge, or null if this edge would create a cycle
-
path
Checks whether or not a path exists from one node to another in this DAG.- Parameters:
before- the node at which the path should startafter- the node at which the path should end- Returns:
- true if such a path was found, false otherwise
-
iterator
-