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:
java.lang.Iterable<T>
public class DirectedAcyclicGraph<T>
extends java.lang.Object
implements java.lang.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 Constructor Description DirectedAcyclicGraph()
Constructs a DAG with no nodes. -
Method Summary
Modifier and Type Method Description DirectedAcyclicGraph<T>
add(T before, T after)
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.java.lang.Iterable<DirectedEdge<T>>
edges()
Returns anIterable
of all the edges in this graph.java.util.Iterator<T>
iterator()
boolean
path(T before, T after)
Checks whether or not a path exists from one node to another in this DAG.java.lang.String
toString()
-
Constructor Details
-
DirectedAcyclicGraph
public DirectedAcyclicGraph()Constructs a DAG with no nodes.
-
-
Method Details
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.lang.Object
-
edges
Returns anIterable
of 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
- Specified by:
iterator
in interfacejava.lang.Iterable<T>
-