类 GraphGenerator
- java.lang.Object
-
- edu.princeton.cs.algs4.GraphGenerator
-
public class GraphGenerator extends java.lang.ObjectTheGraphGeneratorclass provides static methods for creating various graphs, including Erdos-Renyi random graphs, random bipartite graphs, random k-regular graphs, and random rooted trees.For additional documentation, see Section 4.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
方法概要
修饰符和类型 方法 说明 static GraphbinaryTree(int V)Returns a complete binary tree graph onVvertices.static Graphbipartite(int V1, int V2, double p)Returns a random simple bipartite graph onV1andV2vertices, containing each possible edge with probabilityp.static Graphbipartite(int V1, int V2, int E)Returns a random simple bipartite graph onV1andV2vertices withEedges.static Graphcomplete(int V)Returns the complete graph onVvertices.static GraphcompleteBipartite(int V1, int V2)Returns a complete bipartite graph onV1andV2vertices.static Graphcycle(int V)Returns a cycle graph onVvertices.static GrapheulerianCycle(int V, int E)Returns an Eulerian cycle graph onVvertices.static GrapheulerianPath(int V, int E)Returns an Eulerian path graph onVvertices.static voidmain(java.lang.String[] args)Unit tests theGraphGeneratorlibrary.static Graphpath(int V)Returns a path graph onVvertices.static Graphregular(int V, int k)Returns a uniformly randomk-regular graph onVvertices (not necessarily simple).static Graphsimple(int V, double p)Returns a random simple graph onVvertices, with an edge between any two vertices with probabilityp.static Graphsimple(int V, int E)Returns a random simple graph containingVvertices andEedges.static Graphstar(int V)Returns a star graph onVvertices.static Graphtree(int V)Returns a uniformly random tree onVvertices.static Graphwheel(int V)Returns a wheel graph onVvertices.
-
-
-
方法详细资料
-
simple
public static Graph simple(int V, int E)
Returns a random simple graph containingVvertices andEedges.- 参数:
V- the number of verticesE- the number of vertices- 返回:
- a random simple graph on
Vvertices, containing a total ofEedges - 抛出:
java.lang.IllegalArgumentException- if no such simple graph exists
-
simple
public static Graph simple(int V, double p)
Returns a random simple graph onVvertices, with an edge between any two vertices with probabilityp. This is sometimes referred to as the Erdos-Renyi random graph model.- 参数:
V- the number of verticesp- the probability of choosing an edge- 返回:
- a random simple graph 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 Graph complete(int V)
Returns the complete graph onVvertices.- 参数:
V- the number of vertices- 返回:
- the complete graph on
Vvertices
-
completeBipartite
public static Graph completeBipartite(int V1, int V2)
Returns a complete bipartite graph onV1andV2vertices.- 参数:
V1- the number of vertices in one partitionV2- the number of vertices in the other partition- 返回:
- a complete bipartite graph on
V1andV2vertices - 抛出:
java.lang.IllegalArgumentException- if probability is not between 0 and 1
-
bipartite
public static Graph bipartite(int V1, int V2, int E)
Returns a random simple bipartite graph onV1andV2vertices withEedges.- 参数:
V1- the number of vertices in one partitionV2- the number of vertices in the other partitionE- the number of edges- 返回:
- a random simple bipartite graph on
V1andV2vertices, containing a total ofEedges - 抛出:
java.lang.IllegalArgumentException- if no such simple bipartite graph exists
-
bipartite
public static Graph bipartite(int V1, int V2, double p)
Returns a random simple bipartite graph onV1andV2vertices, containing each possible edge with probabilityp.- 参数:
V1- the number of vertices in one partitionV2- the number of vertices in the other partitionp- the probability that the graph contains an edge with one endpoint in either side- 返回:
- a random simple bipartite graph on
V1andV2vertices, containing each possible edge with probabilityp - 抛出:
java.lang.IllegalArgumentException- if probability is not between 0 and 1
-
path
public static Graph path(int V)
Returns a path graph onVvertices.- 参数:
V- the number of vertices in the path- 返回:
- a path graph on
Vvertices
-
binaryTree
public static Graph binaryTree(int V)
Returns a complete binary tree graph onVvertices.- 参数:
V- the number of vertices in the binary tree- 返回:
- a complete binary tree graph on
Vvertices
-
cycle
public static Graph cycle(int V)
Returns a cycle graph onVvertices.- 参数:
V- the number of vertices in the cycle- 返回:
- a cycle graph on
Vvertices
-
eulerianCycle
public static Graph eulerianCycle(int V, int E)
Returns an Eulerian cycle graph onVvertices.- 参数:
V- the number of vertices in the cycleE- the number of edges in the cycle- 返回:
- a graph that is an Eulerian cycle on
Vvertices andEedges - 抛出:
java.lang.IllegalArgumentException- if eitherV <= 0orE <= 0
-
eulerianPath
public static Graph eulerianPath(int V, int E)
Returns an Eulerian path graph onVvertices.- 参数:
V- the number of vertices in the pathE- the number of edges in the path- 返回:
- a graph that is an Eulerian path on
Vvertices andEedges - 抛出:
java.lang.IllegalArgumentException- if eitherV <= 0orE < 0
-
wheel
public static Graph wheel(int V)
Returns a wheel graph onVvertices.- 参数:
V- the number of vertices in the wheel- 返回:
- a wheel graph on
Vvertices: a single vertex connected to every vertex in a cycle onV-1vertices
-
star
public static Graph star(int V)
Returns a star graph onVvertices.- 参数:
V- the number of vertices in the star- 返回:
- a star graph on
Vvertices: a single vertex connected to every other vertex
-
regular
public static Graph regular(int V, int k)
Returns a uniformly randomk-regular graph onVvertices (not necessarily simple). The graph is simple with probability only about e^(-k^2/4), which is tiny when k = 14.- 参数:
V- the number of vertices in the graphk- degree of each vertex- 返回:
- a uniformly random
k-regular graph onVvertices.
-
tree
public static Graph tree(int V)
Returns a uniformly random tree onVvertices. This algorithm uses a Prufer sequence and takes time proportional to V log V.- 参数:
V- the number of vertices in the tree- 返回:
- a uniformly random tree on
Vvertices
-
main
public static void main(java.lang.String[] args)
Unit tests theGraphGeneratorlibrary.- 参数:
args- the command-line arguments
-
-