hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_mip_shortestpath (h03ad)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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, 'nz', nz)
[splen, path, ifail] = nag_mip_shortestpath(n, ns, ne, direct, d, irow, icol, 'nz', nz)

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 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 nag_mip_shortestpath (h03ad) is described in detail in 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, the number of vertices.
Constraint: n2.
2:     ns int64int32nag_int scalar
3:     ne int64int32nag_int scalar
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 – logical scalar
Indicates whether the network is directed or undirected.
direct=true
The network is directed.
direct=false
The network is undirected.
5:     dnz – double array
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. (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 Example.)
Constraint: dk>0.0, for k=1,2,,nz.
6:     irownz int64int32nag_int array
7:     icolnz int64int32nag_int array
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 nag_sparse_real_gen_sort (f11za)):
  • irowk-1<irowk;
  • irowk-1=irowk and icolk-1<icolk, for k=2,3,,nz.
In addition, if direct=true, 1irowkn, 1icolkn and irowkicolk;
  • if direct=false, 1irowk<icolkn.

Optional Input Parameters

1:     nz 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.
Constraints:
  • if direct=true, 1nzn×n-1;
  • if direct=false, 1nzn×n-1/2.

Output Parameters

1:     splen – double scalar
The length of the shortest path between the specified vertices ns and ne.
2:     pathn int64int32nag_int array
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.
3:     ifail int64int32nag_int scalar
ifail=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
On entry,n<2,
orns<1,
orns>n,
orne<1,
orne>n,
orns=ne.
   ifail=2
On entry,nz>n×n-1 when direct=true,
ornz>n×n-1/2 when direct=false,
ornz<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.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

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

Further Comments

nag_mip_shortestpath (h03ad) 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.

Example

This example finds the shortest path between vertices 1 and 11 for the undirected network
function h03ad_example


fprintf('h03ad example results\n\n');

n  = int64(11);
ns = int64(1);
ne = n;
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);

fprintf('Shortest path = ');
j = 1;
while path(j)~=0;
  if j>1
    fprintf('  to  ');
  end
  fprintf('%2d', path(j));
  j = j+1;
end
fprintf('\n\nLength of shortest path = %16.6g\n', splen);


h03ad example results

Shortest path =  1  to   4  to   6  to   8  to   9  to  11

Length of shortest path =               15

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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