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>

public class DirectedAcyclicGraph<T> extends Object implements 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 Details

    • DirectedAcyclicGraph

      public DirectedAcyclicGraph()
      Constructs a DAG with no nodes.
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • edges

      public Iterable<DirectedEdge<T>> edges()
      Returns an Iterable of all the edges in this graph.
      Returns:
      all edges
    • add

      public 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.
      Parameters:
      before - the tail of the new directed edge
      after - the head of the new directed edge
      Returns:
      a new DAG with this edge, or null if this edge would create a cycle
    • path

      public boolean path(T before, T after)
      Checks whether or not a path exists from one node to another in this DAG.
      Parameters:
      before - the node at which the path should start
      after - the node at which the path should end
      Returns:
      true if such a path was found, false otherwise
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>