Class AdjListsGraph

java.lang.Object
  extended by Graph
      extended by AdjListsGraph

public class AdjListsGraph
extends Graph

This is a graph implementation that uses adjacency lists to store edges. It contains two linked-lists for every node i of the graph. The first list for node i, the "from i" list, contains edges from i to other nodes. The second list for node i, the "to i" list, contains edges from other nodes to i. The lists are not sorted; they contain the adjacent nodes in the order of edge insertion. In other words, the edge at the tail of the "from i" list of some node i corresponds to the edge *with source* i that was added to the graph most recently (and has not been removed, yet) and the edge at the tail of the "to i" list of some node i corresponds to the edge *with target* i that was added to the graph most recently (and has not been removed, yet).


Field Summary
protected  java.util.Vector<java.util.LinkedList<Edge>> fromLists
          The vector of "from i" adjacency lists.
protected  java.util.Vector<java.util.LinkedList<Edge>> toLists
          The vector of "to i" adjacency lists.
 
Fields inherited from class Graph
nNodes
 
Constructor Summary
AdjListsGraph(int nodes)
          Class Constructor.
 
Method Summary
 boolean addEdge(Edge e)
          This is method adds safely an edge to the graph, when the edge is given as an object.
 boolean addEdgeUnsafely(Edge e)
          This is method adds an edge to the graph, when the edge is given as an object.
 java.util.LinkedList<Edge> getEdges(int i)
          This is method returns the edges starting from a source node.
 java.util.LinkedList<Edge> getEdges(int source, int target)
          This is method returns the edges between a source node and a target node.
 void removeAllEdges()
          This method removes all edges in the graph.
 boolean removeEdge(Edge e)
          This method removes an edge given as an object.
 Graph toAltGraphRepr(Graph g)
          If G has the same number of nodes as this graph then the method copies this graph to graph G.
 java.lang.String toString()
           
 
Methods inherited from class Graph
getNNodes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

fromLists

protected java.util.Vector<java.util.LinkedList<Edge>> fromLists
The vector of "from i" adjacency lists.


toLists

protected java.util.Vector<java.util.LinkedList<Edge>> toLists
The vector of "to i" adjacency lists.

Constructor Detail

AdjListsGraph

public AdjListsGraph(int nodes)
Class Constructor. It initializes the number of nodes, and instantiates the vectors of adjacency lists so that each list is empty.

Parameters:
nodes - the number of nodes.
Method Detail

addEdge

public boolean addEdge(Edge e)
Description copied from class: Graph
This is method adds safely an edge to the graph, when the edge is given as an object. If an edge connecting the same source node with the same target *and* has the same id exists, the edge is not added to the graph. An IndexOutOfBoundsException is thrown if the source or target of the edge do not correspond to valid node numbers.

Specified by:
addEdge in class Graph
Parameters:
e - the edge to be added.
Returns:
true if the edge is successfully added to the graph, and false otherwise.

addEdgeUnsafely

public boolean addEdgeUnsafely(Edge e)
Description copied from class: Graph
This is method adds an edge to the graph, when the edge is given as an object. This method does not check if the edge already exists in the graph. This is to be used for efficiency when it is a priori known that the edge to be inserted does not already exist. An IndexOutOfBoundsException is thrown if the source or target of the edge do not correspond to valid node numbers.

Specified by:
addEdgeUnsafely in class Graph
Parameters:
e - the edge to be added.
Returns:
true if the edge is successfully added to the graph, and false otherwise.

getEdges

public java.util.LinkedList<Edge> getEdges(int source,
                                           int target)
Description copied from class: Graph
This is method returns the edges between a source node and a target node. The edges are returned in the form of a linked list of edge objects. If there are no edges between the source and target nodes, an empty list is returned. An IndexOutOfBoundsException is thrown if the source or target of the edge do not correspond to valid node numbers.

Specified by:
getEdges in class Graph
Parameters:
source - the source node.
target - the target node.
Returns:
A linked list containing the edges between the source and the target.

getEdges

public java.util.LinkedList<Edge> getEdges(int i)
Description copied from class: Graph
This is method returns the edges starting from a source node. The edges are returned in the form of a linked list of edge objects. If there are no edges adjacent to the the source node, an empty list is returned. An IndexOutOfBoundsException is thrown if the source node does not correspond to a valid node number.

Specified by:
getEdges in class Graph
Parameters:
i - the source node.
Returns:
A linked list containing the edges adjacent to the source.

removeEdge

public boolean removeEdge(Edge e)
Description copied from class: Graph
This method removes an edge given as an object. The removal happens as follows: if the graph contains an edge with the same id from source to target node, then this edge is removed and true is returned. Otherwise, the graph is not modified and false is returned. An IndexOutOfBoundsException is thrown if the source or target of the edge do not correspond to valid node numbers

Specified by:
removeEdge in class Graph
Parameters:
e - the edge to remove.
Returns:
true if the edge is succesfully removed

removeAllEdges

public void removeAllEdges()
Description copied from class: Graph
This method removes all edges in the graph.

Specified by:
removeAllEdges in class Graph

toString

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

toAltGraphRepr

public Graph toAltGraphRepr(Graph g)
Description copied from class: Graph
If G has the same number of nodes as this graph then the method copies this graph to graph G. Otherwise, it does nothing. G may be using a different internal representation than this graph. For efficiency, this method should use only the unsafe version of the edge addition method (of G).

Specified by:
toAltGraphRepr in class Graph
Parameters:
g - the graph to copy this graph to.
Returns:
graph G