-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbb.cc
More file actions
183 lines (148 loc) · 5.72 KB
/
bb.cc
File metadata and controls
183 lines (148 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/******************************************************************************
* *
* Programmer : Guillermo Peris *
* Version : Febrero, 2001 *
* Use : This procedure computes Levenshtein distance restricted to *
* some limits, in order to use it for the Maes algorithm cycle. *
******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "heap.h"
#include "mars.h"
#include "RestrictedLevenshtein.h"
#define min(_x_,_y_) ( (_x_) < (_y_) ? (_x_) : (_y_) )
#define max(_x_,_y_) ( (_x_) > (_y_) ? (_x_) : (_y_) )
using namespace std;
float bb(float distance2, int length1, int length2, unsigned char *pattern1, unsigned char *pattern2, Path *BestPath, Path *path, int bb_type, int *cyc, unsigned int * rotation, unsigned int * distance, int * SlopeToMinimumPath)
{
int rotation2 , cc_bound, c_bound , r_bound, l_bound, i, lll, rrr, ccc;
float null, min_distance, RotatedDistance[length1 + 1], lower_bound ;
Heap partition;
Range range;
Limits Limit ;
int row_length = 2*length1+1 ;
float external_bound=INFINITE ;
/* Memory allocation */
min_distance = external_bound ;
AllocateMemoryPath(&(Limit.Left), row_length) ;
AllocateMemoryPath(&(Limit.Right), row_length) ;
/* Initialize RotatedDistance */
RotatedDistance[0] = distance2 ;
RotatedDistance[length1] = distance2 ;
/* Translate BestPath[0] */
for( i = 0; i< length1; ++i)
{
BestPath[length1].Minimum[i] = 0 ;
BestPath[length1].Maximum[i] = 0 ;
BestPath[length1].Minimum[i+length1] = BestPath[0].Minimum[i] ;
BestPath[length1].Maximum[i+length1] = BestPath[0].Maximum[i] ;
}
BestPath[length1].Minimum[2*length1] = BestPath[0].Minimum[length1] ;
BestPath[length1].Maximum[2*length1] = BestPath[0].Maximum[length1] ;
/* Heap initialization. Insert initial state in heap */
range.left = 0 ;
range.right = length1 ;
if(min_distance > distance2) min_distance = distance2 ;
rotation2 = 0 ;
lower_bound = BoundFunction(0, length1, RotatedDistance[0], RotatedDistance[length1], pattern1, pattern2, length1, length2, bb_type) ;
HeapInit(&partition, length1+1) ;
HeapInsert(&partition, range, lower_bound );
/* Begin b&b cycle */
while ( (HeapSize(&partition) > 0) && (min_distance > HeapMin(&partition) ) )
{
range = HeapExtract(&partition, &null) ;
l_bound = range.left ;
r_bound = range.right ;
c_bound = l_bound + (r_bound - l_bound) / 2 ;
CopyPath(BestPath + l_bound, &(Limit.Left), 2*length1 + 1 ) ;
CopyPath(BestPath + r_bound, &(Limit.Right), 2*length1 + 1 ) ;
distance2 = RestrictedLevenshtein(c_bound, pattern1, pattern2, length1, length2, Limit, path, SlopeToMinimumPath) ;
RotatedDistance[c_bound] = distance2 ;
CopyPath(path, BestPath + c_bound, 2*length1 + 1 ) ;
if(distance2 < min_distance)
{
min_distance = distance2 ;
rotation2 = c_bound;
}
/* Analisis de particion izquierda */
lower_bound = BoundFunction(l_bound, c_bound, RotatedDistance[l_bound], RotatedDistance[c_bound], pattern1, pattern2, length1, length2, bb_type) ;
if(lower_bound < min_distance )
{
lll = l_bound ;
ccc = c_bound ;
if( ccc > lll +2 )
{
range.left = lll ;
range.right = ccc ;
HeapInsert(&partition, range , lower_bound);
}
else if( ccc == lll +2 )
{
CopyPath(BestPath + lll, &(Limit.Left), 2*length1 + 1 ) ;
CopyPath(BestPath + ccc, &(Limit.Right), 2*length1 + 1 ) ;
cc_bound = lll + 1;
distance2 = RestrictedLevenshtein(cc_bound, pattern1, pattern2, length1, length2, Limit, path, SlopeToMinimumPath) ;
RotatedDistance[cc_bound] = distance2 ;
CopyPath(path, BestPath + cc_bound, 2*length1 + 1 ) ;
if(distance2 < min_distance)
{
min_distance = distance2 ;
rotation2 = cc_bound;
}
}
}
/* Analisis de particion derecha */
lower_bound = BoundFunction(c_bound, r_bound, RotatedDistance[c_bound], RotatedDistance[r_bound], pattern1, pattern2, length1, length2, bb_type) ;
if(lower_bound < min_distance )
{
ccc = c_bound ;
rrr = r_bound ;
if( rrr > ccc +2 )
{
range.left = ccc ;
range.right = rrr ;
HeapInsert(&partition, range , lower_bound);
}
else if( rrr == ccc +2 )
{
CopyPath(BestPath + ccc, &(Limit.Left), 2*length1 + 1 ) ;
CopyPath(BestPath + rrr, &(Limit.Right), 2*length1 + 1 ) ;
cc_bound = ccc + 1;
distance2 = RestrictedLevenshtein(cc_bound, pattern1, pattern2, length1, length2, Limit, path, SlopeToMinimumPath) ;
RotatedDistance[cc_bound] = distance2 ;
CopyPath(path, BestPath + cc_bound, 2*length1 + 1 ) ;
if(distance2 < min_distance)
{
min_distance = distance2 ;
rotation2 = cc_bound;
}
}
}
}
CopyPath( BestPath + rotation2, path, 2*length1 + 1 ) ;
*cyc = rotation2 ;
*rotation = rotation2;
*distance = min_distance;
/* Free memory */
FreeMemoryPath(&(Limit.Left)) ;
FreeMemoryPath(&(Limit.Right)) ;
HeapDestroy(&partition) ;
if(min_distance == external_bound)
min_distance = -1.0;
return min_distance;
}
float BoundFunction(int left, int right, float left_cost, float right_cost, unsigned char *pattern1, unsigned char *pattern2, int length1, int length2, int bound_type)
{
int i ;
float distance_approx, distance, suma ;
suma = INS + DEL;
/* Calculo de bb1 */
if(bound_type == 1)
{
distance_approx = (left_cost + right_cost)/2.0 + (float) (left - right)*suma/2.0 ;
if (distance_approx < 0.0) distance_approx = 0.0;
return distance_approx ;
}
return 0;
}