salvo.jesus.graph
Class GraphImpl

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

public class GraphImpl
extends java.lang.Object
implements Graph

An implementation of the Graph interface. A Graph object represents a graph data structure, which are vertices connected by edges, where the edges are non-directional.

See Also:
Serialized Form

Field Summary
protected  java.util.Vector addedgelistener
          Vector of GraphAddEdgeListeners that are interested in listening when new edges are added to the Graph.
protected  java.util.Vector addvertexlistener
          Vector of GraphAddVertexListeners that are interested in listening when new vertices are added to the Graph.
protected  java.util.Collection connectedSetS
          Collection of Collection (no, that was not a typo!) of vertices that are connected.
protected  java.util.Map edges
          Map of edges in the graph.
protected  java.util.Vector removeedgelistener
          Vector of GraphRemoveEdgeListeners that are interested in listening when edges are removed from the Graph.
protected  java.util.Vector removevertexlistener
          Vector of GraphRemoveVertexListeners that are interested in listening when vertices are removed from the Graph.
protected  GraphTraversal traversal
          Delegate object for implementing graph traversal.
protected  java.util.Collection vertices
          Collection of vertices in the graph.
 
Constructor Summary
GraphImpl()
           
GraphImpl(GraphImpl toBeCloned)
          Copy constructor
 
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.
 void addGraphAddEdgeListener(GraphAddEdgeListener listener)
          Adds a GraphAddEdgeListener to the Graph's internal Vector of GraphAddEdgeListeners so that when a new Edge is added, all registered GraphAddEdgeListeners are notified of the event.
 void addGraphAddVertexListener(GraphAddVertexListener listener)
          Adds a GraphAddVertexListener to the Graph's internal Vector of GraphAddVertexListeners so that when a new Vertex is added, all registered GraphAddVertedListeners are notified of the event.
 void addGraphRemoveEdgeListener(GraphRemoveEdgeListener listener)
          Adds a GraphRemoveEdgeListener to the Graph's internal Vector of GraphRemoveEdgeListeners so that when an Edge is removed, all registered GraphRemoveEdgeListeners are notified of the event.
 void addGraphRemoveVertexListener(GraphRemoveVertexListener listener)
          Adds a GraphRemoveVertexListener to the Graph's internal Vector of GraphRemoveVertexListeners so that when a Vertex is removed, all registered GraphRemoveVertexListeners are notified of the event.
 java.lang.Object clone()
           
 java.util.Collection cloneVertices()
          Returns a clone of the Collection of vertices.
 Edge createEdge(Vertex v1, Vertex v2)
          Method to create the proper type of Edge class.
 java.util.Collection getAdjacentVertices(java.util.Collection vertices)
          Returns the vertices adjacent to all the vertices in the given collection.
 java.util.Collection getAdjacentVertices(Vertex v)
          Returns the vertices adjacent to the specified vertex.
 java.util.Collection getConnectedSet()
          Returns the connected sets in the Graph.
 java.util.Collection getConnectedSet(Vertex v)
          Returns the connected set to which the specified vertex belongs.
 int getDegree()
          Returns the degree of the graph, which is simply the highest degree of all the graph's vertices.
 int getDegree(Vertex v)
          Returns the degree of the vertex, which is simply the number of edges of the vertex.
 java.util.Collection getEdges(Vertex v)
          Returns a Collection of edges of the specified vertex.
 GraphTraversal getTraversal()
          Gets the traversal algorithm used by the Graph.
 java.util.Set getVertices(int degree)
          Returns all vertices with the specified degree.
 int getVerticesCount()
          Returns the number of vertices in the graph
 java.util.Iterator getVerticesIterator()
          Returns an iterator that iterates through the graph's vertices.
 boolean isConnected(Vertex v1, Vertex v2)
          Determines if two vertices are connected
 void mergeconnectedSet(Vertex v1, Vertex v2)
          Merges the connected sets to which Vertex v1 and Vertex v2 belongs, if they are not yet connected.
 void remove(Vertex v)
          Removes the specified Edge from the Graph.
 void removeEdge(Edge edge)
          Removes the specified Edge from the Graph.
 void removeEdges(Vertex v)
          Removes incident Edges of a Vertex.
 void removeGraphAddEdgeListener(GraphAddEdgeListener listener)
          Removes a GraphAddEdgeListener from the Graph's internal Vector of GraphAddEdgeListeners.
 void removeGraphAddVertexListener(GraphAddVertexListener listener)
          Removes a GraphAddVertexListener from the Graph's internal Vector of GraphAddVertexListeners.
 void removeGraphRemoveEdgeListener(GraphRemoveEdgeListener listener)
          Removes a GraphRemoveEdgeListener from the Graph's internal Vector of GraphRemoveEdgeListeners.
 void removeGraphRemoveVertexListener(GraphRemoveVertexListener listener)
          Removes a GraphRemoveVertexListener from the Graph's internal Vector of GraphRemoveVertexListeners.
 void setTraversal(GraphTraversal traversal)
          Sets the graph traversal algorithm to be used
 java.lang.String toString()
          Returns a String representation of the Graph.
 java.util.Vector traverse(Vertex startat)
          Traverses the Graph starting at startat Vertex.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

