function R = rnd_bipartite(nrows, ncols, density)
% RND_BIPARTITE Make a randomly connected bipartite graph.
% R = rnd_bipartite(nrows, ncols, density)
%
% We require than there is at least one 1 in every row and at least one 1 in every column.
% If the density is low, this might take a long time!

valid = 0;
while ~valid
  R = (sprand(nrows, ncols, density) > 0);
  valid = 1;
  for i=1:nrows
    if length(find(R(i,:))) ==0
      valid = 0;
     end
  end
  for j=1:ncols
    if length(find(R(:,j))) ==0
      valid = 0;
     end
  end
end
%R=full(R);
