V
- the graph vertex typeE
- the graph edge typepublic class GnmRandomGraphGenerator<V,E> extends Object implements GraphGenerator<V,E,V>
In the G(n, M) model, a graph is chosen uniformly at random from the collection of all graphs which have n nodes and M edges. For example, in the G(3, 2) model, each of the three possible graphs on three vertices and two edges are included with probability 1/3.
The implementation creates the vertices and then randomly chooses an edge and tries to add it. If the add fails for any reason (an edge already exists and multiple edges are not allowed) it will just choose another and try again. The performance therefore varies significantly based on the probability of successfully constructing an acceptable edge.
The implementation tries to guess the number of allowed edges based on the following. If
self-loops or multiple edges are allowed and requested, the maximum number of edges is
Integer.MAX_VALUE
. Otherwise the maximum for undirected graphs with n vertices is
n(n-1)/2 while for directed n(n-1). If the graph type cannot be determined (for example using
adapter classes or user-created custom graph types) the generator assumes the graph is undirected
and therefore uses n(n-1)/2 as the maximum number of edges. If the user requests self-loops
and/or multiple edges and the graph type cannot be determined, the corresponding feature is
silently ignored.
For the G(n, p) model please see GnpRandomGraphGenerator
.
GnpRandomGraphGenerator
Constructor and Description |
---|
GnmRandomGraphGenerator(int n,
int m)
Create a new G(n, M) random graph generator.
|
GnmRandomGraphGenerator(int n,
int m,
long seed)
Create a new G(n, M) random graph generator.
|
GnmRandomGraphGenerator(int n,
int m,
long seed,
boolean loops,
boolean multipleEdges)
Create a new G(n, M) random graph generator
|
GnmRandomGraphGenerator(int n,
int m,
Random rng,
boolean loops,
boolean multipleEdges)
Create a new G(n, M) random graph generator
|
Modifier and Type | Method and Description |
---|---|
void |
generateGraph(Graph<V,E> target,
VertexFactory<V> vertexFactory,
Map<String,V> resultMap)
Generates a random graph based on the G(n, M) model
|
public GnmRandomGraphGenerator(int n, int m)
n
- the number of nodesm
- the number of edgespublic GnmRandomGraphGenerator(int n, int m, long seed)
n
- the number of nodesm
- the number of edgesseed
- seed for the random number generatorpublic GnmRandomGraphGenerator(int n, int m, long seed, boolean loops, boolean multipleEdges)
n
- the number of nodesm
- the number of edgesseed
- seed for the random number generatorloops
- whether the generated graph may contain loopsmultipleEdges
- whether the generated graph many contain multiple edges between the same
two verticespublic GnmRandomGraphGenerator(int n, int m, Random rng, boolean loops, boolean multipleEdges)
n
- the number of nodesm
- the number of edgesrng
- the random number generatorloops
- whether the generated graph may contain loopsmultipleEdges
- whether the generated graph many contain multiple edges between the same
two verticespublic void generateGraph(Graph<V,E> target, VertexFactory<V> vertexFactory, Map<String,V> resultMap)
generateGraph
in interface GraphGenerator<V,E,V>
target
- the target graphvertexFactory
- the vertex factoryresultMap
- not used by this generator, can be nullIllegalArgumentException
- if the number of edges, passed in the constructor, cannot be
created on a graph of the concrete type with the specified number of verticesIllegalArgumentException
- if the graph does not support a requested feature such as
self-loops or multiple edgesCopyright © 2017. All rights reserved.