File: clique1.f

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (887 lines) | stat: -rw-r--r-- 64,124 bytes parent folder | download | duplicates (4)
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
c--------------------------------------------------------------------------
c     max clique algorithm based on quadratic zero-one programming
c     This program is pardalos4 from Panos M. Pardalos & Gregory P. Rodgers
c     described in "A branch and bound algorithm for the maximum clique 
c     problem", Operations Research, Vol. 19, No. 5 (1992), pp. 363-375.
c--------------------------------------------------------------------------
      subroutine clique1(n,m,m2,np1,nwk,kat,hat,wcl,wk,x)
      integer hat,kat,wk,wcl
      logical x                                                         
      dimension hat(m2),kat(np1),x(n),wk(nwk),wcl(n)
      nprocs=1
      do 1,i=1,nwk
         wcl(i)=0
 1    continue
c     heuristic phase using the greedy method                           
      call greedy(n,hat,kat,maxc,x,wk,wk(n+1),wk((2*n)+1),              
     >            wk((3*n)+1) )                                         
c     branch and bound phase                                            
      ihsol= maxc                                                       
      if (nprocs.ne.0)                                                  
     >call bbnd(n,hat,kat,x,maxc,wk,nwk,nsubp,nsubpg,nprocs,nchg)     
c heuristic size  = ihsol ;  maximum size    = maxc                       
c number of min chages   = nchg ; number of subproblems  = nsubp       
c minimizer subproblem   = nsubpg                     
      iptr = 0                                                          
      do 10 i=1,n
         wcl(i)=0
         if (x(i)) then                                                 
            iptr = iptr+1                                               
            wcl(iptr) = i                                                
         endif                                                          
10    continue                                                          
      return
      end                                                               
c= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
      subroutine greedy(n,hat,kat,num1s,x,degfre,free,p,degfx1)         
      integer    n,hat(*),kat(*),num1s,degfre(n),p(n),degfx1(n)         
      logical    x(n),free(n),value                                     
c                                                                       
      imax = 1                                                          
      do 10 i=1,n                                                       
         p(i) = i                                                       
         degfre(i) = kat(i+1)-kat(i)                                    
         if (degfre(i).gt.degfre(imax)) imax= i                         
         degfx1(i) = 0                                                  
         free(i) = .true.                                               
10    continue                                                          
c     put the highest connected node into the greedy            
      num1s      = 0                                                    
      x(imax)    = .true.                                               
      p(1)       = imax                                                 
      p(imax)    = 1                                                    
      free(imax) = .false.                                              
c                                                                       
      lev  = 1                                                          
20    continue                                                          
         levp1 = lev+1                                                  
c    --- update the vectors degfre, degfx1, and num1s                   
         lp = p(lev)                                                    
c        lp was the last variable that was fixed                        
         if (x(lp)) then                                                
            num1s = num1s+1                                             
            do 70 k=kat(lp),kat(lp+1)-1                                 
               j = hat(k)                                               
               if (free(j)) then                                        
                  degfre(j)  = degfre(j)  - 1                           
                  degfx1(j)  = degfx1(j)  + 1                           
               endif                                                    
70          continue                                                    
         else                                                           
c           last vertex was thrown out so dont bother with degfx1       
            do 90 k=kat(lp),kat(lp+1)-1                                 
               degfre(hat(k)) = degfre(hat(k)) - 1                      
90          continue                                                    
         endif                                                          
         if (lev.eq.n) goto 1000                                        
c                                                                       
c    --- determine if any variable can be fixed                         
         do 130 i=levp1,n                                               
            ip = p(i)                                                   
c                                                                       
c           two tests to see if node ip can be fixed                    
c                                                                       
c       -1- is it disconnected to a node currently fixed to one?        
            if (degfx1(ip).lt.num1s) then                               
               value = .false.                                          
               ivar = i                                                 
               goto 150                                                 
            endif                                                       
c                                                                       
c       -2- after the 1st test we know ip is connected to               
c           all nodes fixed to one.  so now we can test if it           
c           is connected to all remaining free nodes.  if it is         
c           then fix ip to 1                                            
c mg            if (degfre(ip).ge.n-levp) then                              
            if (degfre(ip).ge.n-levp1) then                              
               value = .true.                                           
               ivar = i                                                 
               goto 150                                                 
            endif                                                       
c                                                                       
130      continue                                                       
c                                                                       
c    --- none could be forced so find variable with highest degree      
         ivar   = levp1                                                 
         do 140 i=levp1+1,n                                             
            if (degfre(p(i)).gt.degfre(p(ivar))) ivar=i                 
               ivar = i                                                 
