function cycle_stats = perm1_cycle_stats ( p )

%*****************************************************************************80
%
%% perm1_cycle_stats() returns permutation cycle length statistics.
%
%  Discussion:
%
%    The permutation is assumed to be over the integers 1 through N.
%
%  Example:
%
%      P = 2, 3, 9, 6, 7, 8, 5, 4, 1
%
%      Cycles: ( 1, 2, 3, 9 ), ( 4, 6, 8 ), ( 5, 7 )
% 
%      cycle_max = 4
%                      1, 2, 3, 4, 5, 6, 7, 8, 9
%      cycle_stats = ( 0, 1, 1, 1, 0, 0, 0, 0, 0 )
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    18 November 2022
%
%  Author:
%
%    John Burkardt.
%
%  Input:
%
%    integer p(n): the permutation to be analyzed.  
%
%  Output:
%
%    integer cycle_stats(n): the number of cycles of each length.
%
  n = length ( p );

  cycle_stats = zeros ( n, 1 );

  for i = 1 : n

    j = p(i);
    cycle_length = 1;

    while ( j ~= i )
      j = p(j);
      cycle_length = cycle_length + 1;
    end

    cycle_stats(cycle_length) = cycle_stats(cycle_length) + 1;

  end

  for i = 1 : n
    cycle_stats(i) = cycle_stats(i) / i;
  end

  return
end

