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
79 lines (77 loc) · 3.33 KB
/
Copy pathcode.js
File metadata and controls
79 lines (77 loc) · 3.33 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
function createZarr(concat) {
var i,left,right,k,N;
N=concat.length;
left=0;
right=0;
for(i=1;i<N;i++) {
tracer._select(i)._wait();
if(i>right) {
left=right=i;
while(right<N && concat[right]==concat[right-left]) {
concat_tracer._notify(right)._wait();
concat_tracer._select(right-left)._wait();
logger._print(concat[right]+" ( at position "+right+" ) is equal to "+concat[right-left]+" (at position "+(right-left)+")");
concat_tracer._denotify(right)._wait();
concat_tracer._deselect(right-left)._wait();
right++;
}
concat_tracer._notify(right)._wait();
concat_tracer._select(right-left)._wait();
logger._print(concat[right]+" ( at position "+right+" ) is NOT equal to "+concat[right-left]+" (at position "+(right-left)+")");
concat_tracer._denotify(right)._wait();
concat_tracer._deselect(right-left)._wait();
z[i]=(right-left);
logger._print("--------------------------------");
logger._print("Value of z["+i+"] = the length of the substring starting from "+i+" which is also the prefix of the concatinated string(="+(right-left)+")");
logger._print("--------------------------------");
right--;
} else {
if(z[i-left]<(right-i+1)) {
logger._print("The substring from index "+(i-left)+" will not cross the right end.");
concat_tracer._select(i-left)._wait();
concat_tracer._notify(right-i+1)._wait();
z[i]=z[i-left];
concat_tracer._deselect(i-left)._wait();
concat_tracer._denotify(right-i+1)._wait();
} else {
logger._print("The substring from index "+(i-left)+" will cross the right end.");
left=i;
while (right<N && concat[right]==concat[right-left]) {
concat_tracer._notify(right)._wait();
concat_tracer._select(right-left)._wait();
logger._print(concat[right]+" ( at position "+right+" ) is equal to "+concat[right-left]+" (at position "+(right-left)+")");
concat_tracer._denotify(right)._wait();
concat_tracer._deselect(right-left)._wait();
right++;
}
concat_tracer._notify(right)._wait();
concat_tracer._select(right-left)._wait();
logger._print(concat[right]+" ( at position "+right+" ) is NOT equal to "+concat[right-left]+" (at position "+(right-left)+")");
concat_tracer._denotify(right)._wait();
concat_tracer._deselect(right-left)._wait();
z[i]=(right-left);
right--;
logger._print("--------------------------------");
logger._print("Value of z["+i+"] = the length of the substring starting from "+i+" which is also the prefix of the concatinated string(="+(right-left)+")");
logger._print("--------------------------------");
}
}
tracer._deselect(i)._wait();
tracer._setData(z);
}
}
var concat = pattern + "$" + text;
concat_tracer._setData(concat);
var patLen = pattern.length;
createZarr(concat);
tracer._setData(z);
var i;
logger._print("The Values in Z array equal to the length of the pattern indicates the index at which the pattern is present");
logger._print("===================================");
for(i=0;i<len;i++) {
if(z[i]==patLen) {
var pos = i - (patLen+1);
logger._print("Pattern Found at index "+pos);
}
}
logger._print("===================================");