vertices

protected java.util.Collection vertices
Collection of vertices in the graph.


edges

protected java.util.Map edges
Map of edges in the graph. Each element in the Map is a Map in itself, such that each element is are the incident edges of a Vertex. The index of the Vertex in vertices is the same index as that incident edges of the Vertex in edges.


connectedSetS

protected java.util.Collection connectedSetS
Collection of Collection (no, that was not a typo!) of vertices that are connected. The term connected here means that, if there is an Edge from Vertex A to B, then Vertex A is connected to Vertex B and vice-versa, regardless of the direction of the Edge connecting the two vertices. Therefore, if not all vertices are connected to each other, then you have a forest of graphs. The simplest forest of graphs contains two vertices and no edges, in which case you have a one forest that has only one Vertex and another forest also with only one Vertex. That simplest forest of graphs will be represented here as one Collection containing two Collections, each of which has only one Vertex.


addvertexlistener

protected java.util.Vector addvertexlistener
Vector of GraphAddVertexListeners that are interested in listening when new vertices are added to the Graph.

See Also:
GraphAddVertexListener

addedgelistener

protected java.util.Vector addedgelistener
Vector of GraphAddEdgeListeners that are interested in listening when new edges are added to the Graph.

See Also:
GraphAddEdgeListener

removevertexlistener

protected java.util.Vector removevertexlistener
Vector of GraphRemoveVertexListeners that are interested in listening when vertices are removed from the Graph.

See Also:
GraphRemoveVertexListener

removeedgelistener

protected java.util.Vector removeedgelistener
Vector of GraphRemoveEdgeListeners that are interested in listening when edges are removed from the Graph.

See Also:
GraphRemoveEdgeListener

traversal

protected GraphTraversal traversal
Delegate object for implementing graph traversal. The default implementation is DepthFirstGraphTraversal.

Constructor Detail

GraphImpl

public GraphImpl()

GraphImpl

public GraphImpl(GraphImpl toBeCloned)
Copy constructor

Method Detail

clone

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

getVerticesIterator

public java.util.Iterator getVerticesIterator()
Returns an iterator that iterates through the graph's vertices.

Specified by:
getVerticesIterator in interface Graph
Returns:
An itereator of Collection vertices.

cloneVertices

public java.util.Collection cloneVertices()
Returns a clone of the Collection of vertices.

Specified by:
cloneVertices in interface Graph
Returns:
A clone of the Collection of vertices.

getEdges

public java.util.Collection getEdges(Vertex v)
Returns a Collection of edges of the specified vertex.

Specified by:
getEdges in interface Graph
Parameters:
v - The vertex whose edges we want returned
Returns:
A Collection of Edges that are incident edges of the specified vertex.

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 Collection and add the newly added Vertex to its own connected set, thereby adding a new Collection in the connectedSetS Collection. Finally, all GraphAddVertexListeners are informed of the event that a Vertex has been added to the Graph.

Specified by:
add in interface Graph
Parameters:
newvertex - Vertex to be added to the Graph
Throws:
java.lang.Exception

createEdge

public Edge createEdge(Vertex v1,
                       Vertex v2)
Method to create the proper type of Edge class.

Specified by:
createEdge in interface Graph
Parameters:
v1 - One endpoint of the edge
v2 - Other endpoint of the edge

addEdge

public Edge addEdge(Vertex v1,
                    Vertex v2)
             throws java.lang.Exception
Adds an Edge into the Graph. The vertices of the Edge must already 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 Collections in the coonectedSet Collection. Finally, all GraphAddEdgeListeners are informed of the event that a Edge has been added to the Graph.

Specified by:
addEdge in interface Graph
Parameters:
v1 - One endpoint of the edge
v2 - Other endpoint of the edge
Returns:
The Edge object 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 Collections in the coonectedSet Collection. 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 EdgeImpl.

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

