rcm Subroutine

private subroutine rcm(m, adjptr, adj, perm)

Reverse Cuthill-McKee ordering of a symmetric graph.

Each connected component is ordered by a breadth-first search starting from a pseudo-peripheral node (found with the George-Liu algorithm), visiting the neighbors of each node in order of increasing degree. The final ordering is reversed.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: m

number of nodes

integer, intent(in), dimension(:) :: adjptr

adjacency pointers (size m+1)

integer, intent(in), dimension(:) :: adj

adjacency lists

integer, intent(out), dimension(m) :: perm

the ordering: perm(new) = old


Called by

proc~~rcm~~CalledByGraph proc~rcm rcm proc~analyze cholesky_minnorm_solver%analyze proc~analyze->proc~rcm proc~nlesolver_solver nlesolver_type%nlesolver_solver proc~nlesolver_solver->proc~analyze

Source Code

    subroutine rcm(m, adjptr, adj, perm)

    integer,intent(in)                :: m       !! number of nodes
    integer,dimension(:),intent(in)   :: adjptr  !! adjacency pointers (size `m+1`)
    integer,dimension(:),intent(in)   :: adj     !! adjacency lists
    integer,dimension(m),intent(out)  :: perm    !! the ordering: `perm(new) = old`

    integer,dimension(:),allocatable :: deg      !! degree of each node
    logical,dimension(:),allocatable :: visited  !! if the node has been ordered
    integer,dimension(:),allocatable :: order    !! Cuthill-McKee ordering
    integer,dimension(:),allocatable :: q        !! queue for the level structure BFS
    integer,dimension(:),allocatable :: bfsmark  !! marker for the level structure BFS
    integer :: stamp      !! current marker value for `bfsmark`
    integer :: nord       !! number of nodes ordered so far
    integer :: next       !! search pointer for the next unvisited node
    integer :: head, start, v, w, t, i, s, y, h, hy, lstart, lend, iter

    allocate(deg(m), visited(m), order(m), q(m), bfsmark(m))
    do i = 1, m
        deg(i) = adjptr(i+1) - adjptr(i)
    end do
    visited = .false.
    bfsmark = 0
    stamp   = 0
    nord    = 0
    next    = 1

    do  ! loop over the connected components

        do while (next <= m)
            if (.not. visited(next)) exit
            next = next + 1
        end do
        if (next > m) exit
        s = next

        ! find a pseudo-peripheral node in this component:
        call level_structure(s, h, lstart, lend)
        do iter = 1, 50
            ! min degree node in the last level:
            y = q(lstart)
            do i = lstart+1, lend
                if (deg(q(i)) < deg(y)) y = q(i)
            end do
            call level_structure(y, hy, lstart, lend)
            if (hy > h) then
                s = y
                h = hy
            else
                exit
            end if
        end do

        ! Cuthill-McKee ordering of this component:
        nord = nord + 1
        order(nord) = s
        visited(s) = .true.
        head = nord
        do while (head <= nord)
            v = order(head)
            head = head + 1
            start = nord + 1
            do t = adjptr(v), adjptr(v+1)-1
                w = adj(t)
                if (.not. visited(w)) then
                    visited(w) = .true.
                    nord = nord + 1
                    order(nord) = w
                end if
            end do
            call sort_by_degree(order(start:nord))
        end do

    end do

    ! reverse:
    do i = 1, m
        perm(i) = order(m-i+1)
    end do

    contains

        subroutine level_structure(root, nlevels, last_start, last_end)
            !! breadth-first level structure rooted at `root`.
            !! returns the number of levels and the range of the last level in `q`.
            integer,intent(in)  :: root
            integer,intent(out) :: nlevels, last_start, last_end
            integer :: qhead, qtail, lev_end, u, tt, z
            stamp = stamp + 1
            q(1) = root
            bfsmark(root) = stamp
            qhead = 1
            qtail = 1
            nlevels = 0
            do
                ! the current level is q(qhead:qtail)
                nlevels = nlevels + 1
                last_start = qhead
                last_end = qtail
                lev_end = qtail
                do while (qhead <= lev_end)
                    u = q(qhead)
                    qhead = qhead + 1
                    do tt = adjptr(u), adjptr(u+1)-1
                        z = adj(tt)
                        if (bfsmark(z) /= stamp) then
                            bfsmark(z) = stamp
                            qtail = qtail + 1
                            q(qtail) = z
                        end if
                    end do
                end do
                if (qtail == lev_end) exit ! no new level
            end do
        end subroutine level_structure

        subroutine sort_by_degree(list)
            !! insertion sort of a (short) list of nodes by increasing degree
            integer,dimension(:),intent(inout) :: list
            integer :: ii, jj, tmp
            do ii = 2, size(list)
                tmp = list(ii)
                jj = ii - 1
                do while (jj >= 1)
                    if (deg(list(jj)) <= deg(tmp)) exit
                    list(jj+1) = list(jj)
                    jj = jj - 1
                end do
                list(jj+1) = tmp
            end do
        end subroutine sort_by_degree

    end subroutine rcm