From ebdd22b3ad7748c98f644347c9afbbd2e3ae33ad Mon Sep 17 00:00:00 2001 From: amandal3 Date: Sat, 10 Oct 2020 13:44:55 -0400 Subject: [PATCH 1/6] LCS: string and length --- c++/LCS/LCS.cpp | 29 ++++++++++ c++/LCS/LongestCommonSubsequence.hh | 87 +++++++++++++++++++++++++++++ c++/LCS/text1.txt | 1 + c++/LCS/text2.txt | 1 + c++/LongestCommonSubsequence.hh | 20 ------- 5 files changed, 118 insertions(+), 20 deletions(-) create mode 100644 c++/LCS/LCS.cpp create mode 100644 c++/LCS/LongestCommonSubsequence.hh create mode 100644 c++/LCS/text1.txt create mode 100644 c++/LCS/text2.txt delete mode 100644 c++/LongestCommonSubsequence.hh diff --git a/c++/LCS/LCS.cpp b/c++/LCS/LCS.cpp new file mode 100644 index 0000000..1582805 --- /dev/null +++ b/c++/LCS/LCS.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include"LongestCommonSubsequence.hh" + +using namespace std; + +int main () { + + int m,n; + + // Reading Text files + string mytext, mysecondtext; + ifstream myfile("text1.txt"); + getline(myfile,mytext); + + ifstream myfile2("text2.txt"); + getline(myfile2,mysecondtext); + + m = int(mytext.length()); + n = int(mysecondtext.length()); + cout << "String 1: " << m << endl; + cout << "String 2: " << n << endl; + + cout <<"LCS: "<< LCS_V2(mytext.c_str(), mysecondtext.c_str(), m, n) << endl; + cout <<"Print LCS: " << LCS_Common(mytext.c_str(), mysecondtext.c_str(), m, n) << endl; + + return 0; +} \ No newline at end of file diff --git a/c++/LCS/LongestCommonSubsequence.hh b/c++/LCS/LongestCommonSubsequence.hh new file mode 100644 index 0000000..dd48269 --- /dev/null +++ b/c++/LCS/LongestCommonSubsequence.hh @@ -0,0 +1,87 @@ +/* + Example: Longest Common Subsequence calculates the number of letters + in common between two strings. + + This example is terrible because it is O(2**n). The key is to use + dynamic programming for this problem or more efficiently, to create + the array and fill it sequentially since it all must be done in any + case. This placeholder is an example of how to contribute an + algorithm, as a header file with a separate test code + that demonstrates its use. + + */ +int LCS(const char* a, const char* b) { + if (*a == '\0' || *b == '\0') + return 0; + if (*a == *b) + return 1 + LCS(a+1,b+1); + return std::max(LCS(a+1,b), LCS(a, b+1)); +} + +int LCS_V2(const char a[], const char b[], int m, int n) { + int memo[m+1][n+1]; + int i, j; + for (i = 0; i < m; i++){ //0(m) + memo[i][0] = 0; + } + for (j = 0; j < n; j++){ //O(n) + memo[0][j] = 0; + } + for (i = 1; i < m + 1 ; i++){ + for (j = 1; j < n + 1; j++){ + if (a[i] == b[j]){ + memo[i][j] = 1 + memo[i-1][j-1]; + } + else { + memo[i][j] = std::max(memo[i][j-1], memo[i-1][j]); + } + } + } + return memo[m][n]; +} + +int LCS_Common(const char a[], const char b[], int m, int n){ + int memo2[m+1][n+1]; + int i, j; + for (i = 0; i < m; i++){ //0(m) + memo2[i][0] = 0; + } + for (j = 0; j < n; j++){ //O(n) + memo2[0][j] = 0; + } + for (i = 1; i < m + 1 ; i++){ + for (j = 1; j < n + 1; j++){ + if (a[i] == b[j]){ + memo2[i][j] = 1 + memo2[i-1][j-1]; + } + else { + memo2[i][j] = std::max(memo2[i][j-1], memo2[i-1][j]); + } + } + } + + int index = memo2[m][n]; + char lcs[index+1]; + lcs[index] = '\0'; + + int x, y; + x = m; + y = n; + while (x > 0 && y > 0) + { + if (a[x-1] == b[y-1]) + { + lcs[index-1] = a[x-1]; + x--; + y--; + index--; + } + else if (memo2[x-1][y] > memo2[x][y-1]) + x--; + else + y--; + } + std::cout << lcs << std::endl; + + return 0; +} \ No newline at end of file diff --git a/c++/LCS/text1.txt b/c++/LCS/text1.txt new file mode 100644 index 0000000..c8e492a --- /dev/null +++ b/c++/LCS/text1.txt @@ -0,0 +1 @@ +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \ No newline at end of file diff --git a/c++/LCS/text2.txt b/c++/LCS/text2.txt new file mode 100644 index 0000000..e83bcc9 --- /dev/null +++ b/c++/LCS/text2.txt @@ -0,0 +1 @@ +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. \ No newline at end of file diff --git a/c++/LongestCommonSubsequence.hh b/c++/LongestCommonSubsequence.hh deleted file mode 100644 index ffebd93..0000000 --- a/c++/LongestCommonSubsequence.hh +++ /dev/null @@ -1,20 +0,0 @@ -/* - Example: Longest Common Subsequence calculates the number of letters - in common between two strings. - - This example is terrible because it is O(2**n). The key is to use - dynamic programming for this problem or more efficiently, to create - the array and fill it sequentially since it all must be done in any - case. This placeholder is an example of how to contribute an - algorithm, as a header file with a separate test code - that demonstrates its use. - - */ -int LCS(const char* a, const char* b) { - if (*a == '\0' || *b == '\0') - return 0; - if (*a == *b) - return 1 + LCS(a+1,b+1); - return max(LCS(a+1,b), LCS(a, b+1)); -} - From 3bc673b9653a604f5e75ab7afeaaec4d550b5420 Mon Sep 17 00:00:00 2001 From: amandal3 Date: Sat, 10 Oct 2020 16:21:55 -0400 Subject: [PATCH 2/6] Number Algorithms gcd, lcm, power, powermod, fermat --- c++/NumberTheoreticAlgorithms/Primes.cpp | 71 ++++++++++++++++++ c++/NumberTheoreticAlgorithms/algorithms.hh | 79 +++++++++++++++++++++ c++/NumberTheoreticAlgorithms/test.cpp | 27 +++++++ 3 files changed, 177 insertions(+) create mode 100644 c++/NumberTheoreticAlgorithms/Primes.cpp create mode 100644 c++/NumberTheoreticAlgorithms/algorithms.hh create mode 100644 c++/NumberTheoreticAlgorithms/test.cpp diff --git a/c++/NumberTheoreticAlgorithms/Primes.cpp b/c++/NumberTheoreticAlgorithms/Primes.cpp new file mode 100644 index 0000000..a32a66b --- /dev/null +++ b/c++/NumberTheoreticAlgorithms/Primes.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +using namespace std; + +bool isPrime(int n) { //O(sqrt n) + for (int i = 2; i <= sqrt(n); i++) { + if (n % i == 0) + return false; // not prime + } + return true; // prime +} + +void Erathosthenes(int num){ + int count = 0; + bool* sieve = new bool [num]; + + for (int i = 2; i <= num; i++){ //O(num) + sieve[i] = 1; + } + +// remove even + for (int j = 4; j <=num; j+=2){ //O(num) + sieve[j] = 0; + } + + for (int k = 3; k <= num; k += 2){ //O(num) + if (sieve[k] == 1){ + for (int l = k*k; l <= num; l = l + 2*k){ + sieve[l] = 0; + } + } + } + + vector list; + // counter of primes + for(int m = 2; m <=num; m++){ + if (sieve[m] == 1){ + list.push_back(m); + count++; + } + } + cout << count << "\n" << "List of Primes: "; + + // if (list.empty()){ + // cout << "No Primes."; + // } + + for (int n = 0; n > N; + + if (isPrime(N) == 0){ + cout << "Number of prime: "; + Erathosthenes(N); + } + else { + cout << N << " is prime." < b) + return gcd(a - b, b); + + return gcd(a, b - a); +} + +// LCM: lowest common multiple +int lcm(int a, int b) +{ + if (a == 0 && b == 0) + { + std::cout << "A and B can't be both zero for this LCM algorithem."; + return 0; + } + else + { + std::cout << (a * b) / gcd(a, b); + return 0; + } + +} + +// Power +int power(int x, int y) +{ + int prod = 1; + while (y > 0){ + if (y & 1){ + prod = prod * x; + } + x = x * x; + y = y/2; + } + return prod; +} + +/* + Powermod: Modular exponentation + PowerMod[a,b,m] gives the remainder of a^b divided by m. +*/ +int powermod(int a, int b, int m) //O(log n) +{ + int prod = 1; + while (b > 0){ + if (b % 2 != 0){ + prod = (prod * a) % m; + } + a = a * a % m; + b = b/2; + } + return prod; +} + +// Fermat +int fermat(int n, int k) { + return powermod(n, k-2, k); +} + +bool fermat2(int n, int k) { + for (int i = 0; i < k; i++) { + int a = rand() % n+1; + + if (powermod(a, n-1, n) != 1){ + return false; //not prime + } + } + return true; //prime +} + \ No newline at end of file diff --git a/c++/NumberTheoreticAlgorithms/test.cpp b/c++/NumberTheoreticAlgorithms/test.cpp new file mode 100644 index 0000000..9eae9d1 --- /dev/null +++ b/c++/NumberTheoreticAlgorithms/test.cpp @@ -0,0 +1,27 @@ +#include +#include "algorithms.hh" +using namespace std; + +int main(int argc, char *argv[]){ + + int a[argc]; + + cout<<"You have entered: "< Date: Sat, 10 Oct 2020 19:44:16 -0400 Subject: [PATCH 3/6] Sorting BubbleSort, Mergesort, Quicksort, Heapsort --- c++/Sorting/Algorithms.hh | 155 ++++++++++++++++++++++++++++++++++++++ c++/Sorting/input.txt | 1 + c++/Sorting/testing.cpp | 71 +++++++++++++++++ 3 files changed, 227 insertions(+) create mode 100644 c++/Sorting/Algorithms.hh create mode 100644 c++/Sorting/input.txt create mode 100644 c++/Sorting/testing.cpp diff --git a/c++/Sorting/Algorithms.hh b/c++/Sorting/Algorithms.hh new file mode 100644 index 0000000..4a490c9 --- /dev/null +++ b/c++/Sorting/Algorithms.hh @@ -0,0 +1,155 @@ +#include +using namespace std; + +//Bubblesort +void bubblesort(int *a, int n) { //O(n^2) + for (int j = n-1; j > 0; j--){ + for (int i = 0; i < j; i++){ + if (a[i] > a[i+1]) { + swap(a[i], a[i+1]); + } + } + } +} + +void bubblesort_Better(int *a, int n) { //O(n) + bool done; + for (int j = n-1; j > 0; j--){ + done = true; + for (int i = 0; i < j; i++){ + if (a[i] > a[i+1]) { + swap(a[i], a[i+1]); + done = false; + } + } + if (done){ + return; + } + } +} + +//Insertion sort +void insertsort(int x[], int n){ + for (int i=1; i < n; i++){ + if (x[i] < x[i-1]) { + int temp = x[i]; + for (int j = i - 1; j >= 0; j--){ + if (temp < x[j] && j != 0) { + x[j+1] = x[j]; + } + else if (temp < x[j] ){ + x[j+1] = x[j]; + x[j] = temp; + } + else { + x[j] = temp ; + break; + } + } + } + } +} + +// Quicksort +void quicksort(int *x, int L, int R){ + int i = L; + int j = R; + int pivot = x[L + (rand() % R-L)]; + if (L >= R) + return; + while (i <= j) { + while (x[i] < pivot) + i++; + while (x[j] > pivot) + j--; + if (i <= j) { + swap (x[i], x[j]); + i++; + j--; + } + } + if (L < j) + quicksort(x, L, j); + if (i < R) + quicksort(x, i, R); +} + +// Heapsort +void makesubheap(int *arr, int i, int n){ + int root = n; + int left = 2*n + 1; + int right = 2*n + 2; + + if (left < i && arr[left] > arr[root]){ //Left > root + root = left; + } + if (right < i && arr[right] > arr[root]){ // Right > root + root = right; + } + if (root != n) //if the largest value is not the root keep swapping and recalling itself + { + swap(arr[n], arr[root]); + makesubheap(arr, i, root); + } +} + +void makeheap (int *a, int n){ + for (int i = n/2; i >= 0; i--){ + makesubheap(a, n, i); + } +} + +void heapsort(int *a, int n){ + makeheap(a, n); + for (int i = n-1; i >= 0; i--){ + swap(a[0], a[i]); // move to correct place + makesubheap(a, i, 0); // heap gets smaller + } +} + +// Mergesort +void MergeSingle(int *A, int low, int mid, int high){ + int i = low; + int j = mid+1; + int k = low; + int *B = new int[high+1]; + while (i <= mid && j <= high){ + if (A[i] < A[j]){ + B[k++] = A[i++]; + } else { + B[k++] = A[j++]; + } + } + while (i <= mid){ + B[k++] = A[i++]; + } + while (j <= high){ + B[k++] = A[j++]; + } + for (int i=low; i<=high; i++){ + A[i] = B[i]; + } + delete [] B; +} + +void mergeSort(int *A, int n){ + int p, l, h ,mid, i; + for(p = 2; p<=n; p=p*2){ + for(i=0; i+p<=n; i=i+p){ + l = i; + h = i+p-1; + mid = (l+h)/2; + MergeSingle(A,l,mid,h); + } + } + if(p/2 +#include +#include +#include "Algorithms.hh" + +using namespace std; + +int main (){ + int a, b, opt; + char cont = 'C'; + + ifstream myfile("input.txt"); + if (myfile.is_open()){ + myfile>>a>>b; + } + + int *c = new int[a]; + int *randA = new int[a]; + + for(int i=0;i> opt; + + switch(opt){ + + case 1: + cout<> cont; + } + +} \ No newline at end of file From 59df4977f757d1ce336d51cbaa40e0be32938f7d Mon Sep 17 00:00:00 2001 From: amandal3 Date: Sat, 10 Oct 2020 20:39:38 -0400 Subject: [PATCH 4/6] added radix sorting --- c++/Sorting/Algorithms.hh | 43 +++++++++++++++++++++++++++++++++++++++ c++/Sorting/testing.cpp | 10 ++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/c++/Sorting/Algorithms.hh b/c++/Sorting/Algorithms.hh index 4a490c9..d1c59ba 100644 --- a/c++/Sorting/Algorithms.hh +++ b/c++/Sorting/Algorithms.hh @@ -152,4 +152,47 @@ void printResult(int *B, int n){ cout<= 0; i--) { + output[count[(A[i]/radix) % 10] - 1] = A[i]; + count[(A[i]/radix) % 10]--; + } + + for (int i = 0; i < digit; i++){ + A[i] = output[i]; + } +} + +void radix (int *A, int radix){ + int max = A[0]; + for (int i = 1; i < radix; i++){ + if (A[i] > max){ + max = A[i]; + } + } + for (int loc = 1; max/loc > 0; loc *= 10){ + counting_Sort(A, radix, loc); + } } \ No newline at end of file diff --git a/c++/Sorting/testing.cpp b/c++/Sorting/testing.cpp index 12f6c74..1ac20fa 100644 --- a/c++/Sorting/testing.cpp +++ b/c++/Sorting/testing.cpp @@ -24,7 +24,7 @@ int main (){ } while (cont == 'C' || cont == 'c'){ - cout << "====Welcome to Sorting==== \nPress 1 for Quicksort \nPress 2 for Heapsort \nPress 3 for Mergesort \nPress 4 for Bubblesort \nAnything else to Quit." << endl; + cout << "====Welcome to Sorting==== \nPress 1 for Quicksort \nPress 2 for Heapsort \nPress 3 for Mergesort \nPress 4 for Bubblesort \nPress 5 for Radix Sorting \nAnything else to Quit." << endl; cin >> opt; switch(opt){ @@ -61,6 +61,14 @@ int main (){ printResult(c,a); cout< Date: Sat, 10 Oct 2020 20:41:24 -0400 Subject: [PATCH 5/6] Primes Erathosthenes n Good Prime O(sqrtn) --- c++/{NumberTheoreticAlgorithms => }/Primes.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename c++/{NumberTheoreticAlgorithms => }/Primes.cpp (100%) diff --git a/c++/NumberTheoreticAlgorithms/Primes.cpp b/c++/Primes.cpp similarity index 100% rename from c++/NumberTheoreticAlgorithms/Primes.cpp rename to c++/Primes.cpp From 20e535f4f9125003d4741b561a516bde020bd939 Mon Sep 17 00:00:00 2001 From: amandal3 Date: Sat, 10 Oct 2020 21:08:37 -0400 Subject: [PATCH 6/6] Searching Chap 7 of Notes: Linear, Binary, Golden Mean --- c++/Searching/search.hh | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 c++/Searching/search.hh diff --git a/c++/Searching/search.hh b/c++/Searching/search.hh new file mode 100644 index 0000000..b3e5239 --- /dev/null +++ b/c++/Searching/search.hh @@ -0,0 +1,74 @@ +/* + Golden Section Search + http://fourier.eng.hmc.edu/e176/lectures/ch3/node3.html + The golden section search algorithm can be used for finding a minimum (or maximum) + of a single-variable function f(x). If it is known that the function has a minimum between + some two points, then we can search for it iteratively, in a manner similar to the bisection + search for the root of an equation f(x)=0. +*/ +int goldenMeanSearch(int *data, int n){ + int L = 0, R = n-1, phi = 0.5, x, y; + int s = (R - L) / phi; + + while (R > L) { + x = R - s, y = L + s; + if (data[x] > data[y]){ + R = y; + y = x; + s = (R - L) / phi; + x = R - s; + } + else { + L = x; + x = y; + s = (R - L) / phi; + y = L + s; + } + } + return L; +} + +int binarySearch(int *a, int target) { //O(log n) + if (target < a[0] || target > a[a.size() -1]){ + return -1; + } + int L = 0, R = a.size()-1; + while (R > L) { + int guess = (L + R) / 2; + if (a[guess] > target) { + R = guess - 1; + } + else if (a[guess] < target) { + L = guess + 1; + } + else { + return guess; + } + } + return -1; //not found +} + +int binarySearch_V2(int *a, int L, int R, int target) { //O(log n) + if (L > R){ + return -1; + } + int guess = (L + R) / 2; + if (a[guess] > target) { + return binarySearch_V2(a, L, guess - 1, target); + } + else if (a[guess] < target) { + returnn binarySearch_V2(a, guess + 1 , R, target); + } + else { + return guess; + } +} + +int linearSearch(int *a, int n, int target){ //O(n) + for (int i = 0; i < n; i++){ + if (array[i] == target){ + return i; + } + } + return -1; +} \ No newline at end of file