Skip to content

Commit 58473b3

Browse files
committed
Merge pull request algorithm-visualizer#27 from archie94/longestIncreasingSubsequence
Algorithm for Longest Increasing Subsequence
2 parents 550a492 + ccfb9ac commit 58473b3

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

algorithm/etc/dp/desc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"files": {
1111
"fibonacci": "Fibonacci Sequence",
1212
"sliding_window": "Finding the largest sum of three contiguous number",
13-
"max_sum_path": "Finding the maximum sum in a path from (0, 0) to (N-1, M-1) when can only move to right or down"
13+
"max_sum_path": "Finding the maximum sum in a path from (0, 0) to (N-1, M-1) when can only move to right or down",
14+
"longest_increasing_subsequence": "Find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order"
1415
}
1516
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
tracer._pace(500);
2+
3+
// Initialize LIS values for all indexes
4+
for( var i = 0; i < 20; i++) {
5+
LIS[i] = 1;
6+
}
7+
8+
tracer._print( 'Calculating Longest Increasing Subsequence values in bottom up manner ');
9+
// Compute optimized LIS values in bottom up manner
10+
for( var i = 1; i < 10; i++) {
11+
tracer._select(i) ;
12+
tracer._print( ' LIS['+i+'] = ' + LIS[i]);
13+
for( var j =0; j < i; j++) {
14+
tracer._notify(j);
15+
if( A[i] > A[j] && LIS[i] < LIS[j] + 1) {
16+
LIS[i] = LIS[j] + 1;
17+
tracer._print( ' LIS['+i+'] = ' + LIS[i]);
18+
}
19+
}
20+
tracer._deselect(i);
21+
}
22+
23+
// Pick maximum of all LIS values
24+
tracer._print( 'Now calculate maximum of all LIS values ');
25+
var max = LIS[0];
26+
for( var i = 1; i < 10; i++) {
27+
if(max < LIS[i]) {
28+
max = LIS[i];
29+
}
30+
}
31+
tracer._print('Longest Increasing Subsequence = max of all LIS = ' + max);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var tracer = new Array1DTracer();
2+
var A = Array1D.random(10, 0, 10);
3+
var LIS = new Array(10);
4+
tracer._setData(A);

0 commit comments

Comments
 (0)