-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: add duval's algorithm #2725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+118
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* @file duval.cpp | ||
* @brief Implementation of [Duval's algorithm](https://en.wikipedia.org/wiki/Lyndon_word). | ||
* | ||
* @details | ||
* 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](https://github.com/aminegh20) | ||
*/ | ||
|
||
#include <array> /// for std::array | ||
#include <cassert> /// for assert | ||
#include <cstddef> /// for std::size_t | ||
#include <deque> /// for std::deque | ||
#include <iostream> /// for std::cout and std::endl | ||
#include <string> /// for std::string | ||
#include <vector> /// for std::vector | ||
|
||
/** | ||
* @brief string manipulation algorithms | ||
* @namespace | ||
*/ | ||
namespace string { | ||
/** | ||
* @brief Find the lexicographically smallest cyclic shift of a sequence. | ||
* @tparam T type of the sequence | ||
* @param s the sequence | ||
* @returns the 0-indexed position of the least cyclic shift of the sequence | ||
*/ | ||
template <typename T> | ||
aghoussaini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
size_t duval(const T& s) { | ||
size_t n = s.size(); | ||
size_t i = 0, ans = 0; | ||
while (i < n) { | ||
ans = i; | ||
size_t j = i + 1, k = i; | ||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (j < (n + n) && s[j % n] >= s[k % n]) { | ||
if (s[k % n] < s[j % n]) { | ||
k = i; | ||
} else { | ||
k++; | ||
} | ||
j++; | ||
} | ||
while (i <= k) { | ||
i += j - k; | ||
} | ||
} | ||
return ans; | ||
// returns 0-indexed position of the least cyclic shift | ||
} | ||
|
||
} // namespace string | ||
|
||
aghoussaini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @brief self test implementation | ||
* returns void | ||
*/ | ||
static void test() { | ||
using namespace string; | ||
|
||
// Test 1 | ||
std::string s1 = "abcab"; | ||
assert(duval(s1) == 3); | ||
|
||
// Test 2 | ||
std::string s2 = "011100"; | ||
assert(duval(s2) == 4); | ||
|
||
// Test 3 | ||
aghoussaini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::vector<int> v = {5, 2, 1, 3, 4}; | ||
assert(duval(v) == 2); | ||
|
||
aghoussaini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Test 4 | ||
std::array<int, 5> a = {1, 2, 3, 4, 5}; | ||
assert(duval(a) == 0); | ||
|
||
// Test 5 | ||
std::deque<char> d = {'a', 'z', 'c', 'a', 'b'}; | ||
assert(duval(d) == 3); | ||
|
||
// Test 6 | ||
std::string s3; | ||
assert(duval(s3) == 0); | ||
|
||
// Test 7 | ||
std::vector<int> v2 = {5, 2, 1, 3, -4}; | ||
assert(duval(v2) == 4); | ||
|
||
std::cout << "All tests passed!" << std::endl; | ||
} | ||
|
||
aghoussaini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @brief main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); // run self test implementations | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.