Network adjacency matrix for connecting n node with probability p

2 ビュー (過去 30 日間)
mks
mks 2023 年 7 月 30 日
編集済み: Bruno Luong 2023 年 7 月 30 日
i have write this but is not running
clc;
clear all
n=10;
p=0.4;
adj_matrix = generate_adjacency_matrix(n, p)
adj_matrix = 10×10
0 0 0 1 0 0 1 0 0 1 0 0 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 0 1 1 1 1 1 1 1 0 0 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 1 1 1 1 1 0
function adj_matrix = generate_adjacency_matrix(n, p)
% Initialize an n x n matrix with all zeros
adj_matrix = zeros(n, n);
% Loop through all possible node pairs
for i = 1:n
for j = 1:n
% Skip diagonal elements (no self-loops)
if i == j
continue;
end
% Generate a random number between 0 and 1
random_number = rand();
% If the random number is less than p, add a link between nodes i and j
if random_number < p
adj_matrix(i, j) = 1;
adj_matrix(j, i) = 1; % Since it's an undirected network
end
end
end
end
  1 件のコメント
Torsten
Torsten 2023 年 7 月 30 日
編集済み: Torsten 2023 年 7 月 30 日
Works for me (see above).
But you should only run through the indices above (or below) the main diagonal because it's an undirected graph.

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

回答 (3 件)

Torsten
Torsten 2023 年 7 月 30 日
編集済み: Torsten 2023 年 7 月 30 日
clc;
clear all
n=10;
p=0.4;
adj_matrix = generate_adjacency_matrix(n, p)
adj_matrix = 10×10
1 1 1 0 1 0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 0 0 0 1 1 1 1 0 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 0 1 0 0 1 0 0 1 1 1 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 1 0 0 1
(nnz(adj_matrix(:))-n)/(n^2-n)
ans = 0.4222
function adj_matrix = generate_adjacency_matrix(n, p)
% Initialize an n x n matrix with all zeros
adj_matrix = eye(n);
% Loop through all possible node pairs
for i = 1:n-1
for j = i+1:n
% Generate a random number between 0 and 1
random_number = rand();
% If the random number is less than p, add a link between nodes i and j
if random_number < p
adj_matrix(i, j) = 1;
adj_matrix(j, i) = 1; % Since it's an undirected network
end
end
end
end
  2 件のコメント
mks
mks 2023 年 7 月 30 日
Thanks a lot.
please give your contact details .
Torsten
Torsten 2023 年 7 月 30 日
Seems the matrix always has zeros on the diagonal:
rng("default")
n=10;
p=0.4;
adj_matrix = generate_adjacency_matrix(n, p);
nnz(adj_matrix(:))/(n^2-n)
ans = 0.3556
function adj_matrix = generate_adjacency_matrix(n, p)
% Initialize an n x n matrix with all zeros
adj_matrix = zeros(n);
% Loop through all possible node pairs
for i = 1:n-1
for j = i+1:n
% Generate a random number between 0 and 1
random_number = rand();
% If the random number is less than p, add a link between nodes i and j
if random_number < p
adj_matrix(i, j) = 1;
adj_matrix(j, i) = 1; % Since it's an undirected network
end
end
end
end

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


Bruno Luong
Bruno Luong 2023 年 7 月 30 日
編集済み: Bruno Luong 2023 年 7 月 30 日
Method without loop, generate entire matrix, symmetrize then remove self-connexion
p=0.4;
n=10;
A = GenerateAMat(p, n);
A
A = 10×10 logical array
0 1 0 1 0 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 0 0 0 0 1 1 0 1 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 0 0 1 1 0 1 1 1 0 0
nnz(A)/numel(A)
ans = 0.4200
function A = GenerateAMat(p, n)
A = rand(n) <= 1-sqrt(max(1-p*n/(n-1),0));
A = A|A';
A(1:n+1:end)=0;
end

Bruno Luong
Bruno Luong 2023 年 7 月 30 日
編集済み: Bruno Luong 2023 年 7 月 30 日
Pretty similar to Torsen's solution. Adjust the density and no-loop
p=0.1;
n=30;
A = GenerateAMat2(p, n);
nnz(A)/numel(A)
ans = 0.1089
function A = GenerateAMat2(p, n)
A = zeros(n);
A(triu(true(n),1)) = rand(1,n*(n-1)/2) <= p*n/(n-1);
A = A+A';
end

カテゴリ

Help Center および File ExchangeUndirected Graphs についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by