jeudi 28 juillet 2016

How to generate random graph of n vertices with random connections in matlab

Hi I am interested in creating random undirected graph with n vertices and with random connections. This generated graph has to display its adjacency connections in a nxn matrix.I have tried this code but it is not working for my requirement.

   n=input('No. of vertices')
   c=input('Random connections')
   MM=0;   %arbitrary starting value
    all_nums=1:n;

    while MM ~=n*c

        M = sparse([],[],[],n,n,n*c);
        ctin = zeros(1,n);

        for ii=1:n
            noconnect=ctin>=c;
            noconnect(ii)=true;

            rem_nums = all_nums(~noconnect); % remaining numbers
            rp=randperm(n-sum(noconnect));
            rp = rem_nums(rp); % remaining numbers, hussled

            if numel(rp)<c
                break
            else
                r=rp(1:c);
            end
            M(ii,r)=1;
            ctin(r)=ctin(r)+1;
        end
        MM=sum(ctin);
    end
end
   A= adjacency(M)




Aucun commentaire:

Enregistrer un commentaire