H03ADF (PDF version)
H Chapter Contents
H Chapter Introduction
NAG Library Manual

NAG Library Routine Document

H03ADF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

H03ADF finds the shortest path through a directed or undirected acyclic network using Dijkstra's algorithm.

2  Specification

SUBROUTINE H03ADF ( N, NS, NE, DIRECT, NNZ, D, IROW, ICOL, SPLEN, PATH, IWORK, WORK, IFAIL)
INTEGER  N, NS, NE, NNZ, IROW(NNZ), ICOL(NNZ), PATH(N), IWORK(3*N+1), IFAIL
REAL (KIND=nag_wp)  D(NNZ), SPLEN, WORK(2*N)
LOGICAL  DIRECT

3  Description

H03ADF attempts to find the shortest path through a directed or undirected acyclic network, which consists of a set of points called vertices and a set of curves called arcs that connect certain pairs of distinct vertices. An acyclic network is one in which there are no paths connecting a vertex to itself. An arc whose origin vertex is i and whose destination vertex is j can be written as ij. In an undirected network the arcs ij and ji are equivalent (i.e., ij), whereas in a directed network they are different. Note that the shortest path may not be unique and in some cases may not even exist (e.g., if the network is disconnected).
The network is assumed to consist of n vertices which are labelled by the integers 1,2,,n. The lengths of the arcs between the vertices are defined by the n by n distance matrix D, in which the element dij gives the length of the arc ij; dij=0 if there is no arc connecting vertices i and j (as is the case for an acyclic network when i=j). Thus the matrix D is usually sparse. For example, if n=4 and the network is directed, then
D= 0 d12 d13 d14 d21 0 d23 d24 d31 d32 0 d34 d41 d42 d43 0 .  
If the network is undirected, D is symmetric since dij=dji (i.e., the length of the arc ij the length of the arc ji).
The method used by H03ADF is described in detail in Section 9.

4  References

Dijkstra E W (1959) A note on two problems in connection with graphs Numer. Math. 1 269–271

5  Parameters

1:     N – INTEGERInput
On entry: n, the number of vertices.
Constraint: N2.
2:     NS – INTEGERInput
3:     NE – INTEGERInput
On entry: ns and ne, the labels of the first and last vertices, respectively, between which the shortest path is sought.
Constraints:
  • 1NSN;
  • 1NEN;
  • NSNE.
4:     DIRECT – LOGICALInput
On entry: indicates whether the network is directed or undirected.
DIRECT=.TRUE.
The network is directed.
DIRECT=.FALSE.
The network is undirected.
5:     NNZ – INTEGERInput
On entry: the number of nonzero elements in the distance matrix D.
Constraints:
  • if DIRECT=.TRUE., 1NNZN×N-1;
  • if DIRECT=.FALSE., 1NNZN×N-1/2.
6:     DNNZ – REAL (KIND=nag_wp) arrayInput
On entry: the nonzero elements of the distance matrix D, ordered by increasing row index and increasing column index within each row. More precisely, Dk must contain the value of the nonzero element with indices (IROWk,ICOLk); this is the length of the arc from the vertex with label IROWk to the vertex with label ICOLk. Elements with the same row and column indices are not allowed. If DIRECT=.FALSE., then only those nonzero elements in the strict upper triangle of D need be supplied since dij=dji. (F11ZAF may be used to sort the elements of an arbitrarily ordered matrix into the required form. This is illustrated in Section 10.)
Constraint: Dk>0.0, for k=1,2,,NNZ.
7:     IROWNNZ – INTEGER arrayInput
8:     ICOLNNZ – INTEGER arrayInput
On entry: IROWk and ICOLk must contain the row and column indices, respectively, for the nonzero element stored in Dk.
Constraints:
IROW and ICOL must satisfy the following constraints (which may be imposed by a call to F11ZAF):
  • IROWk-1<IROWk;
  • IROWk-1=IROWk and ICOLk-1<ICOLk, for k=2,3,,NNZ.
