salvo.jesus.graph
Class DirectedGraphImpl

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

public class DirectedGraphImpl
extends GraphImpl
implements DirectedGraph

A directed Graph where edges have a specified direction. Edges in this graph are therefore instances of DirectedEdge.

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

Field Summary
(package private)  DirectedGraphWeakImpl graphDirectionDelegate
          Delegate object to handle the implementation of the DirectedGraph interface.
 
Fields inherited from class salvo.jesus.graph.GraphImpl
addedgelistener, addvertexlistener, connectedSetS, edges, removeedgelistener, removevertexlistener, traversal, vertices
 
Constructor Summary
DirectedGraphImpl()
          Creates a new instance of an empty directed Graph.
DirectedGraphImpl(DirectedGraphImpl toBeCopied)
           
 
Method Summary
 void add(Vertex newvertex)
          Adds a Vertex into the Graph.
 void addEdge(Edge edge)
          Adds an Edge into the Graph.
 Edge addEdge(Vertex v1, Vertex v2)
          Adds an Edge into the Graph.
 java.lang.Object clone()
           
 Edge createEdge(Vertex v1, Vertex v2)
          Factory method implementation that creates an instance of a DirectedEdge.
 DirectedEdge getEdge(Vertex fromvertex, Vertex tovertex)
          Returns an Edge in the Graph whose origin is fromVertex and destination is toVertex.
 java.util.Collection getIncomingAdjacentVertices(Vertex v)
          Returns the vertices that are adjacent to a specified Vertex where the Edge is incoming from the specified Vertex to the adjacent vertex.
protected  java.util.Map getIncomingEdges()
          Returns the incoming EdgeSets of the Graph.
 java.util.Collection getIncomingEdges(Vertex v)
          Returns the incoming edges of a particular Vertex in the Graph.
 java.util.Collection getOutgoingAdjacentVertices(Vertex v)
          Returns the vertices that are adjacent to a specified Vertex where the Edge is outgoing from the specified Vertex to the adjacent vertex.
protected  java.util.Map getOutgoingEdges()
          Returns the outgoing EdgeSets of the Graph.
 java.util.Collection getOutgoingEdges(Vertex v)
          Returns the outgoing edges of a particular Vertex in the Graph.
 boolean isCycle(Vertex fromVertex)
          Determines if there is a cycle from Vertex fromVertex.
 boolean isPath(Vertex fromVertex, Vertex toVertex)
          Determines if there is a path from Vertex fromVertex to Vertex toVertex.
 void remove(Vertex vertex)
          Removes a Vertex from the Graph.
 void removeEdge(Edge edge)
          Removes an Edge from the Graph.
 java.lang.String toString()
          Returns a String representation of the Graph.
 
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.Graph
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
 

Field Detail

graphDirectionDelegate

DirectedGraphWeakImpl graphDirectionDelegate
Delegate object to handle the implementation of the DirectedGraph interface.

Constructor Detail

DirectedGraphImpl

public DirectedGraphImpl()
Creates a new instance of an empty directed Graph. The default GraphTraversal object is an instance of DepthFirstDirectedGraphTraversal, a depth-first traversal respecting the direction of edges.


DirectedGraphImpl

public DirectedGraphImpl(DirectedGraphImpl toBeCopied)
Method Detail

clone

public java.lang.Object clone()
Overrides:
clone in class GraphImpl

getOutgoingEdges

protected java.util.Map getOutgoingEdges()
Returns the outgoing EdgeSets of the Graph. Each element in the return Vector is of type EdgeSet. The index of a Vertex in the vertices Vector matches the Vertex's EdgeSet index in the outgoingEdges Vector.

Returns:
Vector containing the outgoing EdgeSets in the Graph. This simply returns this.adjacentEdges.

getIncomingEdges

protected java.util.Map getIncomingEdges()
Returns the incoming EdgeSets of the Graph. Each element in the return Vector is of type EdgeSet. The index of a Vertex in the vertices Vector matches the Vertex's EdgeSet index in the incomingEdges Vector.

Returns:
Vector containing the incoming EdgeSets in the Graph. This simply returns this.adjacentEdges.

getOutgoingEdges

public java.util.Collection getOutgoingEdges(Vertex v)
Returns the outgoing edges of a particular Vertex in the Graph.

Specified by:
getOutgoingEdges in interface DirectedGraph
Parameters:
v - Vertex you want to determine its outgoing edges.
Returns:
Vector of outgoing edges of the specified Vertex.

getIncomingEdges

public java.util.Collection getIncomingEdges(Vertex v)
Returns the incoming edges of a particular Vertex in the Graph.

Specified by:
getIncomingEdges in interface DirectedGraph
Parameters:
v - Vertex you want to determine its incoming edges.
Returns:
Vector of incoming edges of the specified Vertex.

getOutgoingAdjacentVertices

public java.util.Collection getOutgoingAdjacentVertices(Vertex v)
Returns the vertices that are adjacent to a specified Vertex where the Edge is outgoing from the specified Vertex to the adjacent vertex.

