An AMPL Implementation of Dijkstra's Algorithm

The following is an example of how to use the run file dijkstra.txt to run Dijkstra's algorithm on a network represented in the format described by the AMPL model file mcnfp_model.txt. Included in this example is the use of a post-processing routine called find_path.txt that can be used to find the arcs in a shortest path from the source node to a user-specified node i.


In this example, the commands that the user types are in bold, italic type. The data file for this example is dijkstra_data.txt.

ampl: commands dijkstra.txt;
d [*] :=
1  0
2  7
3  5
4  4
5  6
6  9
;

pred [*] :=
2  1
3  4
4  1
5  4
6  5
;

ampl: let i := 6;
ampl: commands find_path.txt;
set NSP := 6 5 4 1;

The shortest path from 1 to 6 has cost 9:
From 1 to 4:	Cost = 4
From 4 to 5:	Cost = 2
From 5 to 6:	Cost = 3
ampl: purge NSP;
ampl: let i := 5;
ampl: commands find_path.txt;
set NSP := 5 4 1;

The shortest path from 1 to 5 has cost 6:
From 1 to 4:	Cost = 4
From 4 to 5:	Cost = 2
ampl: purge NSP;
ampl: commands let i := 3;
ampl: commands find_path.txt;
set NSP := 3 4 1;

The shortest path from 1 to 3 has cost 5:
From 1 to 4:	Cost = 4
From 4 to 3:	Cost = 1
ampl: quit;