Skip to content

Commit 444c9fe

Browse files
authored
Merge pull request #3 from ybalcanci/hw2
hw1 error fix
2 parents 1b60cbb + 955ee8e commit 444c9fe

8 files changed

+256
-251
lines changed

.gitignore

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Logs
2-
logs
3-
*.log
4-
5-
**/Dockerfile
6-
**/*.iml
7-
**/*.idea
8-
9-
# Example Data
10-
#hw1/*.fa
1+
# Logs
2+
logs
3+
*.log
4+
5+
**/Dockerfile
6+
**/*.iml
7+
**/*.idea
8+
9+
# Example Data
10+
**/*.fa
11+
**/*.fm
12+
**/*.bwt

hw1/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
all:
1+
all:
22
g++ -o out readFile.cpp brute_force.cpp knuth_morris_pratt.cpp boyer_moore.cpp rabin_karp.cpp main.cpp

hw1/boyer_moore.cpp

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
#include <string>
2-
#include <iostream>
3-
4-
int badChar(const std::string pattern, const int failureIndex, const char failedChar){
5-
int i = failureIndex;
6-
while(i >= 0){
7-
if(pattern.at(i) == failedChar)
8-
return failureIndex - i;
9-
i--;
10-
}
11-
return failureIndex + 1;
12-
}
13-
14-
int boyer_moore(std::string text, std::string pattern){
15-
int textLength = text.size();
16-
int patternLength = pattern.size();
17-
int numOfCharComparisons = 0;
18-
int i = 0, j = 0; // i -> text || j -> pattern
19-
while(i < textLength - patternLength + 1){
20-
j = 0;
21-
for(int k = patternLength - 1; k >= 0; k--){
22-
numOfCharComparisons++;
23-
if(pattern.at(k) != text.at(i + k)){
24-
i += badChar(pattern, k, text.at(i + k));
25-
break;
26-
}
27-
if(k == 0){
28-
std::cout << "Boyer-Moore # of char comparions: " << numOfCharComparisons << std::endl;
29-
return i;
30-
}
31-
32-
}
33-
}
34-
std::cout << "Boyer-Moore # of char comparions: " << numOfCharComparisons << std::endl;
35-
return -1;
1+
#include <string>
2+
#include <iostream>
3+
4+
int badChar(const std::string pattern, const int failureIndex, const char failedChar){
5+
int i = failureIndex;
6+
while(i >= 0){
7+
if(pattern.at(i) == failedChar)
8+
return failureIndex - i;
9+
i--;
10+
}
11+
return failureIndex + 1;
12+
}
13+
14+
int boyer_moore(std::string text, std::string pattern){
15+
int textLength = text.size();
16+
int patternLength = pattern.size();
17+
int numOfCharComparisons = 0;
18+
int i = 0, j = 0; // i -> text || j -> pattern
19+
while(i < textLength - patternLength + 1){
20+
j = 0;
21+
for(int k = patternLength - 1; k >= 0; k--){
22+
numOfCharComparisons++;
23+
if(pattern.at(k) != text.at(i + k)){
24+
i += badChar(pattern, k, text.at(i + k));
25+
break;
26+
}
27+
if(k == 0){
28+
std::cout << "Boyer-Moore # of char comparions: " << numOfCharComparisons << std::endl;
29+
return i;
30+
}
31+
32+
}
33+
}
34+
std::cout << "Boyer-Moore # of char comparions: " << numOfCharComparisons << std::endl;
35+
return -1;
3636
}

hw1/brute_force.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
#include <string>
2-
#include <iostream>
3-
4-
int brute_force(std::string text, std::string pattern){
5-
int textLength = text.size();
6-
int patternLength = pattern.size();
7-
int numOfCharComparisons = 0;
8-
for(int i = 0; i < textLength - patternLength + 1; i++){
9-
/*
10-
text: compared string.
11-
parameters of compare:
12-
i: Position of the first character in the compared string.
13-
patternLength: Length of compared string.
14-
pattern: comparing string.
15-
*/
16-
for(int j = 0; j < patternLength; j++){
17-
numOfCharComparisons++;
18-
if(text.at(i + j) != pattern.at(j))
19-
break;
20-
if(j == patternLength - 1){
21-
std::cout << "Brute force # of char comparions: " << numOfCharComparisons << std::endl;
22-
return i;
23-
}
24-
}
25-
}
26-
std::cout << "Brute force # of char comparions: " << numOfCharComparisons << std::endl;
27-
return -1;
1+
#include <string>
2+
#include <iostream>
3+
4+
int brute_force(std::string text, std::string pattern){
5+
int textLength = text.size();
6+
int patternLength = pattern.size();
7+
int numOfCharComparisons = 0;
8+
for(int i = 0; i < textLength - patternLength + 1; i++){
9+
/*
10+
text: compared string.
11+
parameters of compare:
12+
i: Position of the first character in the compared string.
13+
patternLength: Length of compared string.
14+
pattern: comparing string.
15+
*/
16+
for(int j = 0; j < patternLength; j++){
17+
numOfCharComparisons++;
18+
if(text.at(i + j) != pattern.at(j))
19+
break;
20+
if(j == patternLength - 1){
21+
std::cout << "Brute force # of char comparions: " << numOfCharComparisons << std::endl;
22+
return i;
23+
}
24+
}
25+
}
26+
std::cout << "Brute force # of char comparions: " << numOfCharComparisons << std::endl;
27+
return -1;
2828
}

