GraphLab Project

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

java.lang.Object
  extended by graphlab.library.BaseGraph<VertexType,EdgeType>
      extended by graphlab.library.MatrixGraph<VertexType,EdgeType>
Type Parameters:
VertexType - Type of the vertices the graph can work with.
EdgeType - Type of the edges the graph can work with.
All Implemented Interfaces:
java.lang.Iterable<VertexType>

public class MatrixGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
extends BaseGraph<VertexType,EdgeType>

Adjacency Matrix Graph. For information about Adjacency Matrix refer to http://en.wikipedia.org/wiki/Adjacency_matrix * @author Omid Aladini

Author:
Omid Aladini

Field Summary
 
Fields inherited from class graphlab.library.BaseGraph
isSubgraph, lastSubgraphIndex, subgraphIndex, superGraph
 
Constructor Summary
MatrixGraph()
          Constructs an undirected graph object that stores graph data using adjacency list data structure.
MatrixGraph(BaseGraph<ImportVertexType,ImportEdgeType> graph, GraphConverter<ImportVertexType,VertexType,ImportEdgeType,EdgeType,ImportGraphType,MatrixGraph<VertexType,EdgeType>> gc)
          Constructs a graph object that stores graph data using adjacency matrix data structure by importing graph data from a pre-existing graph.
MatrixGraph(boolean directed)
          Constructs a MatrixGraph object
MatrixGraph(boolean directed, int expectedNumberOfVertices)
          Constructs a graph object that stores graph data using adjacency matrix data structure.
 
Method Summary
 boolean breadthFirstSearch(VertexType vertex, PreWorkHandler<VertexType> handler)
          Runs Breadth First Search (BFS) algorithm on the graph starting from vertex vertexId.
 void checkVertex(VertexType v)
          If the supplied vertex is invalid (Not one of graph's vertices), throws InvalidVertexException.
 void clear()
          Clears the graph.
 boolean containsVertex(VertexType v)
          This method returns true if the graph contains the specified vertex, false otherwise.
 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).
 MatrixGraph<VertexType,EdgeType> createEmptyGraph()
          Returns a new instance of an empty graph of the current graph type.
 boolean depthFirstSearch(VertexType vertex, PreWorkPostWorkHandler<VertexType> handler)
          Runs Depth First Search (DFS) algorithm on the graph starting from vertex vertexId.
 void dump()
          Prints the Adjacency Matrix to the standard output.
 java.util.Iterator<EdgeType> edgeIterator()
          Returns iterator object for the edges.
 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()
           
 Matrix getAdjacencyMatrix()
          Returns a Jama Matrix object that represents adjacency matrix of the graph.
 VertexType getAVertex()
           
 int[][] getEdgeArray()
          Returns array of array of 'int's where represents a simple adjacency list.
 java.util.ArrayList<EdgeType> getEdges(VertexType source, VertexType target)
          Returns all edges between two vertices.
 int getEdgesCount()
           
 int getInDegree(VertexType v)
          Returns in-degree of vertex vertexId, the number of edges which their target goes to the specified vertex.
 int getOutDegree(VertexType v)
          Returns out-degree of the supplied vertex, the number of edges which their source is attached to the specified vertex.
 BaseVertex[] getVertexArray()
          Returns array of vertices upcasted to BaseVertex.
 int getVerticesCount()
          Returns the number of vertices.
 void insertEdge(EdgeType newEdge)
          Inserts an edge in the graph.
 void insertVertex(VertexType newVertex)
          Inserts a new vertex to the graph.
 boolean isDirected()
          Returns whether the graph is directed.
 boolean isEdge(VertexType source, VertexType target)
          Returns true if there is an edge between specified vertices (direction considered for directed graphs).
 boolean isGraphAcyclic()
          Checks whether the current graph is acyclic.
 boolean isGraphConnected()
          Checks whether the current graph is a connected graph.
 java.util.Iterator<VertexType> iterator()
          Returns iterator object for the vertices.
 java.util.Iterator<EdgeType> lightEdgeIterator()
          Returns a light(weight) Edge Iterator object which iterates through all the edges in the graph.
 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 removeAllEdges(VertexType source, VertexType target)
          Removes all edges between two vertices.
 void removeEdge(EdgeType edge)
          Removes an edge from the graph.
 void removeVertex(VertexType v)
          Removes a vertex and all it's connected edges.
 void setDirected(boolean isDirected)
           
 
Methods inherited from class graphlab.library.BaseGraph
getDegree, getEdges, getId, getNeighbors, getNewSubgraphIndex, registerSubgraph, setSubGraphIndex, vertices
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MatrixGraph

public MatrixGraph(boolean directed,
                   int expectedNumberOfVertices)
