public class Graph
extends java.lang.Object
implements java.lang.Cloneable
Graph is a labeled graph with a fixed number of vertices.
Java Source Code for this class:
http://www.cs.colorado.edu/~main/EDU/colorado/collections/Graph.java
| Constructor and Description |
|---|
Graph(int n)
Initialize a
Graph with n vertices,
no edges, and null labels. |
| Modifier and Type | Method and Description |
|---|---|
void |
addEdge(int source,
int target)
Add a new edge to this
Graph. |
java.lang.Object |
clone()
Generate a copy of this
Graph. |
static void |
depthFirstPrint(Graph g,
int start)
Static method to print the labels of a graph with a depth-first search.
|
static void |
depthFirstRecurse(Graph g,
int v,
boolean[] marked)
Recursive method to carry out the work of
depthFirstPrint. |
java.lang.Object |
getLabel(int vertex)
Accessor method to get the label of a vertex of this
Graph. |
boolean |
isEdge(int source,
int target)
Accessor method to determine whether this
Graph contains
a specified edge. |
int[] |
neighbors(int vertex)
Accessor method to obtain a list of neighbors of a specified vertex of
this
Graph |
void |
removeEdge(int source,
int target)
Remove an edge from this
Graph. |
void |
setLabel(int vertex,
java.lang.Object newLabel)
Modification method to change the label of a vertex of this
Graph. |
int |
size()
Determine the number of vertices in this
Graph. |
public Graph(int n)
Graph with n vertices,
no edges, and null labels.n - the number of vertices for this Graph
Precondition:
n is nonnegative.
Postcondition:
This Graph has n vertices, numbered
0 to n-1. It has no edges and all
vertex labels are null.java.lang.OutOfMemoryError - Indicates insufficient memory for the specified number of nodes.java.lang.NegativeArraySizeException - Indicates that n is negative.public void addEdge(int source,
int target)
Graph.source - the vertex number of the source of the new edgetarget - the vertex number of the target of the new edge
Precondition:
Both source and target are nonnegative and
less than size().
Postcondition:
This Graph has all the edges that it originally had plus
another edge from the specified source to the specified
target. (If the edge was already present, then this
Graph is unchanged.)java.lang.ArrayIndexOutOfBoundsException - Indicates that the source or target was not a
valid vertex number.public java.lang.Object clone()
Graph.clone in class java.lang.ObjectGraph. Subsequent changes to the
copy will not affect the original, nor vice versa. Note that the return
value must be type cast to a Graph before it can be used.java.lang.OutOfMemoryError - Indicates insufficient memory for creating the clone.public static void depthFirstPrint(Graph g, int start)
g - a nonnull Graphstart - a vertex number from the Graph g
Precondition:
start is nonnegative and less than g.size().
Postcondition:
A depth-first search of g has been conducted, starting at
the specified start vertex. Each vertex visited has its label printed
using System.out.println. Note that vertices that are not
connected to the start will not be visited.java.lang.NullPointerException - Indicates that g is null.java.lang.ArrayIndexOutOfBoundsException - Indicates that the vertex was not a valid vertex number.java.lang.OutOfMemoryError - Indicates that there is insufficient memory for an array of boolean values
used by this method.public static void depthFirstRecurse(Graph g, int v, boolean[] marked)
depthFirstPrint.g - a nonnull Graphv - a vertex number from the Graph gmarked - an array to indicate which vertices of g have already
been visited
Precondition:
v is nonnegative and less than g.size().
marked.length is equal to g.size();
for each vertex x of g, marked[x]
is true if x has already been visited by this
search; otherwise marked[x] is false.
The vertex v is an unmarked vertex that the search has
just arrived at.
Postcondition:
The depth-first search of g has been continued through
vertex v and beyond to all vertices that can be reached
from v via a path of unmarked vertices. Each vertex visited
has its label printed using System.out.println.java.lang.NullPointerException - Indicates that g is null.java.lang.ArrayIndexOutOfBoundsException - Indicates that the vertex was not a valid vertex number, or
marked was the wrong size.public java.lang.Object getLabel(int vertex)
Graph.vertex - a vertex number
Precondition:
vertex is nonnegative and
less than size().Graphjava.lang.ArrayIndexOutOfBoundsException - Indicates that the vertex was not a
valid vertex number.public boolean isEdge(int source,
int target)
Graph contains
a specified edge.source - the vertex number of the source of the specified edgetarget - the vertex number of the target of the specified edge
Precondition:
Both source and target are nonnegative and
less than size().Graph has an edge from the
specified source to the specified target.
Otherwise the return value is false.java.lang.ArrayIndexOutOfBoundsException - Indicates that the source or target was not a
valid vertex number.public int[] neighbors(int vertex)
Graphvertex - a vertex number
Precondition:
The value of source is nonnegative and
less than size().
Precondition:
vertex is nonnegative and
less than size().vertex.java.lang.ArrayIndexOutOfBoundsException - Indicates that the source or target was not a
valid vertex number.public void removeEdge(int source,
int target)
Graph.source - the vertex number of the source of the removed edgetarget - the vertex number of the target of the removed edge
Precondition:
Both source and target are nonnegative and
less than size().
Postcondition:
This Graph has all the edges that it originally had minus
the edge from the specified source to the specified
target. (If the edge was not present, then this
Graph is unchanged.)java.lang.ArrayIndexOutOfBoundsException - Indicates that the source or target was not a
valid vertex number.public void setLabel(int vertex,
java.lang.Object newLabel)
Graph.vertex - a vertex numbernewLabel - a new label (which may be null)
Precondition:
vertex is nonnegative and
less than size().
Postcondition:
The label of the specified vertex in this Graph has been
changed to the newLabel.java.lang.IndexOutOfBoundsException - Indicates that the vertex was not a
valid vertex number.public int size()
Graph.Graph