Yia*_*nis 12 algorithm optimization edit-distance lcs
问题:需要两个字符串之间的LCS长度.字符串的大小最多为100个字符.字母表是通常的DNA,4个字符"ACGT".动态方法不够快.
我的问题是我正在处理很多对(我看到的数百万的等级).我相信我已经将LCS_length函数的调用减少到最小可能,因此使程序运行得更快的唯一方法是使用更高效的LCS_Length函数.
我已经开始实施通常的动态编程方法.这给出了正确答案,希望能够正确实施.
#define arrayLengthMacro(a) strlen(a) + 1
#define MAX_STRING 101
static int MaxLength(int lengthA, int lengthB);
/*
* Then the two strings are compared following a dynamic computing
* LCS table algorithm. Since we only require the length of the LCS
* we can get this rather easily.
*/
int LCS_Length(char *a, char *b)
{
int lengthA = arrayLengthMacro(a),lengthB = arrayLengthMacro(b),
LCS = 0, i, j, maxLength, board[MAX_STRING][MAX_STRING];
maxLength = MaxLength(lengthA, lengthB);
//printf("%d %d\n", lengthA, lengthB);
for (i = 0; i < maxLength - 1; i++)
{
board[i][0] = 0;
board[0][i] = 0;
}
for (i = 1; i < lengthA; i++)
{
for (j = 1; j < lengthB; j++)
{
/* If a match is found we allocate the number in (i-1, j-1) incremented
* by 1 to the (i, j) position
*/
if (a[i - 1] == b[j - 1])
{
board[i][j] = board[i-1][j-1] + 1;
if(LCS < board[i][j])
{
LCS++;
}
}
else
{
if (board[i-1][j] > board[i][j-1])
{
board[i][j] = board[i-1][j];
}
else
{
board[i][j] = board[i][j-1];
}
}
}
}
return LCS;
}
Run Code Online (Sandbox Code Playgroud)
那应该是O(mn)(希望如此).
然后在搜索速度时,我发现了这篇帖子最长的共同子序列, 由迈尔斯 给出了O(ND)论文.我试过这个,它将LCS与最短的编辑脚本(SES)联系起来.它们给出的关系是D = M + N-2L,其中D是SES的长度,M和N是两个串的长度,L是LCS长度.但是在我的实现中并不总是正确的.我给出了我的实现(我认为是正确的):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define arrayLengthMacro(a) strlen(a)
int LCS_Length (char *a, char *b);
int MinLength (int A, int B);
int Max (int A, int B);
int snake(int k, int max, char *a, char *b, int lengthA, int lengthB);
int main(void)
{
int L;
char a[] = "tomato", b[] = "potato"; //must give LCS = 4
L = LCS_Length(a, b);
printf("LCS: %d\n", L );
char c[] = "GTCGTTCGGAATGCCGTTGCTCTGTAAA", d[] = "ACCGGTCGAGTGCGCGGAAGCCGGCCGAA"; // must give LCS = 20
L = LCS_Length(c, d);
printf("LCS: %d\n", L );
char e[] = "human", f[] = "chimpanzee"; // must give LCS = 4
L = LCS_Length(e, f);
printf("LCS: %d\n", L );
char g[] = "howareyou", h[] = "whoareyou"; // LCS =8
L = LCS_Length(g, h);
printf("LCS: %d\n", L );
char i[] = "TTCTTTCGGTAACGCCTACTTTATGAAGAGGTTACATTGCAATCGGGTAAATTAACCAACAAGTAATGGTAGTTCCTAGTAGAGAAACCCTCCCGCTCAC",
j[] = "GCACGCGCCTGTTGCTACGCTCTGTCCATCCTTTGTGTGCCGGGTACTCAGACCGGTAACTCGAGTTGCTATCGCGGTTATCAGGATCATTTACGGACTC"; // 61
L = LCS_Length(i, j);
printf("LCS: %d\n", L );
return 0;
}
int LCS_Length(char *a, char *b)
{
int D, lengthA = arrayLengthMacro(a), lengthB = arrayLengthMacro(b),
max, *V_, *V, i, k, x, y;
max = lengthA + lengthB;
V_ = malloc(sizeof(int) * (max+1));
if(V_ == NULL)
{
fprintf(stderr, "Failed to allocate memory for LCS");
exit(1);
}
V = V_ + lengthA;
V[1] = 0;
for (D = 0; D < max; D++)
{
for (k = -D; k <= D; k = k + 2)
{
if ((k == -D && V[k-1] < V[k+1]) || (k != D && V[k-1] < V[k+1]))
{
x = V[k+1];
}
else
{
x = V[k-1] + 1;
}
y = x - k;
while ((x < lengthB) && (y < lengthA) && (a[x+1] == b[y+1]))
{
x++;
y++;
}
V[k] = x;
if ((x >= lengthB) && (y >= lengthA))
{
return (lengthA + lengthB - D)/2;
}
}
}
return (lengthA + lengthB - D)/2;
}
Run Code Online (Sandbox Code Playgroud)
主要有一些例子.例如."番茄"和"马铃薯"(不评论),LCS长度为4.实现发现SES(代码中的D)是2个而不是4个,我预期(删除"t",插入"p",删除"m",插入"t").我倾向于认为O(ND)算法也可能计算替换,但我不确定这一点.
任何可实现的方法(我没有巨大的编程技能)都是受欢迎的.(如果有人知道如何利用小字母表).
编辑:我认为最重要的是,在任何时候我都会在任意两个字符串之间使用LCS函数.所以它不仅仅是字符串s1,而是其他几百万字符串.s1000可能是s200,s100是s000,s100000则是250.也不太可能跟踪大多数使用过的字符串.我要求LCS长度不是近似值,因为我实现了近似算法,我不想添加额外的错误.
编辑:刚刚跑了callgrind.对于10000个字符串的输入,对于不同的字符串对,我似乎将lcs函数称为50,000,000次.(10000个字符串是我想要运行的最低值,最大值是100万(如果可行的话)).
小智 1
有几种方法可以加快计算速度:
编辑:对于比较相同的一组刺的情况,一个人建议使用 BK-Tree 数据结构, 当样本量很大时计算字符串相似度分数的有效方法?
| 归档时间: |
|
| 查看次数: |
4658 次 |
| 最近记录: |