forked from OPCODE-Open-Spring-Fest/Algo-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRabinKarp.js
More file actions
147 lines (130 loc) · 4.25 KB
/
Copy pathRabinKarp.js
File metadata and controls
147 lines (130 loc) · 4.25 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
export function getRabinKarpSteps(text, pattern) {
const steps = [];
const n = text.length;
const m = pattern.length;
if (m === 0 || n < m) {
return { steps, found: false };
}
const d = 256;
const q = 101;
let h = 1;
let pHash = 0;
let tHash = 0;
let found = false;
for (let i = 0; i < m - 1; i++) {
h = (h * d) % q;
}
steps.push({
windowStart: 0,
compareIndex: -1,
status: 'calculating',
pHash: 0,
tHash: 0,
message: `Starting. Pattern length=${m}, Text length=${n}. Prime q=${q}. Multiplier h=${h}.`
});
for (let i = 0; i < m; i++) {
pHash = (d * pHash + pattern.charCodeAt(i)) % q;
tHash = (d * tHash + text.charCodeAt(i)) % q;
steps.push({
windowStart: 0,
compareIndex: i,
status: 'calculating',
pHash,
tHash,
message: `Calculating initial hashes. Index ${i}. Pattern Hash = ${pHash}, Window Hash = ${tHash}.`
});
}
for (let i = 0; i <= n - m; i++) {
steps.push({
windowStart: i,
compareIndex: -1,
status: 'sliding',
pHash,
tHash,
message: `Sliding window to index ${i}. Pattern Hash = ${pHash}, Window Hash = ${tHash}.`
});
if (pHash === tHash) {
steps.push({
windowStart: i,
compareIndex: 0,
status: 'hash_match',
pHash,
tHash,
message: `Hash Match Found! Verifying character by character...`
});
let j = 0;
for (j = 0; j < m; j++) {
steps.push({
windowStart: i,
compareIndex: i + j,
patternCompareIndex: j,
status: 'verifying',
pHash,
tHash,
message: `Verifying: text[${i + j}] ('${text[i + j]}') === pattern[${j}] ('${pattern[j]}')?`
});
if (text[i + j] !== pattern[j]) {
steps.push({
windowStart: i,
compareIndex: i + j,
patternCompareIndex: j,
status: 'spurious_hit',
pHash,
tHash,
message: `Spurious Hit! Mismatch found. Resuming search.`
});
break;
}
}
if (j === m) {
found = true;
steps.push({
windowStart: i,
compareIndex: -1,
status: 'found',
pHash,
tHash,
message: `Pattern Found at index ${i}!`
});
}
}
if (i < n - m) {
const oldHash = tHash;
const charToRemove = text.charCodeAt(i);
const charToAdd = text.charCodeAt(i + m);
tHash = (d * (tHash - charToRemove * h) + charToAdd) % q;
if (tHash < 0) {
tHash += q;
}
steps.push({
windowStart: i,
compareIndex: -1,
status: 'rolling',
removeIndex: i,
addIndex: i + m,
pHash,
tHash: oldHash,
message: `Rolling hash... Removing '${text[i]}' (val ${charToRemove}).`
});
steps.push({
windowStart: i + 1,
compareIndex: -1,
status: 'rolling',
removeIndex: i,
addIndex: i + m,
pHash,
tHash,
message: `Rolling hash... Adding '${text[i + m]}' (val ${charToAdd}). New Window Hash = ${tHash}.`
});
}
}
steps.push({
windowStart: -1,
compareIndex: -1,
status: found ? 'finished_found' : 'finished_not_found',
pHash,
tHash,
message: found ? "Search complete. Pattern was found." : "Search complete. Pattern was not found."
});
return { steps, found };
}