MATLAB Answers

How to create random graph?

조회 수: 25(최근 30일)
Deepthi B
Deepthi B 2021년 5월 10일
댓글: Rayan Glus 2021년 5월 29일
Hi ,
I want to check whether the network that I have obtained is a small world network or not. For that I have to find out the clustering coefficient and shortest path length of a random network with the same number of nodes and links in my network. Is it possible to find out?

채택된 답변

Christine Tobler
Christine Tobler 2021년 5월 10일
You can use
s = randi(n, e, 1);
t = randi(n, e, 1);
G = graph(s, t, [], n);
which will give you a graph with n nodes and e edges. Note that G may have multiple edges connecting the same two nodes, and may also have self-loops (edges connecting a node to itself). If you don't want repeating edges, use the following instead:
G = graph(true(n)); % Self-loops are possible
%G = graph(true(n), 'omitselfloops'); % Alternative without self-loops
p = randperm(numedges(G), e);
G = graph(G.Edges(p, :));
  댓글 수: 3
Rayan Glus
Rayan Glus 2021년 5월 29일
Thank you so much for the nice clarification, Christine.
Your code was able to generate a graph of 6000 nodes with p=0.6, and then plot it in just couple of seconds.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Deepthi B
Deepthi B 2021년 5월 13일
Thank You

Community Treasure Hunt

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

Start Hunting!

Translated by