In addition, if DIRECT=.TRUE., 1IROWkN, 1ICOLkN and IROWkICOLk;
  • if DIRECT=.FALSE., 1IROWk<ICOLkN.
9:     SPLEN – REAL (KIND=nag_wp)Output
On exit: the length of the shortest path between the specified vertices ns and ne.
10:   PATHN – INTEGER arrayOutput
On exit: contains details of the shortest path between the specified vertices ns and ne. More precisely, NS=PATH1PATH2PATHp=NE for some pn. The remaining n-p elements are set to zero.
11:   IWORK3×N+1 – INTEGER arrayWorkspace
12:   WORK2×N – REAL (KIND=nag_wp) arrayWorkspace
13:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL=1
On entry,N<2,
orNS<1,
orNS>N,
orNE<1,
orNE>N,
orNS=NE.
IFAIL=2
On entry,NNZ>N×N-1 when DIRECT=.TRUE.,
orNNZ>N×N-1/2 when DIRECT=.FALSE.,
orNNZ<1.
IFAIL=3
On entry, IROWk<1 or IROWk>N or ICOLk<1 or ICOLk>N or IROWk=ICOLk for some k when DIRECT=.TRUE..
IFAIL=4
On entry, IROWk<1 or IROWkICOLk or ICOLk>N for some k when DIRECT=.FALSE..
IFAIL=5
Dk0.0 for some k.
IFAIL=6
On entry, IROWk-1>IROWk or IROWk-1=IROWk and ICOLk-1>ICOLk for some k.
IFAIL=7
On entry, IROWk-1=IROWk and ICOLk-1=ICOLk for some k.
IFAIL=8
No connected network exists between vertices NS and NE.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

The results are exact, except for the obvious rounding errors in summing the distances in the length of the shortest path.

8  Parallelism and Performance

Not applicable.

9  Further Comments

H03ADF is based upon Dijkstra's algorithm (see Dijkstra (1959)), which attempts to find a path nsne between two specified vertices ns and ne of shortest length dns,ne.
The algorithm proceeds by assigning labels to each vertex, which may be temporary or permanent. A temporary label can be changed, whereas a permanent one cannot. For example, if vertex p has a permanent label q,r, then r is the distance dns,r and q is the previous vertex on a shortest length nsp path. If the label is temporary, then it has the same meaning but it refers only to the shortest nsp path found so far. A shorter one may be found later, in which case the label may become permanent.
The algorithm consists of the following steps.
1. Assign the permanent label -,0 to vertex ns and temporary labels -, to every other vertex. Set k=ns and go to 2.
2. Consider each vertex y adjacent to vertex k with a temporary label in turn. Let the label at k be p,q and at yr,s. If q+dky<s, then a new temporary label k,q+dky is assigned to vertex y; otherwise no change is made in the label of y. When all vertices y with temporary labels adjacent to k have been considered, go to 3.
3. From the set of temporary labels, select the one with the smallest second component and declare that label to be permanent. The vertex it is attached to becomes the new vertex k. If k=ne go to 4. Otherwise go to 2 unless no new vertex can be found (e.g., when the set of temporary labels is ‘empty’ but kne, in which case no connected network exists between vertices ns and ne).
4. To find the shortest path, let y,z denote the label of vertex ne. The column label (z) gives dns,ne while the row label (y) then links back to the previous vertex on a shortest length nsne path. Go to vertex y. Suppose that the (permanent) label of vertex y is w,x, then the next previous vertex is w on a shortest length nsy path. This process continues until vertex ns is reached. Hence the shortest path is
nswyne,  
which has length dns,ne.

10  Example

This example finds the shortest path between vertices 1 and 11 for the undirected network
Figure H03ADF1
Figure 1

10.1  Program Text

Program Text (h03adfe.f90)

10.2  Program Data

Program Data (h03adfe.d)

10.3  Program Results

Program Results (h03adfe.r)


H03ADF (PDF version)
H Chapter Contents
H Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015