140      continue                                                       
c                                                                       
c    --- set to one and stack the subproblem with x(ip) = 0             
         value = .true.                                                 
c                                                                       
c    --- ivar is new fixed variable, increase lev & swap in p           
150      lev      = levp1                                               
         it       = p(ivar)                                             
         p(ivar)  = p(lev)                                              
         p(lev)   = it                                                  
         x(it)    = value                                               
         free(it) = .false.                                             
         goto 20                                                        
1000  continue                                                          
      return                                                            
c     end greedy                                                        
      end                                                               
c**************************************************************************
      subroutine copyl(n,lfrom,lto)                                     
      logical lfrom(n),lto(n)                                           
      integer n                                                         
      do 10 i=1,n                                                       
         lto(i) = lfrom(i)                                              
10    continue                                                          
      return                                                            
c     end of copyl                                                      
      end                                                               
c**************************************************************************
      subroutine bbnd(n, hat,kat,xmin,maxc,iwk,nwk,nsubp,nsubpg,nprocs,
     >   nchg)                                                          
      integer n,nwk,hat(*),kat(*),maxc,iwk(nwk),nsubp,nsubpg,nprocs     
      logical xmin(n)                                                   
c     local variables                                                   
      integer csubp,cycles                                              
      integer nbusy,nfree,busy(6),free(6),strwk(6),split(6)             
      integer ldx,ldf,lstk,lp,lx,lfr,lxm,ltop,llev,lmin,lnsb,lnsg,lnwk, 
     >        lend                                                      
      integer nsplit,ipt,iptz,ipty,imin,imax,minspl,wsize,y,z,i,minsb   
      logical prempt,newmax                                             
c   --- initialize statistics                                           
      nchg   = 0                                                        
      nsubp  = 0                                                        
      nsubpg = 0                                                        
      csubp  = 0                                                        
      cycles = 0                                                        
c   --- initilize the mininum # of subproblems to solve in each cycle   
      minsb = 800                                                       
c   --- divide workspace among nprocs processors                        
      wsize = nwk/nprocs                                                
      strwk(1) = 0                                                      
      do 10 i=2,nprocs                                                  
         strwk(i) = strwk(i-1)+wsize                                    
10    continue                                                          
c   --- set up storage pointers                                         
      ldx =  1                                                          
      ldf =  ldx + (   n    )                                           
      lstk=  ldf + (   n    )                                           
      lp  = lstk + ( 3*(n+1))                                           
      lx  =   lp + (   n    )                                           
      lfr =   lx + (   n    )                                           
      lxm =  lfr + (   n    )                                           
      ltop=  lxm + (   n    )                                           
      llev= ltop + (   1    )                                           
      lmin= llev + (   1    )                                           
      lnsb= lmin + (   1    )                                           
      lnsg= lnsb + (   1    )                                           
      lnwk= lnsg + (   1    )                                           
      lend= lnwk + (   1    )                                           
      if (lend.ge.wsize) then                                           
c ww lend*nprocs,nwk                                   
         return                                                         
      endif                                                             
c   --- initialize subproblem 1                                         
      do 20 i=1,n                                                       
         iwk(lp+i-1)= i                                                 
20    continue                                                          
      iwk(ltop)=  1                                                     
      iwk(lstk)= -1                                                     
      iwk(llev)=  0                                                     
      iwk(lnwk)=  0                                                     
      iwk(lmin)= maxc                                                   
c   --- set up busy and free lists                                      
      nbusy     = 1                                                     
      busy(1)   = 1                                                     
      nfree     = nprocs-1                                              
      do 30 i=1,nfree                                                   
         free(i) = i+1                                                  
30    continue                                                          
c-----------------------------------------------------------------------
c     split subproblems till all procs are marked busy                  
c-----------------------------------------------------------------------
c     while (nbusy.lt.nprocs.and.nbusy.gt.0)                            
1000  if (nbusy.lt.nprocs.and.nbusy.gt.0) then                          
c        find all the split points in the nbusy subproblems             
         nsplit=0                                                       
         do 40 i=1,nbusy                                                
            ipt = strwk(busy(i))                                        
c           is the top of the stack greater than 1?                     
            if (iwk(ipt+ltop).gt.1) then                                
c              get the level from the top element of the stack          
               split(i) = iwk(ipt + lstk + 3)                           
               nsplit   = nsplit  + 1                                   
            else                                                        
               split(i) = 0                                             
            endif                                                       
40       continue                                                       
c                                                                       
         if (nsplit.eq.0) then                                          
