/
heap.cpp
376 lines (319 loc) · 12.8 KB
/
heap.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "headers.h"
#define LEFT(i) (2*i+1)
#define RIGHT(i) (2*i+2)
#define PARENT(i) (qFloor((i-1)/2))
using namespace searchEnv;
//@function testing function
void testor(void)
{
qDebug() << LEFT(2);
qDebug() << RIGHT(2);
qDebug() << PARENT(5);
qDebug() << PARENT(6);
QList<QPoint> openlist;
openlist.push_back(QPoint(1,0));
openlist.push_back(QPoint(0,1));
openlist.push_back(QPoint(2,1));
openlist.push_back(QPoint(2,3));
openlist.push_back(QPoint(1,1));
for (int i =0; i< openlist.size(); i++)
qDebug() << openlist.at(i);
for (int i=0; i< openlist.size(); i++)
{
for(int j=i+1; j< openlist.size(); j++)
{
if (openlist.at(i).x()>openlist.at(j).x())
openlist.swap(i,j);
else if ((openlist.at(i).x()==openlist.at(j).x()) && openlist.at(i).y()>openlist.at(j).y())
openlist.swap(i,j);
}
}
qDebug() << "-----------------";
for (int i =0; i< openlist.size(); i++)
qDebug() << openlist.at(i);
}
// for debugging heap
void writeOutput2File(QPoint *pt, MYINT fvalue)
{
QString configFilePath = "/home/peace/qtConsole.out";
QFile file(configFilePath);
if (!file.open(QIODevice::Append | QIODevice::Text))
{
qDebug() << "Error file opening the config file, Exiting... from composeSearchSpaceFile";
return;
}
QTextStream out(&file);
QString line = "";
// line1
line = "FH: (" + QString::number(pt->x()) + ", " + QString::number(pt->y()) + ")" + " f = " + QString::number(fvalue);
out << line << endl;
out.flush();
file.close();
}
// validate the heap
int validateHeap(QList<POSITION*> *openlist, POSITION *current, QList<POSITION *> *openlistcopy, QList<POSITION *> *decreaseKeyNodeChain)
{
#define PRINT_INCONSISTENCIES
int inconsistencyCount = 0;
// 2n recursive version
//for( int i = 0;)
// nlog(n) version
int i=openlist->size()-1;
int j=0;
for (; i>0; i--)
{
// check i is ok
for (j=i; j>0; j=PARENT(j))
{
if ( openlist->at(PARENT(j))->fForward > openlist->at(j)->fForward )
{
#ifdef PRINT_INCONSISTENCIES
// debug heap [13-June-15]
QList<POSITION *> child2RootList_beforeNodeInsertion;
if ( openlistcopy )
{
for (int a = 0; a<openlistcopy->size(); a++)
{
if ( openlistcopy->at(a) == openlist->at(j) )
{
for (int b=a; b>=0; b=PARENT(b))
{
if ( b < openlistcopy->size() && b > 0 )
child2RootList_beforeNodeInsertion.prepend( openlistcopy->at(b) );
else
qDebug() << "Stuck up in heapValidate";
}
}
}
}
// debug heap [13-June-15]
qDebug() << "FAILURE: Heap is inconsistent";
qDebug() << "j = " << j << ", node f = " << openlist->at(j)->fForward << ", point = " << openlist->at(j)->ptMe;
qDebug() << "PARENT(j) = " << PARENT(j) << ", PARENT(node f) = " << openlist->at(PARENT(j))->fForward << ", point = " << openlist->at(PARENT(j))->ptMe;
#endif
inconsistencyCount++;
break;
}
}
}
if (inconsistencyCount)
{
#ifdef PRINT_INCONSISTENCIES
qDebug() << "Total inconsistencies " << inconsistencyCount;
#endif
return 1; // Failure
}
return 0; // Success
}
void recursiveValidateHeap(QList<POSITION*> *openlist, int pos)
{
}
//@function insert current in openlist heap 2*O(lg n)
void minHeapInsertF(QList<POSITION*> *openlist, POSITION* current, Environment *env, QList<POSITION *> *decreaseKeyNodeChain)
{
//register int pos = heapExistsKey(openlist, current); // O(lg n)
int pos = env->grid[current->ptMe.x()][current->ptMe.y()].heapIndexForward;
if (pos > -1) // if node already exist
heapDecreaseKeyF(openlist, pos, env, decreaseKeyNodeChain); // O(lg n)
else
{
openlist->append(current); // O(lg n)
env->grid[current->ptMe.x()][current->ptMe.y()].heapIndexForward = openlist->size()-1;
heapDecreaseKeyF(openlist, openlist->size()-1, env, decreaseKeyNodeChain); // O(lg n)
}
}
//@function decrease key of an element at position pos O(lg n)
void heapDecreaseKeyF(QList<POSITION*> *openlist, int pos, Environment *env, QList<POSITION *> *decreaseKeyNodeChain)
{
MYINT key = openlist->at(pos)->fForward;
//bool flag = true; // debug heap [13-June-15]
for(; pos > 0 && (openlist->at(PARENT(pos))->fForward > key); pos = PARENT(pos))
{
#ifdef MYASSERT_VALIDATE_HEAP
if ( PARENT(pos)>0 && decreaseKeyNodeChain )
{
if ( flag )
decreaseKeyNodeChain->prepend( openlist->at(pos) ), flag = false; // push child node // debug heap [13-June-15]
decreaseKeyNodeChain->prepend( openlist->at(PARENT(pos)) ); // debug heap [13-June-15]
}
#endif
openlist->swap(pos, PARENT(pos));
env->grid[openlist->at(PARENT(pos))->ptMe.x()][openlist->at(PARENT(pos))->ptMe.y()].heapIndexForward = PARENT(pos);
env->grid[openlist->at(pos)->ptMe.x()][openlist->at(pos)->ptMe.y()].heapIndexForward = pos;
}
// tie breaking in favor of lower h_value
/*if ( pos > 0 && openlist->at(PARENT(pos))->fForward == key && (openlist->at(PARENT(pos))->fForward-openlist->at(PARENT(pos))->gForward)
> (openlist->at(pos)->fForward-openlist->at(pos)->gForward) )
{
openlist->swap(pos, PARENT(pos));
env->grid[openlist->at(PARENT(pos))->ptMe.x()][openlist->at(PARENT(pos))->ptMe.y()].heapIndexForward = PARENT(pos);
env->grid[openlist->at(pos)->ptMe.x()][openlist->at(pos)->ptMe.y()].heapIndexForward = pos;
}*/
}
//@function build min heap O(n)
void buildMinHeapF(QList<POSITION*> *openlist, Environment *env)
{
if (openlist->size())
{
// let everyone know its index
for (int i=0; i<openlist->size(); i++)
env->grid[openlist->at(i)->ptMe.x()][openlist->at(i)->ptMe.y()].heapIndexForward = i;
for(int i = PARENT(openlist->size()-1); i>=0; --i)
minHeapifyF(openlist, i, env);
}
}
//@function extract minimum O(lg n)
POSITION* extractMinF(QList<POSITION*> *openlist, Environment *env)
{
if (!openlist->size())
return NULL;
else
{
// not adjusting heapIndex of the node extracted, because it will not be used again in case of WA*
openlist->swap(0, openlist->size()-1); // swap first with last
env->grid[openlist->at(0)->ptMe.x()][openlist->at(0)->ptMe.y()].heapIndexForward = 0;
POSITION *temp = openlist->takeLast();
// minHeapify at position 0
minHeapifyF(openlist, 0, env);
env->grid[temp->ptMe.x()][temp->ptMe.y()].heapIndexForward = -1; // resetting needed in case of ARA*
#ifdef DEBUG_HEAP
if (env->epsilon == 1) // heap debug [12-June-15]
writeOutput2File(&(temp->ptMe), temp->fForward);
#endif
return temp;
}
}
//@function min Heapify openlist O(lg n)
void minHeapifyF(QList<POSITION*> *openlist, int i, Environment *env)
{
int smallest = -1;
while(1)
{
if ( LEFT(i) < openlist->size() && (openlist->at(i)->fForward > openlist->at(LEFT(i))->fForward) )
smallest = LEFT(i);
/*else if ( LEFT(i) < openlist->size() && (openlist->at(i)->fForward == openlist->at(LEFT(i))->fForward) &&
((openlist->at(i)->fForward - openlist->at(i)->gForward) >
(openlist->at(LEFT(i))->fForward - openlist->at(LEFT(i))->gForward)) )
smallest = LEFT(i);*/
else
smallest = i;
if ( RIGHT(i) < openlist->size() && (openlist->at(smallest)->fForward > openlist->at(RIGHT(i))->fForward) )
smallest = RIGHT(i);
/*else if ( RIGHT(i) < openlist->size() && (openlist->at(smallest)->fForward == openlist->at(RIGHT(i))->fForward) &&
((openlist->at(smallest)->fForward - openlist->at(smallest)->gForward) >
(openlist->at(RIGHT(i))->fForward - openlist->at(RIGHT(i))->gForward)) )
smallest = RIGHT(i);*/
if (smallest != i)
{
openlist->swap(i, smallest);
env->grid[openlist->at(smallest)->ptMe.x()][openlist->at(smallest)->ptMe.y()].heapIndexForward = smallest;
env->grid[openlist->at(i)->ptMe.x()][openlist->at(i)->ptMe.y()].heapIndexForward = i;
i = smallest;
}
else
break;
}
}
//@function check if current exists in heap, return position O(lg n)
int heapExistsKeyF(QList<POSITION*> *openlist, POSITION *current)
{
if (!openlist->size()) // heap empty
return -1;
else
{
//while()
}
}
//@function insert current in openlist heap 2*O(lg n)
void minHeapInsertB(QList<POSITION*> *openlist, POSITION* current, Environment *env)
{
//register int pos = heapExistsKey(openlist, current); // O(lg n)
int pos = env->grid[current->ptMe.x()][current->ptMe.y()].heapIndexBackward;
if (pos > -1) // if node already exist
heapDecreaseKeyB(openlist, pos, env); // O(lg n)
else
{
openlist->append(current); // O(lg n)
env->grid[current->ptMe.x()][current->ptMe.y()].heapIndexBackward = openlist->size()-1;
heapDecreaseKeyB(openlist, openlist->size()-1, env); // O(lg n)
}
}
//@function decrease key of an element at position pos O(lg n)
void heapDecreaseKeyB(QList<POSITION*> *openlist, int pos, Environment *env)
{
MYINT key = openlist->at(pos)->fBackward;
//env->grid[openlist->at(pos)->ptMe.x()][openlist->at(pos)->ptMe.y()].heapIndex = pos;
for(; pos > 0 && (openlist->at(PARENT(pos))->fBackward > key); pos = PARENT(pos))
{
openlist->swap(pos, PARENT(pos));
env->grid[openlist->at(PARENT(pos))->ptMe.x()][openlist->at(PARENT(pos))->ptMe.y()].heapIndexBackward = PARENT(pos);
env->grid[openlist->at(pos)->ptMe.x()][openlist->at(pos)->ptMe.y()].heapIndexBackward = pos;
}
}
//@function build min heap O(n)
void buildMinHeapB(QList<POSITION*> *openlist, Environment *env)
{
if (openlist->size())
{
// let everyone know its index
for (int i=0; i<openlist->size(); i++)
env->grid[openlist->at(i)->ptMe.x()][openlist->at(i)->ptMe.y()].heapIndexBackward = i;
for(int i = PARENT(openlist->size()-1); i>=0; --i)
minHeapifyB(openlist, i, env);
}
}
//@function extract minimum O(lg n)
POSITION* extractMinB(QList<POSITION*> *openlist, Environment *env)
{
if (!openlist->size())
return NULL;
else
{
// not adjusting heapIndex of the node extracted, because it will not be used again in case of WA*
openlist->swap(0, openlist->size()-1); // swap first with last
env->grid[openlist->at(0)->ptMe.x()][openlist->at(0)->ptMe.y()].heapIndexBackward = 0;
POSITION *temp = openlist->takeLast();
// minHeapify at position 0
minHeapifyB(openlist, 0, env);
env->grid[temp->ptMe.x()][temp->ptMe.y()].heapIndexBackward = -1; // needed in case of ARA*
#ifdef DEBUG_HEAP
qDebug("BH: (%d, %d)", temp->ptMe.x(), temp->ptMe.y());
#endif
return temp;
}
}
//@function min Heapify openlist O(lg n)
void minHeapifyB(QList<POSITION*> *openlist, int i, Environment *env)
{
int smallest = -1;
while(1)
{
//if (LEFT(i) < openlist->size() && openlist->at(LEFT(i))->fBackward < openlist->at(i)->fBackward)
if ( LEFT(i) < openlist->size() && (openlist->at(i)->fBackward > openlist->at(LEFT(i))->fBackward) )
smallest = LEFT(i);
else
smallest = i;
//if (RIGHT(i) < openlist->size() && openlist->at(RIGHT(i))->fBackward < openlist->at(smallest)->fBackward)
if ( RIGHT(i) < openlist->size() && (openlist->at(smallest)->fBackward > openlist->at(RIGHT(i))->fBackward) )
smallest = RIGHT(i);
if (smallest != i)
{
openlist->swap(i, smallest);
env->grid[openlist->at(smallest)->ptMe.x()][openlist->at(smallest)->ptMe.y()].heapIndexBackward = smallest;
env->grid[openlist->at(i)->ptMe.x()][openlist->at(i)->ptMe.y()].heapIndexBackward = i;
i = smallest;
}
else
break;
}
}
//@function check if current exists in heap, return position O(lg n)
int heapExistsKeyB(QList<POSITION*> *openlist, POSITION *current)
{
if (!openlist->size()) // heap empty
return -1;
else
{
//while()
}
}