function value = a_to_b ( adj, a, b )
%
%% A_TO_B: Is there a path from node A to node B?
%
%  ADJ(N,N) is the adjacency matrix for the graph G.
%
%  A and B are nodes in the graph.  So we expect 1 <= A, B <= N.
%
%  VALUE is output by the function:
%  TRUE (1), a path from A to B was found.
%  FALSE (0), no path from A to B was found. 
%
  value = false;
%
%  How many nodes are there?
%
  [ n, n ] = size ( adj );
%
%  REACH(I) will be TRUE if we can reach node I from node A.
%
  reach = zeros(1,n);
  reach(a) = true;
%
%  NEW holds the nodes we have just reached on this step.
%
  new = [ a ];
%
%  As long as NEW isn't empty, we reached some new nodes.
%  Use them as stepping stones, and try to reach more nodes.
%
  while ( 0 < length ( new ) )

    old = new;
    new = [];
%
%  For each node J that we found on the previous step...
%
    for k = 1 : length ( old )
      j = old(k);
%
%  ...look for adjacent neighbors I...
%
      for i = 1 : n
        if ( adj(i,j) == 1 )
%
%  ...and if node I hasn't been reached yet...
%
          if ( ~ reach(i) )
%
%  ...then add node I to the reachable set, note that we just found it, and...
%
            reach(i) = true;
            new = [ new, i ];
%
%  ...if the node is B, then B is reachable from A and we are done.
%
            if ( i == b )
              value = true;
              return
            end

          end

        end
      end
    end

  end
%
%  If we got here, we never reached B.
%
  return
end
