forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
60 lines (48 loc) · 1.98 KB
/
Copy pathcode.js
File metadata and controls
60 lines (48 loc) · 1.98 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
function max(a,b) {
if(a>b){
return a;
} else {
return b;
}
}
logger._print("LPS for any string with length = 1 is 1");
for(i=2;i<=N;i++) {
logger._print("--------------------------------------------------");
logger._print("Considering a sub-string of length "+i);
logger._print("--------------------------------------------------");
for(j=0;j<N-i+1;j++) {
var k = j+i-1;
tracer._select(j)._wait();
tracer._notify(k)._wait();
logger._print("Comparing "+seq[j] + " and "+seq[k]);
if(seq[j]==seq[k] && i==2) {
logger._print("They are equal and size of the string in the interval"+j+" to "+k+" is 2, so the Longest Palindromic Subsequence in the Given range is 2");
matrix._notify(j,k)._wait();
L[j][k]=2;
matrix._setData(L);
matrix._denotify(j,k)._wait();
} else if(seq[j]==seq[k]) {
logger._print("They are equal, so the Longest Palindromic Subsequence in the Given range is 2 + the Longest Increasing Subsequence between the indices "+(j+1)+" to "+(k-1));
matrix._notify(j,k)._wait();
matrix._select(j+1,k-1)._wait();
L[j][k] = L[j+1][k-1] + 2;
matrix._setData(L);
matrix._denotify(j,k)._wait();
matrix._deselect(j+1,k-1)._wait();
} else {
logger._print("They are NOT equal, so the Longest Palindromic Subsequence in the Given range is the maximum Longest Increasing Subsequence between the indices "+(j+1)+" to "+(k) + " and "+(j)+" to "+(k-1));
matrix._notify(j,k)._wait();
matrix._select(j+1,k)._wait();
matrix._select(j,k-1)._wait();
L[j][k] = max(L[j+1][k],L[j][k-1]);
matrix._setData(L);
matrix._denotify(j,k)._wait();
matrix._deselect(j+1,k)._wait();
matrix._deselect(j,k-1)._wait();
}
logger._print("--------------------------------------------------");
tracer._deselect(j)._wait();
tracer._denotify(k)._wait();
}
}
logger._print("Longest Increasing Subsequence of the given string = L[0]["+(N-1)+"]="+L[0][N-1]);