From 8621a1db73398891c502f96ad0a3ac2dbcf0b5c5 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Fri, 29 May 2020 07:53:46 -0400 Subject: [PATCH 1/5] documented kmp string search --- strings/knuth_morris_pratt.cpp | 120 ++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 48 deletions(-) diff --git a/strings/knuth_morris_pratt.cpp b/strings/knuth_morris_pratt.cpp index f6f1169d3..b83cab966 100644 --- a/strings/knuth_morris_pratt.cpp +++ b/strings/knuth_morris_pratt.cpp @@ -1,64 +1,88 @@ -/* - The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text - with complexity O(n + m) - 1) Preprocess pattern to identify any suffixes that are identical to prefixes - This tells us where to continue from if we get a mismatch between a character in our pattern - and the text. - 2) Step through the text one character at a time and compare it to a character in the pattern - updating our location within the pattern if necessary -*/ +/** + * \file + * \brief The [Knuth-Morris-Pratt + * Algorithm](https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm) for + * finding a pattern within a piece of text with complexity O(n + m) + * + * 1. Preprocess pattern to identify any suffixes that are identical to + * prefixes. This tells us where to continue from if we get a mismatch between a + * character in our pattern and the text. + * 2. Step through the text one character at a time and compare it to a + * character in the pattern updating our location within the pattern if + * necessary + */ -#include -#include -#include -using namespace std; -vector getFailureArray(string pattern){ - int pattern_length=pattern.size(); - vectorfailure(pattern_length+1); - failure[0]=-1; - int j=-1; - for(int i=0; i +#ifdef _MSC_VER +#include // use this for MS Visucal C++ +#else +#include +#endif +#include + +/** + * Generate the partial match table aka failure function for a pattern to + * search. + * \param[in] pattern text for which to create the partial match table + * \returns the partial match table as a vector array + */ +std::vector getFailureArray(const std::string &pattern) { + int pattern_length = pattern.size(); + std::vector failure(pattern_length + 1); + failure[0] = -1; + int j = -1; + + for (int i = 0; i < pattern_length; i++) { + while (j != -1 && pattern[j] != pattern[i]) { + j = failure[j]; } j++; - failure[i+1]=j; + failure[i + 1] = j; } return failure; } -bool kmp(string pattern,string text){ - int text_length=text.size(),pattern_length=pattern.size(); - vectorfailure=getFailureArray(pattern); - int k=0; - for(int j=0; j failure = getFailureArray(pattern); + + int k = 0; + for (int j = 0; j < text_length; j++) { + while (k != -1 && pattern[k] != text[j]) { + k = failure[k]; } k++; - if(k==pattern_length)return true; + if (k == pattern_length) + return true; } return false; } -int main() -{ - - string text="alskfjaldsabc1abc1abc12k23adsfabcabc"; - string pattern="abc1abc12l"; - if(kmp(pattern,text)==true){ - cout<<"Found"< Date: Fri, 29 May 2020 08:00:58 -0400 Subject: [PATCH 2/5] document brute force string search --- strings/brute_force_string_searching.cpp | 89 ++++++++++++------------ 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index 3288b36b0..9a2b5327a 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -1,52 +1,53 @@ -#include -#include -#include - -using std::string; - -int brute_force(string text, string pattern); -std::vector> test_set = { - // {text, pattern, expected output} - {"a", "aa", "-1"}, - {"a", "a", "0"}, - {"ba", "b", "0"}, - {"bba", "bb", "0"}, - {"bbca", "c", "2"}, - {"ab", "b", "1"} -}; - -int main() { - for (size_t i = 0 ; i < test_set.size(); i++) { - int output = brute_force(test_set[i][0], test_set[i][1]); - if (std::to_string(output) == test_set[i][2]) - std::cout << "success\n"; - else - std::cout << "failure\n"; - } - return 0; -} - -/* - *@description Find a pattern in a string by comparing the pattern - * to every substring. - *@param text Any string that might contain the pattern. - *@param pattern String that we are searching for. - *@return Index where the pattern starts in the text or - * -1 if the pattern was not found. +/** + * @file + * @brief String pattern search - brute force */ +#include +#ifdef _MSC_VER +#include // use this for MS Visucal C++ +#else +#include +#endif +#include -int brute_force(string text, string pattern) { - size_t pat_l = pattern.length(); - size_t txt_l = text.length(); - int index = -1; - if (pat_l <= txt_l) { - for (size_t i = 0; i < txt_l-pat_l+1; i++) { - string s = text.substr(i, pat_l); - if (s == pattern) { - index = i; +/** + * Find a pattern in a string by comparing the pattern to every substring. + * @param text Any string that might contain the pattern. + * @param pattern String that we are searching for. + * @return Index where the pattern starts in the text + * @return -1 if the pattern was not found. + */ +int brute_force(const std::string &text, const std::string &pattern) { + size_t pat_l = pattern.length(); + size_t txt_l = text.length(); + int index = -1; + if (pat_l <= txt_l) { + for (size_t i = 0; i < txt_l - pat_l + 1; i++) { + std::string s = text.substr(i, pat_l); + if (s == pattern) { + index = i; break; } } } return index; } + +/** set of test cases */ +const std::vector> test_set = { + // {text, pattern, expected output} + {"a", "aa", "-1"}, {"a", "a", "0"}, {"ba", "b", "0"}, + {"bba", "bb", "0"}, {"bbca", "c", "2"}, {"ab", "b", "1"}}; + +/** Main function */ +int main() { + for (size_t i = 0; i < test_set.size(); i++) { + int output = brute_force(test_set[i][0], test_set[i][1]); + + if (std::to_string(output) == test_set[i][2]) + std::cout << "success\n"; + else + std::cout << "failure\n"; + } + return 0; +} From 26701b38befee0151c124e4e0ae3702e88ed149a Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Fri, 29 May 2020 08:27:33 -0400 Subject: [PATCH 3/5] document rabin-karp string search --- strings/rabin_karp.cpp | 108 ++++++++++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 40 deletions(-) diff --git a/strings/rabin_karp.cpp b/strings/rabin_karp.cpp index 62e3691c1..018ff5632 100644 --- a/strings/rabin_karp.cpp +++ b/strings/rabin_karp.cpp @@ -1,39 +1,64 @@ -/* - * file name : rabin_karp.cpp - * author : Amit Kumar - * Copyright : 2020 , Amit Kumar - * version : 1.0 +/** + * \file + * \brief The [Rabin-Karp + * Algorithm](https://en.wikipedia.org/wiki/Rabin–Karp_algorithm) for finding a + * pattern within a piece of text with complexity O(n + m) */ -#include -#include -#include -#include +#include +#include +#include +#ifdef _MSC_VER +#include // use this for MS Visucal C++ +#else +#include +#endif -using std::string; -using std::pow; +#define PRIME 5 ///< Prime modulus for hash functions -#define PRIME 5 - -int64_t create_hash(string s , int n) { +/** + * convert a string to an intger - called as hashing function + * \param[in] s source of string to hash + * \param[in] n length of substring to hash + * \returns hash integer + */ +int64_t create_hash(const std::string& s, int n) { int64_t result = 0; - for ( int i = 0; i < n; ++i ) { - result += (int64_t)(s[i] * (int64_t)pow(PRIME , i)); + for (int i = 0; i < n; ++i) { + result += (int64_t)(s[i] * (int64_t)pow(PRIME, i)); } return result; } -int64_t recalculate_hash(string s , int old_index , - int new_index , int64_t old_hash , int patLength) { +/** + * re-hash a string using known existing hash + * \param[in] s source of string to hash + * \param[in] old_index previous index of string + * \param[in] new_index new index of string + * \param[in] old_hash previous hash of substring + * \param[in] patLength length of substring to hash + * \returns new hash integer + */ +int64_t recalculate_hash(const std::string& s, int old_index, int new_index, + int64_t old_hash, int patLength) { int64_t new_hash = old_hash - s[old_index]; new_hash /= PRIME; - new_hash += (int64_t)(s[new_index]*(int64_t)pow(PRIME, patLength-1)); + new_hash += (int64_t)(s[new_index] * (int64_t)pow(PRIME, patLength - 1)); return new_hash; } -bool check_if_equal(string str1 , string str2 , - int start1 , int end1 , - int start2 , int end2) { - if (end1-start1 != end2-start2) { +/** + * compare if two sub-strings are equal + * \param[in] str1 string pattern to search + * \param[in] str2 text in which to search + * \param[in] start1,end1 start and end indices for substring in str1 + * \param[in] start2,end2 start and end indices for substring in str2 + * \returns `true` if pattern was found + * \returns `false` if pattern was not found + * @note can this be replaced by std::string::compare? + */ +bool check_if_equal(const std::string& str1, const std::string& str2, + int start1, int end1, int start2, int end2) { + if (end1 - start1 != end2 - start2) { return false; } while (start1 <= end1 && start2 <= end2) { @@ -46,33 +71,36 @@ bool check_if_equal(string str1 , string str2 , return true; } -/* - * @description : search pattern in the given text - * @param : string str - * @param : string pat - * @return index of first occurrence of pattern or -1 if pattern not found +/** + * Perform string pattern search using Rabin-Karp algorithm + * @param[in] str string to search in + * @param[in] pat pattern to search for + * @return index of first occurrence of pattern + * @return -1 if pattern not found */ -int rabin_karp(const string &str , const string& pat) { - int64_t pat_hash = create_hash(pat , pat.size()); - int64_t str_hash = create_hash(str , pat.size()); - for (int i=0; i <= str.size()-pat.size(); ++i) { +int rabin_karp(const std::string& str, const std::string& pat) { + int64_t pat_hash = create_hash(pat, pat.size()); + int64_t str_hash = create_hash(str, pat.size()); + for (int i = 0; i <= str.size() - pat.size(); ++i) { if (pat_hash == str_hash && - check_if_equal(str , pat , i , i+pat.size()-1 , 0 , pat.size()-1)) { - return i; + check_if_equal(str, pat, i, i + pat.size() - 1, 0, + pat.size() - 1)) { + return i; } - if (i < str.size()-pat.size()) { + if (i < str.size() - pat.size()) { str_hash = - recalculate_hash(str, i, i+pat.size(), str_hash, pat.size()); + recalculate_hash(str, i, i + pat.size(), str_hash, pat.size()); } } return -1; // return -1 if given pattern not found } +/** Main function */ int main(void) { - assert(rabin_karp("helloWorld", "world") == -1); - assert(rabin_karp("helloWorld", "World") == 5); - assert(rabin_karp("this_is_c++" , "c++") == 8); - assert(rabin_karp("happy_coding", "happy") == 0); + assert(rabin_karp("helloWorld", "world") == -1); + assert(rabin_karp("helloWorld", "World") == 5); + assert(rabin_karp("this_is_c++", "c++") == 8); + assert(rabin_karp("happy_coding", "happy") == 0); return 0; } From 82362529dfb4d9f1d8e14be74388ef6e1f4eee55 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Fri, 29 May 2020 08:32:44 -0400 Subject: [PATCH 4/5] fixed mainpage readme --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2cad007c3..85ddf1bb9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ - +{#mainpage} +--> # The Algorithms - C++ [![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/kvedala/C-Plus-Plus/blob/master/CONTRIBUTION.md)  ![GitHub repo size](https://img.shields.io/github/repo-size/kvedala/C-Plus-Plus?color=red&style=flat-square) From a79ce9d2b610ffea591c10a90e6550950cffe7bf Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Fri, 29 May 2020 08:42:21 -0400 Subject: [PATCH 5/5] doxygen v1.8.18 will suppress out the #minipage in the markdown --- README.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 85ddf1bb9..df39ee554 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,5 @@ - -# The Algorithms - C++ +# The Algorithms - C++ # {#mainpage} + [![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/kvedala/C-Plus-Plus/blob/master/CONTRIBUTION.md)  ![GitHub repo size](https://img.shields.io/github/repo-size/kvedala/C-Plus-Plus?color=red&style=flat-square) ![GitHub closed pull requests](https://img.shields.io/github/issues-pr-closed/kvedala/C-Plus-Plus?color=green&style=flat-square)