dfs Module Subroutine

recursive module subroutine dfs(this, vertex_index, visited, order, order_index)

Depth first search algorithm

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in) :: this

Instance of network

integer, intent(in) :: vertex_index

Index of the vertex to start the search from

logical, intent(inout), dimension(this%auto_graph%num_vertices) :: visited

Array to store whether a vertex has been visited

integer, intent(inout), dimension(this%auto_graph%num_vertices) :: order

Array to store the order of the vertices

integer, intent(inout) :: order_index

Index of the current vertex in the order array


Source Code

  recursive module subroutine dfs( &
       this, vertex_index, visited, order, order_index &
  )
    !! Depth first search algorithm
    implicit none

    ! Arguments
    class(network_type), intent(in) :: this
    !! Instance of network
    integer, intent(in) :: vertex_index
    !! Index of the vertex to start the search from
    logical, dimension(this%auto_graph%num_vertices), intent(inout) :: visited
    !! Array to store whether a vertex has been visited
    integer, dimension(this%auto_graph%num_vertices), intent(inout) :: order
    !! Array to store the order of the vertices
    integer, intent(inout) :: order_index
    !! Index of the current vertex in the order array

    ! Local variables
    integer :: i
    !! Loop index

    visited(vertex_index) = .true.
    do i = 1, this%auto_graph%num_vertices, 1
       if(this%auto_graph%adjacency(i,vertex_index).ne.0)then
          if(.not.visited(i)) call this%dfs(i, visited, order, order_index)
       end if
    end do
    order_index = order_index + 1
    order(order_index) = vertex_index

  end subroutine dfs