c           if there are no problems to split then force splitting      
c           of the first process in the busy list by (re)starting       
c           the branch and bound algorithm                              
50          ipt = strwk(busy(1))                                        
            call bbspl1(n,hat,kat,wsize,iwk(ipt+1),nchg)                
            nsubp = nsubp + iwk(ipt+lnsb)                               
            csubp = csubp + iwk(ipt+lnsb)                               
c           did bbspl1 find a new minimum ?                             
            if (iwk(ipt+lmin).gt.maxc) then                             
               maxc = iwk(ipt+lmin)                                     
               call copyl(n,iwk(ipt+lxm),xmin)                          
               do 60 y=1,nprocs                                         
                  iwk(strwk(y)+lmin) = maxc                             
60             continue                                                 
               nsubpg = csubp + iwk(ipt+lnsg)                           
            endif                                                       
c           did bbspl1 solve the problem ?                              
            if (iwk(ipt+llev).eq.-1) then                               
c              attempted split solved the problem                       
c              take y off busy list and add it to the free list         
               nfree = nfree + 1                                        
               nbusy = nbusy - 1                                        
               free(nfree) = busy(1)                                    
               do 70  i=1,nbusy                                         
                  busy(i) = busy(i+1)                                   
70             continue                                                 
c              go back and try to split another subproblem              
               if (nbusy.gt.0) goto 50                                  
            else                                                        
c              no, then a new split point was found                     
               split(1) = iwk(ipt + lstk + 3)                           
               nsplit = 1                                               
            endif                                                       
         endif                                                          
c                                                                       
         if (nbusy.eq.0) goto 3000                                      
c        nsplit problems are now splitable                              
c        find lowest splitpoint (highest pt in tree) and split          
         imin   = 0                                                     
         minspl = n+1                                                   
c        examine splitpoints of all busy subproblems                    
         do 80 i=1,nbusy                                                
            if (split(i).ne.0.and.split(i).lt.minspl) then              
               imin = i                                                 
               minspl = split(imin)                                     
            endif                                                       
80       continue                                                       
c      --- now split the process denoted by busy(imin)                  
         y = busy(imin)                                                 
         z = free(nfree)                                                
         ipty = strwk(y)                                                
         iptz = strwk(z)                                                
c        take z off free list and add it to the busy list               
         nfree = nfree - 1                                              
         nbusy = nbusy + 1                                              
         busy(nbusy) = z                                                
c      --- initialize subproblem z from subproblem y                    
c        permutation vector p                                           
         do 90 i=1,n                                                    
            iwk((iptz+lp-1)+i)= iwk((ipty+lp-1)+i)                      
90       continue                                                       
c        initialize top of stack and stack as empty                     
         iwk(iptz+ltop) =  1                                            
         iwk(iptz+lstk) = -1                                            
         iwk(iptz+lnwk) =  0                                            
c        level of execution                                             
         iwk(iptz+llev) =  split(imin)                                  
c        incumbent                                                      
         iwk(iptz+lmin)= maxc                                           
c        zero-one variables x                                           
         call copyl(n,iwk(ipty+lx),iwk(iptz+lx))                        
c        change one x variable                                          
         call changl(iwk(iptz+lx),iwk(iptz+lp),split(imin))             
c        -->  subroutine changl(x,p,lev)                                
c        -->  x(p(lev)) = .not.x(p(lev))                                
c        remove subproblem from y's stack                               
         call pllstk(iwk(ipty+ltop),iwk(ipty+lstk))                     
         iwk(ipty+lnwk) =  0                                            
c        -->  subroutine pllstk(topstk,stack)                           
c        -->  integer topstk,stack(3,*)                                 
      goto 1000                                                         
      endif                                                             
c     endwhile(nbusy.lt.nprocs.and.nbusy.gt.0)                          
c-----------------------------------------------------------------------
c     solve subproblems in parallel                                     
c-----------------------------------------------------------------------
c     while (nbusy.eq.nprocs)                                           
2000  if (nbusy.eq.nprocs) then                                         
c        execute nprocs in paralel   this is called one cycle           
c        each proc will search its subproblem for maxvt vertices        
         cycles = cycles + 1                                            
         prempt = .false.                                               
         do 100 y=1,nprocs-1                                            
            ipt = strwk(y)+1                                            
c           solve subproblem y                                          
            call bbp(n,hat,kat,minsb,wsize,iwk(ipt),prempt,nchg)        
cccc        call dsptch('bbp',n,hat,kat,minsb,wsize,iwk(ipt),prempt)    
100      continue                                                       
         ipt = strwk(nprocs)+1                                          
         call bbp(n,hat,kat,minsb,wsize,iwk(ipt),prempt,nchg)           