Constructs a graph object that stores graph data using adjacency matrix data structure.

Parameters:
directed - Indicated whether the graph is directed.
expectedNumberOfVertices - Approximate number of vertices that will be added to the graph. This paramether is optional and is available for performance reasons.

MatrixGraph

public MatrixGraph(boolean directed)
Constructs a MatrixGraph object

Parameters:
directed - Whether the graph is directed or undirected.

MatrixGraph

public MatrixGraph()
Constructs an undirected graph object that stores graph data using adjacency list data structure.


MatrixGraph

public MatrixGraph(BaseGraph<ImportVertexType,ImportEdgeType> graph,
                   GraphConverter<ImportVertexType,VertexType,ImportEdgeType,EdgeType,ImportGraphType,MatrixGraph<VertexType,EdgeType>> gc)
            throws InvalidGraphException
Constructs a graph object that stores graph data using adjacency matrix data structure by importing graph data from a pre-existing graph. A GraphConvertor object is passed as a parameter which is reponsible for duplication/type-convertion of graph elements.

Type Parameters:
ImportVertexType - The type of vertex object which the input graph contain.
ImportEdgeType - The type of edge object which the input graph contain.
Parameters:
graph -
converter - A GraphConverter object which is responsible for duplicating/converting graph elements.
Throws:
InvalidGraphException - Throws when the input graph is an invalid graph object.
Method Detail

getVerticesCount

public int getVerticesCount()
Description copied from class: BaseGraph
Returns the number of vertices.

Specified by:
getVerticesCount in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
Number of vertices in the graph.

insertEdge

public void insertEdge(EdgeType newEdge)
                throws InvalidVertexException
Description copied from class: BaseGraph
Inserts an edge in the graph.

Specified by:
insertEdge in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
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.

getEdges

public java.util.ArrayList<EdgeType> getEdges(VertexType source,
                                              VertexType target)
                                                                    throws InvalidVertexException
Returns all edges between two vertices.

Specified by:
getEdges in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Parameters:
from - Index of the edges' start point.
to - Index of the edges' end point.
Returns:
An ArrayList of EdgeType containing all edges between from and to.
Throws:
InvalidVertexException - Thrown when two supplied indexes of vertices are invalid.

insertVertex

public void insertVertex(VertexType newVertex)
Description copied from class: BaseGraph
Inserts a new vertex to the graph.

Specified by:
insertVertex in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Parameters:
newVertex - The new vertex to be inserted.

depthFirstSearch

public boolean depthFirstSearch(VertexType vertex,
                                PreWorkPostWorkHandler<VertexType> handler)
                         throws InvalidVertexException
Runs Depth First Search (DFS) algorithm on the graph starting from vertex vertexId. A reference to a PreWorkPostWorkHandler is supplied that contains implementation of pre-work and post-work operations that depends on the application of DFS.

Parameters:
vertex - Starting vertex of the traversal.
handler - A reference to a PreWorkPostWorkHandler that contains implementation of pre-work and post-work operations that depends on the application of DFS.
Returns:
Whether the traversal has stopped at the middle by the handler.
Throws:
InvalidVertexException

breadthFirstSearch

public boolean breadthFirstSearch(VertexType vertex,
                                  PreWorkHandler<VertexType> handler)
                           throws InvalidVertexException
Runs Breadth First Search (BFS) algorithm on the graph starting from vertex vertexId. A reference to a PreWorkHandler is supplied that contains implementation of pre-work operation that depends on the application of BFS.

Parameters:
vertex - Starting vertex of the traversal.
handler - A reference to a PreWorkHandler that contains implementation of pre-work operation that depends on the application of DFS.
Returns:
Whether the traversal has stopped at the middle by the handler.
Throws:
InvalidVertexException

isGraphConnected

public boolean isGraphConnected()
Checks whether the current graph is a connected graph.

Returns:
True if graph is connected and false otherwise.

isGraphAcyclic

public boolean isGraphAcyclic()
Checks whether the current graph is acyclic.

Returns:
True if graph is acyclic and false otherwise.

iterator

public java.util.Iterator<VertexType> iterator()
Description copied from class: BaseGraph
Returns iterator object for the vertices.

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

isDirected

public boolean isDirected()
Description copied from class: BaseGraph
Returns whether the graph is directed.

Specified by:
isDirected in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
True is graph is constructed as a directed graph and false otherwise.

edgeIterator

public java.util.Iterator<EdgeType> edgeIterator()
Returns iterator object for the edges.

Specified by:
edgeIterator in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
iterator object for the edges.
See Also:
BaseGraph.lightEdgeIterator()

edges

public java.lang.Iterable<EdgeType> edges()
Overrides:
edges in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
See Also:
BaseGraph.lightEdgeIterator()

