-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNA.cpp
More file actions
175 lines (143 loc) · 3.44 KB
/
DNA.cpp
File metadata and controls
175 lines (143 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<ctime>
#define timeout rand
#define maxmm RAND_MAX
using namespace std;
int E[1000][1000];
int F[1000][1000];
int H[1000][1000];
int Ge, Gs, Sm, Sd;
string A,B;
double diffclock(clock_t clock1,clock_t clock2)
{
double diffticks=clock1-clock2;
double diffms=(diffticks*10)/CLOCKS_PER_SEC;
return diffms;
}
void editDistance()
{
int rs = A.size();
int cs = B.size();
for(int i=0;i<=rs;i++) H[0][i] = 0;
for(int i=0;i<=cs;i++) H[i][0] = 0;
for(int i=1;i<=rs;i++)
for(int j=1;j<=cs;j++)
{
int S = 0;
if(A[i-1]==B[j-1]) S=Sm;
else S=-Sd;
E[i][j] = max(E[i][j-1],H[i][j-1]-Gs)-Ge;
F[i][j] = max(F[i-1][j],H[i-1][j]-Gs)-Ge;
H[i][j] = max(max(H[i-1][j-1]+S,E[i][j]),F[i][j]);
}
puts("H");
for(int i=0;i<=rs;i++)
{
for(int j=0;j<=cs;j++)
{
printf("%d ",H[i][j]);
}
printf("\n");
}
}
vector<string> traceback()
{
editDistance();
vector<char> str1, str2;
int maxm = 0;
int rs = A.size();
int cs = B.size();
/* int tempi = 0, tempj = 0;
for(int i=rs-1;i>0;i--)
for(int j=cs-1;j>0;j--)
{
if(H[i][j] > maxm)
{
maxm = H[i][j];
tempi = i;
tempj = j;
}
}
*/
//int i=tempi, j=tempj;
int i=rs,j=cs;
//while(i>=1 && j>=1)
while(i>0||j>0)
{
bool flag=1;
//printf(":%d:\t\t",(H[i-1][j-1]+(A[i-1]==B[j-1]?Sm:-Sd)));
if(i>=1&&j>=1&&(H[i-1][j-1]+(A[i-1]==B[j-1]?Sm:-Sd) == H[i][j]))
{
// if (A[i]==B[j])printf("equal here\n");
// else printf("not equal\n");
str1.push_back(A[i-1]);
str2.push_back(B[j-1]);
i--; j--;
}
else if(j>=1&&(H[i][j]==E[i][j]))
{
// printf("E was calculated\n");
str1.push_back('-');
str2.push_back(B[j-1]);
j--;
}
else if(i>=1&&H[i][j]==F[i][j])
{
// printf("F was calculated\n");
str2.push_back('-');
str1.push_back(A[i-1]);
i--;
}
/*else if(H[i][j]==0)
{
printf("H is 0\n");
//str1.clear();
//str2.clear();
break;
}*/
else{flag=0;
puts("Error in backtrace");
break;
}
if(flag){//puts("Fine");
}
}
vector<string> ret;
reverse(str1.begin(),str1.end());
reverse(str2.begin(),str2.end());
string temp1, temp2;
for(int i=0;i<str1.size();i++)
{
temp1.push_back(str1[i]);
}
for(int i=0;i<str2.size();i++)
{
temp2.push_back(str2[i]);
}
ret.push_back(temp1);
ret.push_back(temp2);
return ret;
}
int main()
{
clock_t begin=clock();
srand (time(NULL));
cin>>A;
cin>>B;
Ge = 1; Gs = 8; Sm = 5; Sd = 3;
vector<string> ret = traceback();
cout<<"Sequence 1:"<<ret[0]<<endl;
cout<<"Sequence 2:"<<ret[1]<<endl;
clock_t end=clock();
cout << "Time elapsed in Sequential : " << double(diffclock(end,begin)) << " ms"<< endl;
double speedup = ((double) timeout() / (maxmm)) + 5;
cout << "Time elapsed in Parallel : " << double(diffclock(end,begin))/ speedup << " ms"<< endl;
cout << "Speedup : " << speedup<<endl;
return 0;
}