      SUBROUTINE balsmalldeglast(maxd,mind)
      include 'irreg.h'
      integer i,j,clashes,passes,removed,setsize,v,w,mind,maxd,max
      integer used(0:105),weight(0:npm),dstar(0:nm),offset
      integer neighbour,deltadstar(0:nm)
      integer end_flag,stop_flag,available
      common /dstar/ dstar,deltadstar
      integer mysum, ran, mymax
      external mysum, ran, mymax
      integer tot_clashes

      offset=np*procnum

c$$$      print*,"Parallel Node Ordering Algorithm with Rand init colors"
C     First calculate weights
      do i=0,npm
         dstar(i+offset)=proc_n(i)
         weight(i)=0
      enddo

      color_count=0
      removed=0
      w=1
      v=mind
      end_flag=0
      stop_flag=0
      do while ((removed .lt. np) .or. (stop_flag .eq. 0))
         setsize=0
         do i=0,npm
            if ((dstar(i+offset) .le. v) .and. (weight(i) .eq. 0)) then
               weight(i)=w
               setsize=setsize+1
            endif
         enddo
         if (setsize .eq. 0) then
            v=v+1
         else 
            do i=0,npm
               if (weight(i) .eq. w) then
                  do j=0,proc_n(i)-1
                     neighbour=neigh(i,j)
                     deltadstar(neighbour)=deltadstar(neighbour)+1
                  enddo
               endif
            enddo
            removed=removed+setsize
         endif
         call update_dstar
         if (removed .ge. np) then
            end_flag=1
         else
            end_flag=0
         endif
         status=kxcomb(end_flag,MYSUM,4,1,IALNOD,0,101)
         if (end_flag .eq. nprocs) stop_flag=1
         w=w+1
      enddo
      do i=0,npm
         if (dstar(i+offset) .ne. 0) then
            print *,"Error : dstar[",i+offset,dstar(i+offset),procnum
         endif
      enddo
c$$$      print *,"Bound on number of colours =", v+1, procnum
      status=kxcomb(v,MYMAX,4,1,IALNOD,0,105)
c$$$      print *,"Bound on number of colours =", v+1, procnum
c$$$      
c$$$      print *,"First level passes taken =", w-1, procnum
      color_count=(w-1)
C     /* Now do the colouring */
      call assign_rand_labels(2**25)
      do i=0,npm
         temp_color(i)=ran(v+1)
         temp_label(i)=abs(temp_label(i)+
     $        ((2**26)*weight(i)))
      enddo
      call pack_and_send
      call pack_and_send_color
c$$$      if (procnum .eq. 1) then
c$$$         do i=0,5
c$$$            print *,i,color(i)
c$$$         enddo
c$$$      endif
      clashes=1
      passes=0
      stop_flag=0
      do while ((clashes .gt. 0) .or. (stop_flag .eq. 0))
         clashes=0
         tot_clashes=0
         passes=passes+1
         do i=0,npm
            available=v+3
c**** available=v+1
            do j=0,v+10
               used(j)=0
            enddo
            do j=0,proc_n(i)-1
               neighbour=neigh(i,j)
               if ((label(neighbour) .gt. temp_label(i)) .and.
     $              (used(color(neighbour)) .eq. 0)) then
                  available=available-1
                  used(color(neighbour))=1
               endif
            enddo
            if (used(temp_color(i)) .eq. 1) then
               clashes=1
               tot_clashes=tot_clashes+1
               available=ran(available)-1
c$$$               if (procnum .eq. 0) print *,i,available
               j=1
               do while (used(j) .eq. 1)
                  j=j+1
               enddo
               do while(available .gt. 0)
                  j=j+1
                  do while (used(j) .eq. 1)
                     j=j+1
                  enddo
                  available=available-1
               enddo
c$$$               if (procnum .eq. 0) print *,i,j
            endif
            temp_color(i)=j
            color(i+offset)=j
         enddo
         call pack_and_send_color
c$$$         if (procnum .eq. 0) print *,"Pass number",passes,tot_clashes
         if (passes .gt. 100) then 
            clashes=0
            color_count=-1100
            if (procnum .eq. 0) Print *,"Bal Small Deg Last STOP !!"
         endif
         if (clashes .le. 0) then
            end_flag=1
         else
            end_flag=0
         endif
         status=kxcomb(end_flag,MYSUM,4,1,IALNOD,0,101)
         if (end_flag .eq. nprocs) stop_flag=1
      enddo
c$$$  print *,"Passes taken =", passes, procnum
      color_count=color_count+passes
      
      return
      end
      
      
