function [ d, v ] = find_nearest ( nv, mind, connected )

%*****************************************************************************80
%
%% find_nearest() finds the nearest unconnected node.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    01 July 2010
%
%  Author:
%
%    Original C version by Norm Matloff, CS Dept, UC Davis.
%    MATLAB version by John Burkardt.
%
%  Input:
%
%    integer NV, the number of nodes.
%
%    integer MIND(NV), the currently computed minimum distance from
%    node 1 to each node.
%
%    integer CONNECTED(NV), is 1 for each connected node, whose 
%    minimum distance to node 1 has been determined.
%
%  Output:
%
%    integer D, the distance from node 1 to the nearest 
%    unconnected node.
%
%    integer V, the index of the nearest unconnected node.
%
  d = Inf; 
  v = -1;
  for i = 1 : nv
    if ( ( ~ connected(i) ) & mind(i) < d )
      d = mind(i);
      v = i;
    end
  end
  return;
end
 
