forked from ivanbrugere/matlab-networks-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisGraphic.m
executable file
·32 lines (25 loc) · 951 Bytes
/
isGraphic.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
%##################################################################
% Check whether a sequence of number is graphic, i.e.
% a graph with this degree sequence exists
% Source: Erdős, P. and Gallai, T. "Graphs with Prescribed Degrees
% of Vertices" [Hungarian]. Mat. Lapok. 11, 264-274, 1960.
%
% INPUTs: a sequence (vector) of numbers
% OUTPUTs: boolean, true or false
%
% Note: Not generalized to directed graph degree sequences.
% GB: last updated, Sep 24, 2012
%##################################################################
function B = isGraphic(seq)
if not(isempty(find(seq<=0))) | mod(sum(seq),2)==1
% there are non-positive degrees or their sum is odd
B = false; return;
end
n=length(seq);
seq=-sort(-seq); % sort in decreasing order
for k=1:n-1
sum_dk = sum(seq(1:k));
sum_dk1 = sum(min([k*ones(1,n-k);seq(k+1:n)]));
if sum_dk > k*(k-1) + sum_dk1; B = false; return; end
end
B = true;