|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectedu.colorado.graphs.Graph
public class Graph
A Graph is a labeled graph with a fixed number of vertices.
| Constructor Summary | |
|---|---|
Graph(int n)
Initialize a Graph with n vertices,
no edges, and null labels. |
|
| Method Summary | |
|---|---|
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. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
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.
- Throws:
java.lang.OutOfMemoryError - Indicates insufficient memory for the specified number of nodes.
java.lang.NegativeArraySizeException - Indicates that n is negative. | Method Detail |
|---|
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
source and target are nonnegative and
less than size().
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.Object- - none
Graph. 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
start is nonnegative and less than g.size().
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
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.
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
vertex is nonnegative and
less than size().
java.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
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)
Graph
vertex - a vertex numbertarget - a vertex number
source and target are nonnegative and
less than size().
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
source and target are nonnegative and
less than size().
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)
vertex is nonnegative and
less than size().
newLabel.
java.lang.ArrayIndexOutOfBoundsException - Indicates that the vertex was not a
valid vertex number.public int size()
Graph.
- - none
Graph
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||