File: Mongoose_QPBoundary.cpp

package info (click to toggle)
suitesparse 1%3A5.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 138,928 kB
  • sloc: ansic: 389,614; cpp: 24,213; makefile: 5,965; fortran: 1,927; java: 1,808; csh: 1,750; ruby: 725; sh: 226; perl: 225; python: 209; sed: 164; awk: 60
file content (753 lines) | stat: -rw-r--r-- 26,905 bytes parent folder | download | duplicates (3)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/* ========================================================================== */
/* === Source/Mongoose_QPBoundary.cpp ======================================= */
/* ========================================================================== */

/* -----------------------------------------------------------------------------
 * Mongoose Graph Partitioning Library  Copyright (C) 2017-2018,
 * Scott P. Kolodziej, Nuri S. Yeralan, Timothy A. Davis, William W. Hager
 * Mongoose is licensed under Version 3 of the GNU General Public License.
 * Mongoose is also available under other licenses; contact authors for details.
 * -------------------------------------------------------------------------- */

/*
   Move all components of x to boundary of the feasible region

    0 <= x <= 1, a'x = b, lo <= b <= hi

   while decreasing the cost function. The algorithm has the following parts

   1. For each i in the free set, see if x_i can be feasibly pushed to either
      boundary while decreasing the cost.

   2. For each i in the bound set, see if x_i can be feasibly flipped to
      opposite boundary while decreasing the cost.

   3. For each i in the free list with a_{ij} = 0 and with j free,
      move either x_i or x_j to the boundary while decreasing
      the cost. The adjustments has the form x_i = s/a_i and x_j = -s/a_j
      where s is a scalar factor. These adjustments must decrease cost.

   4. For the remaining i in the free list, take pair x_i and x_j and
      apply adjustments of the same form as in #2 above to push at least one
      component to boundary. The quadratic terms can only decrease the
      cost function. We choose the sign of s such that g_i x_i + g_j x_j <= 0.
      Hence, this adjustment cannot increase the cost.
 */

/* ========================================================================== */

#include "Mongoose_QPBoundary.hpp"
#include "Mongoose_Debug.hpp"
#include "Mongoose_Internal.hpp"

#define EMPTY (-1)

