Skip to content

Longest Palindromic Subsequence #200

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

Merged
merged 2 commits into from
Jun 29, 2016
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
5 changes: 3 additions & 2 deletions algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"knapsack_problem": "Knapsack Problem",
"longest_common_subsequence": "Longest Common Subsequence",
"longest_increasing_subsequence": "Longest Increasing Subsequence",
"longest_palindromic_subsequence": "Longest Palindromic Subsequence",
"max_subarray": "Maximum Subarray",
"max_sum_path": "Maximum Sum Path",
"pascal_triangle": "Pascal's Triangle",
Expand Down Expand Up @@ -57,8 +58,8 @@
},
"number_theory": {
"list": {
"euclidean_algorithm": "Euclidean Algorithm",
"sieve_of_eratosthenes": "Sieve of Eratosthenes"
"euclidean_algorithm": "Euclidean Algorithm",
"sieve_of_eratosthenes": "Sieve of Eratosthenes"
},
"name": "Number Theory"
},
Expand Down
60 changes: 60 additions & 0 deletions algorithm/dp/longest_palindromic_subsequence/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
function max(a,b) {
if(a>b){
return a;
} else {
return b;
}
}
logger._print("LPS for any string with length = 1 is 1");
for(i=2;i<=N;i++) {
logger._print("--------------------------------------------------");
logger._print("Considering a sub-string of length "+i);
logger._print("--------------------------------------------------");
for(j=0;j<N-i+1;j++) {
var k = j+i-1;
tracer._select(j)._wait();
tracer._notify(k)._wait();

logger._print("Comparing "+seq[j] + " and "+seq[k]);

if(seq[j]==seq[k] && i==2) {
logger._print("They are equal and size of the string in the interval"+j+" to "+k+" is 2, so the Longest Palindromic Subsequence in the Given range is 2");

matrix._notify(j,k)._wait();

L[j][k]=2;
matrix._setData(L);

matrix._denotify(j,k)._wait();

} else if(seq[j]==seq[k]) {
logger._print("They are equal, so the Longest Palindromic Subsequence in the Given range is 2 + the Longest Increasing Subsequence between the indices "+(j+1)+" to "+(k-1));

matrix._notify(j,k)._wait();
matrix._select(j+1,k-1)._wait();

L[j][k] = L[j+1][k-1] + 2;
matrix._setData(L);

matrix._denotify(j,k)._wait();
matrix._deselect(j+1,k-1)._wait();

} else {
logger._print("They are NOT equal, so the Longest Palindromic Subsequence in the Given range is the maximum Longest Increasing Subsequence between the indices "+(j+1)+" to "+(k) + " and "+(j)+" to "+(k-1));
matrix._notify(j,k)._wait();
matrix._select(j+1,k)._wait();
matrix._select(j,k-1)._wait();

L[j][k] = max(L[j+1][k],L[j][k-1]);
matrix._setData(L);

matrix._denotify(j,k)._wait();
matrix._deselect(j+1,k)._wait();
matrix._deselect(j,k-1)._wait();
}
logger._print("--------------------------------------------------");
tracer._deselect(j)._wait();
tracer._denotify(k)._wait();
}
}
logger._print("Longest Increasing Subsequence of the given string = L[0]["+(N-1)+"]="+L[0][N-1]);
23 changes: 23 additions & 0 deletions algorithm/dp/longest_palindromic_subsequence/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

var tracer = new Array1DTracer('Input Text');
var matrix = new Array2DTracer('Matrix');
var logger = new LogTracer();


var seq = "BBABCBCAB";
var N;
N = seq.length;


var L = new Array(N);

var i,j;
for(i=0;i<N;i++) {
L[i]= new Array(N);
}
for(i=0;i<N;i++) {
L[i][i]=1;
}

tracer._setData(seq);
matrix._setData(L);
13 changes: 13 additions & 0 deletions algorithm/dp/longest_palindromic_subsequence/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Longest Palindromic Subsequence": "Find the length of the longest palindromic subsequence in a given sequence",
"Complexity": {
"time": "O(n<sup>2</sup>)",
"space": "O(n<sup>2</sup>)"
},
"References": [
"<a href='http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/'>GeeksForGeeks</a>"
],
"files": {
"basic": "Longest Palindromic Subsequence"
}
}
1 change: 1 addition & 0 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

'use strict';

import path from "path";
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy