Debian Codegraph @

src/thirdparty/VLFeat/aib.c

raw
1 /** @file aib.c
2 ** @brief AIB - Definition
3 ** @author Brian Fulkerson
4 ** @author Andrea Vedaldi
5 **/
6
7 /*
8 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
9 All rights reserved.
10
11 This file is part of the VLFeat library and is made available under
12 the terms of the BSD license (see the COPYING file).
13 */
14
15 /**
16
17 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
18 @page aib Agglomerative Information Bottleneck (AIB)
19 @author Brian Fulkerson
20 @author Andrea Vedaldi
21 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
22
23 @ref aib.h implemens the Agglomerative Information Bottleneck (AIB)
24 algorithm as first described in @cite{slonim99agglomerative}.
25
26 AIB takes a discrete valued feature @f$x@f$ and a label @f$c@f$ and
27 gradually compresses @f$x@f$ by iteratively merging values which
28 minimize the loss in mutual information @f$I(x,c)@f$.
29
30 While the algorithm is equivalent to the one described in
31 @cite{slonim99agglomerative}, it has some speedups that enable
32 handling much larger datasets. Let <em>N</em> be the number of feature
33 values and <em>C</em> the number of labels. The algorithm of
34 @cite{slonim99agglomerative} is @f$O(N^2)@f$ in space and @f$O(C
35 N^3)@f$ in time. This algorithm is @f$O(N)@f$ space and @f$O(C N^2)@f$
36 time in common cases (@f$O(C N^3)@f$ in the worst case).
37
38 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
39 @section aib-overview Overview
40 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
41
42 Given a discrete feature @f$x \in \mathcal{X} = \{x_1,\dots,x_N\}@f$
43 and a category label @f$c = 1,\dots,C@f$ with joint probability
44 @f$p(x,c)@f$, AIB computes a compressed feature @f$[x]_{ij}@f$ by
45 merging two values @f$x_i@f$ and @f$x_j@f$. Among all the pairs
46 @f$ij@f$, AIB chooses the one that yields the smallest loss in the
47 mutual information
48
49 @f[
50 D_{ij} = I(x,c) - I([x]_{ij},c) =
51 \sum_c p(x_i) \log \frac{p(x_i,c)}{p(x_i)p(c)} +
52 \sum_c p(x_i) \log \frac{p(x_i,c)}{p(x_i)p(c)} -
53 \sum_c (p(x_i)+p(x_j)) \log \frac {p(x_i,c)+p(x_i,c)}{(p(x_i)+p(x_j))p(c)}
54 @f]
55
56 AIB iterates this procedure until the desired level of
57 compression is achieved.
58
59 @section aib-algorithm Algorithm details
60
61 Computing @f$D_{ij}@f$ requires @f$O(C)@f$ operations. For example, in
62 standard AIB we need to calculate
63
64 @f[
65 D_{ij} = I(x,c) - I([x]_{ij},c) =
66 \sum_c p(x_i) \log \frac{p(x_i,c)}{p(x_i)p(c)} +
67 \sum_c p(x_i) \log \frac{p(x_i,c)}{p(x_i)p(c)} -
68 \sum_c (p(x_i)+p(x_j)) \log \frac {p(x_i,c)+p(x_i,c)}{(p(x_i)+p(x_j))p(c)}
69 @f]
70
71 Thus in a basic implementation of AIB, finding the optimal pair
72 @f$ij@f$ of feature values requires @f$O(CN^2)@f$ operations in
73 total. In order to join all the @f$N@f$ values, we repeat this
74 procedure @f$O(N)@f$ times, yielding @f$O(N^3 C)@f$ time and
75 @f$O(1)@f$ space complexity (this does not account for the space need
76 to store the input).
77
78 The complexity can be improved by reusing computations. For instance,
79 we can store the matrix @f$D = [ D_{ij} ]@f$ (which requires
80 @f$O(N^2)@f$ space). Then, after joining @f$ij@f$, all of the matrix
81 <em>D</em> except the rows and columns (the matrix is symmetric) of
82 indexes <em>i</em> and <em>j</em> is unchanged. These two rows and
83 columns are deleted and a new row and column, whose computation
84 requires @f$O(NC)@f$ operations, are added for the merged value
85 @f$x_{ij}@f$. Finding the minimal element of the matrix still
86 requires @f$O(N^2)@f$ operations, so the complexity of this algorithm
87 is @f$O(N^2C + N^3)@f$ time and @f$O(N^2)@f$ space.
88
89 We can obtain a much better expected complexity as follows. First,
90 instead of storing the whole matrix <em>D</em>, we store the smallest
91 element (index and value) of each row as @f$(q_i, D_i)@f$ (notice that
92 this is also the best element of each column since <em>D</em> is
93 symmetric). This requires @f$O(N)@f$ space and finding the minimal
94 element of the matrix requires @f$O(N)@f$ operations. After joining
95 @f$ij@f$, we have to efficiently update this representation. This is
96 done as follows:
97
98 - The entries @f$(q_i,D_i)@f$ and @f$(q_j,D_j)@f$ are deleted.
99 - A new entry @f$(q_{ij},D_{ij})@f$ for the joint value @f$x_{ij}@f$
100 is added. This requires @f$O(CN)@f$ operations.
101 - We test which other entries @f$(q_{k},D_{k})@f$ need to
102 be updated. Recall that @f$(q_{k},D_{k})@f$ means that, before the
103 merge, the value
104 closest to @f$x_k@f$ was @f$x_{q_k}@f$ at a distance @f$D_k@f$. Then
105 - If @f$q_k \not = i@f$, @f$q_k \not = j@f$ and @f$D_{k,ij} \geq D_k@f$, then
106 @f$q_k@f$ is still the closest element and we do not do anything.
107 - If @f$q_k \not = i@f$, @f$q_k \not = j@f$ and @f$D_{k,ij} <
108 D_k@f$, then the closest element is @f$ij@f$ and we update the
109 entry in constant time.
110 - If @f$q_k = i@f$ or @f$q_k = j@f$, then we need to re-compute
111 the closest element in @f$O(CN)@f$ operations.
112
113 This algorithm requires only @f$O(N)@f$ space and @f$O(\gamma(N) C
114 N^2)@f$ time, where @f$\gamma(N)@f$ is the expected number of times we
115 fall in the last case. In common cases one has @f$\gamma(N) \approx
116 \mathrm{const.}@f$, so the time saving is significant.
117
118 **/
119
120 #include "aib.h"
121 #include <stdio.h>
122 #include <stdlib.h>
123 #include <float.h>
124 #include <math.h>
125
126 /* The maximum value which beta may take */
127 #define BETA_MAX DBL_MAX
128
129 /** ------------------------------------------------------------------
130 ** @internal
131 ** @brief Normalizes an array of probabilities to sum to 1
132 **
133 ** @param P The array of probabilities
134 ** @param nelem The number of elements in the array
135 **
136 ** @return Modifies P to contain values which sum to 1
137 **/
138
139 void vl_aib_normalize_P (double * P, vl_uint nelem)
140 {
141 vl_uint i;
142 double sum = 0;
143 for(i=0; i<nelem; i++)
144 sum += P[i];
145 for(i=0; i<nelem; i++)
146 P[i] /= sum;
147 }
148
149 /** ------------------------------------------------------------------
150 ** @internal
151 ** @brief Allocates and creates a list of nodes
152 **
153 ** @param nentries The size of the list which will be created
154 **
155 ** @return an array containing elements 0...nentries
156 **/
157
158 vl_uint *vl_aib_new_nodelist (vl_uint nentries)
159 {
160 vl_uint * nodelist = vl_malloc(sizeof(vl_uint)*nentries);
161 vl_uint n;
162 for(n=0; n<nentries; n++)
163 nodelist[n] = n;
164
165 return nodelist;
166 }
167
168 /** ------------------------------------------------------------------
169 ** @internal
170 ** @brief Allocates and creates the marginal distribution Px
171 **
172 ** @param Pcx A two-dimensional array of probabilities
173 ** @param nvalues The number of rows in Pcx
174 ** @param nlabels The number of columns in Pcx
175 **
176 ** @return an array of size @a nvalues which contains the marginal
177 ** distribution over the rows.
178 **/
179
180 double * vl_aib_new_Px(double * Pcx, vl_uint nvalues, vl_uint nlabels)
181 {
182 double * Px = vl_malloc(sizeof(double)*nvalues);
183 vl_uint r,c;
184 for(r=0; r<nvalues; r++)
185 {
186 double sum = 0;
187 for(c=0; c<nlabels; c++)
188 sum += Pcx[r*nlabels+c];
189 Px[r] = sum;
190 }
191 return Px;
192 }
193
194 /** ------------------------------------------------------------------
195 ** @internal @brief Allocates and creates the marginal distribution Pc
196 **
197 ** @param Pcx A two-dimensional array of probabilities
198 ** @param nvalues The number of rows in Pcx
199 ** @param nlabels The number of columns in Pcx
200 **
201 ** @return an array of size @a nlabels which contains the marginal distribution
202 ** over the columns
203 **/
204
205 double * vl_aib_new_Pc(double * Pcx, vl_uint nvalues, vl_uint nlabels)
206 {
207 double * Pc = vl_malloc(sizeof(double)*nlabels);
208 vl_uint r, c;
209 for(c=0; c<nlabels; c++)
210 {
211 double sum = 0;
212 for(r=0; r<nvalues; r++)
213 sum += Pcx[r*nlabels+c];
214 Pc[c] = sum;
215 }
216 return Pc;
217 }
218
219 /** ------------------------------------------------------------------
220 ** @internal @brief Find the two nodes which have minimum beta.
221 **
222 ** @param aib A pointer to the internal data structure
223 ** @param besti The index of one member of the pair which has mininum beta
224 ** @param bestj The index of the other member of the pair which
225 ** minimizes beta
226 ** @param minbeta The minimum beta value corresponding to (@a i, @a j)
227 **
228 ** Searches @a aib->beta to find the minimum value and fills @a minbeta and
229 ** @a besti and @a bestj with this information.
230 **/
231
232 void vl_aib_min_beta
233 (VlAIB * aib, vl_uint * besti, vl_uint * bestj, double * minbeta)
234 {
235 vl_uint i;
236 *minbeta = aib->beta[0];
237 *besti = 0;
238 *bestj = aib->bidx[0];
239
240 for(i=0; i<aib->nentries; i++)
241 {
242 if(aib->beta[i] < *minbeta)
243 {
244 *minbeta = aib->beta[i];
245 *besti = i;
246 *bestj = aib->bidx[i];
247 }
248 }
249 }
250
251 /** ------------------------------------------------------------------
252 ** @internal
253 ** @brief Merges two nodes i,j in the internal datastructure
254 **
255 ** @param aib A pointer to the internal data structure
256 ** @param i The index of one member of the pair to merge
257 ** @param j The index of the other member of the pair to merge
258 ** @param new The index of the new node which corresponds to the union of
259 ** (@a i, @a j).
260 **
261 ** Nodes are merged by replacing the entry @a i with the union of @c
262 ** ij, moving the node stored in last position (called @c lastnode)
263 ** back to jth position and the entry at the end.
264 **
265 ** After the nodes have been merged, it updates which nodes should be
266 ** considered on the next iteration based on which beta values could
267 ** potentially change. The merged node will always be part of this
268 ** list.
269 **/
270
271 void
272 vl_aib_merge_nodes (VlAIB * aib, vl_uint i, vl_uint j, vl_uint new)
273 {
274 vl_uint last_entry = aib->nentries - 1 ;
275 vl_uint c, n ;
276
277 /* clear the list of nodes to update */
278 aib->nwhich = 0;
279
280 /* make sure that i is smaller than j */
281 if(i > j) { vl_uint tmp = j; j = i; i = tmp; }
282
283 /* -----------------------------------------------------------------
284 * Merge entries i and j, storing the result in i
285 * -------------------------------------------------------------- */
286
287 aib-> Px [i] += aib->Px[j] ;
288 aib-> beta [i] = BETA_MAX ;
289 aib-> nodes[i] = new ;
290
291 for (c = 0; c < aib->nlabels; c++)
292 aib-> Pcx [i*aib->nlabels + c] += aib-> Pcx [j*aib->nlabels + c] ;
293
294 /* -----------------------------------------------------------------
295 * Move last entry to j
296 * -------------------------------------------------------------- */
297
298 aib-> Px [j] = aib-> Px [last_entry];
299 aib-> beta [j] = aib-> beta [last_entry];
300 aib-> bidx [j] = aib-> bidx [last_entry];
301 aib-> nodes [j] = aib-> nodes [last_entry];
302
303 for (c = 0 ; c < aib->nlabels ; c++)
304 aib-> Pcx[j*aib->nlabels + c] = aib-> Pcx [last_entry*aib->nlabels + c] ;
305
306 /* delete last entry */
307 aib-> nentries -- ;
308
309 /* -----------------------------------------------------------------
310 * Scan for entries to update
311 * -------------------------------------------------------------- */
312
313 /*
314 * After mergin entries i and j, we need to update all other entries
315 * that had one of these two as closest match. We also need to
316 * update the renewend entry i. This is added by the loop below
317 * since bidx [i] = j exactly because i was merged.
318 *
319 * Additionaly, since we moved the last entry back to the entry j,
320 * we need to adjust the valeus of bidx to reflect this.
321 */
322
323 for (n = 0 ; n < aib->nentries; n++) {
324 if(aib->bidx[n] == i || aib->bidx[n] == j) {
325 aib->bidx [n] = 0;
326 aib->beta [n] = BETA_MAX;
327 aib->which [aib->nwhich++] = n ;
328 }
329 else if(aib->bidx[n] == last_entry) {
330 aib->bidx[n] = j ;
331 }
332 }
333 }
334
335 /** ------------------------------------------------------------------
336 ** @internal
337 ** @brief Updates @c aib->beta and @c aib->bidx according to @c aib->which
338 **
339 ** @param aib AIB data structure.
340 **
341 ** The function calculates @c beta[i] and @c bidx[i] for the nodes @c
342 ** i listed in @c aib->which. @c beta[i] is the minimal variation of mutual
343 ** information (or other score) caused by merging entry @c i with another entry
344 ** and @c bidx[i] is the index of this best matching entry.
345 **
346 ** Notice that for each entry @c i that we need to update, a full
347 ** scan of all the other entries must be performed.
348 **/
349
350 void
351 vl_aib_update_beta (VlAIB * aib)
352 {
353
354 #define PLOGP(x) ((x)*log((x)))
355
356 vl_uint i;
357 double * Px = aib->Px;
358 double * Pcx = aib->Pcx;
359 double * tmp = vl_malloc(sizeof(double)*aib->nentries);
360 vl_uint a, b, c ;
361
362 /*
363 * T1 = I(x,c) - I([x]_ij) = A + B - C
364 *
365 * A = \sum_c p(xa,c) \log ( p(xa,c) / p(xa) )
366 * B = \sum_c p(xb,c) \log ( p(xb,c) / p(xb) )
367 * C = \sum_c (p(xa,c)+p(xb,c)) \log ((p(xa,c)+p(xb,c)) / (p(xa)+p(xb)))
368 *
369 * C = C1 + C2
370 * C1 = \sum_c (p(xa,c)+p(xb,c)) \log (p(xa,c)+p(xb,c))
371 * C2 = - (p(xa)+p(xb) \log (p(xa)+p(xb))
372 */
373
374 /* precalculate A and B */
375 for (a = 0; a < aib->nentries; a++) {
376 tmp[a] = 0;
377 for (c = 0; c < aib->nlabels; c++) {
378 double Pac = Pcx [a*aib->nlabels + c] ;
379 if(Pac != 0) tmp[a] += Pac * log (Pac / Px[a]) ;
380 }
381 }
382
383 /* for each entry listed in which */
384 for (i = 0 ; i < aib->nwhich; i++) {
385 a = aib->which[i];
386
387 /* for each other entry */
388 for(b = 0 ; b < aib->nentries ; b++) {
389 double T1 = 0 ;
390
391 if (a == b || Px [a] == 0 || Px [b] == 0) continue ;
392
393
394 T1 = PLOGP ((Px[a] + Px[b])) ; /* - C2 */
395 T1 += tmp[a] + tmp[b] ; /* + A + B */
396
397 for (c = 0 ; c < aib->nlabels; ++ c) {
398 double Pac = Pcx [a*aib->nlabels + c] ;
399 double Pbc = Pcx [b*aib->nlabels + c] ;
400 if (Pac == 0 && Pbc == 0) continue;
401 T1 += - PLOGP ((Pac + Pbc)) ; /* - C1 */
402 }
403
404 /*
405 * Now we have beta(a,b). We check wether this is the best beta
406 * for entries a and b.
407 */
408 {
409 double beta = T1 ;
410
411 if (beta < aib->beta[a])
412 {
413 aib->beta[a] = beta;
414 aib->bidx[a] = b;
415 }
416 if (beta < aib->beta[b])
417 {
418 aib->beta[b] = beta;
419 aib->bidx[b] = a;
420 }
421 }
422 }
423 }
424 vl_free(tmp);
425 }
426
427 /** ------------------------------------------------------------------
428 ** @internal @brief Calculates the current information and entropy
429 **
430 ** @param aib A pointer to the internal data structure
431 ** @param I The current mutual information (out).
432 ** @param H The current entropy (out).
433 **
434 ** Calculates the current mutual information and entropy of Pcx and sets
435 ** @a I and @a H to these new values.
436 **/
437 void vl_aib_calculate_information(VlAIB * aib, double * I, double * H)
438 {
439 vl_uint r, c;
440 *H = 0;
441 *I = 0;
442
443 /*
444 * H(x) = - sum_x p(x) \ log p(x)
445 * I(x,c) = sum_xc p(x,c) \ log (p(x,c) / p(x)p(c))
446 */
447
448 /* for each entry */
449 for(r = 0 ; r< aib->nentries ; r++) {
450
451 if (aib->Px[r] == 0) continue ;
452 *H += -log(aib->Px[r]) * aib->Px[r] ;
453
454 for(c=0; c<aib->nlabels; c++) {
455 if (aib->Pcx[r*aib->nlabels+c] == 0) continue;
456 *I += aib->Pcx[r*aib->nlabels+c] *
457 log (aib->Pcx[r*aib->nlabels+c] / (aib->Px[r]*aib->Pc[c])) ;
458 }
459 }
460 }
461
462 /** ------------------------------------------------------------------
463 ** @brief Allocates and initializes the internal data structure
464 **
465 ** @param Pcx A pointer to a 2D array of probabilities
466 ** @param nvalues The number of rows in the array
467 ** @param nlabels The number of columns in the array
468 **
469 ** Creates a new @a VlAIB struct containing pointers to all the data that
470 ** will be used during the AIB process.
471 **
472 ** Allocates memory for the following:
473 ** - Px (nvalues*sizeof(double))
474 ** - Pc (nlabels*sizeof(double))
475 ** - nodelist (nvalues*sizeof(vl_uint))
476 ** - which (nvalues*sizeof(vl_uint))
477 ** - beta (nvalues*sizeof(double))
478 ** - bidx (nvalues*sizeof(vl_uint))
479 ** - parents ((2*nvalues-1)*sizeof(vl_uint))
480 ** - costs (nvalues*sizeof(double))
481 **
482 ** Since it simply copies to pointer to Pcx, the total additional memory
483 ** requirement is:
484 **
485 ** (3*nvalues+nlabels)*sizeof(double) + 4*nvalues*sizeof(vl_uint)
486 **
487 ** @returns An allocated and initialized @a VlAIB pointer
488 **/
489 VlAIB * vl_aib_new(double * Pcx, vl_uint nvalues, vl_uint nlabels)
490 {
491 VlAIB * aib = vl_malloc(sizeof(VlAIB));
492 vl_uint i ;
493
494 aib->verbosity = 0 ;
495 aib->Pcx = Pcx ;
496 aib->nvalues = nvalues ;
497 aib->nlabels = nlabels ;
498
499 vl_aib_normalize_P (aib->Pcx, aib->nvalues * aib->nlabels) ;
500
501 aib->Px = vl_aib_new_Px (aib->Pcx, aib->nvalues, aib->nlabels) ;
502 aib->Pc = vl_aib_new_Pc (aib->Pcx, aib->nvalues, aib->nlabels) ;
503
504 aib->nentries = aib->nvalues ;
505 aib->nodes = vl_aib_new_nodelist(aib->nentries) ;
506 aib->beta = vl_malloc(sizeof(double) * aib->nentries) ;
507 aib->bidx = vl_malloc(sizeof(vl_uint) * aib->nentries) ;
508
509 for(i = 0 ; i < aib->nentries ; i++)
510 aib->beta [i] = BETA_MAX ;
511
512 /* Initially we must consider all nodes */
513 aib->nwhich = aib->nvalues;
514 aib->which = vl_aib_new_nodelist (aib->nwhich) ;
515
516 aib->parents = vl_malloc(sizeof(vl_uint)*(aib->nvalues*2-1));
517 /* Initially, all parents point to a nonexistent node */
518 for (i = 0 ; i < 2 * aib->nvalues - 1 ; i++)
519 aib->parents [i] = 2 * aib->nvalues ;
520
521 /* Allocate cost output vector */
522 aib->costs = vl_malloc (sizeof(double) * (aib->nvalues - 1 + 1)) ;
523
524
525 return aib ;
526 }
527
528 /** ------------------------------------------------------------------
529 ** @brief Deletes AIB data structure
530 ** @param aib data structure to delete.
531 **/
532
533 void
534 vl_aib_delete (VlAIB * aib)
535 {
536 if (aib) {
537 if (aib-> nodes) vl_free (aib-> nodes);
538 if (aib-> beta) vl_free (aib-> beta);
539 if (aib-> bidx) vl_free (aib-> bidx);
540 if (aib-> which) vl_free (aib-> which);
541 if (aib-> Px) vl_free (aib-> Px);
542 if (aib-> Pc) vl_free (aib-> Pc);
543 if (aib-> parents) vl_free (aib-> parents);
544 if (aib-> costs) vl_free (aib-> costs);
545
546 vl_free (aib) ;
547 }
548 }
549
550 /** ------------------------------------------------------------------
551 ** @brief Runs AIB on Pcx
552 **
553 ** @param aib AIB object to process
554 **
555 ** The function runs Agglomerative Information Bottleneck (AIB) on
556 ** the joint probability table @a aib->Pcx which has labels along the
557 ** columns and feature values along the rows. AIB iteratively merges
558 ** the two values of the feature @c x that causes the smallest
559 ** decrease in mutual information between the random variables @c x
560 ** and @c c.
561 **
562 ** Merge operations are arranged in a binary tree. The nodes of the
563 ** tree correspond to the original feature values and any other value
564 ** obtained as a result of a merge operation. The nodes are indexed
565 ** in breadth-first order, starting from the leaves. The first index
566 ** is zero. In this way, the leaves correspond directly to the
567 ** original feature values. In total there are @c 2*nvalues-1 nodes.
568 **
569 ** The results may be accessed through vl_aib_get_parents which
570 ** returns an array with one element per tree node. Each
571 ** element is the index the parent node. The root parent is equal to
572 ** zero. The array has @c 2*nvalues-1 elements.
573 **
574 ** Feature values with null probability are ignored by the algorithm
575 ** and their nodes have parents indexing a non-existent tree node (a
576 ** value bigger than @c 2*nvalues-1).
577 **
578 ** Then the function will also compute the information level after each
579 ** merge. vl_get_costs will return a vector with the information level
580 ** after each merge. @a
581 ** cost has @c nvalues entries: The first is the value of the cost
582 ** functional before any merge, and the others are the cost after the
583 ** @c nvalues-1 merges.
584 **
585 **/
586
587 VL_EXPORT
588 void vl_aib_process(VlAIB *aib)
589 {
590 vl_uint i, besti, bestj, newnode, nodei, nodej;
591 double I, H;
592 double minbeta;
593
594 /* Calculate initial value of cost function */
595 vl_aib_calculate_information (aib, &I, &H) ;
596 aib->costs[0] = I;
597
598 /* Initially which = all */
599
600 /* For each merge */
601 for(i = 0 ; i < aib->nvalues - 1 ; i++) {
602
603 /* update entries in aib-> which */
604 vl_aib_update_beta(aib);
605
606 /* find best pair of nodes to merge */
607 vl_aib_min_beta (aib, &besti, &bestj, &minbeta);
608
609 if(minbeta == BETA_MAX)
610 /* only null-probability entries remain */
611 break;
612
613 /* Add the parent pointers for the new node */
614 newnode = aib->nvalues + i ;
615 nodei = aib->nodes[besti];
616 nodej = aib->nodes[bestj];
617
618 aib->parents [nodei] = newnode ;
619 aib->parents [nodej] = newnode ;
620 aib->parents [newnode] = 0 ;
621
622 /* Merge the nodes which produced the minimum beta */
623 vl_aib_merge_nodes (aib, besti, bestj, newnode) ;
624 vl_aib_calculate_information (aib, &I, &H) ;
625
626 aib->costs[i+1] = I;
627
628 if (aib->verbosity > 0) {
629 VL_PRINTF ("aib: (%5d,%5d)=%5d dE: %10.3g I: %6.4g H: %6.4g updt: %5d\n",
630 nodei,
631 nodej,
632 newnode,
633 minbeta,
634 I,
635 H,
636 aib->nwhich) ;
637 }
638 }
639
640 /* fill ignored entries with NaNs */
641 for(; i < aib->nvalues - 1 ; i++)
642 aib->costs[i+1] = VL_NAN_D ;
643 }