Package edu.princeton.cs.algs4
Class GraphGenerator
- Object
-
- edu.princeton.cs.algs4.GraphGenerator
-
public class GraphGenerator extends Object
TheGraphGenerator
class 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.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Graph
binaryTree(int V)
Returns a complete binary tree graph onV
vertices.static Graph
bipartite(int V1, int V2, double p)
Returns a random simple bipartite graph onV1
andV2
vertices, containing each possible edge with probabilityp
.static Graph
bipartite(int V1, int V2, int E)
Returns a random simple bipartite graph onV1
andV2
vertices withE
edges.static Graph
complete(int V)
Returns the complete graph onV
vertices.static Graph
completeBipartite(int V1, int V2)
Returns a complete bipartite graph onV1
andV2
vertices.static Graph
cycle(int V)
Returns a cycle graph onV
vertices.static Graph
eulerianCycle(int V, int E)
Returns an Eulerian cycle graph onV
vertices.static Graph
eulerianPath(int V, int E)
Returns an Eulerian path graph onV
vertices.static void
main(String[] args)
Unit tests theGraphGenerator
library.static Graph
path(int V)
Returns a path graph onV
vertices.static Graph
regular(int V, int k)
Returns a uniformly randomk
-regular graph onV
vertices (not necessarily simple).static Graph
simple(int V, double p)
Returns a random simple graph onV
vertices, with an edge between any two vertices with probabilityp
.static Graph
simple(int V, int E)
Returns a random simple graph containingV
vertices andE
edges.static Graph
star(int V)
Returns a star graph onV
vertices.static Graph
tree(int V)
Returns a uniformly random tree onV
vertices.static Graph
wheel(int V)
Returns a wheel graph onV
vertices.
-
-
-
Method Detail
-
simple
public static Graph simple(int V, int E)
Returns a random simple graph containingV
vertices andE
edges.- Parameters:
V
- the number of verticesE
- the number of vertices- Returns:
- a random simple graph on
V
vertices, containing a total ofE
edges - Throws:
IllegalArgumentException
- if no such simple graph exists
-
simple
public static Graph simple(int V, double p)
Returns a random simple graph onV
vertices, with an edge between any two vertices with probabilityp
. This is sometimes referred to as the Erdos-Renyi random graph model.- Parameters:
V
- the number of verticesp
- the probability of choosing an edge- Returns:
- a random simple graph on
V
vertices, with an edge between any two vertices with probabilityp
- Throws:
IllegalArgumentException
- if probability is not between 0 and 1
-
complete
public static Graph complete(int V)
Returns the complete graph onV
vertices.- Parameters:
V
- the number of vertices- Returns:
- the complete graph on
V
vertices
-
completeBipartite
public static Graph completeBipartite(int V1, int V2)
Returns a complete bipartite graph onV1
andV2
vertices.- Parameters:
V1
- the number of vertices in one partitionV2
- the number of vertices in the other partition- Returns:
- a complete bipartite graph on
V1
andV2
vertices - Throws:
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 onV1
andV2
vertices withE
edges.- Parameters:
V1
- the number of vertices in one partitionV2
- the number of vertices in the other partitionE
- the number of edges- Returns:
- a random simple bipartite graph on
V1
andV2
vertices, containing a total ofE
edges - Throws:
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 onV1
andV2
vertices, containing each possible edge with probabilityp
.- Parameters:
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- Returns:
- a random simple bipartite graph on
V1
andV2
vertices, containing each possible edge with probabilityp
- Throws:
IllegalArgumentException
- if probability is not between 0 and 1
-
path
public static Graph path(int V)
Returns a path graph onV
vertices.- Parameters:
V
- the number of vertices in the path- Returns:
- a path graph on
V
vertices
-
binaryTree
public static Graph binaryTree(int V)
Returns a complete binary tree graph onV
vertices.- Parameters:
V
- the number of vertices in the binary tree- Returns:
- a complete binary tree graph on
V
vertices
-
cycle
public static Graph cycle(int V)
Returns a cycle graph onV
vertices.- Parameters:
V
- the number of vertices in the cycle- Returns:
- a cycle graph on
V
vertices
-
eulerianCycle
public static Graph eulerianCycle(int V, int E)
Returns an Eulerian cycle graph onV
vertices.- Parameters:
V
- the number of vertices in the cycleE
- the number of edges in the cycle- Returns:
- a graph that is an Eulerian cycle on
V
vertices andE
edges - Throws:
IllegalArgumentException
- if eitherV <= 0
orE <= 0
-
eulerianPath
public static Graph eulerianPath(int V, int E)
Returns an Eulerian path graph onV
vertices.- Parameters:
V
- the number of vertices in the pathE
- the number of edges in the path- Returns:
- a graph that is an Eulerian path on
V
vertices andE
edges - Throws:
IllegalArgumentException
- if eitherV <= 0
orE < 0
-
wheel
public static Graph wheel(int V)
Returns a wheel graph onV
vertices.- Parameters:
V
- the number of vertices in the wheel- Returns:
- a wheel graph on
V
vertices: a single vertex connected to every vertex in a cycle onV-1
vertices
-
star
public static Graph star(int V)
Returns a star graph onV
vertices.- Parameters:
V
- the number of vertices in the star- Returns:
- a star graph on
V
vertices: a single vertex connected to every other vertex
-
regular
public static Graph regular(int V, int k)
Returns a uniformly randomk
-regular graph onV
vertices (not necessarily simple). The graph is simple with probability only about e^(-k^2/4), which is tiny when k = 14.- Parameters:
V
- the number of vertices in the graphk
- degree of each vertex- Returns:
- a uniformly random
k
-regular graph onV
vertices.
-
tree
public static Graph tree(int V)
Returns a uniformly random tree onV
vertices. This algorithm uses a Prufer sequence and takes time proportional to V log V.- Parameters:
V
- the number of vertices in the tree- Returns:
- a uniformly random tree on
V
vertices
-
main
public static void main(String[] args)
Unit tests theGraphGenerator
library.- Parameters:
args
- the command-line arguments
-
-