salvo.jesus.graph
Class DirectedAcyclicGraphImpl

java.lang.Object
  extended by salvo.jesus.graph.GraphImpl
      extended by salvo.jesus.graph.DirectedGraphImpl
          extended by salvo.jesus.graph.DirectedAcyclicGraphImpl
All Implemented Interfaces:
java.io.Serializable, DirectedAcyclicGraph, DirectedGraph, Graph

public class DirectedAcyclicGraphImpl
extends DirectedGraphImpl
implements DirectedAcyclicGraph

The DirectedAcyclicGraph class represents a directed acyclic graph (DAG) where there is no cyclic paths for any of its vertices. A cylic path is a path from a vertex back to itself by following the direction of the edges.

Author:
Jesus M. Salvo Jr.
See Also:
Serialized Form

Field Summary
(package private)  TopologicalSorting topologicalsorting
          Delegate object to handle topological sorting
 
Fields inherited from class salvo.jesus.graph.DirectedGraphImpl
graphDirectionDelegate
 
Fields inherited from class salvo.jesus.graph.GraphImpl
addedgelistener, addvertexlistener, connectedSetS, edges, removeedgelistener, removevertexlistener, traversal, vertices
 
Constructor Summary
DirectedAcyclicGraphImpl()
          Creates a DirectedAcyclicGraph object.
 
Method Summary
 void addEdge(Edge edge)
          Adds an Edge into the DirectedAcyclicGraph.
 Edge addEdge(Vertex fromVertex, Vertex toVertex)
          Adds an Edge into the DirectedAcyclicGraph.
 java.util.Collection getRoot()
          Returns a Vector of vertices that is not depended on by other vertices.
 java.util.Vector reverseTopologicalSort()
          Perform a reverse topological sort of the entire directed acyclic graph.
 java.util.Vector reverseTopologicalSort(Vertex startat)
          Perform a reverse topological sort of the connected set of a directed acyclic graph to which Vertex startat belongs, starting at Vertex startat.
 java.util.Vector topologicalSort()
          Perform a topological sort of the entire directed acyclic graph.
 java.util.Vector topologicalSort(Vertex startat)
          Perform a topological sort of the connected set of a directed acyclic graph to which Vertex startat belongs, starting at Vertex startat.
 
Methods inherited from class salvo.jesus.graph.DirectedGraphImpl
add, clone, createEdge, getEdge, getIncomingAdjacentVertices, getIncomingEdges, getIncomingEdges, getOutgoingAdjacentVertices, getOutgoingEdges, getOutgoingEdges, isCycle, isPath, remove, removeEdge, toString
 
Methods inherited from class salvo.jesus.graph.GraphImpl
addGraphAddEdgeListener, addGraphAddVertexListener, addGraphRemoveEdgeListener, addGraphRemoveVertexListener, cloneVertices, getAdjacentVertices, getAdjacentVertices, getConnectedSet, getConnectedSet, getDegree, getDegree, getEdges, getTraversal, getVertices, getVerticesCount, getVerticesIterator, isConnected, mergeconnectedSet, removeEdges, removeGraphAddEdgeListener, removeGraphAddVertexListener, removeGraphRemoveEdgeListener, removeGraphRemoveVertexListener, setTraversal, traverse
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface salvo.jesus.graph.DirectedGraph
getEdge, getIncomingAdjacentVertices, getIncomingEdges, getOutgoingAdjacentVertices, getOutgoingEdges, isCycle, isPath
 
Methods inherited from interface salvo.jesus.graph.Graph
add, addGraphAddEdgeListener, addGraphAddVertexListener, addGraphRemoveEdgeListener, addGraphRemoveVertexListener, cloneVertices, createEdge, getAdjacentVertices, getAdjacentVertices, getConnectedSet, getConnectedSet, getDegree, getDegree, getEdges, getTraversal, getVertices, getVerticesCount, getVerticesIterator, isConnected, mergeconnectedSet, remove, removeEdge, removeEdges, removeGraphAddEdgeListener, removeGraphAddVertexListener, removeGraphRemoveEdgeListener, removeGraphRemoveVertexListener, setTraversal, traverse
 

