How to generate random graph of n vertices with random connections with specified number of Edge in matlab

11 views (last 30 days)
Though i have used the below code for generating the random graph of n vertices with random connections, it is even choosing the number of edges randomly. Could some one please help me to find the solution for this?
G = round(rand(n));
G = triu(G) + triu(G,1)';
G = G - diag(diag(G));
Example: n = 5; number of edges = 10
Response:
0 1 0 1 0
1 0 1 0 0
1 1 0 1 0
0 0 1 0 1
0 1 0 0 0

Accepted Answer

Matt J
Matt J on 3 Dec 2021
Edited: Matt J on 3 Dec 2021
n=5; %number of nodes
e=10; %number of edges
T=table(randi(n,e,2),'Var',{'EndNodes'});
G=graph(T);
plot(G)
  6 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!