Problems Creating a Random Graph
6 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
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
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;
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!