hw1/knuth_morris_pratt.cpp

+55-54
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
1-
#include <string>
2-
#include <iostream>
3-
4-
int* failureFunctionKMP(std::string pattern){
5-
int m = pattern.size();
6-
int* F = new int[m];
7-
int i = 1, j = 0;
8-
while(i < m){
9-
if(pattern.at(i) == pattern.at(j)){
10-
F[i] = j + 1;
11-
i++;
12-
j++;
13-
}
14-
else if(j > 0){
15-
j = F[j - 1];
16-
}
17-
else{
18-
F[i] = 0;
19-
i++;
20-
}
21-
}
22-
return F;
23-
}
24-
25-
int knuth_morris_pratt(std::string text, std::string pattern){
26-
int textLength = text.size();
27-
int patternLength = pattern.size();
28-
int numOfCharComparisons = 0;
29-
int* F = failureFunctionKMP(pattern);
30-
int i = 0, j = 0;
31-
while(i < textLength){
32-
numOfCharComparisons++;
33-
if(text.at(i) == pattern.at(j)){
34-
if(j == patternLength - 1){
35-
std::cout << "Knuth-Morris-Pratt # of char comparions: " << numOfCharComparisons << std::endl;
36-
return i - j;
37-
}
38-
else{
39-
i++;
40-
j++;
41-
}
42-
}
43-
else{
44-
if(j > 0){
45-
j = F[j - 1];
46-
}
47-
else{
48-
i++;
49-
j = 0;
50-
}
51-
}
52-
}
53-
std::cout << "Knuth-Morris-Pratt # of char comparions: " << numOfCharComparisons << std::endl;
54-
return -1;
1+
#include <string>
2+
#include <iostream>
3+
4+
int* failureFunctionKMP(std::string pattern){
5+
int m = pattern.size();
6+
int* F = new int[m];
7+
int i = 1, j = 0;
8+
F[0] = 0;
9+
while(i < m){
10+
if(pattern.at(i) == pattern.at(j)){
11+
F[i] = j + 1;
12+
i++;
13+
j++;
14+
}
15+
else if(j > 0){
16+
j = F[j - 1];
17+
}
18+
else{
19+
F[i] = 0;
20+
i++;
21+
}
22+
}
23+
return F;
24+
}
25+
26+
int knuth_morris_pratt(std::string text, std::string pattern){
27+
int textLength = text.size();
28+
int patternLength = pattern.size();
29+
int numOfCharComparisons = 0;
30+
int* F = failureFunctionKMP(pattern);
31+
int i = 0, j = 0;
32+
while(i < textLength){
33+
numOfCharComparisons++;
34+
if(text.at(i) == pattern.at(j)){
35+
if(j == patternLength - 1){
36+
std::cout << "Knuth-Morris-Pratt # of char comparions: " << numOfCharComparisons << std::endl;
37+
return i - j;
38+
}
39+
else{
40+
i++;
41+
j++;
42+
}
43+
}
44+
else{
45+
if(j > 0){
46+
j = F[j - 1];
47+
}
48+
else{
49+
i++;
50+
j = 0;
51+
}
52+
}
53+
}
54+
std::cout << "Knuth-Morris-Pratt # of char comparions: " << numOfCharComparisons << std::endl;
55+
return -1;
5556
}

hw1/main.cpp

+62-61
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,63 @@
1-
#include <iostream>
2-
#include <fstream>
3-
#include <string>
4-
#include <sstream>
5-
#include <iomanip>
6-
#include <chrono>
7-
8-
using namespace std;
9-
using namespace std::chrono;
10-
11-
string readFile(std::ifstream& in);
12-
int brute_force(string textFileName, string patternFileName);
13-
int knuth_morris_pratt(string textFileName, string patternFileName);
14-
int boyer_moore(std::string text, std::string pattern);
15-
int rabin_karp(std::string text, std::string pattern);
16-
17-
int main(int argc, char *argv[]) {
18-
if (argc < 3) {
19-
fprintf(stderr, "usage: ./out <textFileName patternFileName>\n");
20-
return -1;
21-
}
22-
23-
ifstream myTextFile(argv[1]);
24-
string text = readFile(myTextFile);
25-
26-
ifstream myPatternFile(argv[2]);
27-
string pattern = readFile(myPatternFile);
28-
29-
int textLength = text.size();
30-
int patternLength = pattern.size();
31-
32-
auto start = high_resolution_clock::now();
33-
int bruteForceRes = brute_force(text, pattern);
34-
auto stop = high_resolution_clock::now();
35-
auto duration = duration_cast<microseconds>(stop - start);
36-
cout << "Brute force execution time: " << duration.count() << endl;
37-
cout << "Brute force found pattern in index: " << bruteForceRes << endl;
38-
cout << endl;
39-
40-
start = high_resolution_clock::now();
41-
int KMPRes = knuth_morris_pratt(text, pattern);
42-
stop = high_resolution_clock::now();
43-
duration = duration_cast<microseconds>(stop - start);
44-
cout << "Knuth-Morris-Pratt execution time: " << duration.count() << endl;
45-
cout << "Knuth-Morris-Pratt found pattern in index: " << KMPRes << endl;
46-
cout << endl;
47-
48-
start = high_resolution_clock::now();
49-
int BoyerMooreRes = boyer_moore(text, pattern);
50-
stop = high_resolution_clock::now();
51-
duration = duration_cast<microseconds>(stop - start);
52-
cout << "Boyer-Moore execution time: " << duration.count() << endl;
53-
cout << "Boyer-Moore found pattern in index " << BoyerMooreRes << endl;
54-
cout << endl;
55-
56-
start = high_resolution_clock::now();
57-
int rabinKarpRes = rabin_karp(text, pattern);
58-
stop = high_resolution_clock::now();
59-
duration = duration_cast<microseconds>(stop - start);
60-
cout << "Rabin-Karp execution time: " << duration.count() << endl;
61-
cout << "Rabin-Karp found pattern in index: " << rabinKarpRes << endl;
1+
#include <iostream>
2+
#include <fstream>
3+
#include <string>
4+
#include <sstream>
5+
#include <iomanip>
6+
#include <chrono>
7+
8+
using namespace std;
9+
using namespace std::chrono;
10+
11+
string readFile(std::ifstream& in);
12+
int brute_force(string textFileName, string patternFileName);
13+
int knuth_morris_pratt(string textFileName, string patternFileName);
14+
int boyer_moore(std::string text, std::string pattern);
15+
int rabin_karp(std::string text, std::string pattern);
16+
17+
int main(int argc, char *argv[]) {
18+
if (argc < 3) {
19+
fprintf(stderr, "usage: ./out <textFileName patternFileName>\n");
20+
return -1;
21+
}
22+
23+
ifstream file1(argv[1]);
24+
ifstream file2(argv[2]);
25+
26+
string text, pattern, text1 = readFile(file1), text2 = readFile(file2);
27+
text = text1.at(1) == 't' ? text1.substr(6) : text2.substr(6);
28+
pattern = text2.at(1) == 'p' ? text2.substr(9) : text1.substr(9);
29+
30+
int textLength = text.size();
31+
int patternLength = pattern.size();
32+
33+
auto start = high_resolution_clock::now();
34+
int bruteForceRes = brute_force(text, pattern);
35+
auto stop = high_resolution_clock::now();
36+
auto duration = duration_cast<microseconds>(stop - start);
37+
cout << "Brute force execution time: " << duration.count() << endl;
38+
cout << "Brute force found pattern in index: " << bruteForceRes << endl;
39+
cout << endl;
40+
41+
start = high_resolution_clock::now();
42+
int KMPRes = knuth_morris_pratt(text, pattern);
43+
stop = high_resolution_clock::now();
44+
duration = duration_cast<microseconds>(stop - start);
45+
cout << "Knuth-Morris-Pratt execution time: " << duration.count() << endl;
46+
cout << "Knuth-Morris-Pratt found pattern in index: " << KMPRes << endl;
47+
cout << endl;
48+
49+
start = high_resolution_clock::now();
50+
int BoyerMooreRes = boyer_moore(text, pattern);
51+
stop = high_resolution_clock::now();
52+
duration = duration_cast<microseconds>(stop - start);
53+
cout << "Boyer-Moore execution time: " << duration.count() << endl;
54+
cout << "Boyer-Moore found pattern in index " << BoyerMooreRes << endl;
55+
cout << endl;
56+
57+
start = high_resolution_clock::now();
58+
int rabinKarpRes = rabin_karp(text, pattern);
59+
stop = high_resolution_clock::now();
60+
duration = duration_cast<microseconds>(stop - start);
61+
cout << "Rabin-Karp execution time: " << duration.count() << endl;
62+
cout << "Rabin-Karp found pattern in index: " << rabinKarpRes << endl;
6263
}

0 commit comments

Comments
 (0)