Walk prev from target back to source and return it forwards.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=default_int), | intent(in) | :: | prev(:) |
Predecessor array, with |
||
| integer(kind=default_int), | intent(in) | :: | source |
First vertex of the path |
||
| integer(kind=default_int), | intent(in) | :: | target |
Last vertex of the path |
||
| integer(kind=default_int), | intent(out), | allocatable | :: | path(:) |
|
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=default_int), | private | :: | k | ||||
| integer(kind=default_int), | private | :: | n_hops | ||||
| integer(kind=default_int), | private | :: | v |
pure subroutine trace_back(prev, source, target, path) !! Walk `prev` from `target` back to `source` and return it forwards. integer(default_int), intent(in) :: prev(:) !! Predecessor array, with `GRAPH_NO_PREDECESSOR` at the source integer(default_int), intent(in) :: source !! First vertex of the path integer(default_int), intent(in) :: target !! Last vertex of the path integer(default_int), allocatable, intent(out) :: path(:) !! `source` first, `target` last integer(default_int) :: n_hops, v, k n_hops = 1 v = target do while (v /= source) v = prev(v) n_hops = n_hops + 1 end do allocate (path(n_hops)) v = target do k = n_hops, 1, -1 path(k) = v v = prev(v) end do end subroutine trace_back