traverse Subroutine

recursive subroutine traverse(node_num, idist, nodes_visited)

traverse the graph until we get to the end and check the max distance

Arguments

Type IntentOptional Attributes Name
integer(kind=ip), intent(in) :: node_num

current node

integer(kind=ip), intent(in) :: idist

distance to get here

logical, intent(in), dimension(:) :: nodes_visited

Called by

proc~~traverse~2~~CalledByGraph proc~traverse~2 problem_23::traverse proc~traverse~2->proc~traverse~2 proc~go~5 problem_23::go proc~go~5->proc~traverse~2 program~problem_23 problem_23 program~problem_23->proc~go~5

Source Code

    recursive subroutine traverse(node_num, idist, nodes_visited)
        !! traverse the graph until we get to the end and check the max distance
        integer(ip),intent(in) :: node_num !! current node
        integer(ip),intent(in) :: idist !! distance to get here
        logical,dimension(:),intent(in) :: nodes_visited

        logical,dimension(:),allocatable :: tmp_nodes_visited
        integer :: i

        if (nodes_visited(node_num)) return ! already visited this node
        if (node_num==total_nodes) then ! reached the destination
            if (idist>max_dist) max_dist = idist  ! best so far
        else
            ! are their child nodes?
            if (allocated(nodes(node_num)%inext)) then
                tmp_nodes_visited = nodes_visited
                tmp_nodes_visited(node_num) = .true. ! mark this node
                do i = 1, size(nodes(node_num)%inext)
                    call traverse(nodes(node_num)%inext(i), idist + nodes(node_num)%idist(i), tmp_nodes_visited)
                end do
            end if
        end if

    end subroutine traverse