RandomGraph - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Mozilla Firefox.

Online Help

All Products    Maple    MapleSim


GraphTheory[RandomGraphs]

  

RandomGraph

  

generate random graph

 

Calling Sequence

Parameters

Options

Description

Examples

Calling Sequence

RandomGraph(V,p,options)

RandomGraph(V,m,options)

RandomGraph(n,p,options)

RandomGraph(n,m,options)

Parameters

V

-

list of vertex labels

n

-

positive integer

p

-

numeric value between 0.0 and 1.0

m

-

non-negative integer

options

-

sequence of options (see below)

Options

• 

connected = truefalse 

  

If the option connected is specified, the graph created is connected, and hence has at least n-1 edges.

  

For RandomGraph(n,m,connected), m must be at least n-1. A random tree is first created, then the remaining m-n+1 edges are

  

For RandomGraph(n,p,connected), a random tree is first created then each remaining edge is present with probability p.

• 

degree = nonnegint

  

If the option degree=d is specified, and d-regular n vertex graph is possible, then a random d-regular graph having n vertices will be returned. Note that this option cannot be present with the directed option. This is equivalent to using the RandomRegularGraph command.

• 

directed = truefalse 

  

If the option directed is specified, a random directed graph is chosen. This is equivalent to using the RandomDigraph command. Default value is false.

• 

seed = integer or none

  

Seed for the random number generator. When an integer is specified, this is equivalent to calling randomize(seed).

• 

weights = range

  

If the option weights=m..n is specified, where m≤n are integers, the graph is a weighted graph with integer edge weights chosen from [m,n] uniformly at random.  The weight matrix W in the graph has datatype=integer, and if the edge from vertex i to j is not in the graph then W[i,j] = 0.

  

If the option weights=x..y where x≤y are decimals is specified, the graph is a weighted graph with numerical edge weights chosen from [x,y] uniformly at random.  The weight matrix W in the graph has datatype=float[8], that is, double precision floats (16 decimal digits), and if the edge from vertex i to j is not in the graph then W[i,j] = 0.0.

  

If the option weights=f where f is a function (a Maple procedure) that returns a number (integer, rational, or decimal number), then f is used to generate the edge weights.  The weight matrix W in the graph has datatype=anything, and if the edge from vertex i to j is not in the graph then W[i,j] = 0.

Description

• 

RandomGraph(n,p) creates an undirected unweighted graph on n vertices where each possible edge is present with probability p where 0.0≤p≤1.0.

• 

RandomGraph(n,m) creates an undirected unweighted graph on n vertices and m edges where the m edges are chosen uniformly at random. The value of m must satisfy 0≤m≤binomial⁡n,2=n⁢n−12.

• 

If the first input is a positive integer n, then the vertices are labeled 1,2,...,n.  Alternatively, you may specify the vertex labels in a list.

• 

This model of random graph generation, in which edges are selected with uniform probability from all possible edges in a graph on the specified vertices, is known as the Erdős–Rényi model.

Examples

> 

with⁡GraphTheory:

> 

with⁡RandomGraphs:

> 

G≔RandomGraph⁡8,0.5

G≔Graph 1: an undirected graph with 8 vertices and 10 edges

(1)
> 

G≔RandomGraph⁡8,10

G≔Graph 2: an undirected graph with 8 vertices and 10 edges

(2)
> 

G≔RandomGraph⁡8,10,connected

G≔Graph 3: an undirected graph with 8 vertices and 10 edges

(3)
> 

IsConnected⁡G

true

(4)
> 

G≔RandomGraph⁡6,degree=3

G≔Graph 4: an undirected graph with 6 vertices and 9 edges

(5)
> 

IsRegular⁡G

true

(6)
> 

H≔RandomGraph⁡4,1.0,weights=0...1.0

H≔Graph 5: an undirected weighted graph with 4 vertices and 6 edges

(7)
> 

WeightMatrix⁡H

0.0.8097345519119300.2301560659520940.7617312084830850.8097345519119300.0.1580575789408720.5809566791893210.2301560659520940.1580575789408720.0.4231651198811190.7617312084830850.5809566791893210.4231651198811190.

(8)
> 

H≔RandomGraph⁡8,10,connected,weights=1..4

H≔Graph 6: an undirected weighted graph with 8 vertices and 10 edges

(9)
> 

WeightMatrix⁡H

0410020040010020100320300130004400200000200000000234000000040000

(10)
> 

U≔rand⁡1..4:

> 

f := proc() local x; x := U(); if x=1 then 1 else 2 end if; end proc:

> 

H≔RandomGraph⁡6,1.0,weights=f

H≔Graph 7: an undirected weighted graph with 6 vertices and 15 edges

(11)
> 

WeightMatrix⁡H

021122201121110212112022221202212220

(12)

See Also

AssignEdgeWeights

GraphTheory:-IsConnected

GraphTheory:-WeightMatrix

RandomBipartiteGraph

RandomDigraph

RandomNetwork

RandomRegularGraph

RandomTournament

RandomTree