d = [13,13,13,13,11,12,12,5,7,12,7,11,7,12,12,5,3,5,7,5,3,5,3,3,7,11,11]; n = length(d); V = []; tab1 = []; tab2 = []; s=0; count = 0; alen = 1; blen = 1; vlen = 1; for i = 1:n s = s + d(i); end if mod(s,2)~=0 disp('No'); return ; end while 1 for i = 1:n if(d(i)<0) disp('No'); break; end end %If all degrees are 0, output G for i = 1:n if(d(i) ==0) count = count+1; end if (count == n) G = graph(tab1,tab2); plot(G); return; end end Arr = randperm(n); for i = 1:n v = Arr(i); if d(v)>0 %Randomly select a vertex with degree greater than 0 v a = d(v); %a One and v Connected nodes d(v) = 0; [B,I] = sort(d); %And return the largest a Number,take d Sort from small to large, B Is the sorted sequence, I Is the sorted index for j = n-a+1:n %Will this a Nodes exist V in V(vlen) = I(j); vlen = vlen+1; end for k = 1:a tab2(blen) = V(blen); w = tab2(blen); d(w) = d(w)-1; tab1(alen) = v; alen = alen+1; blen = blen+1; end end i = i+1; end end
Generating a random graph by degree sequence
This is the first matlab program I wrote. Under the pressure of the teacher, I have to implement the pseudo code in the ...