1

I'm writing a code that calculates the edit distance of two given strings t and s with m = strlen(t) and n = strlen(s) and the code should only use memory in O(m). Furthermore, it should not need longer than 4 seconds for the calculation of two strings with about 50 characters. My written code satisfies the latter, but I'm not sure about the first, so I'd be happy if you could check if it uses not more than O(m) memory. If it doesn't, it would also be useful to hear some tips how to do so. Thanks.

#include <stdio.h>

/************************************/
/* TODO: Insert possible help       */
/*       functions in here          */
/************************************/

int min (int a[], int l) {
    int i, min = a[0];
    for (i = 1; i < l; i++) {
        if (a[i] < min) {
            min = a[i];
        }
    }
    return min;
}

int main() {
    /* n, m, s, t are configurable in the source, i.e. the values here are 
            only an example and shall be changed to any value.*/
    const int n = 58; /* length of s, > 0 */
    const int m = 54; /* length of t, > 0 */
    char *s = "Calculateanddisplaythenumberofcharacterswithinarewrwegurie";
    char *t = "Simplycopyeverythinginsideandpasteitwhereogwrgheigrber";

/* Save the edit distance in res */
    int res;

int matrix[n+1][m+1];

int i, j;

for (i = 0; i <= m; i++) {
        matrix[n][i] = m-i;
    }
    for (i = 0; i <= n; i++) {
        matrix[i][m] = n-i;
    }

for (i = n-1; i >= 0; i--) {
        for (j = m-1; j >= 0; j--) {
        int cost = (s[i] != t[j]);
        int b[3];
        b[0] = 1+matrix[i][j+1];
        b[1] = 1+matrix[i+1][j];
        b[2] = matrix[i+1][j+1]+cost;
        matrix[i][j] = min(b, 3);
        }
    }

res = matrix[0][0];

/* Output */
    printf("The edit distance is %d.\n\n", res);
    return 0;
}
  • 2
    You have an `m x n` matrix, so it has memory `O(m*n)` You might want to try using recursion. – tvanfosson Dec 08 '12 at 21:02
  • 3
    You dont need the full matrix to compute the distance (cost). Review your algorithm and think what is the minimun you need to compute each iteration of the loop. You can do this in O(min(n,m)) space. – aromero Dec 08 '12 at 21:15
  • 2
    Also, it would grandly ease our life if you provided English language comments. –  Dec 08 '12 at 21:19

1 Answers1

0

You only need to look 1 row back in the matrix, so the third row can go in the same place as the first row, the fourth row can go in the same place as the second row, etc. You only need two rows in the matrix. so:

int matrix[n+1][m+1];

becomes

int matrix[2][m + 1];
Jeremy List
  • 1,756
  • 9
  • 16