Field Detail

topologicalsorting

TopologicalSorting topologicalsorting
Delegate object to handle topological sorting

Constructor Detail

DirectedAcyclicGraphImpl

public DirectedAcyclicGraphImpl()
Creates a DirectedAcyclicGraph object.

Method Detail

addEdge

public Edge addEdge(Vertex fromVertex,
                    Vertex toVertex)
             throws java.lang.Exception
Adds an Edge into the DirectedAcyclicGraph. This will only add the Edge if there is currently no path from the Vertex toVertex to Vertex fromVertex. If there is, there will be a cycle thereby violating the property of a directed acyclic graph.

Specified by:
addEdge in interface Graph
Overrides:
addEdge in class DirectedGraphImpl
Parameters:
fromVertex - Vertex that will be the origin of the Edge that will be added into the Graph.
toVertex - Vertex that will be the destination of the Edge that will be added into the Graph.
Returns:
The Edge object added to the Graph. The Edge object will be an instance of DirectedEdge. If there is already a path from fromVertex to toVertex, then this method returns null.
Throws:
java.lang.Exception

addEdge

public void addEdge(Edge edge)
             throws java.lang.Exception
Adds an Edge into the DirectedAcyclicGraph. This will only add the Edge if there is currently no path from the Vertex toVertex to Vertex fromVertex of the edge being added. If there is, there will be a cycle thereby violating the property of a directed acyclic graph.

Specified by:
addEdge in interface Graph
Overrides:
addEdge in class DirectedGraphImpl
Parameters:
e - The edge to be added to the Graph.
Throws:
java.lang.Exception

getRoot

public java.util.Collection getRoot()
Returns a Vector of vertices that is not depended on by other vertices. That is, a Vector of vertices where there are no Edges pointing to it.

Specified by:
getRoot in interface DirectedAcyclicGraph
Returns:
Vector of vertices

topologicalSort

public java.util.Vector topologicalSort()
Perform a topological sort of the entire directed acyclic graph. Note that the sequence of vertices in the return Vector will not distinguish between connected components of the graph. This method is not part of the GraphTraversal abstract class, but is added here for convenience.

Specified by:
topologicalSort in interface DirectedAcyclicGraph
Returns:
Vector containing the sequence of the vertices visited in the entire directed acyclic graph, regardless of the connected components of the graph.
See Also:
DirectedAcyclicGraph.reverseTopologicalSort()

reverseTopologicalSort

public java.util.Vector reverseTopologicalSort()
Perform a reverse topological sort of the entire directed acyclic graph. Note that the sequence of vertices in the return Vector will not distinguish between connected components of the graph. This method is not part of the GraphTraversal abstract class, but is added here for convenience.

Specified by:
reverseTopologicalSort in interface DirectedAcyclicGraph
Returns:
Vector containing the sequence of the vertices visited in the entire directed acyclic graph, regardless of the connected components of the graph.
See Also:
DirectedAcyclicGraph.topologicalSort()

topologicalSort

public java.util.Vector topologicalSort(Vertex startat)
Perform a topological sort of the connected set of a directed acyclic graph to which Vertex startat belongs, starting at Vertex startat.

Specified by:
topologicalSort in interface DirectedAcyclicGraph
Parameters:
startat - The Vertex to which you want to start the traversal.
Returns:
A Vector of vertices in the order that they were visited.

reverseTopologicalSort

public java.util.Vector reverseTopologicalSort(Vertex startat)
Perform a reverse topological sort of the connected set of a directed acyclic graph to which Vertex startat belongs, starting at Vertex startat.

Specified by:
reverseTopologicalSort in interface DirectedAcyclicGraph
Parameters:
startat - The Vertex to which you want to start the traversal.
Returns:
A Vector of vertices in the order that they were visited.