convert_to_sparse Module Subroutine

module subroutine convert_to_sparse(this)

Convert the graph to a sparse representation.

Arguments

Type IntentOptional Attributes Name
class(graph_type), intent(inout) :: this

Parent. Instance of the graph structure.


Source Code

  module subroutine convert_to_sparse(this)
    !! Convert the graph to a sparse representation.
    implicit none

    ! Arguments
    class(graph_type), intent(inout) :: this
    !! Parent. Instance of the graph structure.

    ! Local variables
    integer :: v, e, idx
    !! Loop indices.

    if(this%is_sparse) return

    this%is_sparse = .true.

    if(allocated(this%adjacency)) deallocate(this%adjacency)
    if(allocated(this%vertex_features)) deallocate(this%vertex_features)
    if(allocated(this%edge_features)) deallocate(this%edge_features)
    if(allocated(this%edge_weights)) deallocate(this%edge_weights)

    allocate(this%vertex_features(this%num_vertex_features, this%num_vertices))
    allocate(this%edge_features(this%num_edge_features, this%num_edges))
    allocate(this%edge_weights(this%num_edges))

    do v = 1, this%num_vertices
       idx = this%vertex(v)%id
       if(idx.eq.-1) idx = v
       this%vertex_features(:,idx) = this%vertex(v)%feature
    end do

    do e = 1, this%num_edges
       idx = this%edge(e)%id
       if(idx.eq.-1) idx = e
       this%edge_features(:,e) = this%edge(e)%feature
       this%edge_weights(e) = this%edge(e)%weight
    end do

    deallocate(this%vertex)

    allocate(this%adj_ia(this%num_vertices+1))
    allocate(this%adj_ja(2,this%num_vertices+2*this%num_edges))
    this%adj_ia(1) = 1
    this%adj_ja(1,:) = 0
    this%adj_ja(2,:) = 0
    do v = 1, this%num_vertices
       this%adj_ia(v+1) = this%adj_ia(v)
       do e = 1, this%num_edges
          if(this%directed.and.this%edge(e)%index(1).eq.v)then
             this%adj_ja(1,this%adj_ia(v+1)) = this%edge(e)%index(2)
             this%adj_ja(2,this%adj_ia(v+1)) = e
             this%adj_ia(v+1) = this%adj_ia(v+1) + 1
          elseif(.not.this%directed.and.this%edge(e)%index(1).eq.v)then
             this%adj_ja(1,this%adj_ia(v+1)) = this%edge(e)%index(2)
             this%adj_ja(2,this%adj_ia(v+1)) = e
             this%adj_ia(v+1) = this%adj_ia(v+1) + 1
          elseif(.not.this%directed.and.this%edge(e)%index(2).eq.v)then
             this%adj_ja(1,this%adj_ia(v+1)) = this%edge(e)%index(1)
             this%adj_ja(2,this%adj_ia(v+1)) = e
             this%adj_ia(v+1) = this%adj_ia(v+1) + 1
          end if
       end do
    end do
    if(allocated(this%adjacency)) deallocate(this%adjacency)

  end subroutine convert_to_sparse