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 日

1 投票

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, :));

5 件のコメント

Rayan Glus
Rayan Glus 2021 年 5 月 28 日
Thanks Christine!
Is what you wrote the same as Erdos - Renyi random graph model? I'm sorry for the stupid question.
Christine Tobler
Christine Tobler 2021 年 5 月 28 日
Hi Rayan, great question!
The second code I included matches the G(n, M) model of Erdos-Renyi (usually would be done omitting self-loops). The first code doesn't match any of the Erdos-Renyi models, since it can generate multiple edges between the same two nodes.
You can generate the G(n, p) model of Erdos-Renyi by the following code (notice I renamed variable p above, since this is now needed for the probability of each edge being in the graph).
G = graph(true(n), 'omitselfloops');
edgesToKeep = rand(numedges(G), 1) < p;
G = graph(G.Edges(edgesToKeep, :));
Note for both variants, this could probably be computed in a faster way by avoiding the creation of G and instead coming up with some code that does the mapping from an edge ID in the complete graph with n nodes into the source and target node of that edge. But this is much more compact and simple code.
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.
Norbert Cyran
Norbert Cyran 2021 年 12 月 19 日
Why are you generating true matrix for nodes and random values for edges??
Christine Tobler
Christine Tobler 2021 年 12 月 20 日
Hi Norbert,
The true(n) matrix is an adjacency matrix that contains all possible edges in the graph (omitting self-loops or not depending if that option is used or not). Then, I'm choosing a random subset of those edges using the randperm function.

サインインしてコメントする。

その他の回答 (1 件)

Deepthi B
Deepthi B 2021 年 5 月 13 日

0 投票

Thank You

カテゴリ

ヘルプ センター および File ExchangeNetworks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by