类 DigraphGenerator
- java.lang.Object
-
- edu.princeton.cs.algs4.DigraphGenerator
-
public class DigraphGenerator extends java.lang.ObjectTheDigraphGeneratorclass provides static methods for creating various digraphs, including Erdos-Renyi random digraphs, random DAGs, random rooted trees, random rooted DAGs, random tournaments, path digraphs, cycle digraphs, and the complete digraph.For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
方法概要
修饰符和类型 方法 说明 static DigraphbinaryTree(int V)Returns a complete binary tree digraph onVvertices.static Digraphcomplete(int V)Returns the complete digraph onVvertices.static DigraphcompleteRootedInDAG(int V)Returns a complete rooted-in DAG onVvertices.static DigraphcompleteRootedOutDAG(int V)Returns a complete rooted-out DAG onVvertices.static Digraphcycle(int V)Returns a cycle digraph onVvertices.static Digraphdag(int V, int E)Returns a random simple DAG containingVvertices andEedges.static DigrapheulerianCycle(int V, int E)Returns an Eulerian cycle digraph onVvertices.static DigrapheulerianPath(int V, int E)Returns an Eulerian path digraph onVvertices.static voidmain(java.lang.String[] args)Unit tests theDigraphGeneratorlibrary.static Digraphpath(int V)Returns a path digraph onVvertices.static DigraphrootedInDAG(int V, int E)Returns a random rooted-in DAG onVvertices andEedges.static DigraphrootedInTree(int V)Returns a random rooted-in tree onVvertices.static DigraphrootedOutDAG(int V, int E)Returns a random rooted-out DAG onVvertices andEedges.static DigraphrootedOutTree(int V)Returns a random rooted-out tree onVvertices.static Digraphsimple(int V, double p)Returns a random simple digraph onVvertices, with an edge between any two vertices with probabilityp.static Digraphsimple(int V, int E)Returns a random simple digraph containingVvertices andEedges.static Digraphstrong(int V, int E, int c)Returns a random simple digraph onVvertices,Eedges and (at least)cstrong components.static Digraphtournament(int V)Returns a random tournament digraph onVvertices.
-
-
-
方法详细资料
-
simple
public static Digraph simple(int V, int E)
Returns a random simple digraph containingVvertices andEedges.- 参数:
V- the number of verticesE- the number of vertices- 返回:
- a random simple digraph on
Vvertices, containing a total ofEedges - 抛出:
java.lang.IllegalArgumentException- if no such simple digraph exists
-
simple
public static Digraph simple(int V, double p)
Returns a random simple digraph onVvertices, with an edge between any two vertices with probabilityp. This is sometimes referred to as the Erdos-Renyi random digraph model. This implementations takes time propotional to V^2 (even ifpis small).- 参数:
V- the number of verticesp- the probability of choosing an edge- 返回:
- a random simple digraph on
Vvertices, with an edge between any two vertices with probabilityp - 抛出:
java.lang.IllegalArgumentException- if probability is not between 0 and 1
-
complete
public static Digraph complete(int V)
Returns the complete digraph onVvertices. In a complete digraph, every pair of distinct vertices is connected by two antiparallel edges. There areV*(V-1)edges.- 参数:
V- the number of vertices- 返回:
- the complete digraph on
Vvertices
-
dag
public static Digraph dag(int V, int E)
Returns a random simple DAG containingVvertices andEedges. Note: it is not uniformly selected at random among all such DAGs.- 参数:
V- the number of verticesE- the number of vertices- 返回:
- a random simple DAG on
Vvertices, containing a total ofEedges - 抛出:
java.lang.IllegalArgumentException- if no such simple DAG exists
-
tournament
public static Digraph tournament(int V)
Returns a random tournament digraph onVvertices. A tournament digraph is a DAG in which for every two vertices, there is one directed edge. A tournament is an oriented complete graph.- 参数:
V- the number of vertices- 返回:
- a random tournament digraph on
Vvertices
-
completeRootedInDAG
public static Digraph completeRootedInDAG(int V)
Returns a complete rooted-in DAG onVvertices. A rooted in-tree is a DAG in which there is a single vertex reachable from every other vertex. A complete rooted in-DAG has V*(V-1)/2 edges.- 参数:
V- the number of vertices- 返回:
- a complete rooted-in DAG on
Vvertices
-
rootedInDAG
public static Digraph rootedInDAG(int V, int E)
Returns a random rooted-in DAG onVvertices andEedges. A rooted in-tree is a DAG in which there is a single vertex reachable from every other vertex. The DAG returned is not chosen uniformly at random among all such DAGs.- 参数:
V- the number of verticesE- the number of edges- 返回:
- a random rooted-in DAG on
Vvertices andEedges
-
completeRootedOutDAG
public static Digraph completeRootedOutDAG(int V)
Returns a complete rooted-out DAG onVvertices. A rooted out-tree is a DAG in which every vertex is reachable from a single vertex. A complete rooted in-DAG has V*(V-1)/2 edges.- 参数:
V- the number of vertices- 返回:
- a complete rooted-out DAG on
Vvertices
-
rootedOutDAG
public static Digraph rootedOutDAG(int V, int E)
Returns a random rooted-out DAG onVvertices andEedges. A rooted out-tree is a DAG in which every vertex is reachable from a single vertex. The DAG returned is not chosen uniformly at random among all such DAGs.- 参数:
V- the number of verticesE- the number of edges- 返回:
- a random rooted-out DAG on
Vvertices andEedges
-
rootedInTree
public static Digraph rootedInTree(int V)
Returns a random rooted-in tree onVvertices. A rooted in-tree is an oriented tree in which there is a single vertex reachable from every other vertex. The tree returned is not chosen uniformly at random among all such trees.- 参数:
V- the number of vertices- 返回:
- a random rooted-in tree on
Vvertices
-
rootedOutTree
public static Digraph rootedOutTree(int V)
Returns a random rooted-out tree onVvertices. A rooted out-tree is an oriented tree in which each vertex is reachable from a single vertex. It is also known as a arborescence or branching. The tree returned is not chosen uniformly at random among all such trees.- 参数:
V- the number of vertices- 返回:
- a random rooted-out tree on
Vvertices
-
path
public static Digraph path(int V)
Returns a path digraph onVvertices.- 参数:
V- the number of vertices in the path- 返回:
- a digraph that is a directed path on
Vvertices
-
binaryTree
public static Digraph binaryTree(int V)
Returns a complete binary tree digraph onVvertices.- 参数:
V- the number of vertices in the binary tree- 返回:
- a digraph that is a complete binary tree on
Vvertices
-
cycle
public static Digraph cycle(int V)
Returns a cycle digraph onVvertices.- 参数:
V- the number of vertices in the cycle- 返回:
- a digraph that is a directed cycle on
Vvertices
-
eulerianCycle
public static Digraph eulerianCycle(int V, int E)
Returns an Eulerian cycle digraph onVvertices.- 参数:
V- the number of vertices in the cycleE- the number of edges in the cycle- 返回:
- a digraph that is a directed Eulerian cycle on
Vvertices andEedges - 抛出:
java.lang.IllegalArgumentException- if eitherV <= 0orE <= 0
-
eulerianPath
public static Digraph eulerianPath(int V, int E)
Returns an Eulerian path digraph onVvertices.- 参数:
V- the number of vertices in the pathE- the number of edges in the path- 返回:
- a digraph that is a directed Eulerian path on
Vvertices andEedges - 抛出:
java.lang.IllegalArgumentException- if eitherV <= 0orE < 0
-
strong
public static Digraph strong(int V, int E, int c)
Returns a random simple digraph onVvertices,Eedges and (at least)cstrong components. The vertices are randomly assigned integer labels between0andc-1(corresponding to strong components). Then, a strong component is creates among the vertices with the same label. Next, random edges (either between two vertices with the same labels or from a vetex with a smaller label to a vertex with a larger label). The number of components will be equal to the number of distinct labels that are assigned to vertices.- 参数:
V- the number of verticesE- the number of edgesc- the (maximum) number of strong components- 返回:
- a random simple digraph on
Vvertices andEedges, with (at most)cstrong components - 抛出:
java.lang.IllegalArgumentException- ifcis larger thanV
-
main
public static void main(java.lang.String[] args)
Unit tests theDigraphGeneratorlibrary.- 参数:
args- the command-line arguments
-
-