Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

## Purpose

nag_mip_shortestpath (h03ad) finds the shortest path through a directed or undirected acyclic network using Dijkstra's algorithm.

## Syntax

[splen, path, ifail] = h03ad(n, ns, ne, direct, d, irow, icol, 'nnz', nnz)
[splen, path, ifail] = nag_mip_shortestpath(n, ns, ne, direct, d, irow, icol, 'nnz', nnz)

## Description

nag_mip_shortestpath (h03ad) 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$i$ and whose destination vertex is j$j$ can be written as ij$i\to j$. In an undirected network the arcs ij$i\to j$ and ji$j\to i$ are equivalent (i.e., ij$i↔j$), 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$n$ vertices which are labelled by the integers 1,2,,n$1,2,\dots ,n$. The lengths of the arcs between the vertices are defined by the n$n$ by n$n$ distance matrix d${\mathbf{d}}$, in which the element dij${d}_{ij}$ gives the length of the arc ij$i\to j$; dij = 0${d}_{ij}=0$ if there is no arc connecting vertices i$i$ and j$j$ (as is the case for an acyclic network when i = j$i=j$). Thus the matrix D$D$ is usually sparse. For example, if n = 4$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
.
$d= 0 d12 d13 d14 d21 0 d23 d24 d31 d32 0 d34 d41 d42 d43 0 .$
If the network is undirected, d${\mathbf{d}}$ is symmetric since dij = dji${d}_{ij}={d}_{ji}$ (i.e., the length of the arc ij$i\to j\equiv \text{}$ the length of the arc ji$j\to i$).
The method used by nag_mip_shortestpath (h03ad) is described in detail in Section [Further Comments].

## References

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

## Parameters

### Compulsory Input Parameters

1:     n – int64int32nag_int scalar
n$n$, the number of vertices.
Constraint: n2${\mathbf{n}}\ge 2$.
2:     ns – int64int32nag_int scalar
3:     ne – int64int32nag_int scalar
ns${n}_{s}$ and ne${n}_{e}$, the labels of the first and last vertices, respectively, between which the shortest path is sought.
Constraints:
• 1nsn$1\le {\mathbf{ns}}\le {\mathbf{n}}$;
• 1nen$1\le {\mathbf{ne}}\le {\mathbf{n}}$;
• nsne${\mathbf{ns}}\ne {\mathbf{ne}}$.
4:     direct – logical scalar
Indicates whether the network is directed or undirected.
direct = true${\mathbf{direct}}=\mathbf{true}$
The network is directed.
direct = false${\mathbf{direct}}=\mathbf{false}$
The network is undirected.
5:     d(nnz) – double array
nnz, the dimension of the array, must satisfy the constraint
• if direct = true${\mathbf{direct}}=\mathbf{true}$, 1nnzn × (n1)$1\le {\mathbf{nnz}}\le {\mathbf{n}}×\left({\mathbf{n}}-1\right)$;
• if direct = false${\mathbf{direct}}=\mathbf{false}$, 1nnzn × (n1) / 2$1\le {\mathbf{nnz}}\le {\mathbf{n}}×\left({\mathbf{n}}-1\right)/2$.
The nonzero elements of the distance matrix D$D$, ordered by increasing row index and increasing column index within each row. More precisely, d(k)${\mathbf{d}}\left(k\right)$ must contain the value of the nonzero element with indices (irow(k),icol(k)${\mathbf{irow}}\left(k\right),{\mathbf{icol}}\left(k\right)$); this is the length of the arc from the vertex with label irow(k)${\mathbf{irow}}\left(k\right)$ to the vertex with label icol(k)${\mathbf{icol}}\left(k\right)$. Elements with the same row and column indices are not allowed. If direct = false${\mathbf{direct}}=\mathbf{false}$, then only those nonzero elements in the strict upper triangle of d${\mathbf{d}}$ need be supplied since dij = dji${d}_{ij}={d}_{ji}$. (nag_sparse_real_gen_sort (f11za) may be used to sort the elements of an arbitrarily ordered matrix into the required form. This is illustrated in Section [Example].)
Constraint: d(k) > 0.0${\mathbf{d}}\left(\mathit{k}\right)>0.0$, for k = 1,2,,nnz$\mathit{k}=1,2,\dots ,{\mathbf{nnz}}$.
6:     irow(nnz) – int64int32nag_int array
7:     icol(nnz) – int64int32nag_int array
nnz, the dimension of the array, must satisfy the constraint
• if direct = true${\mathbf{direct}}=\mathbf{true}$, 1nnzn × (n1)$1\le {\mathbf{nnz}}\le {\mathbf{n}}×\left({\mathbf{n}}-1\right)$;
• if direct = false${\mathbf{direct}}=\mathbf{false}$, 1nnzn × (n1) / 2$1\le {\mathbf{nnz}}\le {\mathbf{n}}×\left({\mathbf{n}}-1\right)/2$.
irow(k)${\mathbf{irow}}\left(k\right)$ and icol(k)${\mathbf{icol}}\left(k\right)$ must contain the row and column indices, respectively, for the nonzero element stored in d(k)${\mathbf{d}}\left(k\right)$.
Constraints:
irow and icol must satisfy the following constraints (which may be imposed by a call to nag_sparse_real_gen_sort (f11za)):
• irow(k1) < irow(k)${\mathbf{irow}}\left(k-1\right)<{\mathbf{irow}}\left(k\right)$;
• irow(k1) = irow(k)${\mathbf{irow}}\left(\mathit{k}-1\right)={\mathbf{irow}}\left(\mathit{k}\right)$ and icol(k1) < icol(k)${\mathbf{icol}}\left(\mathit{k}-1\right)<{\mathbf{icol}}\left(\mathit{k}\right)$, for k = 2,3,,nnz$\mathit{k}=2,3,\dots ,{\mathbf{nnz}}$.
In addition, if direct = true${\mathbf{direct}}=\mathbf{true}$, 1irow(k)n$1\le {\mathbf{irow}}\left(k\right)\le {\mathbf{n}}$, 1icol(k)n$1\le {\mathbf{icol}}\left(k\right)\le {\mathbf{n}}$ and irow(k)icol(k)${\mathbf{irow}}\left(k\right)\ne {\mathbf{icol}}\left(k\right)$;
• if direct = false${\mathbf{direct}}=\mathbf{false}$, 1irow(k) < icol(k)n$1\le {\mathbf{irow}}\left(k\right)<{\mathbf{icol}}\left(k\right)\le {\mathbf{n}}$.

