forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Simplify Rabin-Karp functionality * Created Rabin Fingerprinting module within util directory * Updated Rabin-Karp search to use rolling hash module Incorporate tests from @dubzzz
- Loading branch information
1 parent
f32172e
commit c4605ea
Showing
4 changed files
with
139 additions
and
88 deletions.
There are no files selected for viewing
16 changes: 6 additions & 10 deletions
16
src/algorithms/string/rabin-karp/__test__/rabinKarp.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,20 @@ | ||
import { rabinKarp, hashWord, reHashWord } from '../rabinKarp'; | ||
import rabinKarp from '../rabinKarp'; | ||
|
||
describe('rabinKarp', () => { | ||
it('should correctly calculates hash and re-hash', () => { | ||
expect(hashWord('a')).toBe(97); | ||
expect(hashWord('b')).toBe(98); | ||
expect(hashWord('abc')).toBe(941094); | ||
expect(hashWord('bcd')).toBe(950601); | ||
expect(reHashWord(hashWord('abc'), 'abc', 'bcd')).toBe(950601); | ||
expect(reHashWord(hashWord('abc'), 'abc', 'bcd')).toBe(hashWord('bcd')); | ||
}); | ||
|
||
it('should find substring in a string', () => { | ||
expect(rabinKarp('', '')).toBe(0); | ||
expect(rabinKarp('a', '')).toBe(0); | ||
expect(rabinKarp('a', 'a')).toBe(0); | ||
expect(rabinKarp('ab', 'b')).toBe(1); | ||
expect(rabinKarp('abcbcglx', 'abca')).toBe(-1); | ||
expect(rabinKarp('abcbcglx', 'bcgl')).toBe(3); | ||
expect(rabinKarp('abcxabcdabxabcdabcdabcy', 'abcdabcy')).toBe(15); | ||
expect(rabinKarp('abcxabcdabxabcdabcdabcy', 'abcdabca')).toBe(-1); | ||
expect(rabinKarp('abcxabcdabxaabcdabcabcdabcdabcy', 'abcdabca')).toBe(12); | ||
expect(rabinKarp('abcxabcdabxaabaabaaaabcdabcdabcy', 'aabaabaaa')).toBe(11); | ||
expect(rabinKarp('^ !/\'#\'pp', ' !/\'#\'pp')).toBe(1); | ||
expect(rabinKarp('a\u{ffff}', '\u{ffff}')).toBe(1); | ||
expect(rabinKarp('a\u{10000}', '\u{10000}')).toBe(1); | ||
expect(rabinKarp('\u0000耀\u0000', '耀\u0000')).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Generates fingerprints using Rabin scheme with x = 2 (for potential compiler optimizations). | ||
* Guaranteed not to over or underflow if function assumptions are met. | ||
*/ | ||
export default class RabinFingerprint { | ||
/** | ||
* @param { function() : number } [primeGenerator] | ||
* @assumes Output from any function call is prime less than Number.MAX_SAFE_INTEGER / 2. | ||
*/ | ||
constructor(primeGenerator) { | ||
this.prime = primeGenerator(); | ||
} | ||
|
||
/** | ||
* @param { array[number] } [values] | ||
* @returns {number} - The hash value after digesting input. | ||
* @assumes All array elements are non-negative. | ||
* @note First element in array is considered to be oldest value. | ||
*/ | ||
init(values) { | ||
this.val = 0; | ||
this.len = values.length; | ||
|
||
for (let i = 0; i < values.length; i += 1) { | ||
this.val = (((this.val * 2) % this.prime) + (values[i] % this.prime)) % this.prime; | ||
} | ||
|
||
return this.val; | ||
} | ||
|
||
/* | ||
* @param {number} [oldValue] | ||
* @param {number} [newValue] | ||
* @returns {number} - The hash value after removing the oldest value & inserting the newest. | ||
* @assumes Instance has already been initialized. | ||
* @assumes oldValue is the oldest value still processed by the hash. | ||
* @assumes newValue is non-negative. | ||
*/ | ||
roll(oldValue, newValue) { | ||
let oldVal = oldValue % this.prime; | ||
for (let i = 1; i < this.len; i += 1) { | ||
oldVal = (oldVal * 2) % this.prime; | ||
} | ||
this.val = (this.val + this.prime - (oldVal % this.prime)) % this.prime; | ||
|
||
const newVal = newValue % this.prime; | ||
this.val = (((this.val * 2) % this.prime) + (newVal % this.prime)) % this.prime; | ||
|
||
return this.val; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import RabinFingerprint from '../Rabin_Fingerprint'; | ||
|
||
describe('Rabin fingerprint Hash Family', () => { | ||
it('should hash deterministically', () => { | ||
const primeVals = [3, 5, 19, 53, 97, 401, 7039, 193939]; | ||
for (let primeIdx = 0; primeIdx < primeVals.length; primeIdx += 1) { | ||
const primeVal = primeVals[primeIdx]; | ||
const hasher = new RabinFingerprint(() => primeVal); | ||
|
||
// Test basic values | ||
expect(hasher.init([])).toEqual(0); | ||
expect(hasher.init([1])).toEqual(1); | ||
|
||
// Test overflow | ||
const largeVal = Number.MAX_SAFE_INTEGER; | ||
expect(hasher.init([primeVal])).toEqual(0); | ||
expect(hasher.init([largeVal])).toEqual(largeVal % primeVal); | ||
|
||
const numLargeVal = 2; // 2 ^ numLargeVal fits in javascript number | ||
const largeValues = new Array(numLargeVal).fill(largeVal); | ||
|
||
const expVal = ((largeVal % primeVal) * ((2 ** numLargeVal) - 1)) % primeVal; | ||
expect(hasher.init(largeValues)).toEqual(expVal); | ||
|
||
// Test using Fermat's little theorem | ||
const fermatValues = new Array(primeVal).fill(primeVal); | ||
const numFermatTests = 100; | ||
for (let i = 0; i < numFermatTests; i += 1) { | ||
const randomValue = Math.floor(Math.random() * largeVal); | ||
fermatValues[0] = randomValue; | ||
expect(hasher.init(fermatValues)).toEqual(randomValue % primeVal); | ||
} | ||
} | ||
}); | ||
|
||
it('should roll appropriately', () => { | ||
const primeVals = [3, 5, 19, 53, 97, 401, 7039, 193939]; | ||
|
||
for (let primeIdx = 0; primeIdx < primeVals.length; primeIdx += 1) { | ||
const primeVal = primeVals[primeIdx]; | ||
const hasher = new RabinFingerprint(() => primeVal); | ||
|
||
// Test basic values | ||
const largeVal = Number.MAX_SAFE_INTEGER; | ||
expect(hasher.init([0])).toEqual(0); | ||
expect(hasher.roll(0, 1)).toEqual(1); | ||
expect(hasher.roll(1, primeVal)).toEqual(0); | ||
expect(hasher.roll(primeVal, largeVal)).toEqual(largeVal % primeVal); | ||
|
||
const numRollTest = 100; | ||
let previousValue = largeVal; | ||
for (let i = 0; i < numRollTest; i += 1) { | ||
const randomVal = Math.floor(Math.random() * largeVal); | ||
expect(hasher.roll(previousValue, randomVal)).toEqual(randomVal % primeVal); | ||
previousValue = randomVal; | ||
} | ||
} | ||
}); | ||
}); |