function [ A ] = gen_er2( n , p , quiet) %GEN_ER2 Generates a random graph of the Erdös-Renyi type % % A = GEN_ER2( n , p ) returns the adjacency matrix A of the % undirected graph with n nodes, every pair of nodes % being connecte by an edges with the probability p. % % Alternative implementation. % % Last change: 10/2/2005 - Florian Knorn if nargin < 2 error('Please provide n and p'); else if n < 2 error('n must be greater or equal 2'); end if p < 0 || p > 1 error('p must be a probability, so 0 <= p <= 1 !'); end end if nargin ~= 3 quiet = 0; end ind = find(triu(true(n,n),1)); % fetch indices of upper triang ind = ind(randperm(n*(n-1)/2)); % randomly reorder them A = logical(sparse(n,n)); % create empty A A(ind(1:binornd(n*(n-1)/2,p))) = true; % set some of the entries true A = A | A'; % create full (symmetric) A if ~quiet disp(sprintf('Erdös-Renyi graph with %i nodes and %i edges successfully created',n,nnz(A)/2)); end