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
1 change: 1 addition & 0 deletions algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"integer_partition": "Integer Partition",
"knapsack_problem": "Knapsack Problem",
"longest_increasing_subsequence": "Longest Increasing Subsequence",
"longest_common_subsequence": "Longest Common Subsequence",
"max_subarray": "Maximum Subarray",
"max_sum_path": "Maximum Sum Path",
"pascal_triangle": "Pascal's Triangle",
Expand Down
36 changes: 36 additions & 0 deletions algorithm/dp/longest_common_subsequence/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var i,j;

// Build the memo table in bottom up fashion
for( i = 0; i <= m; i++ ) {
for( j = 0; j <= n; j++ ) {
if ( i === 0 || j === 0 ) {
A[i][j] = 0;
} else if ( string1[i-1] == string2[j-1] ) {
tracer1._select ( i-1 )._wait ();
tracer2._select ( j-1 )._wait ();
tracer3._select ( i-1, j-1 )._wait ();

A[i][j] = A[i-1][j-1] + 1;

tracer1._deselect ( i-1 );
tracer2._deselect ( j-1 );
tracer3._deselect ( i-1, j-1 );
} else {
tracer3._select ( i-1, j )._wait ();
tracer3._select ( i, j-1 )._wait ();

if( A[i-1][j] > A[i][j-1] ) {
A[i][j] = A[i-1][j];
} else {
A[i][j] = A[i][j-1];
}

tracer3._deselect ( i-1, j );
tracer3._deselect ( i, j-1 );
}
tracer3._notify ( i, j , A[i][j] )._wait ();
tracer3._denotify( i, j );
}
}

logger._print ( 'Longest Common Subsequence is ' + A[m][n] );
13 changes: 13 additions & 0 deletions algorithm/dp/longest_common_subsequence/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var string1 = 'AGGTAB';
var string2 = 'GXTXAYB';
var m = string1.length;
var n = string2.length;
var A = new Array (m+1);
for (var i = 0; i < m+1; i++ ) {
A[i] = new Array (n+1);
}

var tracer1 = new Array1DTracer ( 'String 1')._setData ( string1 );
var tracer2 = new Array1DTracer ( 'String 2')._setData ( string2 );
var tracer3 = new Array2DTracer ( 'Memo Table')._setData ( A );
var logger = new LogTracer ();
13 changes: 13 additions & 0 deletions algorithm/dp/longest_common_subsequence/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Longest Common Subsequence": "The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences)." ,
"Complexity": {
"time": "O(mn)",
"space": "O(mn)"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Longest_common_subsequence_problem'>Wikipedia</a>"
],
"files": {
"basic": "Longest common subsequence"
}
}