c                                                                       
c        synchronize subproblems (wait for all to finish)               
cccc     if (nprocs.gt.1) call syncro                                   
c-----------------------------------------------------------------------
c        analysis and synchronization phase                             
c-----------------------------------------------------------------------
         nbusy  = 0                                                     
         nfree  = 0                                                     
         newmax = .false.                                               
         imax   = 0                                                     
         do 110 y=1,nprocs                                              
            ipt = strwk(y)                                              
c           iwk(ipt+lnsb)is the # of subproblems solved for this problem
            nsubp = nsubp + iwk(ipt+lnsb)                               
c           find which problem did the most to calculate critical path  
            if (iwk(ipt+lnsb).gt.imax) imax=iwk(ipt+lnsb)               
            if (iwk(ipt+lmin).gt.maxc) then                             
               nsubpg = csubp + iwk(ipt+lnsg)                           
               newmax = .true.                                          
               maxc   = iwk(ipt+lmin)                                   
               call copyl(n,iwk(ipt+lxm),xmin)                          
            endif                                                       
            if (iwk(ipt+llev).ne.-1) then                               
               nbusy = nbusy + 1                                        
               busy(nbusy) = y                                          
            else                                                        
               nfree = nfree + 1                                        
               free(nfree) = y                                          
            endif                                                       
110      continue                                                       
c        keep track of critical path count of subproblems               
         csubp = csubp + imax                                           
c        update new bound if one was found                              
         if (newmax) then                                               
            do 120 y=1,nprocs                                           
               iwk(strwk(y)+lmin) = maxc                                
120         continue                                                    
         endif                                                          
      goto 2000                                                         
      endif                                                             
c     endwhile(nbusy.eq.nprocs)                                         
c                                                                       
c     repeat until all subproblems have been solved (nbusy.eq.0)        
      if (nbusy.ne.0) goto 1000                                         
c--------------------------------------------------------------------   
c     end of cyeturn to q01                                     
c--------------------------------------------------------------------   
3000  continue                                                          
c nb of cycles for solving the problem = cycles
c critical path subproblem count = csubp              
200   format(' not enough workspace:',i8,' needed',i8,' specified')     
      return                                                            
c                                                                       
c     end of                                                    
      end                                                               
c**************************************************************************
      subroutine changl(x,p,lev)                                        
c     change the zero-one value in x(p(lev))                            
      integer p(*),lev                                                  
      logical x(*)                                                      
      x(p(lev)) = .not.x(p(lev))                                        
      return                                                            
      end                                                               
c**************************************************************************
      subroutine pllstk(topstk,stack)                                   
c     pull the top most element off of the stack                        
      integer topstk,stack(3,*)                                         
      topstk = topstk-1                                                 
c     -1 is at top of stack.  do not touch it                           
      do 10 i=2,topstk                                                  
         stack(1,i) = stack(1,i+1)                                      
         stack(2,i) = stack(2,i+1)                                      
c        cause bounds to be recalculated (save synchronization time)    
         stack(3,i) = -1                                                
10    continue                                                          
      return                                                            
      end                                                               
c**************************************************************************
      subroutine bbp(n,hat,kat,minsb,nwk,iwk,prempt,nchg)               
      integer n,hat(*),kat(*),minsb,nwk,iwk(nwk)                        
      logical prempt                                                    
c     local variables                                                   
      integer maxwk                                                     
c   --- set up storage pointers                                         
      ldx =  1                                                          
      ldf =  ldx + (   n    )                                           
      lstk=  ldf + (   n    )                                           
      lp  = lstk + ( 3*(n+1))                                           
      lx  =   lp + (   n    )                                           
      lfr =   lx + (   n    )                                           
      lxm =  lfr + (   n    )                                           
      ltop=  lxm + (   n    )                                           
      llev= ltop + (   1    )                                           
      lmin= llev + (   1    )                                           
      lnsb= lmin + (   1    )                                           
      lnsg= lnsb + (   1    )                                           
      lnwk= lnsg + (   1    )                                           
      lend= lnwk + (   1    )                                           
      maxwk = nwk-lend                                                  
      call bb(n,hat,kat,minsb,maxwk,                                    
     >    iwk(ldx),iwk(ldf),iwk(lstk),iwk(lp),iwk(lx),                  
     >    iwk(lfr),iwk(lxm),                                            
     >    iwk(ltop),iwk(llev),iwk(lmin),iwk(lnsb),iwk(lnsg),iwk(lnwk),  
     >    iwk(lend),prempt,nchg)                                        
      return                                                            
c     end of bbp                                                        
      end                                                               