namespace Mongoose
{

void QPBoundary(EdgeCutProblem *graph, const EdgeCut_Options *options, QPDelta *QP)
{
    (void)options; // Unused variable
    /* ---------------------------------------------------------------------- */
    /* Step 0. read in the needed arrays                                      */
    /* ---------------------------------------------------------------------- */

    /* input and output */

    //--- FreeSet
    Int nFreeSet        = QP->nFreeSet;
    Int *FreeSet_list   = QP->FreeSet_list; /* list for free indices */
    Int *FreeSet_status = QP->FreeSet_status;
    /* FreeSet_status [i] = +1, -1, or 0
       if x_i = 1, 0, or 0 < x_i < 1 */
    //---

    PR(("Mongoose_QPBoundary nFreeSet %ld\n", nFreeSet));

    if (nFreeSet == 0)
    {
        // quick return if FreeSet is empty
        return;
    }

    double *x    = QP->x;        /* current estimate of solution */
    double *grad = QP->gradient; /* gradient at current x */
    Int ib       = QP->ib;       /* ib = +1, -1, or 0 ,
         if b = hi, lo, or lo < b < hi, respectively.  Note there are cases
         where roundoff occurs, and ib can be zero even though b == lo or
         b == hi.  The value of be can even be < lo or > hi, but only by a tiny
         amount of roundoff error.  This is OK. */

    double b = QP->b; /* current value for a'x */

    /* problem specification for the graph G */
    Int n      = graph->n; /* problem dimension */
    double *Ex = graph->x; /* numerical values for edge weights */
    Int *Ei    = graph->i; /* adjacent vertices for each vertex */
    Int *Ep    = graph->p; /* points into Ex or Ei */
    double *a  = graph->w; /* a'x = b, lo <= b <= hi */

    double lo = QP->lo;
    double hi = QP->hi;

    /* work array */
    double *D = QP->D; /* diagonal of quadratic */

    PR(("\n----- QPBoundary start: [\n"));
    DEBUG(QPcheckCom(graph, options, QP, 1, QP->nFreeSet, QP->b)); // check b

    /* ---------------------------------------------------------------------- */
    /* Step 1. if lo < b < hi, then for each free k,                          */
    /*         see if x_k can be pushed to 0 or 1                             */
    /* ---------------------------------------------------------------------- */

    DEBUG(FreeSet_dump("QPBoundary start", n, FreeSet_list, nFreeSet,
                       FreeSet_status, 0, x));

    PR(("Boundary 1 start: ib %ld lo %g b %g hi %g b-lo %g hi-b %g\n", ib, lo,
        b, hi, b - lo, hi - b));

    Int kfree2 = 0;
    for (Int kfree = 0; kfree < nFreeSet; kfree++)
    {
        // Once b becomes bounded, the remainder of the FreeSet is unchanged,
        // and no further changes are made to x.  However, this loop must still
        // continue, so as to compact the FreeSet from deletions made by earlier
        // iterations.

        // get the next k from the FreeSet
        Int k = FreeSet_list[kfree];

        PR(("Step 1: k %ld  x[k] %g  ib %ld b %g\n", k, x[k], ib, b));

        // only modify x[k] if ib == 0 (which means lo < b < hi)
        if (ib == 0)
        {
            double delta_xk;
            double ak = (a) ? a[k] : 1;
            if (grad[k] > 0.0)
            {
                // decrease x [k]
                delta_xk = (b - lo) / ak; // note that delta_xk > 0
                if (delta_xk < x[k])
                {
                    // x [k] decreases by delta_xk but does not hit zero
                    // b hits the lower bound, lo
                    ib = -1;
                    b  = lo;
                    x[k] -= delta_xk;
                    //--- keep k in the FreeSet
                    FreeSet_list[kfree2++] = k;
                }
                else
                {
                    // x [k] hits lower bound of zero
                    // b does not hit lo; still between lower and upper bound
                    delta_xk          = x[k];
                    x[k]              = 0.;
                    FreeSet_status[k] = -1;
                    b -= delta_xk * ak;
                    //--- remove k from the FreeSet by not incrementing kfree2
                }
            }
            else
            {
                // increase x [k]
                delta_xk = (b - hi) / ak; // note that delta_xk < 0
                if (delta_xk < x[k] - 1.)
                {
                    // x [k] hits upper bound of one
                    // b does not reach hi; still between lower and upper bound
                    delta_xk          = x[k] - 1.;
                    x[k]              = 1.;
                    FreeSet_status[k] = +1;
                    b -= delta_xk * ak;
                    //--- remove k from the FreeSet by not incrementing kfree2
                }
                else
                {
                    // x [k] increases by -delta_xk but does not hit one
                    // b hits the upper bound, hi.
                    ib = +1;
                    b  = hi;
                    x[k] -= delta_xk;
                    //--- keep k in the FreeSet
                    FreeSet_list[kfree2++] = k;
                }
            }
            // x [k] has dropped by delta_xk, so update the gradient
            for (Int p = Ep[k]; p < Ep[k + 1]; p++)
            {
                grad[Ei[p]] += delta_xk * ((Ex) ? Ex[p] : 1);
            }
            grad[k] += delta_xk * D[k];
        }
        else
        {
            // b is at lo or hi and thus x [k] is not changed.
            // Once this happens, the remainder of this loop does this next
            // step only, and no further changes are made to x and the FreeSet.
            //--- keep k in the FreeSet
            FreeSet_list[kfree2++] = k;
        }
    }

    // update the size of the FreeSet, after pruning
    nFreeSet = kfree2;

    /* ---------------------------------------------------------------------- */
    /* Step 2. Examine flips of x_k from 0 to 1 or from 1 to 0 */
    /* ---------------------------------------------------------------------- */

    PR(("Boundary step 2:\n"));

    for (Int k = 0; k < n; k++)
    {
        Int FreeSet_status_k = FreeSet_status[k];
        if (FreeSet_status_k == 0)
        {
            // k is in FreeSet so it cannot be simply flipped 0->1 or 1->0
            continue;
        }

        // k not in FreeSet, so no changes here to FreeSet

        double ak = (a) ? a[k] : 1;
        if (FreeSet_status_k > 0) /* try changing x_k from 1 to 0 */
        {
            if (b - ak >= lo)
            {
                if (0.5 * D[k] + grad[k] >= 0) /* flip lowers cost */
                {
                    b -= ak;
                    ib                = (b <= lo ? -1 : 0);
                    x[k]              = 0.0;
                    FreeSet_status[k] = -1;
                }
            }
        }
        else /* try changing x_k from 0 to 1 */
        {
            if (b + ak <= hi)
            {
                if (grad[k] - 0.5 * D[k] <= 0) /* flip lowers cost */
                {
                    b += ak;
                    ib                = (b >= hi ? 1 : 0);
                    x[k]              = 1.0;
                    FreeSet_status[k] = +1;
                }
            }
        }

        if (FreeSet_status_k != FreeSet_status[k])
        {
            if (FreeSet_status_k == 1) /* x [k] was 1, now it is 0 */
            {
                for (Int p = Ep[k]; p < Ep[k + 1]; p++)
                {
                    grad[Ei[p]] += (Ex) ? Ex[p] : 1;
                }
                grad[k] += D[k];
            }
            else /* x [k] was 0, now it is 1 */
            {
                for (Int p = Ep[k]; p < Ep[k + 1]; p++)
                {
                    grad[Ei[p]] -= (Ex) ? Ex[p] : 1;
                }
                grad[k] -= D[k];
            }
        }
        // DEBUG (QPcheckCom (graph, options, QP, 1, nFreeSet, b)) ;         //
        // check b
    }

    /* ---------------------------------------------------------------------- */
    // quick return if FreeSet is now empty
    /* ---------------------------------------------------------------------- */

    if (nFreeSet == 0)
    {
        PR(("Boundary quick: ib %ld lo %g b %g hi %g b-lo %g hi-b %g\n", ib, lo,
            b, hi, b - lo, hi - b));
        QP->nFreeSet = nFreeSet;
        QP->b        = b;
        QP->ib       = ib;
        PR(("------- QPBoundary end ]\n"));
        return;
    }

    /* ---------------------------------------------------------------------- */
    /* Step 3. Search for a_{ij} = 0 in the free index set */
    /* ---------------------------------------------------------------------- */

    // look for where both i and j are in the FreeSet,
    // but i and j are not adjacent in the graph G.

    DEBUG(FreeSet_dump("step 3", n, FreeSet_list, nFreeSet, FreeSet_status, 0,
                       x));

    // for each j in FreeSet, except for the last one
    for (Int jfree = 0; jfree < nFreeSet - 1; jfree++)
    {

        // get j from the FreeSet
        Int j = FreeSet_list[jfree];
        if (j == EMPTY)
        {
            // j has already been deleted, skip it
            continue;
        }

        /* -------------------------------------------------------------- */
        /* find i and j both free and where a_{ij} = 0 */
        /* -------------------------------------------------------------- */

        // mark all vertices i adjacent to j in the FreeSet
        for (Int p = Ep[j]; p < Ep[j + 1]; p++)
        {
            Int i = Ei[p];
            ASSERT(i != j); // graph has no self edges
            graph->mark(i);
        }
        graph->mark(j);

        // for each i that follows after j in the FreeSet
        for (Int ifree = jfree + 1; ifree < nFreeSet; ifree++)
        {

            // get i from the FreeSet
            Int i = FreeSet_list[ifree];
            if (i == EMPTY)
            {
                // i has already been deleted it; skip it
                continue;
            }

            if (!graph->isMarked(i))
            {
                // vertex i is not adjacent to j in the graph G
                double aj = (a) ? a[j] : 1;
                double ai = (a) ? a[i] : 1;
                double xi = x[i];
                double xj = x[j];

                /* cost change if x_j increases dx_j = s/a_j, dx_i = s/a_i */
                double s;
                Int bind1, bind2;
                if (aj * (1. - xj) < ai * xi) // x_j hits upper bound
                {
                    s     = aj * (1. - xj);
                    bind1 = 1;
                }
                else /* x_i hits lower bound */
                {
                    s     = ai * xi;
                    bind1 = 0;
                }
                double dxj = s / aj;
                double dxi = -s / ai;
                double c1  = (grad[j] - .5 * D[j] * dxj) * dxj
                            + (grad[i] - .5 * D[i] * dxi) * dxi;

                /* cost change if x_j decreases dx_j = s/a_j, dx_i = s/a_i */
                if (aj * xj < ai * (1. - xi)) // x_j hits lower bound
                {
                    s     = -aj * xj;
                    bind2 = -1;
                }
                else /* x_i hits upper bound */
                {
                    s     = -ai * (1. - xi);
                    bind2 = 0;
                }
                dxj       = s / aj;
                dxi       = -s / ai;
                double c2 = (grad[j] - 0.5 * D[j] * dxj) * dxj
                            + (grad[i] - 0.5 * D[i] * dxi) * dxi;

                Int new_FreeSet_status;
                if (c1 < c2) /* increase x_j */
                {
                    if (bind1 == 1)
                    {
                        // j is bound (not i) and x_j becomes 1
                        dxj  = 1. - xj;
                        dxi  = -aj * dxj / ai;
                        x[j] = 1.;
                        x[i] += dxi;
                        new_FreeSet_status = +1; /* j is bound at 1 */
                    }
                    else // bind1 is zero
                    {
                        // i is bound (not j) and x_i becomes 0
                        dxi  = -xi;
                        dxj  = -ai * dxi / aj;
                        x[i] = 0.;
                        x[j] += dxj;
                        new_FreeSet_status = -1; /* i is bound at 0 */
                    }
                }
                else
                {
                    if (bind2 == -1)
                    {
                        // j is bound (not i) and x_j becomes 0
                        bind1 = 1;
                        x[j]  = 0.;
                        x[i] += dxi;
                        new_FreeSet_status = -1; /* j is bound at 0 */
                    }
                    else /* x_i = 1 */
                    {
                        // i is bound (not j) and x_i becomes 1
                        bind1 = 0;
                        x[i]  = 1;
                        x[j] += dxj;
                        new_FreeSet_status = +1; /* i is bound at 1 */
                    }
                }

                for (Int p = Ep[j]; p < Ep[j + 1]; p++)
                {
                    grad[Ei[p]] -= ((Ex) ? Ex[p] : 1) * dxj;
                }
                for (Int p = Ep[i]; p < Ep[i + 1]; p++)
                {
                    grad[Ei[p]] -= ((Ex) ? Ex[p] : 1) * dxi;
                }
                grad[j] -= D[j] * dxj;
                grad[i] -= D[i] * dxi;

                // Remove either i or j from the FreeSet.  Note that it
                // is possible for both x[i] and x[j] to reach their bounds
                // at the same time.  Only one is removed from the FreeSet;
                // the other will be removed later.

                if (bind1)
                {
                    // remove j from the FreeSet by setting its place to EMPTY
                    PR(("(b1):remove j = %ld from the FreeSet\n", j));
                    ASSERT(j == FreeSet_list[jfree]);
                    ASSERT(FreeSet_status[j] == 0);
                    FreeSet_list[jfree] = EMPTY;
                    FreeSet_status[j]   = new_FreeSet_status;
                    ASSERT(FreeSet_status[j] != 0);
                    //---
                    // no longer consider j, so skip all of remainder of i loop
                    break;
                }
                else
                {
                    // remove i from the FreeSet by setting its place to EMPTY
                    PR(("(b2):remove i = %ld from the FreeSet\n", i));
                    ASSERT(i == FreeSet_list[ifree]);
                    ASSERT(FreeSet_status[i] == 0);
                    FreeSet_list[ifree] = EMPTY;
                    FreeSet_status[i]   = new_FreeSet_status;
                    ASSERT(FreeSet_status[i] != 0);
                    //---
                    // keep j, and consider it with the next i
                    continue;
                }
            }
        }

        // clear the marks from all the vertices
        graph->clearMarkArray();
    }

    // remove deleted vertices from the FreeSet
    kfree2 = 0;
    for (Int kfree = 0; kfree < nFreeSet; kfree++)
    {
        Int k = FreeSet_list[kfree];
        if (k != EMPTY)
        {
            // keep k in the FreeSet
            FreeSet_list[kfree2++] = k;
            ASSERT(0 <= k && k < n);
            ASSERT(FreeSet_status[k] == 0);
        }
    }
    nFreeSet = kfree2;

    DEBUG(FreeSet_dump("step 3 done", n, FreeSet_list, nFreeSet, FreeSet_status,
                       1, x));

    DEBUG(QPcheckCom(graph, options, QP, 1, nFreeSet, b)); // check b

#ifndef NDEBUG
    // the vertices in the FreeSet now form a single clique.  Check this.
    // this test is for debug mode only
    ASSERT(nFreeSet >= 1); // we can have 1 or more vertices still in FreeSet
    for (Int kfree = 0; kfree < nFreeSet; kfree++)
    {
        // j must be adjacent to all other vertices in the FreeSet
        Int j               = FreeSet_list[kfree];
        Int nfree_neighbors = 0;
        for (Int p = Ep[j]; p < Ep[j + 1]; p++)
        {
            Int i = Ei[p];
            ASSERT(i != j);
            if (FreeSet_status[i] == 0)
                nfree_neighbors++;
        }
        ASSERT(nfree_neighbors == nFreeSet - 1);
    }
#endif

    /* ---------------------------------------------------------------------- */
    /* Step 4. dxj = s/aj, dxi = -s/ai, choose s with g_j dxj + g_i dxi <= 0 */
    /* ---------------------------------------------------------------------- */

    DEBUG(FreeSet_dump("step 4 starts", n, FreeSet_list, nFreeSet,
                       FreeSet_status, 0, x));

    // consider pairs of vertices in the FreeSet, until only one is left
    while (nFreeSet > 1)
    {
        /* free variables: 0 < x_j < 1 */
        /* choose s so that first derivative terms decrease */

        // i and j are the last two vertex in the FreeSet_list, as in:
        // FreeSet_list = [ .... i j ]
        // at the end of this iteration, one will be deleted, thus becoming
        // FreeSet_list = [ .... j ]
        // or
        // FreeSet_list = [ .... i ]
        Int j = FreeSet_list[nFreeSet - 1];
        ASSERT(FreeSet_status[j] == 0);
        Int i = FreeSet_list[nFreeSet - 2];
        ASSERT(FreeSet_status[i] == 0);

        double ai = (a) ? a[i] : 1;
        double aj = (a) ? a[j] : 1;
        double xi = x[i];
        double xj = x[j];

        Int new_FreeSet_status;
        Int bind1;
        double dxj, dxi, s = grad[j] / aj - grad[i] / ai;

        if (s < 0.) /* increase x_j */
        {
            if (aj * (1. - xj) < ai * xi) /* x_j hits upper bound */
            {
                dxj  = 1. - xj;
                dxi  = -aj * dxj / ai;
                x[j] = 1.;
                x[i] += dxi;
                new_FreeSet_status = +1;
                bind1              = 1; /* x_j is bound at 1 */
            }
            else /* x_i hits lower bound */
            {
                dxi  = -xi;
                dxj  = -ai * dxi / aj;
                x[i] = 0.;
                x[j] += dxj;
                new_FreeSet_status = -1;
                bind1              = 0; /* x_i is bound at 0 */
            }
        }
        else /* decrease x_j */
        {
            if (aj * xj < ai * (1. - xi)) /* x_j hits lower bound */
            {
                dxj  = -xj;
                dxi  = -aj * dxj / ai;
                x[j] = 0;
                x[i] += dxi;
                new_FreeSet_status = -1;
                bind1              = 1; /* x_j is bound */
            }
            else /* x_i hits upper bound */
            {
                dxi  = 1 - xi;
                dxj  = -ai * dxi / aj;
                x[i] = 1;
                x[j] += dxj;
                new_FreeSet_status = +1;
                bind1              = 0; /* x_i is bound */
            }
        }

        for (Int k = Ep[j]; k < Ep[j + 1]; k++)
        {
            grad[Ei[k]] -= ((Ex) ? Ex[k] : 1) * dxj;
        }
        for (Int k = Ep[i]; k < Ep[i + 1]; k++)
        {
            grad[Ei[k]] -= ((Ex) ? Ex[k] : 1) * dxi;
        }
        grad[j] -= D[j] * dxj;
        grad[i] -= D[i] * dxi;

        // ---------------------------------------------------------------------
        // the following 2 cases define the next j in the iteration:
        // ---------------------------------------------------------------------

        // Remove either i or j from the FreeSet.  Note that it is possible for
        // both x[i] and x[j] to reach their bounds at the same time.  Only one
        // is removed from the FreeSet; the other will be removed later.

        if (bind1)
        {
            // j is bound.
            // remove j from the FreeSet, and keep i.  The FreeSet_list was
            // FreeSet_list = [ .... i j ] becomes FreeSet_list = [ .... i ]
            PR(("(b3):remove j = %ld from the FreeSet\n", j));
            ASSERT(FreeSet_status[j] == 0);
            FreeSet_status[j] = new_FreeSet_status;
            ASSERT(FreeSet_status[j] != 0);
        }
        else
        {
            // i is bound.
            // remove i from the FreeSet, and keep j.  The FreeSet_list was
            // FreeSet_list = [ .... i j ] becomes FreeSet_list = [ .... j ]
            PR(("(b4):remove i = %ld from the FreeSet\n", i));
            ASSERT(FreeSet_status[i] == 0);
            FreeSet_status[i] = new_FreeSet_status;
            ASSERT(FreeSet_status[i] != 0);
            // shift j down by one in the list, thus discarding j.
            ASSERT(FreeSet_list[nFreeSet - 2] == i);
            FreeSet_list[nFreeSet - 2] = j;
        }

        // one fewer vertex in the FreeSet (i or j removed)
        nFreeSet--;

        DEBUG(FreeSet_dump("step 4", n, FreeSet_list, nFreeSet, FreeSet_status,
                           0, x));
        DEBUG(QPcheckCom(graph, options, QP, 1, nFreeSet, b)); // check b
    }

    DEBUG(FreeSet_dump("wrapup", n, FreeSet_list, nFreeSet, FreeSet_status, 0,
                       x));

    /* ---------------------------------------------------------------------- */
    /* step 5: at most one free variable remaining */
    /* ---------------------------------------------------------------------- */

    ASSERT(nFreeSet == 0 || nFreeSet == 1);

    PR(("Step 5: ib %ld lo %g b %g hi %g b-lo %g hi-b %g\n", ib, lo, b, hi,
        b - lo, hi - b));

    if (nFreeSet == 1) /* j is free, optimize over x [j] */
    {
        // j is the first and only item in the FreeSet
        Int j = FreeSet_list[0];
        PR(("ONE AND ONLY!! j = %ld x[j] %g\n", j, x[j]));

        Int bind1  = 0;
        double aj  = (a) ? a[j] : 1;
        double dxj = (hi - b) / aj;
        PR(("dxj %g  x[j] %g  (1-x[j]): %g\n", dxj, x[j], 1 - x[j]));
        if (dxj < 1. - x[j])
        {
            bind1 = 1;
        }
        else
        {
            dxj = 1. - x[j];
        }

        Int bind2  = 0;
        double dxi = (lo - b) / aj;
        PR(("dxi %g  x[j] %g  (-x[j]): %g\n", dxi, x[j], -x[j]));
        if (dxi > -x[j])
        {
            bind2 = 1;
        }
        else
        {
            dxi = -x[j];
        }

        double c1 = (grad[j] - 0.5 * D[j] * dxj) * dxj;
        double c2 = (grad[j] - 0.5 * D[j] * dxi) * dxi;
        if (c1 <= c2) /* x [j] += dxj */
        {
            if (bind1)
            {
                PR(("bind1: xj changes from %g", x[j]));
                x[j] += dxj;
                PR((" to %g, b now at hi\n", x[j]));
                ib = +1;
                b  = hi;
            }
            else
            {
                x[j] = 1.;
                b += dxj * aj;
                /// remove j from the FreeSet, which is now empty
                PR(("(b5):remove j = %ld from FreeSet, now empty\n", j));
                ASSERT(FreeSet_status[j] == 0);
                FreeSet_status[j] = 1;
                ASSERT(FreeSet_status[j] != 0);
                nFreeSet--;
                ASSERT(nFreeSet == 0);
            }
        }
        else /* x [j] += dxi */
        {
            dxj = dxi;
            if (bind2)
            {
                PR(("bind2: xj changes from %g", x[j]));
                x[j] += dxj;
                PR((" to %g, b now at lo\n", x[j]));
                ib = -1;
                b  = lo;
            }
            else
            {
                x[j] = 0.;
                b += dxj * aj;
                /// remove j from the FreeSet, which is now empty
                PR(("(b6):remove j = %ld from FreeSet, now empty\n", j));
                ASSERT(FreeSet_status[j] == 0);
                FreeSet_status[j] = -1;
                ASSERT(FreeSet_status[j] != 0);
                nFreeSet--;
                ASSERT(nFreeSet == 0);
            }
        }

        if (dxj != 0.)
        {
            for (Int p = Ep[j]; p < Ep[j + 1]; p++)
            {
                grad[Ei[p]] -= ((Ex) ? Ex[p] : 1) * dxj;
            }
            grad[j] -= D[j] * dxj;
        }
    }

    /* ---------------------------------------------------------------------- */
    // wrapup
    /* ---------------------------------------------------------------------- */

    PR(("QBboundary, done:\n"));
    DEBUG(FreeSet_dump("QPBoundary: done ", n, FreeSet_list, nFreeSet,
                       FreeSet_status, 0, x));
    ASSERT(nFreeSet == 0 || nFreeSet == 1);
    PR(("Boundary done: ib %ld lo %g b %g hi %g b-lo %g hi-b %g\n", ib, lo, b,
        hi, b - lo, hi - b));

    QP->nFreeSet = nFreeSet;
    QP->b        = b;
    QP->ib       = ib;

    // clear the marks from all the vertices
    graph->clearMarkArray();

    DEBUG(QPcheckCom(graph, options, QP, 1, nFreeSet, b)); // check b
    PR(("----- QPBoundary end ]\n"));
}

} // end namespace Mongoose