structure5
Class GraphMatrix<V,E>

java.lang.Object
  extended by structure5.AbstractStructure<V>
      extended by structure5.GraphMatrix<V,E>
All Implemented Interfaces:
Iterable<V>, Graph<V,E>, Structure<V>
Direct Known Subclasses:
GraphMatrixDirected, GraphMatrixUndirected

public abstract class GraphMatrix<V,E>
extends AbstractStructure<V>
implements Graph<V,E>

Implementation of graph using adjacency matrices. User must commit to maximum size of graph (in vertices); it may be smaller. Edges are stored in matrix. Not suitable for large graphs. Class is abstract: you must use GraphMatrixDirected or GraphMatrixUndirected to construct particular instances of graphs. Typical usage:

     Graph g = new GraphMatrixUndirected();
     g.add("harry");
     g.add("sally");
     g.addEdge("harry","sally","unfriendly");
     ...
 

See Also:
GraphMatrixDirected, GraphMatrixUndirected, GraphList

Method Summary
 void add(V label)
          Add a vertex to the graph.
abstract  void addEdge(V v1, V v2, E label)
          Add an edge between two vertices within the graph.
 void clear()
          Remove all vertices (and thus, edges) of the graph.
 boolean contains(V label)
          Test for vertex membership.
 boolean containsEdge(V vLabel1, V vLabel2)
          Test for edge membership.
 int degree(V label)
          Determine out degree of vertex.
abstract  int edgeCount()
          Determine the number of edges in graph.
abstract  Iterator<Edge<V,E>> edges()
          Construct an traversal over all edges.
 V get(V label)
          Get reference to actual label of vertex.
 Edge<V,E> getEdge(V label1, V label2)
          Get reference to actual edge.
 boolean isDirected()
          Determine if graph is directed.
 boolean isEmpty()
          Determine if graph is empty.
 boolean isVisited(V label)
          Return visited flag of vertex.
 boolean isVisitedEdge(Edge<V,E> e)
          Return visited flag of edge.
 Iterator<V> iterator()
          Construct vertex traversal.
 Iterator<V> neighbors(V label)
          Construct an adjacent vertex traversal.
 V remove(V label)
          Remove a vertex from the graph.
abstract  E removeEdge(V vLabel1, V vLabel2)
          Remove possible edge between vertices labeled vLabel1 and vLabel2.
 void reset()
          Clear visited flags of edges and vertices.
 int size()
          Determine number of vertices within graph.
 boolean visit(V label)
          Test and set visited flag of vertex.
 boolean visitEdge(Edge<V,E> e)
          Test and set visited flag of edge.
 
Methods inherited from class structure5.AbstractStructure
elements, hashCode, values
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface structure5.Structure
elements, values
 

Method Detail

add

public void add(V label)
Add a vertex to the graph.

Specified by:
add in interface Graph<V,E>
Specified by:
add in interface Structure<V>
Parameters:
label - Label of the vertex; should be non-null.

addEdge

public abstract void addEdge(V v1,
                             V v2,
                             E label)
Add an edge between two vertices within the graph. Edge is directed iff graph is directed. Duplicate edges are silently replaced. Labels on edges may be null.

Specified by:
addEdge in interface Graph<V,E>
Parameters:
vtx1 - First (or source, if directed) vertex.
vtx2 - Second (or destination, if directed) vertex.
label - Label associated with the edge.

remove

public V remove(V label)
Remove a vertex from the graph. Associated edges are also removed. Non-vertices are silently ignored.

Specified by:
remove in interface Graph<V,E>
Specified by:
remove in interface Structure<V>
Parameters:
label - The label of the vertex within the graph.
Returns:
The label associated with the vertex.

removeEdge

public abstract E removeEdge(V vLabel1,
                             V vLabel2)
Remove possible edge between vertices labeled vLabel1 and vLabel2. Directed edges consider vLabel1 to be the source.

Specified by:
removeEdge in interface Graph<V,E>
Parameters:
vLabel1 - First (or source, if directed) vertex.
vLabel2 - Second (or destination, if directed) vertex.
Returns:
The label associated with the edge removed.

get

public V get(V label)
Get reference to actual label of vertex. Vertex labels are matched using their equals method, which may or may not test for actual equivalence. Result remains part of graph.

Specified by:
get in interface Graph<V,E>
Parameters:
label - The label of the vertex sought.
Returns:
The actual label, or null if none is found.