cprocess vector(report) directive('*vdir:')                             
c**************************************************************************
      subroutine bb(n,hat,kat,maxs,maxwk,degfx1,degfre,stack,p,x,             
     > free,xmin,topstk,lev,maxc,nsubp,nsubpg,endwk,iwk,prempt,nchg) 
c  parameters                                                           
      integer n,maxs,maxwk,topstk,lev,maxc,nsubp,nsubpg,endwk           
      integer hat(*),kat(*),degfx1(n),degfre(n),stack(3,n),p(n),        
     >        iwk(maxwk)                                                
      logical x(n),xmin(n),free(n),prempt                               
c  local variables                                                      
      integer g,levp1,ip,lp,i,j,it,dgneed,num1s,nmlevp                  
      integer nfree,newewk,itemp,iptr,ivar                              
      logical uselst,value                                              
c*vdir:  prefer scalar on                                               
      nsubp  = 0                                                        
      nsubpg = 0                                                        
      uselst = .false.                                                  
c     calculate the upper bound on maxc, g                              
      g = n                                                             
      do 10 i=1,lev                                                     
         ip = p(i)                                                      
         free(ip) = .false.                                             
         if(.not.x(ip)) g = g-1                                         
10    continue                                                          
      do 20 i=lev+1,n                                                   
         free(p(i)) = .true.                                            
20    continue                                                          
                                                                        
30    if (lev.eq.-1.or.(nsubp.ge.maxs.and.prempt)) goto 1000            
c                                                                       
         levp1  = lev    + 1                                            
c                                                                       
         if (g.le.maxc.or.lev.eq.n) then                                
c                                                                       
c           terminal node found                                         
c                                                                       
c         --- check to see if it was a leaf                             
            if (g.gt.maxc) then                                         
               maxc = g                                                 
               do 40 j=1,n                                              
                  xmin(j) = x(j)                                        
40             continue                                                 
               nsubpg = nsubp+1                                         
               nchg = nchg+1                                            
            endif                                                       
c                                                                       
c         --- get new subproblem from the stack                         
            lev    = stack(1,topstk)                                    
            g      = stack(2,topstk)                                    
            itemp  = stack(3,topstk)                                    
            topstk = topstk - 1                                         
            if (lev.ne.-1) then                                         
               x(p(lev)) = (.not.x(p(lev)))                             
               do 50 i=lev+1,n                                          
                  free(p(i)) = .true.                                   
50             continue                                                 
c         ---  get degree counts from storage if there was enough room  
               if (itemp.ne.-1) then                                    
                  endwk = itemp                                         
                  itemp = endwk + (n-lev)                               
                  iptr = 0                                              
                  do 55 i=1,n                                           
                     if (free(i)) then                                  
                        iptr = iptr+1                                   
c                       the free list in p is now in order              
                        p(lev+iptr) = i                                 
                        degfre(i) = iwk(endwk+iptr)                     
                        degfx1(i) = iwk(itemp+iptr)                     
                     endif                                              
55                continue                                              
                  uselst = .true.                                       
               else                                                     
c                 there wasn't enough room, counts must be recalculated 
                  uselst = .false.                                      
               endif                                                    
            endif                                                       
            nsubp = nsubp + 1                                           
c                                                                       
         else                                                           
c                                                                       
c           proceed depth first                                         
c                                                                       
c         --- calculate the degree of free nodes to free nodes          
            if (uselst) then                                            
               lp = p(lev)                                              
c              lp was the last variable that was fixed                  
               if (x(lp)) then                                          
c*vdir: prefer vector                                                   
c*vdir: ignore recrdeps(degfre,degfx1)                                  
                  do 70 k=kat(lp),kat(lp+1)-1                           
                     degfre(hat(k))  = degfre(hat(k))  - 1              
                     degfx1(hat(k))  = degfx1(hat(k))  + 1              
70                continue                                              
               else                                                     
c                 last vertex was thrown out so dont bother with degfx1 
c*vdir: prefer vector                                                   
c*vdir: ignore recrdeps(degfre)                                         
                  do 90 k=kat(lp),kat(lp+1)-1                           
                     degfre(hat(k)) = degfre(hat(k)) - 1                
90                continue                                              
               endif                                                    
            else                                                        
c              calculate free node connectivity from scratch            
               do 120 i = levp1,n                                       
                  ip = p(i)                                             
                  degfre(ip) = 0                                        
                  degfx1(ip) = 0                                        
                  do 110 k=kat(ip),kat(ip+1)-1                          
                     j = hat(k)                                         
                     if (free(j)) then                                  
                        degfre(ip) = degfre(ip) + 1                     
                     else                                               
                        if (x(j)) degfx1(ip) = degfx1(ip) + 1           
                     endif                                              
