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 an Iterable 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()  

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

  • Method Details

    • toString

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

      public java.lang.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 java.util.Iterator<T> iterator()
      Specified by:
      iterator in interface java.lang.Iterable<T>