Example #1
0
///
// Version 3: Dynamic programming version
///
int edit_distance3(char *s1, char *s2) {
    int i, j;
    int opt[3];

    for (i = 0; i < MAX; ++i) {
        row_init(i);
        column_init(i);
    }

    for (i = 1; i < strlen(s1); ++i) {
        for (j = 1; j < strlen(s2); ++j) {
            opt[MATCH] = m[i - 1][j - 1].cost + match(s1[i], s2[j]);
            opt[INSERT] = m[i][j - 1].cost + del(s2[j]);
            opt[DELETE] = m[i - 1][j].cost + ins(s1[i]);

            m[i][j].cost = opt[MATCH];
            m[i][j].parent = MATCH;
            for(int k = INSERT; k <= DELETE; ++k) {
                if (opt[k] < m[i][j].cost) {
                    m[i][j].cost = opt[k];
                    m[i][j].parent = k;
                }
            }
        }
    }

    goal_cell(s1, s2, &i, &j);
    return m[i][j].cost;
}
Example #2
0
int string_compare(char *s, char *t)
{
    uint i, j, k;
    int opt[3];

    for (i = 0; i < MAXLEN; i++) {
        row_init(i);
        column_init(i);
    }
    //print_matrix(s, t, TRUE);

    for (i = 1; i < strlen(s); i++) {
        for (j = 1; j < strlen(t); j++) {
            opt[MATCH]  = m[i-1][j-1].cost + match(s[i], t[j]);
            opt[INSERT] = m[i][j-1].cost + indel(t[j]);
            opt[DELETE] = m[i-1][j].cost + indel(s[i]);

            m[i][j].cost = opt[MATCH];
            m[i][j].parent = MATCH;
            for (k = INSERT; k <= DELETE; k++) {
                if (opt[k] < m[i][j].cost) {
                    m[i][j].cost = opt[k];
                    m[i][j].parent = k;
                }
            }
        }
    }

    goal_cell(s, t, &i, &j);
    
    return m[i][j].cost;
} 
Example #3
0
int string_compare(char *s, char *t)
{
	int i,j,k;		/* counters */
	int opt[3];		/* cost of the three options */

	for (i=0; i<MAXLEN; i++) {
		row_init(i);
		column_init(i);
	}

	for (i=1; i<strlen(s); i++)
		for (j=1; j<strlen(t); j++) {
			opt[MATCH] = m[i-1][j-1].cost + match(s[i],t[j]);
			opt[INSERT] = m[i][j-1].cost + indel(t[j]);
			opt[DELETE] = m[i-1][j].cost + indel(s[i]);

			m[i][j].cost = opt[MATCH];
			m[i][j].parent = MATCH;
			for (k=INSERT; k<=DELETE; k++)
				if (opt[k] < m[i][j].cost) {
					m[i][j].cost = opt[k];
					m[i][j].parent = k;
				}
		}

	goal_cell(s,t,&i,&j);
	return( m[i][j].cost );
} 
Example #4
0
int main(void) {
	int i, j;
	char s[MAXLEN], t[MAXLEN];		/* input strings */

	s[0] = t[0] = ' ';

	scanf("%s", &(s[1])); scanf("%s", &(t[1]));
	printf("matching cost = %d \n", string_compare(s, t));
	print_matrix(s, t, TRUE);	printf("\n");	print_matrix(s, t, FALSE);

	goal_cell(s, t, &i, &j); printf("%d %d\n", i, j);
	reconstruct_path(s,t, i ,j); printf("\n");
	
	return 0;
}
Example #5
0
// return the edit distance for changing s into t by means of matching, swapping,
// changing, inserting, or deleting chars.
int string_compare(char* s, char* t)
{
  int i,j,k;
  int opt[5];

  for (i = 0; i < MAXLEN; i++)
  {
    row_init(i);
    column_init(i);
  }

  for (i = 1; i < strlen(s); i++)
  {
    for (j = 1; j < strlen(t); j++)
    {
      opt[MATCH]  = m[i-1][j-1].cost + match(s[i],t[j]);
      opt[SWAP]   = m[i-1][j-1].cost + swap(s, t, i);
      opt[CHANGE] = m[i-1][j-1].cost + 1;
      opt[INSERT] = m[i][j-1].cost + 1;
      opt[DELETE] = m[i-1][j].cost + 1;

      m[i][j].cost = opt[MATCH];
      m[i][j].parent = MATCH;
      for (k = SWAP; k <= DELETE; k++)
      {
        if (opt[k] < m[i][j].cost)
        {
          // if the last in-place action was a swap, and this action is a
          // change, then we are actually continuing the swap. Swap changes
          // two characters for a cost of one, so we change the first char for
          // 1, and change the second char for 0.
          if (k == CHANGE && m[i-1][j-1].parent == SWAP)
            m[i][j].cost = m[i-1][j-1].cost;
          // otherwise it's the least cost action, as identified
          else
            m[i][j].cost = opt[k];
          
          m[i][j].parent = k;
        }
      }
    }
  }

  goal_cell(s,t,&i,&j);
  return(m[i][j].cost);
}
Example #6
0
main() {
    int i,j;
    char s[MAXLEN],t[MAXLEN];		/* input strings */

    s[0] = t[0] = ' ';

    printf("enter p :\n");
    scanf("%s",&(s[1]));
    printf("enter t :\n");
    scanf("%s",&(t[1]));

    printf("matching cost = %d \n", string_compare(s,t, strlen(s)-1,strlen(t)-1));

    print_matrix(s,t,TRUE);
    printf("\n");
    print_matrix(s,t,FALSE);

    goal_cell(s,t,&i,&j);

    printf("%d %d\n",i,j);

    reconstruct_path(s,t,i,j);
    printf("\n");
}