110               continue                                              
120            continue                                                 
            endif                                                       
c                                                                       
c         --- determine if any variable can be fixed                    
            num1s = lev + g - n                                         
            dgneed = maxc - num1s -1                                    
            nmlevp = n - levp1                                          
            do 130 i=levp1,n                                            
               ip = p(i)                                                
c                                                                       
c              three tests to see if node ip can be fixed               
c                                                                       
c            -1- is it disconnected to a node currently fixed to one?   
               if (degfx1(ip).lt.num1s) then                            
                  value = .false.                                       
                  ivar = i                                              
                  g = g - 1                                             
                  goto 150                                              
               endif                                                    
c                                                                       
c            -2- after the 1st test we know ip is connected to          
c                all nodes fixed to one.  so now we can test if it      
c                is connected to all remaining free nodes.  if it is    
c                then fix ip to 1                                       
               if (degfre(ip).ge.nmlevp) then                           
                  value = .true.                                        
                  ivar = i                                              
                  goto 150                                              
               endif                                                    
c                                                                       
c            -3- is its total degree + 1 smaller than a known   
c              each free node needs at least degree dgneed to make      
c              a f size maxc. if not we can throw node away.    
               if (degfre(ip).le.dgneed) then                           
c                 fix to 0                                              
                  value = .false.                                       
                  g     = g - 1                                         
                  ivar  = i                                             
                  goto 150                                              
               endif                                                    
c                                                                       
130         continue                                                    
c                                                                       
c           note: at this point we know that all free nodes are         
c                 connected to all nodes fixed to one and that they     
c                 are also disconnected to some free nodes.  so         
c                 we may choose variable to branch on based on how      
c                 much they are connected or disconnected to other      
c                 free nodes.                                           
c                                                                       
c         --- none could be fixed so find variable i to branch on.      
ccc           rule1: choose node with highest free connectivity         
c             rule2: choose node with lowest free connectivity          
c             rule 2 is faster in verifying optimality and is used here 
c             rule 1 is the greedy method.                              
            ivar   = levp1                                              
            do 140 i=levp1+1,n                                          
               if (degfre(p(i)).lt.degfre(p(ivar))) ivar= i             
140         continue                                                    
c                                                                       
c         --- set to one and stack the subproblem with x(ip) = 0        
            value = .true.                                              
            if (g-1.gt.maxc) then                                       
c              only stack subproblems that are not suboptimal           
               topstk = topstk + 1                                      
               stack(1,topstk) = levp1                                  
               stack(2,topstk) = g-1                                    
               nfree = n-levp1                                          
               newewk = endwk + (nfree+nfree)                           
               if (newewk.le.maxwk) then                                
                  stack(3,topstk) = endwk                               
                  free(p(ivar)) = .false.                               
                  itemp = endwk + nfree                                 
                  iptr = 0                                              
                  do 145 i=1,n                                          
c                    note: the following requires that the free list in 
c                          the permutation p be in order                
                     if (free(i)) then                                  
                        iptr = iptr+1                                   
                        iwk(endwk+iptr) = degfre(i)                     
                        iwk(itemp+iptr) = degfx1(i)                     
                     endif                                              
145               continue                                              
                  endwk = newewk                                        
               else                                                     
                  stack(3,topstk) = -1                                  
               endif                                                    
            endif                                                       
c                                                                       
c         --- ivar is new fixed variable, increase lev & swap in p      
150         lev      = levp1                                            
            it       = p(ivar)                                          
            p(ivar)  = p(lev)                                           
            p(lev)   = it                                               
            x(it)    = value                                            
            free(it) = .false.                                          
            uselst   = .true.                                           
c                                                                       
         endif                                                          
         goto 30                                                        
c                                                                       
1000  prempt = .true.                                                   
      return                                                            
c                                                                       
c     end of bb                                                         
      end                                                               
c**************************************************************************
      subroutine bbspl1(n,hat,kat,nwk,iwk,nchg)                         
      integer n,hat(*),kat(*),nwk,iwk(nwk)                              
c   --- set up storage pointers                                         
      ldx =  1                                                          
      ldf =  ldx + (   n    )                                           
      lstk=  ldf + (   n    )                                           
      lp  = lstk + ( 3*(n+1))                                           
      lx  =   lp + (   n    )                                           
      lfr =   lx + (   n    )                                           
      lxm =  lfr + (   n    )                                           
      ltop=  lxm + (   n    )                                           
      llev= ltop + (   1    )                                           
      lmin= llev + (   1    )                                           
      lnsb= lmin + (   1    )                                           
      lnsg= lnsb + (   1    )                                           
      lnwk= lnsg + (   1    )                                           
      lend= lnwk + (   1    )                                           
      maxwk = nwk-lend                                                  
