类 Digraph
- java.lang.Object
-
- edu.princeton.cs.algs4.Digraph
-
public class Digraph extends java.lang.ObjectTheDigraphclass represents a directed graph of vertices named 0 through V - 1. It supports the following two primary operations: add an edge to the digraph, iterate over all of the vertices adjacent from a given vertex. Parallel edges and self-loops are permitted.This implementation uses an adjacency-lists representation, which is a vertex-indexed array of
Bagobjects. All operations take constant time (in the worst case) except iterating over the vertices adjacent from a given vertex, which takes time proportional to the number of such vertices.For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
方法概要
修饰符和类型 方法 说明 voidaddEdge(int v, int w)Adds the directed edge v→w to this digraph.java.lang.Iterable<java.lang.Integer>adj(int v)Returns the vertices adjacent from vertexvin this digraph.intE()Returns the number of edges in this digraph.intindegree(int v)Returns the number of directed edges incident to vertexv.static voidmain(java.lang.String[] args)Unit tests theDigraphdata type.intoutdegree(int v)Returns the number of directed edges incident from vertexv.Digraphreverse()Returns the reverse of the digraph.java.lang.StringtoString()Returns a string representation of the graph.intV()Returns the number of vertices in this digraph.
-
-
-
构造器详细资料
-
Digraph
public Digraph(int V)
Initializes an empty digraph with V vertices.- 参数:
V- the number of vertices- 抛出:
java.lang.IllegalArgumentException- ifV < 0
-
Digraph
public Digraph(In in)
Initializes a 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, 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 negativejava.lang.IllegalArgumentException- if the input stream is in the wrong format
-
Digraph
public Digraph(Digraph G)
Initializes a new digraph that is a deep copy of the specified digraph.- 参数:
G- the digraph to copy
-
-
方法详细资料
-
V
public int V()
Returns the number of vertices in this digraph.- 返回:
- the number of vertices in this digraph
-
E
public int E()
Returns the number of edges in this digraph.- 返回:
- the number of edges in this digraph
-
addEdge
public void addEdge(int v, int w)Adds the directed edge v→w to this digraph.- 参数:
v- the tail vertexw- the head vertex- 抛出:
java.lang.IllegalArgumentException- unless both0 <= v < Vand0 <= w < V
-
adj
public java.lang.Iterable<java.lang.Integer> adj(int v)
Returns the vertices adjacent from vertexvin this digraph.- 参数:
v- the vertex- 返回:
- the vertices adjacent from vertex
vin this digraph, 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
-
reverse
public Digraph reverse()
Returns the reverse of the digraph.- 返回:
- the reverse of the digraph
-
toString
public java.lang.String toString()
Returns a string representation of the graph.- 覆盖:
toString在类中java.lang.Object- 返回:
- the number of vertices V, followed by the number of edges E, followed by the V adjacency lists
-
main
public static void main(java.lang.String[] args)
Unit tests theDigraphdata type.- 参数:
args- the command-line arguments
-
-