-
Notifications
You must be signed in to change notification settings - Fork 26
/
RandomGraph.m
34 lines (30 loc) · 1.16 KB
/
RandomGraph.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
%% RANDOMGRAPH Generates the adjacency matrix of a random graph
% This function has one required argument:
% N: the number of vertices in the graph
%
% A = RandomGraph(N) generates the adjacency matrix of a random N-vertex
% graph where each edge is present, independently of the others, with
% probability 0.5.
%
% This function has one optional argument:
% P (default 0.5): a real number between 0 and 1; the probability of
% each edge being present
%
% A = RandomGraph(N,P) generates the adjacency matrix of a random
% N-vertex graph where each edge is present, independently of the
% others, with probability P.
%
% URL: http://www.qetlab.com/RandomGraph
% author: Nathaniel Johnston (nathaniel@njohnston.ca)
% package: QETLAB
% last updated: August 4, 2023
function A = RandomGraph(n,varargin)
% set optional argument defaults: p=0.5
[p] = opt_args({ 0.5 },varargin{:});
if(p >= 1)% avoid division by 0 is upcoming formula
A = ones(n) - eye(n);
else
A = triu(min(max(round(rand(n)/(2-2*p)),0),1),1);% random upper triangular part
A = A + A';% symmetrize it
end
end