function balanced = isGraphBalanced(A)

N = size(A,1);
Ap = A > 0; % "Positive adjacency matrix"

% First, we identify the candidate faction by looking
%   at the edges of node 1. We encode the candidate factions
%   into vectors g1 and g2.
%------------------------------------------------------------
g1 = zeros(N,1); g1(1) = 1;
g2 = zeros(N,1);
for i = 2:N
    if (A(1,i) > 0)
        g1(i) = 1;
    else
        g2(i) = 1;
    end
end

% Second, we check if the candidate faction vectors are eigenvectors
%   of the positive adjacency matrix, with eigenvalue equal to the 
%   size of the respective faction
%------------------------------------------------------------
n1 = sum(g1);
n2 = sum(g2);

balanced = all(Ap*g1 == n1*g1) && ...
           all(Ap*g2 == n2*g2);