Skip to content
Merged
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
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"string": {
"name": "String",
"list": {
"edit_distance": "Edit Distance"
"edit_distance": "Edit Distance",
"knuth_morris_pratt": "KMP Substring Search"
}
},
"dp": {
Expand Down
4 changes: 2 additions & 2 deletions algorithm/graph_search/bridges/desc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Bridges": "An edge in an undirected connected graph is a bridge iff removing it disconnects the graph. A naive solution to finding bridges in a graph is to:<br />1.Delete an edge E<br />2.Perform DFS Exploration to check is Graph is connected<br />3.Restore Edge E. E is a bridge only if DFS explore determines that the graph is disconnected without E",
"Bridges": "An edge in an undirected connected graph is a bridge iff removing it disconnects the graph. A naive solution to finding bridges in a graph is to:<br />1.Delete an edge E<br />2.Perform DFS Exploration to check if the Graph is still connected<br />3.Restore Edge E. E is a bridge only if DFS exploration determines that the graph is disconnected without E",
"Applications": [
"Find vulnerabilities in Graphs and Electrical Circuits"
"Finding vulnerabilities in Graphs and Electrical Circuits"
],
"Complexity": {
"time": "worst O(|E|.(|V|+|E|))",
Expand Down
16 changes: 16 additions & 0 deletions algorithm/string/knuth_morris_pratt/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Knuth-Morris-Pratt": "searches for occurrences of a substring W with length K within a main string S with Length N by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin, thus bypassing re-examination of previously matched characters",
"Applications": [
"Substring Search"
],
"Complexity": {
"time": "worst O(|N|+|K|)",
"space": "worst O(|K|)"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm'>Wikipedia</a>"
],
"files": {
"substring_search": "Efficiently find is A is a substring of B (and A's position(s))"
}
}
113 changes: 113 additions & 0 deletions algorithm/string/knuth_morris_pratt/substring_search/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//Fix JS Negative number modulo Bug
Number.prototype.mod = function (n) {
return ((this%n)+n)%n;
};

function randString (length) {
var result = Math.random ().toString (36);
return result.substring (result.length - length);
}

function tracker (substring) {
var i = 1, j = 0;

logger._print ('Initializing i to 1, j to 0.');
substrTracer._select (j);
while (i < track.length) {
substrTracer._select (i)._wait ();

while ( (substring [i] !== substring [j]) && (j > 0) ) {
logger._print ('j = ' + track [j-1]);
trackTracer._select (j-1)._wait ();
trackTracer._deselect (j-1)._wait ();

substrTracer._deselect (j);
j = track [j-1];
logger._print ('j = ' + j);
substrTracer._select (j);
}

if (substring [i] === substring [j]) {
substrTracer._deselect (j);
track [i] = ++j;
trackTracer._notify (i, track [i])._wait ();
trackTracer._denotify (i)._wait ();
logger._print ('substring [ ' + i + ' ] (' + substring [i] + ') equals substring [ ' + j + ' ] (' + substring [j] + '), track [ ' + i + ' ] updated to: ' + track [i]);

logger._print ('j = ' + j);
substrTracer._select (j);
}
else {
track [i] = 0;
logger._print ('substring [ ' + i + ' ] (' + substring [i] + ') is not equal to substring [ ' + j + ' ] (' + substring [j] + '), setting track [' + i + '] to 0');
trackTracer._select (i)._wait ();
trackTracer._deselect (i)._wait ();
}

substrTracer._deselect (i)._wait ();
i++;
logger._print ('i = ' + i);
}

return track;
}

function kmp (string, substr) {
var positions = [], j = 0, startPos;

logger._print ('Constructing Tracker for substring <b>' + substr + '</b>');
track = tracker (substr);
logger._print ('Tracker for substring constructed: [ ' + String (track) + ' ]');
logger._print ('--------------------------------------------------------------------------');
logger._print ('Running KMP...');

logger._print ('Initializing i = 0, j = 0');
for (var i = 0; i < string.length; i++) {
logger._print ('comparing string [' + i + '] (' + string [i] + ') and substring [' + j + '] (' + substr [j] + ')...');
stringTracer._select (i)._wait ();
stringTracer._select (j)._wait ();

if (string [i] === substr [j]) {
logger._print ('they\'re equal!');

if (j === substr.length-1) {
logger._print ('j (' + j + ') equals length of substring - 1 (' + substr.length + '-1), we\'ve found a new match in the string!');
startPos = i - substr.length + 1;
positions.push (startPos);

logger._print ('Adding start position of the substring (' + startPos + ') to the results.');
stringTracer._select (startPos)._wait ();
}
else {
stringTracer._deselect (j)._wait ();
logger._print ('But j (' + j + ') does not equal length of substring (' + substr.length + ') Incrementing j and moving forward.');
j++;
logger._print ('j = ' + j);
stringTracer._select (j)._wait ();
}
}
else {
var tempJ = j - 1;
logger._print ('they\'re NOT equal');
trackTracer._select (tempJ)._wait ();
stringTracer._deselect (j)._wait ();

j = track [(j-1).mod (substr.length)]; //use modulo to wrap around, i.e., if index = -1, access the LAST element of array (PYTHON-LIKE)

logger._print ('Setting j to ' + j);
stringTracer._select (j)._wait ();
trackTracer._deselect (tempJ)._wait ();
}

stringTracer._deselect (i)._wait ();
}

return positions;
}

var positions = kmp (string, substring);

logger._print ('Substring positions are: ' + (positions.length ? String (positions) : 'NONE'));
for (var i = 0; i < positions.length; i++) {
stringTracer._select (positions [i], positions [i] + substring.length - 1)._wait ();
}
20 changes: 20 additions & 0 deletions algorithm/string/knuth_morris_pratt/substring_search/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function randString (length) {
var result = Math.random ().toString (36);
return result.substring (result.length - length);
}

var string = randString (15), substring = randString (5);
//var string = 'abcxabcdabxabcdabcdabxabcda', substring = 'xabcda';
//var string = 'abcxabcdabxabcdabcdabcyiuhsiuhduiahdubhbuuabcdabcysbhbh', substring = 'abcdabcy';

var track = Array.apply (null, Array (substring.length)).map (Number.prototype.valueOf, 0);

var logger = new LogTracer ();
var trackTracer = new Array1DTracer (),
substrTracer = new Array1DTracer (),
stringTracer = new Array1DTracer ();


trackTracer._setData (track);
substrTracer._setData (substring);
stringTracer._setData (string);