dag_remove_node Subroutine

private subroutine dag_remove_node(me, ivertex)

Remove a node from a dag. Will also remove any edges connected to it.

This will renumber the nodes and edges internally. Note that any default integer labels generated in dag_set_vertices would then be questionable.

Type Bound

dag

Arguments

Type IntentOptional Attributes Name
class(dag), intent(inout) :: me
integer(kind=ip), intent(in) :: ivertex

the node to remove


Calls

proc~~dag_remove_node~~CallsGraph proc~dag_remove_node dag_module::dag%dag_remove_node proc~remove_edge dag_module::vertex%remove_edge proc~dag_remove_node->proc~remove_edge

Source Code

    subroutine dag_remove_node(me,ivertex)

    class(dag),intent(inout) :: me
    integer(ip),intent(in) :: ivertex !! the node to remove

    integer(ip) :: i !! counter
    type(vertex),dimension(:),allocatable :: tmp !! for resizing `me%vertices`

    if (allocated(me%vertices)) then
        associate (n => size(me%vertices))
            do i = 1, n
                ! first remove any edges:
                call me%vertices(i)%remove_edge(ivertex)
                ! next, renumber the existing edges so they will be
                ! correct after ivertex is deleted
                ! Example (removing 2): 1 [2] 3 4 ==> 1 2 3
                if (allocated(me%vertices(i)%edges)) then
                    where (me%vertices(i)%edges%ivertex>ivertex)
                        me%vertices(i)%edges%ivertex = me%vertices(i)%edges%ivertex - 1
                    end where
                end if
            end do
            ! now, remove the node:
            allocate(tmp(n-1))
            if (ivertex>1) tmp(1:ivertex-1) = me%vertices(1:ivertex-1)
            if (ivertex<n) tmp(ivertex:n-1) = me%vertices(ivertex+1:n)
            call move_alloc(tmp,me%vertices)
        end associate
    end if
    me%n = size(me%vertices)
    if (me%n==0) deallocate(me%vertices)

    end subroutine dag_remove_node