c     bbspl2 will stop after the first subproblem is stacked            
      call bbspl2(n,hat,kat,maxwk,                                      
     >    iwk(ldx),iwk(ldf),iwk(lstk),iwk(lp),iwk(lx),                  
     >    iwk(lfr),iwk(lxm),                                            
     >    iwk(ltop),iwk(llev),iwk(lmin),iwk(lnsb),iwk(lnsg),iwk(lnwk),  
     >    iwk(lend),nchg)                                               
      return                                                            
c                                                                       
c     end of bbspl1                                                     
      end                                                               
c**************************************************************************
      subroutine bbspl2(n,hat,kat,maxwk,                                
     >              degfx1,degfre,stack,p,x,free,xmin,                  
     >              topstk,lev,maxc,nsubp,nsubpg,endwk,iwk,nchg)        
c  parameters                                                           
      integer n,maxwk,topstk,lev,maxc,nsubp,nsubpg,endwk                
      integer hat(*),kat(*),degfx1(n),degfre(n),stack(3,n),p(n),        
     >        iwk(maxwk)                                                
      logical x(n),xmin(n),free(n)                                      
c  local variables                                                      
      integer g,levp1,ip,lp,i,j,it,dgneed,num1s,nmlevp                  
      integer nfree,newewk,itemp,iptr,ivar                              
      logical uselst,value                                              
c*vdir: prefer scalar on                                                
      nsubp  = 0                                                        
      nsubpg = 0                                                        
      uselst = .false.                                                  
c     calculate the upper bound on maxc, g                              
      g = n                                                             
      do 10 i=1,lev                                                     
         ip = p(i)                                                      
         free(ip) = .false.                                             
         if(.not.x(ip)) g = g-1                                         
10    continue                                                          
      do 20 i=lev+1,n                                                   
         free(p(i)) = .true.                                            
20    continue                                                          
                                                                        
30    if (lev.eq.-1.or.topstk.gt.1) goto 1000                           
c                                                                       
         levp1  = lev    + 1                                            
c                                                                       
         if (g.le.maxc.or.lev.eq.n) then                                
c                                                                       
c           terminal node found                                         
c                                                                       
c         --- check to see if it was a leaf                             
            if (g.gt.maxc) then                                         
               maxc = g                                                 
               do 40 j=1,n                                              
                  xmin(j) = x(j)                                        
40             continue                                                 
               nsubpg = nsubp+1                                         
               nchg = nchg + 1                                          
            endif                                                       
c                                                                       
c         --- get new subproblem from the stack                         
            lev    = stack(1,topstk)                                    
            g      = stack(2,topstk)                                    
            itemp  = stack(3,topstk)                                    
            topstk = topstk - 1                                         
            if (lev.ne.-1) then                                         
               x(p(lev)) = (.not.x(p(lev)))                             
               do 50 i=lev+1,n                                          
                  free(p(i)) = .true.                                   
50             continue                                                 
c         ---  get degree counts from storage if there was enough room  
               if (itemp.ne.-1) then                                    
                  endwk = itemp                                         
                  itemp = endwk + (n-lev)                               
                  iptr = 0                                              
                  do 55 i=1,n                                           
                     if (free(i)) then                                  
                        iptr = iptr+1                                   
c                       the free list in p is now in order              
                        p(lev+iptr) = i                                 
                        degfre(i) = iwk(endwk+iptr)                     
                        degfx1(i) = iwk(itemp+iptr)                     
                     endif                                              
55                continue                                              
                  uselst = .true.                                       
               else                                                     
c                 there wasn't enough room, counts must be recalculated 
                  uselst = .false.                                      
               endif                                                    
            endif                                                       
            nsubp = nsubp + 1                                           
c                                                                       
         else                                                           
c                                                                       
c           proceed depth first                                         
c                                                                       
c         --- calculate the degree of free nodes to free nodes          
            if (uselst) then                                            
               lp = p(lev)                                              
c              lp was the last variable that was fixed                  
               if (x(lp)) then                                          
c*vdir: prefer vector                                                   
c*vdir: ignore recrdeps(degfre,degfx1)                                  
                  do 70 k=kat(lp),kat(lp+1)-1                           
                     degfre(hat(k))  = degfre(hat(k))  - 1              
                     degfx1(hat(k))  = degfx1(hat(k))  + 1              
