Problems Creating a Random Graph

6 views (last 30 days)
Joshua Mogyoros
Joshua Mogyoros on 17 Oct 2017
Commented: Joshua Mogyoros on 17 Oct 2017
I am trying to create a random graph, this piece of code I found here looks like exactly what I want to do (except I would also like to have random weights):
Input:
G := Graph::createRandomGraph(5,6):
Graph::printGraphInformation(G)
Output:
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.
When I paste the input into matlab I get these errors:
Error: File: random_graph.m Line: 2 Column: 7
Unexpected MATLAB operator.
When I mess around with that unexpected matlab operator I just get a slew of other errors.
Any idea why this code doesn't work right off the back? Thank you very much for any help.

Answers (2)

dpb
dpb on 17 Oct 2017
That's MUPAD syntax; requires the Symbolic Toolbox.
  1 Comment
Joshua Mogyoros
Joshua Mogyoros on 17 Oct 2017
Edited: Joshua Mogyoros on 17 Oct 2017
Hello, I have the Symbolic Toolbox installed (unless Symbolic 'Math' Toolbox is different), it still does not work - can you please expand on this. Thank you.
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.3.0.713579 (R2017b)
MATLAB License Number: DEMO
Operating System: Mac OS X Version: 10.12.6 Build: 16G29
Java Version: Java 1.8.0_121-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 9.3 (R2017b)
Curve Fitting Toolbox Version 3.5.6 (R2017b)
Database Toolbox Version 8.0 (R2017b)
Global Optimization Toolbox Version 3.4.3 (R2017b)
Neural Network Toolbox Version 11.0 (R2017b)
Optimization Toolbox Version 8.0 (R2017b)
Parallel Computing Toolbox Version 6.11 (R2017b)
Statistics and Machine Learning Toolbox Version 11.2 (R2017b)
Symbolic Math Toolbox Version 8.0 (R2017b)
Trial>> random_graph
Error: File: random_graph.m Line: 2 Column: 7
Unexpected MATLAB operator.

Sign in to comment.


Steven Lord
Steven Lord on 17 Oct 2017
As dpb said that syntax is intended for use in MuPAD, not in MATLAB directly.
To generate a random directed graph, you can generate an adjacency matrix and pass that adjacency matrix into the digraph function. In this example I create a 5 vertex random graph with roughly 6 edges. I then plotted it to see its structure.
S = 10*sprand(5, 5, 6/25);
D = digraph(S);
h = plot(D);
You can use the functions and information in this section of the documentation to calculate with and manipulate the digraph D and the GraphPlot h.
  3 Comments
Steven Lord
Steven Lord on 17 Oct 2017
For the first part of your question, I said in the text before my example code that I wanted a random graph with 5 vertices and roughly 6 edges. The documentation for sprand to which you linked states in part:
R = sprand(m,n,density) is a random, m-by-n, sparse matrix
with approximately density*m*n uniformly distributed nonzero
entries (0 <= density <= 1).
Since m = 5, n = 5, and I want S to contain approximately 6 nonzero elements (corresponding to the 6 edges) that means density*5*5 = 6. I solved for density manually. Note that even thought density is 6/25, that doesn't guarantee that sprand will generate a matrix with exactly 6 nonzero elements, since the documentation does say "approximately" density*m*n entries.
For the second part of your question, if you look at the Edges property of the digraph D, you'll see that it has a variable named Weight. If you compare that Weight variable with the elements in S, they should look familiar. [They may be displayed in a slightly different order in D.Edges and in S, but if you look at the value of the EndNodes variable in the row containing a given Weight value you can find the corresponding elements in S.]
If you want to see the impact of the weights, you could change h's LineWidth property. The higher the weight, the thicker the line.
h.LineWidth = D.Edges.Weight;
Note that sprand generates uniformly distributed weights.
  • If you wanted normally distributed weights you could use sprandn.
  • If you wanted to make an undirected graph, you'd probably want sprandsym.
  • If you want a graph or digraph with weights randomly generated from a different distribution, you can generate the vector of weights and set the Weight variable in the Edges table of the graph or digraph directly.
An example of that last approach (albeit with non-random data) is:
D.Edges.Weight = 2*(1:numedges(D)).';
h.LineWidth = D.Edges.Weight;
Joshua Mogyoros
Joshua Mogyoros on 17 Oct 2017
Hello Steven,
I wasn't quite able to follow your hints when it came to D.Edges/EndNodes, but this is how I went about trying to get randomly generated weights.
s = 10*sprand(100, 100, 300/10000);
nnz(s)
weights = randi(100,nnz(s),1);
G= digraph(s);
G = rmedge(G, 1:numnodes(G), 1:numnodes(G)); %(I didn't want any self-loops)
p = plot(G);
centrality_values = centrality(G,'pagerank', 'Importance', weights)
I would've loved to used the sprandn and get normalized weights, but sprandn(s) gave me an output that looked like:
weights =
(24,1) -2.6054
(26,1) -0.5552
(27,1) -0.0977
(87,1) -1.5349
(10,2) -0.4978
And this was very hard for me to convert to a vector that was usable for the weights (and I was confused why it wasn't returning random values between 0 and 1):
Error using digraph/centrality (line 84)
Expected Importance to be a vector.
The issue I am getting with the code is that the number of nonzero matrix elements in s (which you said isn't exactly equal to the density) differs from the nnz value:
ans =
290
Error using digraph/centrality (line 84)
Expected Importance to be an array with number of elements equal to 289.
I don't understand why this would be the case as I see this as the best way to input the number of weights needed.
Thank you again for your time.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!