### Optional Input Parameters

1:     nnz – int64int32nag_int scalar
Default: The dimension of the arrays irow, icol, d. (An error is raised if these dimensions are not equal.)
The number of nonzero elements in the distance matrix D$D$.
Constraints:
• if direct = true${\mathbf{direct}}=\mathbf{true}$, 1nnzn × (n1)$1\le {\mathbf{nnz}}\le {\mathbf{n}}×\left({\mathbf{n}}-1\right)$;
• if direct = false${\mathbf{direct}}=\mathbf{false}$, 1nnzn × (n1) / 2$1\le {\mathbf{nnz}}\le {\mathbf{n}}×\left({\mathbf{n}}-1\right)/2$.

iwork work

### Output Parameters

1:     splen – double scalar
The length of the shortest path between the specified vertices ns${n}_{s}$ and ne${n}_{e}$.
2:     path(n) – int64int32nag_int array
Contains details of the shortest path between the specified vertices ns${n}_{s}$ and ne${n}_{e}$. More precisely, ns = path(1)path(2)path(p) = ne${\mathbf{ns}}={\mathbf{path}}\left(1\right)\to {\mathbf{path}}\left(2\right)\to \dots \to {\mathbf{path}}\left(p\right)={\mathbf{ne}}$ for some pn$p\le n$. The remaining (np)$\left(n-p\right)$ elements are set to zero.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 2${\mathbf{n}}<2$, or ns < 1${\mathbf{ns}}<1$, or ns > n${\mathbf{ns}}>{\mathbf{n}}$, or ne < 1${\mathbf{ne}}<1$, or ne > n${\mathbf{ne}}>{\mathbf{n}}$, or ns = ne${\mathbf{ns}}={\mathbf{ne}}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, nnz > n × (n − 1)${\mathbf{nnz}}>{\mathbf{n}}×\left({\mathbf{n}}-1\right)$ when direct = true${\mathbf{direct}}=\mathbf{true}$, or nnz > n × (n − 1) / 2${\mathbf{nnz}}>{\mathbf{n}}×\left({\mathbf{n}}-1\right)/2$ when direct = false${\mathbf{direct}}=\mathbf{false}$, or nnz < 1${\mathbf{nnz}}<1$.
ifail = 3${\mathbf{ifail}}=3$
On entry, irow(k) < 1${\mathbf{irow}}\left(k\right)<1$ or irow(k) > n${\mathbf{irow}}\left(k\right)>{\mathbf{n}}$ or icol(k) < 1${\mathbf{icol}}\left(k\right)<1$ or icol(k) > n${\mathbf{icol}}\left(k\right)>{\mathbf{n}}$ or irow(k) = icol(k)${\mathbf{irow}}\left(k\right)={\mathbf{icol}}\left(k\right)$ for some k$k$ when direct = true${\mathbf{direct}}=\mathbf{true}$.
ifail = 4${\mathbf{ifail}}=4$
On entry, irow(k) < 1${\mathbf{irow}}\left(k\right)<1$ or irow(k)icol(k)${\mathbf{irow}}\left(k\right)\ge {\mathbf{icol}}\left(k\right)$ or icol(k) > n${\mathbf{icol}}\left(k\right)>{\mathbf{n}}$ for some k$k$ when direct = false${\mathbf{direct}}=\mathbf{false}$.
ifail = 5${\mathbf{ifail}}=5$
d(k)0.0${\mathbf{d}}\left(k\right)\le 0.0$ for some k$k$.
ifail = 6${\mathbf{ifail}}=6$
On entry, irow(k1) > irow(k)${\mathbf{irow}}\left(k-1\right)>{\mathbf{irow}}\left(k\right)$ or irow(k1) = irow(k)${\mathbf{irow}}\left(k-1\right)={\mathbf{irow}}\left(k\right)$ and icol(k1) > icol(k)${\mathbf{icol}}\left(k-1\right)>{\mathbf{icol}}\left(k\right)$ for some k$k$.
ifail = 7${\mathbf{ifail}}=7$
On entry, irow(k1) = irow(k)${\mathbf{irow}}\left(k-1\right)={\mathbf{irow}}\left(k\right)$ and icol(k1) = icol(k)${\mathbf{icol}}\left(k-1\right)={\mathbf{icol}}\left(k\right)$ for some k$k$.
ifail = 8${\mathbf{ifail}}=8$
No connected network exists between vertices ns and ne.

