structure5
Interface Graph<V,E>

All Superinterfaces:
Iterable<V>, Structure<V>
All Known Implementing Classes:
GraphList, GraphListDirected, GraphListUndirected, GraphMatrix, GraphMatrixDirected, GraphMatrixUndirected

public interface Graph<V,E>
extends Structure<V>

The interface describing all Graph objects. Graphs are collections of vertices connected by a set of edges. Edges may or may not be directed. Portions of the graph may be marked visited to support iterative algorithms. Iteration is provided over vertices, edges, and vertices adjacent to a particular vertex

Example usage:

To visit all of the vertices reachable from a given vertex we could use the following:

   static void reachableFrom(Graph g, Object vertexLabel)
   {
         g.visit(vertexLabel);       // visit this vertex

         // recursively visit unvisited neighbor vertices
         Iterator ni = g.neighbors(vertexLabel);
         while (ni.hasNext())
         {
             Object neighbor = ni.next(); // adjacent node label
             if (!g.isVisited(neighbor)x)
             {
                 reachableFrom(g,neighbor); // depth-first search
             } 
         }
   }
 

See Also:
structure.GraphList, structure.GraphMatrix

Method Summary
 void add(V label)
          Add a vertex to the graph
 void addEdge(V vtx1, V vtx2, 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.
 int edgeCount()
          Determine the number of edges in graph.
 Iterator<Edge<V,E>> edges()
          Construct an iterator 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 iterator.
 Iterator<V> neighbors(V label)
          Construct an adjacent vertex iterator.
 V remove(V label)
          Remove a vertex from the graph.
 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 interface structure5.Structure
elements, values
 

Method Detail

add

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

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

addEdge

void addEdge(V vtx1,
             V vtx2,
             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.

Parameters:
vtx1 - First (or source, if directed) vertex.
vtx2 - Second (or destination, if directed) vertex.
label - Label associated with the edge.

remove

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 Structure<V>
Parameters:
label - The label of the vertex within the graph.
Returns:
The label associated with the vertex.

removeEdge

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

Parameters:
vLabel1 - First (or source, if directed) vertex.
vLabel2 - Second (or destination, if directed) vertex.
Returns:
The label associated with the edge removed.

get

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.

Parameters:
label - The label of the vertex sought.
Returns:
The actual label, or null if none is found.

getEdge

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.

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

boolean contains(V label)
Test for vertex membership.

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

containsEdge

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

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

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

Parameters:
label - Label of vertex to be visited.
Returns:
Previous value of visited flag on vertex.

visitEdge

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

Parameters:
e - Edge object that is part of graph.
Returns:
Previous value of the Edge's visited flag.

isVisited

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

Parameters:
label - Label of vertex.
Returns:
True if vertex has been visited.

isVisitedEdge

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

Parameters:
e - Edge of graph to be considered.
Returns:
True if the edge has been visited.

reset

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


size

int size()
Determine number of vertices within graph.

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

degree

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

Parameters:
label - Label associated with vertex.
Returns:
The number of edges with this vertex as source.

edgeCount

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

Returns:
Number of edges in graph.

iterator

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

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

neighbors

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

Parameters:
label - Label of the vertex.
Returns:
Iterator traversing the adjacent vertices of labeled vertex.

edges

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

Returns:
Iterator over edges.

clear

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

Specified by:
clear in interface Structure<V>

isEmpty

boolean isEmpty()
Determine if graph is empty.

Specified by:
isEmpty in interface Structure<V>
Returns:
True iff there are no vertices in graph.

isDirected

boolean isDirected()
Determine if graph is directed.

Returns:
True iff the graph is directed.