Graph
Class Graph

java.lang.Object
  extended by Graph.Graph
Direct Known Subclasses:
AdjListsGraph, AdjMatrixGraph

public abstract class Graph
extends java.lang.Object

This abstract class defines a set of variables and implements some methods that are common in every implementation of a directed multigraph object. It also specifies the minimum interface that a graph implementation should support. Each graph is described by the number of its nodes and by the set of its weighted edges. The number of nodes and the * is determined when the graph is instantiated, and does not change during the lifetime of the graph. The nodes of the graph are labeled by integers 0,1,..,(n-1), where n is the number of nodes. The set of edges of a graph is not fixed. Initially, a graph contains no edges at all. A graph implementation should support methods for inserting and removing edges. Multi-edges (i.e., from the same source to the same target node) are allowed.


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

Field Detail

nNodes

protected int nNodes
The number of nodes of the graph.

Constructor Detail

Graph

public Graph(int nodes)
Class constructor.

Method Detail

getNNodes

public int getNNodes()
Returns the number of nodes of the graph.


addEdge

public abstract boolean addEdge(Edge e)
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.

Parameters:
e - the edge to be added.
Returns:
true if the edge is successfully added to the graph, and false otherwise.

addEdgeUnsafely

protected abstract boolean addEdgeUnsafely(Edge e)
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.

Parameters:
e - the edge to be added.
Returns:
true if the edge is successfully added to the graph, and false otherwise.

getEdges

public abstract java.util.LinkedList<Edge> getEdges(int source)
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.

Parameters:
source - the source node.
Returns:
A linked list containing the edges adjacent to the source.

getEdges

public abstract java.util.LinkedList<Edge> getEdges(int source,
                                                    int target)
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.

Parameters:
source - the source node.
target - the target node.
Returns:
A linked list containing the edges between the source and the target.

removeEdge

public abstract boolean removeEdge(Edge e)
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

Parameters:
e - the edge to remove.
Returns:
true if the edge is succesfully removed

removeAllEdges

public abstract void removeAllEdges()
This method removes all edges in the graph.


toAltGraphRepr

public abstract Graph toAltGraphRepr(Graph G)
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).

Parameters:
G - the graph to copy this graph to.
Returns:
graph G