% p = etree_balance(A)
%
% Return an equivalent fill ordering to balance the etree.
% This is the Jess and Kees idea, but with a better implementation due to
% Tarjan, if I remember correctly.

function p = etree_balance(A)

n = length(A);

% Replace A with the filled in form of the symmetrized A.
A = A|A';
[count,h,parent,post,A] = symbfact(A);
A = A|A';

p = [];
remaining = ones(1,n);
while length(p) < n
   U = remaining;
   for v = 1:n;
      if U(v)
         nbrs = find(A(:,v).*remaining');
         if all(all(A(nbrs,nbrs)))   % if its neighbourhood is a clique
            U(nbrs) = 0;
            remaining(v) = 0;
            p(end+1) = v;
         end
      end
   end
end


