类 EdgeWeightedDigraph
- java.lang.Object
-
- edu.princeton.cs.algs4.EdgeWeightedDigraph
-
public class EdgeWeightedDigraph extends java.lang.Object
TheEdgeWeightedDigraph
class represents a edge-weighted digraph of vertices named 0 through V - 1, where each directed edge is of typeDirectedEdge
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
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.
-
-
构造器概要
构造器 构造器 说明 EdgeWeightedDigraph(int V)
Initializes an empty edge-weighted digraph withV
vertices and 0 edges.EdgeWeightedDigraph(int V, int E)
Initializes a random edge-weighted digraph withV
vertices and E edges.EdgeWeightedDigraph(EdgeWeightedDigraph G)
Initializes a new edge-weighted digraph that is a deep copy ofG
.EdgeWeightedDigraph(In in)
Initializes an edge-weighted digraph from the specified input stream.
-
方法概要
修饰符和类型 方法 说明 void
addEdge(DirectedEdge e)
Adds the directed edgee
to this edge-weighted digraph.java.lang.Iterable<DirectedEdge>
adj(int v)
Returns the directed edges incident from vertexv
.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 vertexv
.static void
main(java.lang.String[] args)
Unit tests theEdgeWeightedDigraph
data type.int
outdegree(int v)
Returns the number of directed edges incident from vertexv
.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.
-
-
-
构造器详细资料
-
EdgeWeightedDigraph
public EdgeWeightedDigraph(int V)
Initializes an empty edge-weighted digraph withV
vertices and 0 edges.- 参数:
V
- the number of vertices- 抛出:
java.lang.IllegalArgumentException
- ifV < 0
-
EdgeWeightedDigraph
public EdgeWeightedDigraph(int V, int E)
Initializes a random edge-weighted digraph withV
vertices and E edges.- 参数:
V
- the number of verticesE
- the number of edges- 抛出:
java.lang.IllegalArgumentException
- ifV < 0
java.lang.IllegalArgumentException
- ifE < 0
-
EdgeWeightedDigraph
public EdgeWeightedDigraph(In in)
Initializes an edge-weighted digraph from the specified input stream. The format is the number of vertices V, followed by the number of edges E, followed by E pairs of vertices and edge weights, with each entry separated by whitespace.- 参数:
in
- the input stream- 抛出:
java.lang.IllegalArgumentException
- if the endpoints of any edge are not in prescribed rangejava.lang.IllegalArgumentException
- if the number of vertices or edges is negative
-
EdgeWeightedDigraph
public EdgeWeightedDigraph(EdgeWeightedDigraph G)
Initializes a new edge-weighted digraph that is a deep copy ofG
.- 参数:
G
- the edge-weighted digraph to copy
-
-
方法详细资料
-
V
public int V()
Returns the number of vertices in this edge-weighted digraph.- 返回:
- the number of vertices in this edge-weighted digraph
-
E
public int E()
Returns the number of edges in this edge-weighted digraph.- 返回:
- the number of edges in this edge-weighted digraph
-
addEdge
public void addEdge(DirectedEdge e)
Adds the directed edgee
to this edge-weighted digraph.- 参数:
e
- the edge- 抛出:
java.lang.IllegalArgumentException
- unless endpoints of edge are between0
andV-1
-
adj
public java.lang.Iterable<DirectedEdge> adj(int v)
Returns the directed edges incident from vertexv
.- 参数:
v
- the vertex- 返回:
- the directed edges incident from vertex
v
as an Iterable - 抛出:
java.lang.IllegalArgumentException
- unless0 <= v < V
-
outdegree
public int outdegree(int v)
Returns the number of directed edges incident from vertexv
. This is known as the outdegree of vertexv
.- 参数:
v
- the vertex- 返回:
- the outdegree of vertex
v
- 抛出:
java.lang.IllegalArgumentException
- unless0 <= v < V
-
indegree
public int indegree(int v)
Returns the number of directed edges incident to vertexv
. This is known as the indegree of vertexv
.- 参数:
v
- the vertex- 返回:
- the indegree of vertex
v
- 抛出:
java.lang.IllegalArgumentException
- unless0 <= v < V
-
edges
public java.lang.Iterable<DirectedEdge> edges()
Returns all directed edges in this edge-weighted digraph. To iterate over the edges in this edge-weighted digraph, use foreach notation:for (DirectedEdge e : G.edges())
.- 返回:
- all edges in this edge-weighted digraph, as an iterable
-
toString
public java.lang.String toString()
Returns a string representation of this edge-weighted digraph.- 覆盖:
toString
在类中java.lang.Object
- 返回:
- the number of vertices V, followed by the number of edges E, followed by the V adjacency lists of edges
-
main
public static void main(java.lang.String[] args)
Unit tests theEdgeWeightedDigraph
data type.- 参数:
args
- the command-line arguments
-
-