function crep = boundary_representative ( c )

%*****************************************************************************80
%
%% boundary_representative(): representative for a boundary word.
%
%  Discussion:
%
%    Mathematically, a boundary word is a "necklace", that is, an equivalence
%    class of strings of characters, invariant under rotation.  Thus,
%    the following boundary words are in the same equivalence class:
%      '12346', '123456', '234561', '345612', '456123', '561234' 
%
%    This function is given a boundary word, and returns the representative
%    for the class, that is, the lexically first element.
%
%    MATLAB makes lexical comparison easy for strings, which are 
%    actually distinct from character arrays.  We assume the user is
%    working with strings, and so we have to internally convert back
%    and forth.
%
%    Two boundary words are equal if and only if they have the same representative.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    30 June 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    char c(*): a boundary word.
%
%  Output:
%
%    char crep(*): the representative for the boundary word.
%
  n = length ( c );

  if ( is_octave() )

    for i = 1 : n
      if ( i == 1 )
        c1 = c;
        crep = c;
      else
        c1 = [ c1(2:n), c1(1) ];
        if ( chvec_lt ( c1, crep ) )
          crep = c1;
        end
      end
    end

  else

    for i = 1 : n
      if ( i == 1 )
        c1 = c;
        srep = string ( c );
      else
        c1 = [ c1(2:n), c1(1) ];
        s1 = string ( c1 );
        if ( s1 < srep )
          srep = s1;
        end
      end
    end

    crep = char ( srep );

  end

  return
end
 
