GeezerVison is showing github.com — Merge pull request #191 from archie94/longestCommonSubsequence · tangjun1990/AlgorithmVisualizer@c4d6f69
File treeExpand file treeCollapse file tree Open diff view settings dp/shortest_common_supersequence
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: algorithm/category.json +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 | "fibonacci": "Fibonacci Sequence", | 19 | 19 | "integer_partition": "Integer Partition", | 20 | 20 | "knapsack_problem": "Knapsack Problem", | 21 | | - "longest_increasing_subsequence": "Longest Increasing Subsequence", | 22 | 21 | "longest_common_subsequence": "Longest Common Subsequence", | | 22 | + "longest_increasing_subsequence": "Longest Increasing Subsequence", | 23 | 23 | "max_subarray": "Maximum Subarray", | 24 | 24 | "max_sum_path": "Maximum Sum Path", | 25 | 25 | "pascal_triangle": "Pascal's Triangle", | | 26 | + "shortest_common_supersequence": "Shortest Common Supersequence", | 26 | 27 | "sliding_window": "Sliding Window", | 27 | 28 | "ugly_numbers": "Ugly Numbers" | 28 | 29 | }, | |
Collapse file Copy file name to clipboard +38Lines changed: 38 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +var i,j; | | 2 | + | | 3 | +// Fill memo table in bottom up manner | | 4 | +for ( i = 0; i <= m; i++ ) { | | 5 | + for ( j = 0; j <= n; j++ ) { | | 6 | + if( i === 0 ) { | | 7 | + A[i][j] = j; | | 8 | + } else if ( j === 0 ) { | | 9 | + A[i][j] = i; | | 10 | + } else if ( string1[i-1] == string2[j-1] ) { | | 11 | + tracer1._select ( i-1 )._wait (); | | 12 | + tracer2._select ( j-1 )._wait (); | | 13 | + tracer3._select ( i-1, j-1 )._wait (); | | 14 | + | | 15 | + A[i][j] = A[i-1][j-1] + 1; | | 16 | + | | 17 | + tracer1._deselect ( i-1 ); | | 18 | + tracer2._deselect ( j-1 ); | | 19 | + tracer3._deselect ( i-1, j-1 ); | | 20 | + } else { | | 21 | + tracer3._select ( i-1, j )._wait (); | | 22 | + tracer3._select ( i, j-1 )._wait (); | | 23 | + | | 24 | + if ( A[i-1][j] < A[i][j-1] ) { | | 25 | + A[i][j] = 1 + A[i-1][j]; | | 26 | + } else { | | 27 | + A[i][j] = 1 + A[i][j-1]; | | 28 | + } | | 29 | + | | 30 | + tracer3._deselect ( i-1, j ); | | 31 | + tracer3._deselect ( i, j-1 ); | | 32 | + } | | 33 | + tracer3._notify ( i, j , A[i][j] )._wait (); | | 34 | + tracer3._denotify( i, j ); | | 35 | + } | | 36 | +} | | 37 | + | | 38 | + logger._print ( 'Shortest Common Supersequence is ' + A[m][n] ); |
Collapse file Copy file name to clipboard +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +var string1 = 'AGGTAB'; | | 2 | +var string2 = 'GXTXAYB'; | | 3 | +var m = string1.length; | | 4 | +var n = string2.length; | | 5 | +var A = new Array (m+1); | | 6 | +for (var i = 0; i < m+1; i++ ) { | | 7 | + A[i] = new Array (n+1); | | 8 | +} | | 9 | + | | 10 | +var tracer1 = new Array1DTracer ( 'String 1')._setData ( string1 ); | | 11 | +var tracer2 = new Array1DTracer ( 'String 2')._setData ( string2 ); | | 12 | +var tracer3 = new Array2DTracer ( 'Memo Table')._setData ( A ); | | 13 | +var logger = new LogTracer (); |
Collapse file Copy file name to clipboard +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "Shortest Common Supersequence": "n computer science, the shortest common supersequence problem is a problem closely related to the longest common subsequence problem. Given two sequences X = < x1,...,xm > and Y = < y1,...,yn >, a sequence U = < u1,...,uk > is a common supersequence of X and Y if U is a supersequence of both X and Y. In other words, a shortest common supersequence of strings x and y is a shortest string z such that both x and y are subsequences of z." , | | 3 | + "Complexity": { | | 4 | + "time": "O(mn)", | | 5 | + "space": "O(mn)" | | 6 | + }, | | 7 | + "References": [ | | 8 | + "<a href='https://en.wikipedia.org/wiki/Shortest_common_supersequence_problem'>Wikipedia</a>" | | 9 | + ], | | 10 | + "files": { | | 11 | + "basic": "Shortest common supersequence" | | 12 | + } | | 13 | +} |
|