anchor.diff:              cnt = cnt + 1
               cntall = cntall + 1
               lv_item(cntall) = knode
  -            if (cntall == N) exit PRLV
  +            !if (cntall == N) exit PRLV
             end if
           end do
           do k = indexU(jnode-1)+1, indexU(jnode)
  
positive.code:   public :: hecmw_matrix_ordering_CM
    public :: hecmw_matrix_ordering_RCM
  
  contains
  
    subroutine hecmw_matrix_ordering_CM(N, indexL, itemL, indexU, itemU, &
  
negative.code:         end do
        endif
        lv_index(level) = cntall
        call sort_nodes_by_degree(lv_item, lv_index(level-1)+1, lv_index(level), degs)
        if (cntall == N) then
          nlevel = level
          exit
        end if
      end do
      if (DEBUG > 0) then
        level = 0
        do j = 1, N
          jnode = lv_item(j)
          if (jnode <= 0 .or. jnode > N) stop 'ERROR: ordering_CM_inner: out of range'
          if (iwk(jnode) < 0) stop 'ERROR: ordering_CM_inner: duplicated node found'
          if (iwk(jnode) < level) stop 'ERROR: ordering_CM_inner: not in level order'
          level = iwk(jnode)
          iwk(jnode) = -1
        enddo
        do j = 1, N
          if (iwk(j) /= -1) stop 'ERROR: ordering_CM_inner: non-numbered node found'
        enddo
      endif
      deallocate(iwk)
    end subroutine ordering_CM_inner
  
    subroutine count_degrees(N, indexL, indexU, itemU, degs)
      implicit none
      integer(kind=kint), intent(in) :: N
      integer(kind=kint), intent(in) :: indexL(0:), indexU(0:)
      integer(kind=kint), intent(in) :: itemU(:)
      integer(kind=kint), intent(out) :: degs(:)
      integer(kind=kint) :: i, j
      do i=1,N
        degs(i) = indexL(i) - indexL(i-1)
        do j = indexU(i-1)+1, indexU(i)
          if (itemU(j) <= N) degs(i) = degs(i) + 1
        end do
      end do
    end subroutine count_degrees
  
    subroutine find_minimum_degrees(N, degs, nminmax, nmin, mins)
      implicit none
      integer(kind=kint), intent(in) :: N
      integer(kind=kint), intent(in) :: degs(:)
      integer(kind=kint), intent(in) :: nminmax
      integer(kind=kint), intent(out) :: nmin
      integer(kind=kint), intent(out) :: mins(nminmax)
      integer(kind=kint) :: degmin, i, j
      degmin = N
      nmin = 0
      do i=1,N
        ! skip unconnected nodes
        if (degs(i) == 0) cycle
        if (degs(i) < degmin) then
          degmin = degs(i)
          nmin = 1
          mins(1) = i
        else if (degs(i) == degmin) then
          nmin = nmin + 1
          if (nmin <= nminmax) mins(nmin) = i
        end if
      end do
      if (DEBUG > 0) write(*,*) 'DEBUG:: find_minimum_degrees: nmin, deg = ', nmin, degmin
      if (nmin > nminmax) nmin = nminmax
    end subroutine find_minimum_degrees
  
    recursive subroutine sort_nodes_by_degree(lv_item, istart, iend, degs)
      implicit none
      integer(kind=kint), intent(inout) :: lv_item(:)
      integer(kind=kint), intent(in) :: istart, iend
      integer(kind=kint), intent(in) :: degs(:)
      integer(kind=kint) :: pivot, left, right, itmp
      if (istart >= iend) return
      pivot = degs(lv_item((istart + iend) / 2))
      left = istart
      right = iend
      do
        do while (degs(lv_item(left)) < pivot)
          left = left + 1
        end do
        do while (pivot < degs(lv_item(right)))
          right = right - 1
        end do
        if (left >= right) exit
        itmp = lv_item(left)
        lv_item(left) = lv_item(right)
        lv_item(right) = itmp
        left = left + 1
        right = right - 1
      end do
      call sort_nodes_by_degree(lv_item, istart, left-1, degs)
      call sort_nodes_by_degree(lv_item, right+1, iend, degs)
    end subroutine sort_nodes_by_degree
  
    subroutine reverse_ordering(N, perm, iperm)
      implicit none
      integer(kind=kint), intent(in) :: N
      integer(kind=kint), intent(inout) :: perm(:), iperm(:)
      integer(kind=kint) :: i, N1
      N1 = N + 1
      do i=1,N
        iperm(i) = N1 - iperm(i)
        perm(iperm(i)) = i
      end do
    end subroutine reverse_ordering
  
    subroutine write_nonzero_profile(N, indexL, itemL, indexU, itemU, perm, iperm)
      implicit none
      integer(kind=kint), intent(in) :: N
      integer(kind=kint), intent(in) :: indexL(0:), indexU(0:)
      integer(kind=kint), intent(in) :: itemL(:), itemU(:)
      integer(kind=kint), intent(in) :: perm(:), iperm(:)
      integer(kind=kint), parameter :: F_ORG = 901
      integer(kind=kint), parameter :: F_NEW = 902
      integer(kind=kint) :: i, j, irow, jcol
      open(F_ORG, file='nzprof_org.txt', status='replace')
      do irow = 1, N
        i = irow
        do j = indexL(i-1)+1, indexL(i)
          jcol = itemL(j)
          write(F_ORG,*) irow, jcol
        end do
        do j = indexU(i-1)+1, indexU(i)
          jcol = itemU(j)
          if (jcol > N) cycle
          write(F_ORG,*) irow, jcol
        end do
      end do
      close(F_ORG)
      open(F_NEW, file='nzprof_new.txt', status='replace')
      do irow = 1, N
        i = perm(irow)
        do j = indexL(i-1)+1, indexL(i)
          jcol = itemL(j)
          write(F_NEW,*) irow, iperm(jcol)
        end do
        do j = indexU(i-1)+1, indexU(i)
          jcol = itemU(j)
          if (jcol > N) cycle
          write(F_NEW,*) irow, iperm(jcol)
        end do
      end do
      close(F_NEW)
    end subroutine write_nonzero_profile
  
    subroutine write_perm(N, perm, iperm)
      implicit none
      integer(kind=kint), intent(in) :: N
      integer(kind=kint), intent(in) :: perm(:), iperm(:)
      integer(kind=kint), parameter :: F_PERM = 903
      integer(kind=kint) :: i
      open(F_PERM, file='perm_iperm.txt', status='replace')
      do i = 1, N
        write(F_PERM,*) i, perm(i), iperm(i)
      end do
      close(F_PERM)
    end subroutine write_perm
  
  end module m_hecmw_matrix_ordering_CM
  