2 * @brief Edit distance calculation algorithm.
4 * Based on that described in:
6 * "An extension of Ukkonen's enhanced dynamic programming ASM algorithm"
7 * by Hal Berghel, University of Arkansas
8 * and David Roach, Acxiom Corporation
10 * http://berghel.net/publications/asm/asm.php
12 /* Copyright (C) 2003 Richard Boulton
13 * Copyright (C) 2007,2008,2009 Olly Betts
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "editdistance.h"
43 edist_seq(const CHR
* ptr_
, int len_
) : ptr(ptr_
), len(len_
) { }
50 /// Don't allow assignment.
51 void operator=(const edist_state
&);
53 /// Don't allow copying.
54 edist_state(const edist_state
&);
59 /* Array of f(k,p) values, where f(k,p) = the largest index i such that
60 * d(i,j) = p and d(i,j) is on diagonal k.
61 * ie: f(k,p) = largest i s.t. d(i,k+i) = p
62 * Where: d(i,j) = edit distance between substrings of length i and j.
67 /* Maximum possible edit distance (this is referred to as ZERO_K in
68 * the algorithm description by Berghel and Roach). */
71 int calc_index(int k
, int p
) const {
72 return (k
+ maxdist
) * fkp_cols
+ p
+ 1;
77 edist_state(const CHR
* ptr1
, int len1
, const CHR
* ptr2
, int len2
);
81 int get_f_kp(int k
, int p
) const {
82 return fkp
[calc_index(k
, p
)];
85 void set_f_kp(int k
, int p
, int val
) {
86 fkp
[calc_index(k
, p
)] = val
;
89 bool is_transposed(int pos1
, int pos2
) const {
90 if (pos1
<= 0 || pos2
<= 0 || pos1
>= seq1
.len
|| pos2
>= seq2
.len
) return false;
91 return (seq1
.ptr
[pos1
- 1] == seq2
.ptr
[pos2
] &&
92 seq1
.ptr
[pos1
] == seq2
.ptr
[pos2
- 1]);
95 void edist_calc_f_kp(int k
, int p
);
99 void edist_state
<CHR
>::edist_calc_f_kp(int k
, int p
)
101 int maxlen
= get_f_kp(k
, p
- 1) + 1; /* dist if do substitute */
102 int maxlen2
= get_f_kp(k
- 1, p
- 1); /* dist if do insert */
103 int maxlen3
= get_f_kp(k
+ 1, p
- 1) + 1; /* dist if delete */
105 if (is_transposed(maxlen
, maxlen
+ k
)) {
110 if (maxlen
>= maxlen2
) {
111 if (maxlen
>= maxlen3
) {
112 // Transposition or Substitution.
118 if (maxlen2
>= maxlen3
) {
127 /* Check for exact matches, and increase the length until we don't have
129 while (maxlen
< seq1
.len
&&
130 maxlen
+ k
< seq2
.len
&&
131 seq1
.ptr
[maxlen
] == seq2
.ptr
[maxlen
+ k
]) {
134 set_f_kp(k
, p
, maxlen
);
139 edist_state
<CHR
>::edist_state(const CHR
* ptr1
, int len1
,
140 const CHR
* ptr2
, int len2
)
141 : seq1(ptr1
, len1
), seq2(ptr2
, len2
), maxdist(len2
)
143 Assert(len2
>= len1
);
144 /* Each row represents a value of k, from -maxdist to maxdist. */
145 int fkp_rows
= maxdist
* 2 + 1;
146 /* Each column represents a value of p, from -1 to maxdist. */
147 fkp_cols
= maxdist
+ 2;
148 /* fkp is stored as a rectangular array, row by row. */
149 fkp
= new int[fkp_rows
* fkp_cols
];
152 for (int k
= 1; k
<= maxdist
; ++k
) {
153 for (int p
= -1; p
< k
- 1; ++p
) {
154 set_f_kp(k
, p
, -INF
);
155 set_f_kp(-k
, p
, -INF
);
157 set_f_kp(k
, k
- 1, -1);
158 set_f_kp(-k
, k
- 1, k
- 1);
163 edist_state
<CHR
>::~edist_state() {
169 seqcmp_editdist(const CHR
* ptr1
, int len1
, const CHR
* ptr2
, int len2
,
172 int lendiff
= len2
- len1
;
173 /* Make sure second sequence is longer (or same length). */
180 /* Special case for if one or both sequences are empty. */
181 if (len1
== 0) return len2
;
183 edist_state
<CHR
> state(ptr1
, len1
, ptr2
, len2
);
185 int p
= lendiff
; /* This is the minimum possible edit distance. */
186 while (p
<= max_distance
) {
187 for (int temp_p
= 0; temp_p
!= p
; ++temp_p
) {
188 int inc
= p
- temp_p
;
189 if (abs(lendiff
- inc
) <= temp_p
) {
190 state
.edist_calc_f_kp(lendiff
- inc
, temp_p
);
192 if (abs(lendiff
+ inc
) <= temp_p
) {
193 state
.edist_calc_f_kp(lendiff
+ inc
, temp_p
);
196 state
.edist_calc_f_kp(lendiff
, p
);
198 if (state
.get_f_kp(lendiff
, p
) == len1
) break;
206 edit_distance_unsigned(const unsigned * ptr1
, int len1
,
207 const unsigned * ptr2
, int len2
,
210 return seqcmp_editdist
<unsigned>(ptr1
, len1
, ptr2
, len2
, max_distance
);