function ierror = graph_dist_check ( dist, nnode )

%*****************************************************************************80
%
%% graph_dist_check() checks a distance matrix for consistency.
%
%  Discussion:
%
%    The checks made are:
%
%      1): DIST(I,I) = 0
%      2): DIST(I,J) > 0 for I different from J
%      3): DIST(I,J) = DIST(J,I) for I different from J.
%      4): DIST(I,J) + DIST(J,K) >= DIST(I,K).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 November 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real DIST(NNODE,NNODE), the distance matrix.
%    DIST(I,J) is the distance FROM node I TO node J.
%
%    integer NNODE, the number of nodes.
%
%  Output:
%
%    integer IERROR, error flag.
%    0, no errors.
%    1, DIST(I,I) is nonzero for some I;
%    2, DIST(I,J) <= 0 for some distinct I, J
%    3, DIST(I,J) not equal to DIST(J,I) for some distinct I, J.
%    4, DIST(I,J) + DIST(J,K) < DIST(I,K) for some I, J, K.
%
  ierror = 0;

  for i = 1 : nnode
    if ( dist(i,i) ~= 0.0 )
      ierror = 1;
      fprintf ( 1, '\n' );
      fprintf ( 1, 'graph_dist_check(): Failed test #1:\n' );
      fprintf ( 1, '  DIST(I,I) nonzero for I = %d\n', i );
      return
    end
  end

  for i = 1 : nnode
    for j = 1 : nnode
      if ( i ~= j )
        if ( dist(i,j) <= 0.0 )
          ierror = 2;
          fprintf ( 1, '\n' );
          fprintf ( 1, 'graph_dist_check(): Failed test #2:\n' );
          fprintf ( 1, '  DIST(%d,%d) <= 0\n', i, j );
          return
        end
      end
    end
  end

  for i = 1 : nnode
    for j = 1 : i - 1
      if ( dist(i,j) ~= dist(j,i) )
        ierror = 3;
        fprintf ( 1, '\n' );
        fprintf ( 1, 'graph_dist_check(): Failed test #3:n' );
        fprintf ( 1, '  DIST(%d,%d) not equal to DIST(%d,%d)\n', i, j, j, i );
        return
      end
    end
  end

  for i = 1 : nnode
    for j = 1 : nnode
      for k = 1 : i - 1
        if ( dist(i,j) + dist(j,k) < dist(i,k) )
          ierror = 4;
          fprintf ( 1, '\n' );
          fprintf ( 1, 'graph_dist_check(): Failed test #4:\n' );
          fprintf ( 1, '  DIST(I,J) + DIST(J,K) < DIST(I,K)\n' );
          fprintf ( 1, '  I = %d, J = %d, K = %d\n', i, j, k );
          fprintf ( 1, '  DIST(I,J) = %g\n', dist(i,j) );
          fprintf ( 1, '  DIST(J,K) = %g\n', dist(j,k) );
          fprintf ( 1, '  DIST(I,K) = %g\n', dist(i,k) );
          return
        end
      end
    end
  end

  return
end