70                continue                                              
               else                                                     
c                 last vertex was thrown out so dont bother with degfx1 
c*vdir: prefer vector                                                   
c*vdir: ignore recrdeps(degfre)                                         
                  do 90 k=kat(lp),kat(lp+1)-1                           
                     degfre(hat(k)) = degfre(hat(k)) - 1                
90                continue                                              
               endif                                                    
            else                                                        
c              calculate free node connectivity from scratch            
               do 120 i = levp1,n                                       
                  ip = p(i)                                             
                  degfre(ip) = 0                                        
                  degfx1(ip) = 0                                        
                  do 110 k=kat(ip),kat(ip+1)-1                          
                     j = hat(k)                                         
                     if (free(j)) then                                  
                        degfre(ip) = degfre(ip) + 1                     
                     else                                               
                        if (x(j)) degfx1(ip) = degfx1(ip) + 1           
                     endif                                              
110               continue                                              
120            continue                                                 
            endif                                                       
c                                                                       
c         --- determine if any variable can be fixed                    
            num1s = lev + g - n                                         
            dgneed = maxc - num1s -1                                    
            nmlevp = n - levp1                                          
            do 130 i=levp1,n                                            
               ip = p(i)                                                
c                                                                       
c              three tests to see if node ip can be fixed               
c                                                                       
c            -1- is it disconnected to a node currently fixed to one?   
               if (degfx1(ip).lt.num1s) then                            
                  value = .false.                                       
                  ivar = i                                              
                  g = g - 1                                             
                  goto 150                                              
               endif                                                    
c                                                                       
c            -2- after the 1st test we know ip is connected to          
c                all nodes fixed to one.  so now we can test if it      
c                is connected to all remaining free nodes.  if it is    
c                then fix ip to 1                                       
               if (degfre(ip).ge.nmlevp) then                           
                  value = .true.                                        
                  ivar = i                                              
                  goto 150                                              
               endif                                                    
c                                                                       
c            -3- is its total degree + 1 smaller than a known   
c              each free node needs at least degree dgneed to make      
c              a f size maxc. if not we can throw node away.    
               if (degfre(ip).le.dgneed) then                           
c                 fix to 0                                              
                  value = .false.                                       
                  g     = g - 1                                         
                  ivar  = i                                             
                  goto 150                                              
               endif                                                    
c                                                                       
130         continue                                                    
c note: at this point we know that all free nodes are connected to all
c nodes connected to all nodes fixed to one and that they are also    
c disconnected to some free nodes.  so we may choose variable to branch  on     
c based on how much they are connected or disconnected to other free nodes.
c                                                                       
c         --- none could be fixed so find variable i to branch on.      
ccc           rule1: choose node with highest free connectivity         
c             rule2: choose node with lowest free connectivity          
c             rule 2 is faster in verifying optimality and is used here 
c             rule 1 is the greedy method.                              
            ivar   = levp1                                              
            do 140 i=levp1+1,n                                          
               if (degfre(p(i)).lt.degfre(p(ivar))) ivar=i              
140         continue                                                    
c         --- set to one and stack the subproblem with x(ip) = 0        
            value = .true.                                              
            if (g-1.gt.maxc) then                                       
c              only stack subproblems that are not suboptimal           
               topstk = topstk + 1                                      
               stack(1,topstk) = levp1                                  
               stack(2,topstk) = g-1                                    
               nfree = n-levp1                                          
               newewk = endwk + (nfree+nfree)                           
               if (newewk.le.maxwk) then                                
                  stack(3,topstk) = endwk                               
                  free(p(ivar)) = .false.                               
                  itemp = endwk + nfree                                 
                  iptr = 0                                              
                  do 145 i=1,n                                          
c                    note: the following requires that the free list in 
c                          the permutation p be in order                
                     if (free(i)) then                                  
                        iptr = iptr+1                                   
                        iwk(endwk+iptr) = degfre(i)                     
                        iwk(itemp+iptr) = degfx1(i)                     
                     endif                                              
145               continue                                              
                  endwk = newewk                                        
               else                                                     
                  stack(3,topstk) = -1                                  
               endif                                                    
            endif                                                       
c         --- ivar is new fixed variable, increase lev & swap in p      
150         lev      = levp1                                            
            it       = p(ivar)                                          
            p(ivar)  = p(lev)                                           
            p(lev)   = it                                               
            x(it)    = value                                            
            free(it) = .false.                                          
            uselst   = .true.                                           
c                                                                       
         endif                                                          
         goto 30                                                        
c                                                                       
1000  return
c     end of bbspl2                                                     
      end