## Accuracy

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

nag_mip_shortestpath (h03ad) is based upon Dijkstra's algorithm (see Dijkstra (1959)), which attempts to find a path nsne${n}_{s}\to {n}_{e}$ between two specified vertices ns${n}_{s}$ and ne${n}_{e}$ of shortest length d(ns,ne)$d\left({n}_{s},{n}_{e}\right)$.
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$p$ has a permanent label (q,r)$\left(q,r\right)$, then r$r$ is the distance d(ns,r)$d\left({n}_{s},r\right)$ and q$q$ is the previous vertex on a shortest length nsp${n}_{s}\to p$ path. If the label is temporary, then it has the same meaning but it refers only to the shortest nsp${n}_{s}\to p$ 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)$\left(-,0\right)$ to vertex ns${n}_{s}$ and temporary labels (,)$\left(-,\infty \right)$ to every other vertex. Set k = ns$k={n}_{s}$ and go to 2..
2. Consider each vertex y$y$ adjacent to vertex k$k$ with a temporary label in turn. Let the label at k$k$ be (p,q)$\left(p,q\right)$ and at y(r,s)$y\left(r,s\right)$. If q + dky < s$q+{d}_{ky}, then a new temporary label (k,q + dky)$\left(k,q+{d}_{ky}\right)$ is assigned to vertex y$y$; otherwise no change is made in the label of y$y$. When all vertices y$y$ with temporary labels adjacent to k$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$k$. If k = ne$k={n}_{e}$ 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$k\ne {n}_{e}$, in which case no connected network exists between vertices ns${n}_{s}$ and ne${n}_{e}$).
4. To find the shortest path, let (y,z)$\left(y,z\right)$ denote the label of vertex ne${n}_{e}$. The column label (z$z$) gives d(ns,ne)$d\left({n}_{s},{n}_{e}\right)$ while the row label (y$y$) then links back to the previous vertex on a shortest length nsne${n}_{s}\to {n}_{e}$ path. Go to vertex y$y$. Suppose that the (permanent) label of vertex y$y$ is (w,x)$\left(w,x\right)$, then the next previous vertex is w$w$ on a shortest length nsy${n}_{s}\to y$ path. This process continues until vertex ns${n}_{s}$ is reached. Hence the shortest path is
 ns → … → w → y → ne, $ns→…→w→y→ne,$
which has length d(ns,ne)$d\left({n}_{s},{n}_{e}\right)$.

## Example

```function nag_mip_shortestpath_example
n = int64(11);
ns = int64(1);
ne = int64(11);
direct = false;
d = [5;
6;
5;
2;
4;
1;
4;
1;
3;
1;
9;
1;
6;
7;
8;
1;
4;
2;
2;
4];
irow = [int64(1);1;1;2;2;3;3;4;4;5;5;6;6;6;7;8;8;9;9;10];
icol = [int64(2);3;4;3;5;4;6;6;7;6;9;7;8;10;9;9;11;10;11;11];
[splen, path, ifail] = nag_mip_shortestpath(n, ns, ne, direct, d, irow, icol)
```
```

splen =

15

path =

1
4
6
8
9
11
0
0
0
0
0

ifail =

0

```
```function h03ad_example
n = int64(11);
ns = int64(1);
ne = int64(11);
direct = false;
d = [5;
6;
5;
2;
4;
1;
4;
1;
3;
1;
9;
1;
6;
7;
8;
1;
4;
2;
2;
4];
irow = [int64(1);1;1;2;2;3;3;4;4;5;5;6;6;6;7;8;8;9;9;10];
icol = [int64(2);3;4;3;5;4;6;6;7;6;9;7;8;10;9;9;11;10;11;11];
[splen, path, ifail] = h03ad(n, ns, ne, direct, d, irow, icol)
```
```

splen =

15

path =

1
4
6
8
9
11
0
0
0
0
0

ifail =

0

```