Class AdjMatrixGraph

java.lang.Object
  extended by Graph
      extended by AdjMatrixGraph

public class AdjMatrixGraph
extends Graph

This is a graph implementation that uses an adjacency matrix to represent edges. The [i,j] element of the adjacency matrix contains a linked list with all the directed edges between nodes i and j.


Field Summary
protected  java.util.Vector<java.util.Vector<java.util.LinkedList<Edge>>> adjMtrx
          The adjacency matrix;
 
Fields inherited from class Graph
nNodes
 
Constructor Summary
AdjMatrixGraph(int nodes)
          It initializes the number of nodes and sets the value of every element of the adjacency matrix to an empty list.
 
Method Summary
 boolean addEdge(Edge e)
          This is method adds safely an edge to the graph, when the edge is given as an object.
protected  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 i, int j)
          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

adjMtrx

protected java.util.Vector<java.util.Vector<java.util.LinkedList<Edge>>> adjMtrx
The adjacency matrix;

Constructor Detail

AdjMatrixGraph

public AdjMatrixGraph(int nodes)
It initializes the number of nodes and sets the value of every element of the adjacency matrix to an empty list.

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

protected 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 i,
                                           int j)
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:
i - the source node.
j - 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