getEdge

public Edge<V,E> getEdge(V label1,
                         V label2)
Get reference to actual edge. Edge is identified by the labels on associated vertices. If edge is directed, the label1 indicates source.

Specified by:
getEdge in interface Graph<V,E>
Parameters:
label1 - The first (or source, if directed) vertex.
label2 - The second (or destination, if directed) vertex.
Returns:
The edge, if found, or null.

contains

public boolean contains(V label)
Test for vertex membership.

Specified by:
contains in interface Graph<V,E>
Specified by:
contains in interface Structure<V>
Overrides:
contains in class AbstractStructure<V>
Parameters:
label - The label of the vertex sought.
Returns:
True iff vertex with matching label is found.

containsEdge

public boolean containsEdge(V vLabel1,
                            V vLabel2)
Test for edge membership. If edges are directed, vLabel1 indicates source.

Specified by:
containsEdge in interface Graph<V,E>
Parameters:
vLabel1 - First (or source, if directed) vertex.
vLabel2 - Second (or destination, if directed) vertex.
Returns:
True iff the edge exists within the graph.

visit

public boolean visit(V label)
Test and set visited flag of vertex.

Specified by:
visit in interface Graph<V,E>
Parameters:
label - Label of vertex to be visited.
Returns:
Previous value of visited flag on vertex.

visitEdge

public boolean visitEdge(Edge<V,E> e)
Test and set visited flag of edge.

Specified by:
visitEdge in interface Graph<V,E>
Parameters:
e - Edge object that is part of graph.
Returns:
Previous value of the Edge's visited flag.

isVisited

public boolean isVisited(V label)
Return visited flag of vertex.

Specified by:
isVisited in interface Graph<V,E>
Parameters:
label - Label of vertex.
Returns:
True if vertex has been visited.

isVisitedEdge

public boolean isVisitedEdge(Edge<V,E> e)
Return visited flag of edge.

Specified by:
isVisitedEdge in interface Graph<V,E>
Parameters:
e - Edge of graph to be considered.
Returns:
True if the edge has been visited.

reset

public void reset()
Clear visited flags of edges and vertices.

Specified by:
reset in interface Graph<V,E>

size

public int size()
Determine number of vertices within graph.

Specified by:
size in interface Graph<V,E>
Specified by:
size in interface Structure<V>
Returns:
The number of vertices within graph.

degree

public int degree(V label)
Determine out degree of vertex.

Specified by:
degree in interface Graph<V,E>
Parameters:
label - Label associated with vertex.
Returns:
The number of edges with this vertex as source.

edgeCount

public abstract int edgeCount()
Determine the number of edges in graph.

Specified by:
edgeCount in interface Graph<V,E>
Returns:
Number of edges in graph.

iterator

public Iterator<V> iterator()
Construct vertex traversal. Vertices are not visited in any guaranteed order.

Specified by:
iterator in interface Iterable<V>
Specified by:
iterator in interface Graph<V,E>
Specified by:
iterator in interface Structure<V>
Returns:
AbstractIterator traversing vertices in graph.
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()

neighbors

public Iterator<V> neighbors(V label)
Construct an adjacent vertex traversal. Adjacent vertices (those on destination of edge, if directed) are considered, but not in any guaranteed order.

Specified by:
neighbors in interface Graph<V,E>
Parameters:
label - Label of the vertex.
Returns:
AbstractIterator traversing the adjacent vertices of labeled vertex.

edges

public abstract Iterator<Edge<V,E>> edges()
Construct an traversal over all edges. Every directed/undirected edge is considered exactly once. Order is not guaranteed.

Specified by:
edges in interface Graph<V,E>
Returns:
AbstractIterator over edges.

clear

public void clear()
Remove all vertices (and thus, edges) of the graph.

Specified by:
clear in interface Graph<V,E>
Specified by:
clear in interface Structure<V>

isEmpty

public boolean isEmpty()
Determine if graph is empty.

Specified by:
isEmpty in interface Graph<V,E>
Specified by:
isEmpty in interface Structure<V>
Overrides:
isEmpty in class AbstractStructure<V>
Returns:
True iff there are no vertices in graph.

isDirected

public boolean isDirected()
Determine if graph is directed.

Specified by:
isDirected in interface Graph<V,E>
Returns:
True iff the graph is directed.