edgeIterator

public java.util.Iterator<EdgeType> edgeIterator(VertexType v)
                                                                       throws InvalidVertexException
Description copied from class: BaseGraph
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.

Specified by:
edgeIterator in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
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:
BaseGraph.lightEdgeIterator(BaseVertex), BaseGraph.getNeighbors(BaseVertex)

getAdjacencyMatrix

public Matrix getAdjacencyMatrix()
Description copied from class: BaseGraph
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.

Specified by:
getAdjacencyMatrix in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
Adjacency Matrix of the graph as a Jama Matrix object.

dump

public void dump()
Description copied from class: BaseGraph
Prints the Adjacency Matrix to the standard output.

Specified by:
dump in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>

removeAllEdges

public void removeAllEdges(VertexType source,
                           VertexType target)
                    throws InvalidVertexException
Description copied from class: BaseGraph
Removes all edges between two vertices.

Specified by:
removeAllEdges in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
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.

removeVertex

public void removeVertex(VertexType v)
                  throws InvalidVertexException
Description copied from class: BaseGraph
Removes a vertex and all it's connected edges.

Specified by:
removeVertex in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Throws:
InvalidVertexException

getInDegree

public int getInDegree(VertexType v)
                throws InvalidVertexException
Description copied from class: BaseGraph
Returns in-degree of vertex vertexId, the number of edges which their target goes to the specified vertex.

Specified by:
getInDegree in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
in-degree of vertex vertexId.
Throws:
InvalidVertexException
See Also:
BaseGraph.getDegree(BaseVertex)

getOutDegree

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

Specified by:
getOutDegree in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
out-degree of vertex vertexId.
Throws:
InvalidVertexException
See Also:
BaseGraph.getDegree(BaseVertex)

removeEdge

public void removeEdge(EdgeType edge)
                throws InvalidEdgeException
Description copied from class: BaseGraph
Removes an edge from the graph.

Specified by:
removeEdge in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Parameters:
edge - Edge to be removed.
Throws:
InvalidEdgeException - If edge is an invalid edge object.

getAVertex

public VertexType getAVertex()

copy

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

Specified by:
copy in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
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.

containsVertex

public boolean containsVertex(VertexType v)
Description copied from class: BaseGraph
This method returns true if the graph contains the specified vertex, false otherwise.

Specified by:
containsVertex in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Parameters:
v - Vertex to check existance.
Returns:
True if the graph contains the specified vertex, false otherwise.

checkVertex

public void checkVertex(VertexType v)
                 throws InvalidVertexException
Description copied from class: BaseGraph
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.

Specified by:
checkVertex in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Parameters:
v - The vertex to be checked.
Throws:
InvalidVertexException - If the supplied vertex is invalid.

isEdge

public boolean isEdge(VertexType source,
                      VertexType target)
               throws InvalidVertexException
Description copied from class: BaseGraph
Returns true if there is an edge between specified vertices (direction considered for directed graphs).

Specified by:
isEdge in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
true if there is an edge between specified vertices (direction considered for directed graphs).
Throws:
InvalidVertexException - if supplied source or target are invalid.

createEmptyGraph

public MatrixGraph<VertexType,EdgeType> createEmptyGraph()
Description copied from class: BaseGraph
Returns a new instance of an empty graph of the current graph type.

Specified by:
createEmptyGraph in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
A new instance of an empty graph of the current graph type.

setDirected

public void setDirected(boolean isDirected)
Specified by:
setDirected in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>

getVertexArray

public BaseVertex[] getVertexArray()
Description copied from class: BaseGraph
Returns array of vertices upcasted to BaseVertex.

Specified by:
getVertexArray in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
Array of vertices upcasted to BaseVertex.

getEdgeArray

public int[][] getEdgeArray()
Description copied from class: BaseGraph
Returns array of array of 'int's where represents a simple adjacency list.

Specified by:
getEdgeArray in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
Array of array of 'int's where represents a simple adjacency list.

lightEdgeIterator

public java.util.Iterator<EdgeType> lightEdgeIterator()
Description copied from class: BaseGraph
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.

Specified by:
lightEdgeIterator in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Returns:
Returns a light(weight) Edge Iterator object.

lightEdgeIterator

public java.util.Iterator<EdgeType> lightEdgeIterator(VertexType v)
                                                                            throws InvalidVertexException
Description copied from class: BaseGraph
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.

Specified by:
lightEdgeIterator in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
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 void clear()
Description copied from class: BaseGraph
Clears the graph.

Specified by:
clear in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>

getEdgesCount

public int getEdgesCount()
Specified by:
getEdgesCount in class BaseGraph<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>

GraphLab Project