remove

public void remove(Vertex v)
            throws java.lang.Exception
Description copied from interface: Graph
Removes the specified Edge from the Graph.

Specified by:
remove in interface Graph
Throws:
java.lang.Exception

removeEdge

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

Specified by:
removeEdge in interface Graph
Parameters:
edge - The Edge object to be removed.
Throws:
java.lang.Exception

removeEdges

public void removeEdges(Vertex v)
                 throws java.lang.Exception
Removes incident Edges of a Vertex. The Edges removed are those whose either endpoints has the specified vertex. This method is usually called just prior to removing a Vertex from a Graph.

Specified by:
removeEdges in interface Graph
Parameters:
v - Vertex whose Edges are to be removed
Throws:
java.lang.Exception

getVerticesCount

public int getVerticesCount()
Returns the number of vertices in the graph

Specified by:
getVerticesCount in interface Graph
Returns:
The number of vertices in the graph.

getVertices

public java.util.Set getVertices(int degree)
Returns all vertices with the specified degree.

Specified by:
getVertices in interface Graph
Parameters:
degree - The degree of the vertex to be returned.
Returns:
A collection of vertices with the above specified degree.

getAdjacentVertices

public java.util.Collection getAdjacentVertices(Vertex v)
Returns the vertices adjacent to the specified vertex.

Specified by:
getAdjacentVertices in interface Graph
Parameters:
v - The Vertex you want to determine its adjacent vertices.
Returns:
Collection of vertices adjacent to the specified vertex v.

getAdjacentVertices

public java.util.Collection getAdjacentVertices(java.util.Collection vertices)
Returns the vertices adjacent to all the vertices in the given collection.

Specified by:
getAdjacentVertices in interface Graph
Parameters:
vertices - Collection of Vertex where each vertex in the returned Set must be adjacent to.
Returns:
Set of vertices adjacent to all the vertices in the supplied Collection.

getConnectedSet

public java.util.Collection getConnectedSet()
Returns the connected sets in the Graph. Each Collection in the return Collection is a Collection of vertices that are connected to each other, regardless of the direction of the Edge conneting them together.

Specified by:
getConnectedSet in interface Graph
Returns:
Collection of Collection of connected vertices.

getConnectedSet

public java.util.Collection getConnectedSet(Vertex v)
Returns the connected set to which the specified vertex belongs.

Specified by:
getConnectedSet in interface Graph
Parameters:
v - Vertex to which you want the connected set returned.
Returns:
Collection of connected vertices where the specified vertex belongs.

mergeconnectedSet

public void mergeconnectedSet(Vertex v1,
                              Vertex v2)
Merges the connected sets to which Vertex v1 and Vertex v2 belongs, if they are not yet connected. This ma result in decreasing the number of Collections in the connectedSetS Collection.

Specified by:
mergeconnectedSet in interface Graph
Parameters:
v1 - Vertex whose connected set you want merged with the connected set of Vertex v2.
v2 - Vertex whose connected set you want merged with the connected set of Vertex v1.

traverse

public java.util.Vector traverse(Vertex startat)
Traverses the Graph starting at startat Vertex. Only the connected components to which startat belongs to will be traversed.

Specified by:
traverse in interface Graph
Parameters:
startat - The Vertex to which you want to start the traversal.

getTraversal

public GraphTraversal getTraversal()
Gets the traversal algorithm used by the Graph.

Specified by:
getTraversal in interface Graph
Returns:
GraphTraversal object performing traversal for the Graph.

setTraversal

public void setTraversal(GraphTraversal traversal)
Sets the graph traversal algorithm to be used

Specified by:
setTraversal in interface Graph
Parameters:
traversal - A concrete implementation of the GraphTraversal object.

isConnected

public boolean isConnected(Vertex v1,
                           Vertex v2)
Determines if two vertices are connected

Specified by:
isConnected in interface Graph
Parameters:
v1 - starting Vertex for the path
v2 - ending Vertex for the path
Returns:
true if v1 and v2 are connected.

getDegree

public int getDegree()
Returns the degree of the graph, which is simply the highest degree of all the graph's vertices.

Specified by:
getDegree in interface Graph
Returns:
An int indicating the degree of the graph.

getDegree

public int getDegree(Vertex v)
Returns the degree of the vertex, which is simply the number of edges of the vertex.

Specified by:
getDegree in interface Graph
Returns:
The degree of the vertex.

addGraphAddVertexListener

