TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
duval.cpp File Reference

Implementation of Duval's algorithm. More...

#include <array>
#include <cassert>
#include <cstddef>
#include <deque>
#include <iostream>
#include <string>
#include <vector>
Include dependency graph for duval.cpp:

Go to the source code of this file.

Namespaces

namespace  string
 string manipulation algorithms
 

Functions

template<typename T >
size_t string::duval (const T &s)
 Find the lexicographically smallest cyclic shift of a sequence.
 
static void test ()
 self test implementation returns void
 
int main ()
 main function
 

Detailed Description

Implementation of Duval's algorithm.

Duval's algorithm is an algorithm to find the lexicographically smallest rotation of a string. It is based on the concept of Lyndon words. Lyndon words are defined as the lexicographically smallest string in a rotation equivalence class. A rotation equivalence class is a set of strings that can be obtained by rotating a string. For example, the rotation equivalence class of "abc" is {"abc", "bca", "cab"}. The lexicographically smallest string in this class is "abc".

Duval's algorithm works by iterating over the string and finding the smallest rotation of the string that is a Lyndon word. This is done by comparing the string with its suffixes and finding the smallest suffix that is lexicographically smaller than the string. This suffix is then added to the result and the process is repeated with the remaining string. The algorithm has a time complexity of O(n) where n is the length of the string.

Note
While Lyndon words are described in the context of strings, Duval's algorithm can be used to find the lexicographically smallest cyclic shift of any sequence of comparable elements.
Author
Amine Ghoussaini

Definition in file duval.cpp.

Function Documentation

◆ main()

int main ( void )

main function

Returns
0 on exit

Definition at line 115 of file duval.cpp.

115 {
116 test(); // run self test implementations
117 return 0;
118}
static void test()
self test implementation returns void
Definition duval.cpp:77

◆ test()

static void test ( )
static

self test implementation returns void

Definition at line 77 of file duval.cpp.

77 {
78 using namespace string;
79
80 // Test 1
81 std::string s1 = "abcab";
82 assert(duval(s1) == 3);
83
84 // Test 2
85 std::string s2 = "011100";
86 assert(duval(s2) == 4);
87
88 // Test 3
89 std::vector<int> v = {5, 2, 1, 3, 4};
90 assert(duval(v) == 2);
91
92 // Test 4
93 std::array<int, 5> a = {1, 2, 3, 4, 5};
94 assert(duval(a) == 0);
95
96 // Test 5
97 std::deque<char> d = {'a', 'z', 'c', 'a', 'b'};
98 assert(duval(d) == 3);
99
100 // Test 6
101 std::string s3;
102 assert(duval(s3) == 0);
103
104 // Test 7
105 std::vector<int> v2 = {5, 2, 1, 3, -4};
106 assert(duval(v2) == 4);
107
108 std::cout << "All tests passed!" << std::endl;
109}
string manipulation algorithms
Definition duval.cpp:41
size_t duval(const T &s)
Find the lexicographically smallest cyclic shift of a sequence.
Definition duval.cpp:49