public class EdgeWeightedDigraph
extends java.lang.Object
EdgeWeightedDigraph
class represents a edge-weighted
digraph of vertices named 0 through V - 1, where each
directed edge is of type DirectedEdge
and has a real-valued weight.
It supports the following two primary operations: add a directed edge
to the digraph and iterate over all of edges incident from a given vertex.
It also provides
methods for returning the number of vertices V and the number
of edges E. Parallel edges and self-loops are permitted.
This implementation uses an adjacency-lists representation, which is a vertex-indexed array of @link{Bag} objects. All operations take constant time (in the worst case) except iterating over the edges incident from a given vertex, which takes time proportional to the number of such edges.
For additional documentation, see Section 4.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
Modifier and Type | Field and Description |
---|---|
Bag<DirectedEdge>[] |
adj |
int |
E |
int |
V |
Constructor and Description |
---|
EdgeWeightedDigraph(EdgeWeightedDigraph G)
validate
Initializes a new edge-weighted digraph that is a deep copy of
G . |
EdgeWeightedDigraph(In in)
Initializes an edge-weighted digraph from the specified input stream.
|
EdgeWeightedDigraph(int V)
Initializes an empty edge-weighted digraph with
V vertices and 0 edges. |
EdgeWeightedDigraph(int V,
int E)
Initializes a random edge-weighted digraph with
V vertices and E edges. |
Modifier and Type | Method and Description |
---|---|
void |
addEdge(DirectedEdge e)
Adds the directed edge
e to this edge-weighted digraph. |
java.lang.Iterable<DirectedEdge> |
adj(int v)
Returns the directed edges incident from vertex
v . |
int |
E()
Returns the number of edges in this edge-weighted digraph.
|
java.lang.Iterable<DirectedEdge> |
edges()
Returns all directed edges in this edge-weighted digraph.
|
int |
indegree(int v)
Returns the number of directed edges incident to vertex
v . |
boolean |
isIn(DirectedEdge e) |
void |
merge(EdgeWeightedDigraph G) |
int |
outdegree(int v)
Returns the number of directed edges incident from vertex
v . |
void |
removeEdges(DirectedEdge e2remove) |
java.lang.String |
toString()
Returns a string representation of this edge-weighted digraph.
|
int |
V()
Returns the number of vertices in this edge-weighted digraph.
|
public final int V
public int E
public Bag<DirectedEdge>[] adj
public EdgeWeightedDigraph(int V)
V
vertices and 0 edges.V
- the number of verticesjava.lang.IllegalArgumentException
- if V < 0
public EdgeWeightedDigraph(int V, int E)
V
vertices and E edges.V
- the number of verticesE
- the number of edgesjava.lang.IllegalArgumentException
- if V < 0
java.lang.IllegalArgumentException
- if E < 0
public EdgeWeightedDigraph(In in)
in
- the input streamjava.lang.IndexOutOfBoundsException
- if the endpoints of any edge are not in prescribed rangejava.lang.IllegalArgumentException
- if the number of vertices or edges is negativepublic EdgeWeightedDigraph(EdgeWeightedDigraph G)
G
.G
- the edge-weighted digraph to copypublic void merge(EdgeWeightedDigraph G)
public int V()
public int E()
public void addEdge(DirectedEdge e)
e
to this edge-weighted digraph.e
- the edgejava.lang.IndexOutOfBoundsException
- unless endpoints of edge are between 0
and V-1
public boolean isIn(DirectedEdge e)
public void removeEdges(DirectedEdge e2remove)
public java.lang.Iterable<DirectedEdge> adj(int v)
v
.v
- the vertexv
as an Iterablejava.lang.IndexOutOfBoundsException
- unless 0 <= v < V
public int outdegree(int v)
v
.
This is known as the outdegree of vertex v
.v
- the vertexv
java.lang.IndexOutOfBoundsException
- unless 0 <= v < V
public int indegree(int v)
v
.
This is known as the indegree of vertex v
.v
- the vertexv
java.lang.IndexOutOfBoundsException
- unless 0 <= v < V
public java.lang.Iterable<DirectedEdge> edges()
for (DirectedEdge e : G.edges())
.public java.lang.String toString()
toString
in class java.lang.Object