public void addGraphAddVertexListener(GraphAddVertexListener listener)
Adds a GraphAddVertexListener to the Graph's internal Vector of GraphAddVertexListeners so that when a new Vertex is added, all registered GraphAddVertedListeners are notified of the event.

Specified by:
addGraphAddVertexListener in interface Graph
Parameters:
listener - GraphAddVertexListener you want registered or be notified when a new Vertex is added
See Also:
GraphAddVertexListener, removeGraphAddVertexListener( GraphAddVertexListener )

addGraphAddEdgeListener

public void addGraphAddEdgeListener(GraphAddEdgeListener listener)
Adds a GraphAddEdgeListener to the Graph's internal Vector of GraphAddEdgeListeners so that when a new Edge is added, all registered GraphAddEdgeListeners are notified of the event.

Specified by:
addGraphAddEdgeListener in interface Graph
Parameters:
listener - GraphAddEdgeListener you want registered or be notified when a new Edge is added
See Also:
GraphAddEdgeListener, removeGraphAddEdgeListener( GraphAddEdgeListener )

addGraphRemoveEdgeListener

public void addGraphRemoveEdgeListener(GraphRemoveEdgeListener listener)
Adds a GraphRemoveEdgeListener to the Graph's internal Vector of GraphRemoveEdgeListeners so that when an Edge is removed, all registered GraphRemoveEdgeListeners are notified of the event.

Specified by:
addGraphRemoveEdgeListener in interface Graph
Parameters:
listener - GraphRemoveEdgeListener you want registered or be notified when an Edge is removed
See Also:
GraphRemoveEdgeListener, removeGraphRemoveEdgeListener( GraphRemoveEdgeListener )

addGraphRemoveVertexListener

public void addGraphRemoveVertexListener(GraphRemoveVertexListener listener)
Adds a GraphRemoveVertexListener to the Graph's internal Vector of GraphRemoveVertexListeners so that when a Vertex is removed, all registered GraphRemoveVertexListeners are notified of the event.

Specified by:
addGraphRemoveVertexListener in interface Graph
Parameters:
listener - GraphRemoveVertexListener you want registered or be notified when a Vertex is removed
See Also:
GraphRemoveVertexListener, removeGraphRemoveVertexListener( GraphRemoveVertexListener )

removeGraphAddVertexListener

public void removeGraphAddVertexListener(GraphAddVertexListener listener)
Removes a GraphAddVertexListener from the Graph's internal Vector of GraphAddVertexListeners.

Specified by:
removeGraphAddVertexListener in interface Graph
Parameters:
listener - GraphAddVertexListener you no longer want registered or be notified when a Vertex is added
See Also:
GraphAddVertexListener, addGraphAddVertexListener( GraphAddVertexListener )

removeGraphAddEdgeListener

public void removeGraphAddEdgeListener(GraphAddEdgeListener listener)
Removes a GraphAddEdgeListener from the Graph's internal Vector of GraphAddEdgeListeners.

Specified by:
removeGraphAddEdgeListener in interface Graph
Parameters:
listener - GraphAddEdgeListener you no longer want registered or be notified when an Edge is added
See Also:
GraphAddEdgeListener, addGraphAddEdgeListener( GraphAddEdgeListener )

removeGraphRemoveEdgeListener

public void removeGraphRemoveEdgeListener(GraphRemoveEdgeListener listener)
Removes a GraphRemoveEdgeListener from the Graph's internal Vector of GraphRemoveEdgeListeners.

Specified by:
removeGraphRemoveEdgeListener in interface Graph
Parameters:
listener - GraphRemoveEdgeListener you no longer want registered or be notified when an Edge is removed
See Also:
GraphRemoveEdgeListener, addGraphRemoveEdgeListener( GraphRemoveEdgeListener )

removeGraphRemoveVertexListener

public void removeGraphRemoveVertexListener(GraphRemoveVertexListener listener)
Removes a GraphRemoveVertexListener from the Graph's internal Vector of GraphRemoveVertexListeners.

Specified by:
removeGraphRemoveVertexListener in interface Graph
Parameters:
listener - GraphRemoveVertexListener you no longer want registered or be notified when a Vertex is removed
See Also:
GraphRemoveVertexListener, addGraphRemoveVertexListener( GraphRemoveVertexListener )

toString

public java.lang.String toString()
Returns a String representation of the Graph. The string returned in the form: "Vertices: " + this.vertices.toString() + "\n " + "Edges: " + this.edges.toString()

Overrides:
toString in class java.lang.Object
Returns:
String representation of the Graph