lcs

Analyzing time complexity of a function written in C

家住魔仙堡 提交于 2019-12-23 17:16:08
问题 I was implementing Longest Common Subsequence problem in C. I wish to compare the time taken for execution of recursive version of the solution and dynamic programming version. How can I find the time taken for running the LCS function in both versions for various inputs? Also can I use SciPy to plot these values on a graph and infer the time complexity? Thanks in advance, Razor 回答1: For the second part of your question: the short answer is yes, you can. You need to get the two data sets (one

Longest Common Subsequence

大城市里の小女人 提交于 2019-12-18 15:55:18
问题 Consider 2 sequences X[1..m] and Y[1..n]. The memoization algorithm would compute the LCS in time O(m*n). Is there any better algorithm to find out LCS wrt time? I guess memoization done diagonally can give us O(min(m,n)) time complexity. 回答1: Gene Myers in 1986 came up with a very nice algorithm for this, described here: An O(ND) Difference Algorithm and Its Variations. This algorithm takes time proportional to the edit distance between sequences, so it is much faster when the difference is

Convert string to palindrome string with minimum insertions

蓝咒 提交于 2019-12-18 13:09:51
问题 In order to find the minimal number of insertions required to convert a given string(s) to palindrome I find the longest common subsequence of the string(lcs_string) and its reverse. Therefore the number of insertions to be made is length(s) - length(lcs_string) What method should be employed to find the equivalent palindrome string on knowing the number of insertions to be made? For example : 1) azbzczdzez Number of insertions required : 5 Palindrome string : azbzcezdzeczbza Although

longest common subsequence: why is this wrong?

这一生的挚爱 提交于 2019-12-14 03:55:55
问题 int lcs(char * A, char * B) { int m = strlen(A); int n = strlen(B); int *X = malloc(m * sizeof(int)); int *Y = malloc(n * sizeof(int)); int i; int j; for (i = m; i >= 0; i--) { for (j = n; j >= 0; j--) { if (A[i] == '\0' || B[j] == '\0') X[j] = 0; else if (A[i] == B[j]) X[j] = 1 + Y[j+1]; else X[j] = max(Y[j], X[j+1]); } Y = X; } return X[0]; } This works, but valgrind complains loudly about invalid reads. How was I messing up the memory? Sorry, I always fail at C memory allocation. 回答1: The

Is this an acceptable algorithm?

此生再无相见时 提交于 2019-12-12 20:04:40
问题 I've designed an algorithm to find the longest common subsequence. these are steps: Pick the first letter in the first string. Look for it in the second string and if its found, Add that letter to common_subsequence and store its position in index , Otherwise compare the length of common_subsequence with the length of lcs and if its greater, asign its value to lcs . Return to the first string and pick the next letter and repeat the previous step again, But this time start searching from index

How to print all possible solutions for Longest Common subsequence

谁说胖子不能爱 提交于 2019-12-12 14:03:53
问题 I want to print all the possible solutions to LCS problem. The two strings abcbdab and bdcaba should print following 3 strings: bdab,bcba,bcab. C is the global matrix table which takes values according to algorithm and m, n are the length of the sequences a, b. But The output is something unexpected. #include<stdio.h> #include<conio.h> int co=0,m=0,n=0,c[10][10]; char a[10],b[10]; void main() { int i,j; clrscr(); printf("Enter Two strings: "); scanf("%s",a); scanf("%s",b); m=strlen(a); n

All Longest Common Subsequences

天涯浪子 提交于 2019-12-11 08:32:09
问题 [NOTE: I searched beforehand and couldn't find advice on solving the LCS problem for all subsequences.] I'm having trouble coding up a solution to the "longest common subsequence" problem where I return all the longest common subsequences for two input Strings. I looked at the Wikipedia page and tried to implement the pseudo-code on there, but ran into a problem with my "backtrackAll" method. I believe I'm computing the LCS matrix correctly below, but my "backtrackAll" method returns an empty

Longest common subsequence implementation-python

北战南征 提交于 2019-12-11 02:23:21
问题 I have implemented the longest common subsequence problem as instructed in this video. It just execute first set of code and produces an empty list. What is wrong with this implementation? def lcs_recursive(xlist,ylist): if not xlist or ylist: return [] x,xs,y,ys, = xlist[0],xlist[1:],ylist[0],ylist[1:] if x == y: return [x] + lcs_recursive(xs,ys) else: return max(lcs_recursive(xlist,ys),lcs_recursive(xs,ylist),key=len) s1 = 'abc' s2 = 'aeb' print lcs_recursive(s1,s2) 回答1: if not xlist or

Getting the longest common subsequence in ERLANG

百般思念 提交于 2019-12-10 19:13:13
问题 I'm new to this ERLANG, I know the basics. It's like scheme but broader. I know how to create a function but I'm having problems with creating a function that gets the Longest Common Subsequence. lcs(str1,str2) is a function that will accept two string and output an integer: lcs(algorithm,logarithm) will output 7 because the longest common subsequence that can be made is lgrithm which is 7 in size. Any answer is greatly appreciated. 回答1: You have a pretty good implementation of an LCS

All possible LCS(Longest Common Subsequence) of two strings

我的梦境 提交于 2019-12-10 16:05:33
问题 We can find the LCS(Longest Common Subsequence) of two strings with DP(Dynamic Programming). By keeping track with DP Table we can get the LCS. But if there exists more than one LCS how can we get all of them? Example: string1 : bcab string2 : abc Here both "ab" and "bc" are LCS. 回答1: Here is a working java solution. For explanation you can see my answer How to print all possible solutions for Longest Common subsequence static int arr[][]; static void lcs(String s1, String s2) { for (int i =