matlab 无尺度网络scale-free network模拟

这是非官方的容易理解的同样的网络模型模拟

下面的官方的模拟(但自己没有去看,只是粘过来而已):

function A = BAgraph_dir(N,mo,m)
% Generates a scale-free directed adjacency matrix using Barabasi and Albert algorithm
% Input: N - number of nodes in the network, mo - size of seed, m = average degree. Use mo=m or m<mo.
% Example: A = BAgraph(300,10,10);
% Ref: Methods for generating complex networks with selected structural properties for simulations, Pretterjohn et al, Frontiers Comp Neurosci

% Author:
% Tapan P Patel, Ph.D., [email protected]
% University of Pennsylvania
hwait = waitbar(0,'Please wait. Generating directed scale-free adjacency matrix');
A = zeros(N);
E = 0;
for i=1:mo
    for j=i+1:mo

        A(i,j) =1;
        A(j,i) = 1;
        E = E + 2;
    end
end
% Second add remaining nodes with a preferential attachment bias - rich get
% richer
for i=mo+1:N
    waitbar(i/N,hwait,sprintf('Please wait. Generating directed scale-free adjacency matrix\n%.1f %%',(i/N)*100));
    curr_deg =0;
    while(curr_deg<m)
        sample = setdiff(1:N,[i find(A(i,:))]);
        j = datasample(sample,1);
        b = sum(A(j,:))/E;
        r = rand(1);
        if(b>r)
            r = rand(1);
            if(b>r)
            A(i,j) = 1;
            A(j,i) = 1;
            E = E +2;
            else
            A(i,j) = 1;
            E = E +1;
            end
        else
            no_connection = 1;
            while(no_connection)
                sample = setdiff(1:N,[i find(A(i,:))]);
                h = datasample(sample,1);
                b = sum(A(h,:))/E;
                r = rand(1);
                if(b>r)
                    r = rand(1);
                    if(b>r)
                    A(h,i) = 1;
                    A(i,h) = 1;
                    E = E +2;
                    else
                    A(i,h) = 1;
                    E = E+1;
                    end
                    no_connection = 0;
                end
            end
        end
        curr_deg = sum(A(i,:));
    end
end
delete(hwait);

最后若需要作图的话,就G=dirgraph(A);plot(G)即可

猜你喜欢

转载自blog.csdn.net/normol/article/details/79155703