Specified by:
getOutgoingAdjacentVertices in interface DirectedGraph
Parameters:
v - Vertex you want to determine its outgoing adjacent vertices.
Returns:
Vector of outgoing vertices adjacent to the specified Vertex.

getIncomingAdjacentVertices

public java.util.Collection getIncomingAdjacentVertices(Vertex v)
Returns the vertices that are adjacent to a specified Vertex where the Edge is incoming from the specified Vertex to the adjacent vertex.

Specified by:
getIncomingAdjacentVertices in interface DirectedGraph
Parameters:
v - Vertex you want to determine its incoming adjacent vertices.
Returns:
Vector of incoming vertices adjacent to the specified Vertex.

getEdge

public DirectedEdge getEdge(Vertex fromvertex,
                            Vertex tovertex)
Returns an Edge in the Graph whose origin is fromVertex and destination is toVertex. If there is more than one Edge that has the same origin and destination in the Graph, the first matching Edge is returned.

Specified by:
getEdge in interface DirectedGraph
Parameters:
fromVertex - Vertex that is the origin of the directed Edge
toVertex - Vertex that is the destination of the directed Edge
Returns:
Edge whose origin is fromVertex and destination is toVertex
See Also:
Edge

add

public void add(Vertex newvertex)
         throws java.lang.Exception
Adds a Vertex into the Graph. This will also create a new entry in the edges Vector and add the newly added Vertex to its own connected set, thereby adding a new Vector in the connectedSet Vector. Finally, all GraphAddVertexListeners are informed of the event that a Vertex has been added to the Graph.

Specified by:
add in interface Graph
Overrides:
add in class GraphImpl
Parameters:
newvertex - The Vertex object to be added to the graph.
Throws:
java.lang.Exception

createEdge

public Edge createEdge(Vertex v1,
                       Vertex v2)
Factory method implementation that creates an instance of a DirectedEdge.

Specified by:
createEdge in interface Graph
Overrides:
createEdge in class GraphImpl
Parameters:
v1 - One endpoint of the vertex
v2 - The other endpoint of the vertex

addEdge

public Edge addEdge(Vertex v1,
                    Vertex v2)
             throws java.lang.Exception
Adds an Edge into the Graph. This first creates a new instance of Edge before calling the overloaded method addEdge( Edge ).

Specified by:
addEdge in interface Graph
Overrides:
addEdge in class GraphImpl
Parameters:
fromVertex - Vertex that will be the source of the Edge
toVertex - Vertex that will be the sink of the Edge
Returns:
The Edge object that was created and added to the Graph.
Throws:
java.lang.Exception

addEdge

public void addEdge(Edge edge)
             throws java.lang.Exception
Adds an Edge into the Graph. The vertices of the Edge need not be existing in the Graph for this method to work properly. The vertices in both ends of the Edge are merged into one connected set, thereby possibly decreasing the number of Vectors in the coonectedSet Vector. Finally, all GraphAddEdgeListeners are informed of the event that a Edge has been added to the Graph.

In the event that any one of the vertices are not existing in the Graph, they are added to the Graph.

Note: It is the caller's responsibility to make sure that the type of Edge being added is an DirectedEdgeImpl.

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

removeEdge

public void removeEdge(Edge edge)
                throws java.lang.Exception
Removes an Edge from the Graph.

Specified by:
removeEdge in interface Graph
Overrides:
removeEdge in class GraphImpl
Parameters:
edge - Edge to be removed from the Graph
Throws:
java.lang.Exception

remove

public void remove(Vertex vertex)
            throws java.lang.Exception
Removes a Vertex from the Graph.

Specified by:
remove in interface Graph
Overrides:
remove in class GraphImpl
Parameters:
vertex - Vertex to be removed
Throws:
java.lang.Exception

isPath

public boolean isPath(Vertex fromVertex,
                      Vertex toVertex)
Determines if there is a path from Vertex fromVertex to Vertex toVertex. This will not return true if the only path has at least one Edge pointing in the opposite direction of the path.

Specified by:
isPath in interface DirectedGraph
Parameters:
fromVertex - starting Vertex for the path
toVertex - ending Vertex for the path
Returns:
true if there is a path from Vertex to toVertex. false otherwise.

isCycle

public boolean isCycle(Vertex fromVertex)
Determines if there is a cycle from Vertex fromVertex. A cycle occurs when there is a path from the specified Vertex back to itself, taking into consideration that direction of the Edges along the path. This simply calls isPath(), where both parameters are the same Vertex.

Specified by:
isCycle in interface DirectedGraph
Parameters:
fromVertex - Vertex to be tested for a cycle path.
Returns:
true if there is a cycle path from fromVertex to itself.

toString

public java.lang.String toString()
Returns a String representation of the Graph. The string returned is of the form: super.toString() + "\n" + "Incoming Edges: " + this.incomingEdges.toString() + "\n" + "Outgoing Edges: " + this.outgoingEdges.toString();

Overrides:
toString in class GraphImpl
Returns:
String representation of the Graph