Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Graph::createRandomGraph

Generates a random graph.

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Syntax

Graph::createRandomGraph(VertexNr, EdgeNr, <Directed | Undirected>)

Description

Graph::createRandomGraph generates a random graph.

Graph::createRandomGraph(VertexNr, EdgeNr) generates a random graph with VertexNr vertices and EdgeNr edges.

    Note:   If the number EdgeNr is too great (i.e. ), a complete graph will be created.

Graph::createRandomGraph(VertexNr, EdgeNr, Undirected) generates a random graph with VertexNr vertices and 2 EdgeNr edges is created. This is due to the fact that no odd number of undirected edges could be created otherwise.

Examples

Example 1

The following graph was created randomly, meaning that your results will most probably differ:

G := Graph::createRandomGraph(5,6):
Graph::printGraphInformation(G)
Vertices: [1, 2, 3, 4, 5]
Edges: [[1, 4], [2, 1], [2, 4], [2, 5], [3, 1], [3, 4]]
Vertex weights: no vertex weights.
Edge descriptions: no edge descriptions.
Edge weights: no edge weights.
Edge costs: no edge costs.
Adjacency list (out): 1 = [4], 2 = [1, 4, 5], 3 = [1, 4], 4 = [], 5 = []
Adjacency list (in): 1 = [2, 3], 2 = [], 3 = [], 4 = [1, 2, 3], 5 = [2]
Graph is directed.

Example 2

The same number of vertices, but this time the edges are undirected (and therefore the number of Edges is (2 EdgeNr)). As you can clearly see, the edges differ from the edges created above:

G := Graph::createRandomGraph(5, 6, Undirected):
Graph::printGraphInformation(G)
Vertices: [1, 2, 3, 4, 5]
Edges: [[1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 2], [4, 1], [4, 2], [4\
, 5], [5, 1], [5, 2], [5, 4]]
Vertex weights: no vertex weights.
Edge descriptions: no edge descriptions.
Edge weights: no edge weights.
Edge costs: no edge costs.
Adjacency list (out): 1 = [4, 5], 2 = [3, 4, 5], 3 = [2], 4 = [1, 2, 5], 5\
 = [1, 2, 4]
Adjacency list (in): 1 = [4, 5], 2 = [3, 4, 5], 3 = [2], 4 = [1, 2, 5], 5 \
= [1, 2, 4]
Graph is undirected.

Example 3

If the number of edges to be created extends the possible limit ( ), a complete graph will be returned:

G := Graph::createRandomGraph(3, 6, Undirected):
Graph::printGraphInformation(G)
Warning: Cannot produce the required number of edges. Creating a complete graph instead. [Graph::createRandomGraph]
Vertices: [1, 2, 3]
Edges: [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
Vertex weights: no vertex weights.
Edge descriptions: no edge descriptions.
Edge weights: no edge weights.
Edge costs: no edge costs.
Adjacency list (out): 1 = [2, 3], 2 = [1, 3], 3 = [1, 2]
Adjacency list (in): 1 = [2, 3], 2 = [1, 3], 3 = [1, 2]
Graph is undirected.

Parameters

VertexNr

Positive integer

EdgeNr

Positive integer

Options

Directed

If Directed is stated, a directed Graph is created Default

Undirected

If Undirected is stated, an undirected Graph is created.

Return Values

Graph

Was this topic helpful?