Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Longest_Common_Subsequence and Rabin_karp #119

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/algorithms/string/Longest_Common_Subsequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

const Longest_Common_Subsequence = (arr1, arr2) => {

let LCS_def = Array.from({ length: arr1.length + 1 },
() => Array.from({ length: arr2.length + 1 },
() => 0));

for (let i = 0; i < arr1.length + 1; i++){
for (let j = 0; j < arr2.length+1; j++){

if (i == 0 || j == 0) {
LCS_def[i][j] = 0;
}

// when the last character of both match, increase length of lcs by 1
else if (arr1[i - 1] == arr2[j - 1]) {
LCS_def[i][j] = LCS_def[i - 1][j - 1] + 1;
}

/*
when the last character is not same, take maximum obtained by adding
one character to one of the subsequences.
*/
else {
LCS_def[i][j] = Math.max(LCS_def[i - 1][j], LCS_def[i][j - 1])
}
}
}

return `The length of longest subsequence is: ${LCS_def[arr1.length][arr2.length]}`;

}

module.exports = Longest_Common_Subsequence;

// I/P and O/P Examples

const inputSet1 = [10, 15, 20, 25, 30, 35, 40];
const inputSet2 = [10, 12, 23, 25, 28, 30, 32, 40];
const result = Longest_Common_Subsequence(inputSet1, inputSet2);

// The length of longest subsequence is: 4
console.log(result);
43 changes: 43 additions & 0 deletions src/algorithms/string/Rabin_karp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const rabinKarp = (pat, str) => {
let p = 31
let m = 1000000007
let S = pat.length
let T = str.length

let p_pow = [] // precomputing powers of p mod m
p_pow.push(1) // p^0 = 1
for (let i = 1; i < Math.max(S, T); i++) {
p_pow.push((p_pow[i - 1] * p) % m);
}

let h = [] // length-wise hash i.e h[i] = hash of the prefix with i characters
h.push(0); // h[0] = 0
for (let i = 0; i < T; i++) {
h.push((h[i] + (str[i].charCodeAt(0) - 97 + 1) * p_pow[i]) % m);
}

let h_s = 0; // hash value of the pattern
for (let i = 0; i < S; i++)
h_s = (h_s + (pat[i].charCodeAt(0) - 97 + 1) * p_pow[i]) % m;

let occurs = []; // desired array storing the indices of given string where pattern occurs
for (let i = 0; i + S - 1 < T; i++) {
// slide a window of length S of the pattern
let cur_h = (h[i + S] - h[i] + m) % m;
// if current_hash matches with the hash of out pattern, then a match is found at this index
if (cur_h == h_s * p_pow[i] % m)
occurs.push(i);
}

return occurs;
}

// Calling the function
let str = "girlscriptsummerofcodewithgirlsandboys"
let pat = "girl"

let ids = rabinKarp(pat, str) // desired array storing the indices of given string where pattern occurs
console.log("Pattern found at indexes: ", ids)
module.export = rabinKarp;
// Output
// Pattern found at indexes: [ 0, 26 ]
6 changes: 5 additions & 1 deletion src/algorithms/string/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const levenshteindistance = require('./levenshtein_distance');
const Longest_Common_Subsequence = require('./Longest_Common_Subsequence');
const rabinKarp = require('./Rabin_karp');

module.exports = {
levenshteindistance
levenshteindistance,
Longest_Common_Subsequence,
rabinKarp,
};