GraphLab Project

graphlab.library
Class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>

java.lang.Object
  extended by graphlab.library.BaseGraph<VertexType,EdgeType>
All Implemented Interfaces:
java.lang.Iterable<VertexType>
Direct Known Subclasses:
ListGraph, MatrixGraph

public abstract class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
extends java.lang.Object
implements java.lang.Iterable<VertexType>

Generic base class for representation of all types of graphs.

Author:
Omid Aladini

Field Summary
protected  boolean isSubgraph
          Whether the graph is a subgraph.
protected  int lastSubgraphIndex
           
protected  int subgraphIndex
          If zero, indicates that the graph is not a subgraph.
protected  BaseGraph<VertexType,EdgeType> superGraph
           
 
Constructor Summary
BaseGraph()
           
 
Method Summary
abstract  void checkVertex(VertexType v)
          If the supplied vertex is invalid (Not one of graph's vertices), throws InvalidVertexException.
abstract  void clear()
          Clears the graph.
abstract  boolean containsVertex(VertexType v)
          This method returns true if the graph contains the specified vertex, false otherwise.
abstract  BaseGraph<VertexType,EdgeType> copy(EdgeVertexCopier<VertexType,EdgeType> gc)
          Creates a clone of the current graph using the GraphConverter object which is responsible for duplication of the graph elements (edges and vertices).
abstract
<GraphType extends BaseGraph<VertexType,EdgeType>>
GraphType
createEmptyGraph()
          Returns a new instance of an empty graph of the current graph type.
abstract  void dump()
          Prints the Adjacency Matrix to the standard output.
abstract  java.util.Iterator<EdgeType> edgeIterator()
          Constructs and returns an Edge Iterator object which iterates through all the edges in the graph.
abstract  java.util.Iterator<EdgeType> edgeIterator(VertexType v)
          Constructs an Edge Iterator object which iterates through all the edges going to or coming from the specified vertex v.
 java.lang.Iterable<EdgeType> edges()
           
abstract  Matrix getAdjacencyMatrix()
          Returns a Jama Matrix object that represents adjacency matrix of the graph.
 int getDegree(VertexType vertex)
          Returns degree of vertex, the number of edges which their target or source is the specified vertex.
abstract  int[][] getEdgeArray()
          Returns array of array of 'int's where represents a simple adjacency list.
 java.lang.Iterable<EdgeType> getEdges()
          Its the same as edges
abstract  java.util.AbstractList<EdgeType> getEdges(VertexType source, VertexType target)
          Returns a collection of all edges which connects two vertices supplied as first and second arguments of this method.
abstract  int getEdgesCount()
           
protected  int getId(VertexType v)
          A wrapper for getting vertex Id's which supports multiple vertex owners.
abstract  int getInDegree(VertexType v)
          Returns in-degree of vertex vertexId, the number of edges which their target goes to the specified vertex.
 java.lang.Iterable<VertexType> getNeighbors(VertexType vertex)
           
 int getNewSubgraphIndex()
          Get new id for a new subgraph;
abstract  int getOutDegree(VertexType v)
          Returns out-degree of the supplied vertex, the number of edges which their source is attached to the specified vertex.
abstract  BaseVertex[] getVertexArray()
          Returns array of vertices upcasted to BaseVertex.
abstract  int getVerticesCount()
          Returns the number of vertices.
abstract  void insertEdge(EdgeType newEdge)
          Inserts an edge in the graph.
abstract  void insertVertex(VertexType newVertex)
          Inserts a new vertex to the graph.
abstract  boolean isDirected()
          Returns whether the graph is directed.
abstract  boolean isEdge(VertexType source, VertexType target)
          Returns true if there is an edge between specified vertices (direction considered for directed graphs).
abstract  java.util.Iterator<VertexType> iterator()
          Returns iterator object for the vertices.
abstract  java.util.Iterator<EdgeType> lightEdgeIterator()
          Returns a light(weight) Edge Iterator object which iterates through all the edges in the graph.
abstract  java.util.Iterator<EdgeType> lightEdgeIterator(VertexType v)
          Constructs a light(weight) Edge Iterator object which iterates through all the edges going to or coming from the specified vertex v.
 void registerSubgraph(BaseGraph<VertexType,EdgeType> superGraph)
          Sets the graph as a subgraph.
abstract  void removeAllEdges(VertexType source, VertexType target)
          Removes all edges between two vertices.
abstract  void removeEdge(EdgeType edge)
          Removes an edge from the graph.
abstract  void removeVertex(VertexType v)
          Removes a vertex and all it's connected edges.
abstract  void setDirected(boolean isDirected)
           
 void setSubGraphIndex(int i)
          Set's the subgraph index.
 java.lang.Iterable<VertexType> vertices()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

subgraphIndex

protected int subgraphIndex
If zero, indicates that the graph is not a subgraph. If greater than zero, it indicates that it's a subgraph with Id subgraphId.


isSubgraph

protected boolean isSubgraph
Whether the graph is a subgraph. Then it shares it's contents.


lastSubgraphIndex

protected int lastSubgraphIndex

superGraph

protected BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> superGraph
Constructor Detail

BaseGraph

public BaseGraph()
Method Detail

getVerticesCount

public abstract int getVerticesCount()
Returns the number of vertices.

Returns:
Number of vertices in the graph.

copy

public abstract BaseGraph<VertexType,EdgeType> copy(EdgeVertexCopier<VertexType,EdgeType> gc)
                                                                                             throws InvalidGraphException
Creates a clone of the current graph using the GraphConverter object which is responsible for duplication of the graph elements (edges and vertices).

Parameters:
gc - Reference to EdgeVertexCopier object.
Returns:
Clone of the current graph which is independent of it's source graph.
Throws:
InvalidGraphException - If the graph is not a valid graph object.

insertEdge

public abstract void insertEdge(EdgeType newEdge)
                         throws InvalidVertexException
Inserts an edge in the graph.

Parameters:
newEdge - Reference to the new edge object.
Throws:
InvalidVertexException - Thrown when the edge object tries to connect two vertices whom their indexes are invalid.

removeAllEdges

public abstract void removeAllEdges(VertexType source,
                                    VertexType target)
                             throws InvalidVertexException
Removes all edges between two vertices.

Parameters:
source - Index of the edges' start point.
target - Index of the edges' end point.
Throws:
InvalidVertexException - Thrown when two supplied indexes of vertices are invalid.

removeEdge

public abstract void removeEdge(EdgeType edge)
                         throws InvalidEdgeException
Removes an edge from the graph.

Parameters:
edge - Edge to be removed.
Throws:
InvalidEdgeException - If edge is an invalid edge object.

getEdges

public abstract java.util.AbstractList<EdgeType> getEdges(VertexType source,
                                                          VertexType target)
                                                                                throws InvalidVertexException
Returns a collection of all edges which connects two vertices supplied as first and second arguments of this method.

Parameters:
Source - of the desired edges.
Target - of the desired edges.
Returns:
Returns a collection of all edges which connects two vertices supplied as first and second arguments of this method.
Throws:
InvalidVertexException - if supplied source or target are invalid.

isEdge

public abstract boolean isEdge(VertexType source,
                               VertexType target)
                        throws InvalidVertexException
Returns true if there is an edge between specified vertices (direction considered for directed graphs).

Parameters:
Source - of the edge for existance check.
Target - of the edge for existance check.
Returns:
true if there is an edge between specified vertices (direction considered for directed graphs).
Throws:
InvalidVertexException - if supplied source or target are invalid.

insertVertex

public abstract void insertVertex(VertexType newVertex)
Inserts a new vertex to the graph.

Parameters:
newVertex - The new vertex to be inserted.

removeVertex

public abstract void removeVertex(VertexType v)
                           throws InvalidVertexException
Removes a vertex and all it's connected edges.

Parameters:
Vertex - to be removed.
Throws:
InvalidVertexException

iterator

public abstract java.util.Iterator<VertexType> iterator()
Returns iterator object for the vertices.

Specified by:
iterator in interface java.lang.Iterable<VertexType extends BaseVertex>
Returns:
iterator object for the vertices.

getInDegree

public abstract int getInDegree(VertexType v)
                         throws InvalidVertexException
Returns in-degree of vertex vertexId, the number of edges which their target goes to the specified vertex.

Returns:
in-degree of vertex vertexId.
Throws:
InvalidVertexException
See Also:
getDegree(BaseVertex)

getOutDegree

public abstract int getOutDegree(VertexType v)
                          throws InvalidVertexException
Returns out-degree of the supplied vertex, the number of edges which their source is attached to the specified vertex.

Returns:
out-degree of vertex vertexId.
Throws:
InvalidVertexException
See Also:
getDegree(BaseVertex)

getAdjacencyMatrix

public abstract Matrix getAdjacencyMatrix()
Returns a Jama Matrix object that represents adjacency matrix of the graph. the Matrix object have the ability apply simple linear algebra operations on the adjacency matrix.

Returns:
Adjacency Matrix of the graph as a Jama Matrix object.

isDirected

public abstract boolean isDirected()
Returns whether the graph is directed.

Returns:
True is graph is constructed as a directed graph and false otherwise.

dump

public abstract void dump()
Prints the Adjacency Matrix to the standard output.


edgeIterator

public abstract java.util.Iterator<EdgeType> edgeIterator()
Constructs and returns an Edge Iterator object which iterates through all the edges in the graph. Note that if the graph object is changed during iteration, the iteration may not actually represent current state of the graph. For example, if you deleted an edge after construction of this object, the edge would be included in the iteration.

Returns:
Iterator object on edges.
See Also:
lightEdgeIterator()

edgeIterator

public abstract java.util.Iterator<EdgeType> edgeIterator(VertexType v)
                                                                                throws InvalidVertexException
Constructs an Edge Iterator object which iterates through all the edges going to or coming from the specified vertex v. Note that if the graph object is changed during iteration, the iteration may not actually represent current state of the graph. For example, if you deleted an edge after construction of this object, the edge would be included in the iteration.

Parameters:
v - Source or target of desired edges.
Returns:
Iterator object on edges which their sources or targets are the supplied vertex.
Throws:
InvalidVertexException
See Also:
lightEdgeIterator(BaseVertex), getNeighbors(BaseVertex)

containsVertex

public abstract boolean containsVertex(VertexType v)
This method returns true if the graph contains the specified vertex, false otherwise.

Parameters:
v - Vertex to check existance.
Returns:
True if the graph contains the specified vertex, false otherwise.

checkVertex

public abstract void checkVertex(VertexType v)
                          throws InvalidVertexException
If the supplied vertex is invalid (Not one of graph's vertices), throws InvalidVertexException. This method should be called before any operation by algorithms where some vertices are supplied as their arguments.

Parameters:
v - The vertex to be checked.
Throws:
InvalidVertexException - If the supplied vertex is invalid.

createEmptyGraph

public abstract <GraphType extends BaseGraph<VertexType,EdgeType>> GraphType createEmptyGraph()
Returns a new instance of an empty graph of the current graph type.

Returns:
A new instance of an empty graph of the current graph type.

getVertexArray

public abstract BaseVertex[] getVertexArray()
Returns array of vertices upcasted to BaseVertex.

Returns:
Array of vertices upcasted to BaseVertex.

getEdgeArray

public abstract int[][] getEdgeArray()
Returns array of array of 'int's where represents a simple adjacency list.

Returns:
Array of array of 'int's where represents a simple adjacency list.

lightEdgeIterator

public abstract java.util.Iterator<EdgeType> lightEdgeIterator()
Returns a light(weight) Edge Iterator object which iterates through all the edges in the graph. The light(weight) edge iterator presents an iterator with O(1) constructor. Note that you should not change the content of the graph during your iteration. You can still change properties of each edge or vertex.

Returns:
Returns a light(weight) Edge Iterator object.

lightEdgeIterator

public abstract java.util.Iterator<EdgeType> lightEdgeIterator(VertexType v)
                                                                                     throws InvalidVertexException
Constructs a light(weight) Edge Iterator object which iterates through all the edges going to or coming from the specified vertex v. The light(weight) edge iterator presents an iterator with O(1) constructor. Note that you should not change the content of the graph during your iteration. You can still change properties of each edge or vertex.

Parameters:
v - Source or target of desired edges.
Returns:
A light(weight) Edge Iterator object which iterates through all the edges going to or coming from the specified vertex.
Throws:
InvalidVertexException

clear

public abstract void clear()
Clears the graph.


setDirected

public abstract void setDirected(boolean isDirected)

registerSubgraph

public void registerSubgraph(BaseGraph<VertexType,EdgeType> superGraph)
Sets the graph as a subgraph.

Parameters:
b -

getNewSubgraphIndex

public int getNewSubgraphIndex()
Get new id for a new subgraph;

Parameters:
b -

setSubGraphIndex

public void setSubGraphIndex(int i)
Set's the subgraph index.

Parameters:
i - the subgraph index.

getId

protected int getId(VertexType v)
A wrapper for getting vertex Id's which supports multiple vertex owners.

Parameters:
v - Vertex which the caller intends to get its Id.
Returns:
The Id.

getEdgesCount

public abstract int getEdgesCount()

edges

public java.lang.Iterable<EdgeType> edges()
See Also:
lightEdgeIterator()

getDegree

public int getDegree(VertexType vertex)
Returns degree of vertex, the number of edges which their target or source is the specified vertex.

Parameters:
vertex -

getNeighbors

public java.lang.Iterable<VertexType> getNeighbors(VertexType vertex)
Parameters:
vertex -
Returns:
an Iterable object which can be iterated trough all neighbours of the given vertex using lightEdgeIterator(vertex)
See Also:
lightEdgeIterator(BaseVertex)

getEdges

public java.lang.Iterable<EdgeType> getEdges()
Its the same as edges


vertices

public java.lang.Iterable<VertexType> vertices()
Returns:
an Iterable which can iterated on all vertices of graph

GraphLab Project