function [r,is_independent]=rank_graphic(A,X)
n=size(A,1);
Ind=find(A(:));
Ind=[floor((Ind-1)/n)+1,rem(Ind-1,n)+1];
Ind=Ind(Ind(:,1)<Ind(:,2),:);
m=size(Ind,1);
l=size(X,1);
r=zeros(l,1);is_independent=false(l,1);
for i=1:l
    Y=false(n,n);
    for j=1:m
        Y(Ind(j,1),Ind(j,2))=X(i,j);
        Y(Ind(j,2),Ind(j,1))=X(i,j);
    end
    r(i)=n-count_cc(Y);
    is_independent(i)=(r(i)==sum(X(i,:)));
end

function cc=count_cc(edge_ind)% Count connected components
ver=false(1,n);cc=0;
for j=1:n
    if ver(j);continue;end
    ver(j)=true;cc=cc+1;
    new_ver=false(1,n);new_ver(j)=true;
    while sum(new_ver)>0
        new_ver=any(edge_ind(new_ver,:),1);
        new_ver(ver)=false;
        ver(new_ver)=true;
    end
end
end
end