<?xml version="1.0" encoding="UTF-8"?>
<Worksheet><Version major="6" minor="1"/><View-Properties><Zoom percentage="100"/></View-Properties><Styles><Layout alignment="left" bullet="none" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" name="Normal" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Layout alignment="centred" bullet="none" name="Maple Plot"/><Layout alignment="centred" bullet="none" linespacing="0.5" name="Maple Output"/><Font background="[0,0,0]" bold="true" executable="true" family="Monospaced" foreground="[255,0,0]" name="Maple Input" opaque="false" size="12"/><Font background="[0,0,0]" family="Monospaced" foreground="[0,0,255]" name="Line Printed Output" opaque="false" readonly="true" size="12"/><Font background="[0,0,0]" family="Times New Roman" foreground="[0,0,255]" name="2D Output" opaque="false" readonly="true" size="12"/></Styles><Group><Input><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">Jay Pedersen</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">University of Nebraska at Omaha student<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
2005-05-12<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
Project:	Implement Dijkstras Algorithm for determining shortest paths in a network</Font></Font></Font></Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	This is a well-known algorithm.  One place where it is defined is in  'Network Flows'</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	by Ravindra Ahuga, Thomas Magnanti, <Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">and James Orlin, (c) 1993, ISBN 0-13-617549-X.</Font></Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	Storage of a network is done into a 'Forward Star' and 'Reverse Star' data structure.</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	These structures are also defined in 'Network Flows' in section 2.3 (Network</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	Representations).</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	Maple programming is described in "maple 9; Introductory Programming Guide"</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	from Maplesoft; ISBN 189451143-3.  Available from the maplesoft website.</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	Notes: global variables have the first character in upper-case, eg: NodeCount.</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">Input:	The algorithm takes as input a text file which contains one ore more network</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	definitions.<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	The assumption is made that nodes are numberd 1:N, where N is the number</Font></Font></Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	of nodes.</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	1. A network definition begins with a '#START' line.</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	2. Following this a line containing the number of nodes in the network,<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	    the number of arcs in the network and the source node.</Font></Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	    For example: 4,7,5 defines a network with 4 nodes and 7 arcs<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	    and says that node 5 is the source node to find shortest paths from.</Font></Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	3. Following this is a series of lines, one per arc in the network,</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	    which define the arcs in the network.  Each line contains the</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	    start node, end node and cost of traveling the arc.</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	    For example: 2,5,15 defines an arc from 2 to 5 with cost 15.</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	The definition ends with a '#END' line.</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	Example network definion (4 nodes, 6 arcs, source node is 1):</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	#START<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	4 6 1<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	1 2 1<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	1 3 3<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	2 3 2<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	3 2 4<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	2 4 5<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	3 4 9</Font></Font></Font></Font></Font></Font></Font></Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	#END</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	Many networks can be defined in a single file by using multiple</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	#START : #END sequences.</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">Usage:	The subroutine Dijkstras_Algorithm is invoked to determine the shortest</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	paths on the networks defined in the text file.<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	The code can optionally create a graph of the shortest paths of the last</Font></Font></Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	network in the file.  Specifying the make_graph argument as true will</Font></Text-field><Text-field alignment="left" firstindent="0.0" leftmargin="0.0" linebreak="space" linespacing="0.0" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"><Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">	make global variable G contain a graph.  The Maple statement 'draw(G)'<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	can <Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">then be used to show a graphical representation of the shortest paths.<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	Example usage (process graphs in "test1.txt"):<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	Dijstras_Algorithm("test1.txt", true);<Font background="[0,0,0]" bold="true" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" size="12" underline="false">
	draw(G); # graphical representation of shortest paths</Font></Font></Font></Font></Font></Font></Font></Font></Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">restart;
with(networks):</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input"/></Input></Group><Group><Input><Pagebreak/><Text-field layout="Normal" prompt="&gt; " style="Maple Input"># Routine:	LoadNetworkFile
# Abstract:	Reads network definition file and loads into
#		a Forward Star data structure
#
#		File assumed open with file descriptor FileDesc,
#		if EOF reached before any data ... exit cleanly.
#
# Arguments:	none
# Input:	FileDesc - file opened for input
# Output:	NodeCount, ArcCount,
#		SourceNode, SinkNode,
#		FS_tail, FS_head, FS_cost
#		FS_point, FS_rpoint, FS_trace
# Returns:	boolean, true if valid network found and loaded
#
# File syntax:	(1) comment Lines - lines beginning with '#'
#		1st Data Line -
#			node count, arc count, source node
#		2nd : nth Data Lines (arc definitions)
#			from node #, to node #, arc cost

loadNetworkFile := proc ()

    local s, all_done, result, first_dataline_read,
	  file_data, arc_number,
          raw_tail, raw_head, raw_cost,
          node_num, arc_num;

    global FileDesc, NodeCount, ArcCount, SourceNode,
           FS_point, FS_tail, FS_head, FS_cost,
           FS_trace, FS_rpoint;

    # initialize
    result      := true;
    all_done    := false;
    first_dataline_read := false;
    arc_number  := 0;

    # read file
    while (not all_done) do;
        s := readline(FileDesc);
        if (s = 0) then # EOF
            all_done := true;
            fclose(FileDesc);
            if (not first_dataline_read) then
                NodeCount := 0; # indicate EOF condition
                ArcCount  := 0;
                return true; # clean exit, no more definitions
            end if;
	elif (length(s) = 0) then
            ; # ignore blank line
        elif (s = "#END") then  # end of this definition
            all_done := true;
        elif (s[1] = "#") then
	    ; # ignore comment line
        else
	    if (not first_dataline_read) then
                # number of nodes, number of arcs, source, sink
		file_data := sscanf(s, "%d %d %d %d");
                if (file_data = 0) then # parse error
                    all_done := true;
                elif (nops(file_data) &lt;&gt; 3) then # parse error
                    all_done := true;
                elif ((file_data[1] &lt; 1) or (file_data[2] &lt; 1) or
                      (file_data[3] &lt; 1) or (file_data[3] &gt; file_data[1])) then
                    all_done := true;
                end if;
                if (all_done) then
                    result := false;
                    printf("Invalid data: %s\n", s);
                    printf("Possibly invalid NodeCount, ArcCount, SourceNode, SinkNode\n");
                else
                    NodeCount      := file_data[1];
                    ArcCount       := file_data[2];
		    SourceNode     := file_data[3];
                    raw_tail       := array(1..ArcCount);
                    raw_head       := array(1..ArcCount);
                    raw_cost       := array(1..ArcCount);
                    FS_point       := array(1..NodeCount+1);
                    FS_tail        := array(1..ArcCount);
                    FS_head        := array(1..ArcCount);
                    FS_cost        := array(1..ArcCount);
                    FS_trace       := array(1..ArcCount);
                    FS_rpoint      := array(1..NodeCount+1);
                    first_dataline_read := true;
                end if;
	    else
                # arc definition - from node, to node, cost, arc capacity
                arc_number := arc_number + 1;
                if (arc_number &gt; ArcCount) then
                    printf("Invalid data: too many arc definitions, &gt; %d\n",
                           ArcCount);
                    all_done := true;
                    result := false;
                else
		    file_data := sscanf(s, "%d %d %d");
                    if (file_data = 0) then # EOF
                        all_done := true:
                    elif (nops(file_data) &lt;&gt; 3) then
                        all_done := true;
                    elif ((file_data[1] &lt; 1) or (file_data[1] &gt; NodeCount) or
                            (file_data[2] &lt; 1) or (file_data[2] &gt; NodeCount) or
                            (file_data[3] &lt; 0)) then
                        all_done := true;
                    end if;
                    if (all_done) then
                        result := false;
                        printf("Invalid arc definition: %s\n", s);
                        printf("Possibly invalid node number or cost\n");
                    else
                        raw_tail[arc_number] := file_data[1];
                        raw_head[arc_number] := file_data[2];
                        raw_cost[arc_number] := file_data[3];
                    end if;
                end if;
	    end if;
        end if;
    end;

    # validation of data
    if (result = true) then
        if (arc_number &lt;&gt; ArcCount) then
            printf("Invalid data: wrong number of arc definions, %d &lt;&gt; %d\n",
                   arc_number, ArcCount);
            result := FALSE;
        end if;
    end if;

    # Load into forward-star data structure
    if (result = true) then
        # forward-direction, arcs emanating from node i
        arc_number := 0;
        for node_num to NodeCount do;
            FS_point[node_num] := arc_number + 1;
            for arc_num to ArcCount do;
                if (raw_tail[arc_num] = node_num) then
                    arc_number := arc_number + 1;
                    FS_tail[arc_number] := raw_tail[arc_num];
                    FS_head[arc_number] := raw_head[arc_num];
                    FS_cost[arc_number] := raw_cost[arc_num];
                end if;
            end;
        end;
        FS_point[NodeCount+1] := ArcCount + 1;
        # reverse-direction, arcs coming into node i
        arc_number := 0;
        for node_num to NodeCount do;
            FS_rpoint[node_num] := arc_number + 1;
            for arc_num to ArcCount do;
                if (FS_head[arc_num] = node_num) then
                    arc_number := arc_number + 1;
                    FS_trace[arc_number] := arc_num;
                end if;
            end;
        end;
        FS_rpoint[NodeCount+1] := ArcCount + 1;
    end if;

    # all done
    return result;

end proc: # loadNetworkFile</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input"/><Pagebreak/><Text-field layout="Normal" prompt="&gt; " style="Maple Input"># Routine:	ShowArcs
# Abstract:	Display Arcs in network, debugging routine
#               example of manipulating forward-star structure
# Arguments:	none
# Input:	Forward-Star network defintion, vbls FS_x
# Output:	Display to screen

ShowArcs := proc()

    local node_num, arc_num;

    global NodeCount, ArcCount,
           FS_point, FS_tail, FS_head,
           FS_trace, FS_rpoint;

    printf("Forward arcs:\n\n");
    for node_num to NodeCount do;
        printf ("From %d:\n", node_num);
        for arc_num from FS_point[node_num] to
                        (FS_point[node_num+1] - 1) do;
            printf("%d. %d - %d\n",
                   arc_num, FS_tail[arc_num],
                   FS_head[arc_num]);
        end;
    end;

    printf("\nBackward arcs:\n\n");
    for node_num to NodeCount do;
        printf("To %d:\n", node_num);
        for arc_num from FS_rpoint[node_num] to
                        (FS_rpoint[node_num+1]-1) do;
            printf("%d. %d - %d\n",
               arc_num, FS_tail[FS_trace[arc_num]],
               FS_head[FS_trace[arc_num]]);
        end;
    end;

    # all done
    return true;

end proc: # ShowArcs</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input"/></Input></Group><Group><Input><Pagebreak/><Text-field layout="Normal" prompt="&gt; " style="Maple Input">Dijkstras_Algorithm := proc (target_filename::string, make_graph::boolean)

    local s :: set, dist, pred, i :: integer, j :: integer,
          in_S :: integer, graph_count :: integer,
          arc_num :: integer, testval :: integer, found_one :: boolean,
          result :: boolean, sink :: integer;

    global NodeCount, ArcCount, FS_tail, FS_head, FS_cost,
           FS_point, FS_rpoint, FS_trace, G, FileDesc;

    # open file (exits procedure if fails)
    FileDesc := fopen(target_filename, READ, TEXT);

    graph_count := 0;

    do
        # read network definition
        result := loadNetworkFile();
        if (not result) then
            return result;  # false
        elif (NodeCount = 0) then # EOF
            return true; # clean exit
        end if;

        graph_count := graph_count + 1;

        # create necessary arrays
        pred := array(1..NodeCount, [seq(0,i=1..NodeCount)]);  	   # predecessor of each node
        dist := array(1..NodeCount, [seq(infinity,i=1..NodeCount)]);	# dist to of each node

        # process
        i := SourceNode; dist[i] := 0; pred[i] := 0; in_S := 0; s := { };
        while (in_S &lt; NodeCount) do

            # find minimum distance node, not in s
            found_one := false;
            for j to NodeCount do;
                if ((dist[j] &lt;&gt; infinity) and (not (j in s))) then
                    if (not found_one) then
                        found_one := true;
                        i := j;
                        testval := dist[j];
                    elif (dist[j] &lt; testval) then
                        i := j;
                        testval := dist[j];
                    end if;
                end if;
            end;

            # sanity check
            if (not found_one) then
                printf("Aborting Dijkstras Algorithm, next node not determined.\n");
                printf("Network : %d\n", graph_count);
                printf("Possible network definition issue, fully connected?\n");
                return false;
            end if;

            # add next item to list
            s := s union { i };
            in_S := in_S + 1;
            sink := i;

            # process all arcs emanating from i
            for arc_num from FS_point[i] to
                            (FS_point[i+1] - 1) do;
                j := FS_head[arc_num];
                if (not (j in s)) then
                    testval := dist[i] + FS_cost[arc_num];
                    if (testval &lt; dist[j]) then
                        dist[j] := testval;
                        pred[j] := i;
                    end if;
                end if;
            end;

        end; # while (in_S &lt; NodeCount)

	# At this point, shortest paths for graph is determined.

        printf("\nGraph %d shortest paths from node %d:\n\n",
               graph_count, SourceNode);
        printf("  Node   Dist   Prev\n");
        for i to NodeCount do;
            if (i &lt;&gt; SourceNode) then
                printf ("%6d %6d %6d\n", i, dist[i], pred[i]);
            end if;
        end;
        printf("\n");

        # create graph G if requested
        if (make_graph) then
            new(G):
            addvertex(seq(i,i=1..NodeCount),G);
            for i to NodeCount do;
                if (i &lt;&gt; SourceNode) then
                    j := pred[i];
                    connect({i},{j},G);
                end if;
            end;
        end if;

    end;

    return true;

end proc: # Dijkstras_Algorithm
</Text-field><Pagebreak/></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input"># test
Dijkstras_Algorithm("test1.txt", true);</Text-field></Input><Output><Text-field layout="Normal" style="Line Printed Output">Graph 1 shortest paths from node 1:

  Node   Dist   Prev
     2      1      1
     3      3      1
     4      6      2


Graph 2 shortest paths from node 2:

  Node   Dist   Prev
     1     13      4
     3      2      2
     4      5      2


Graph 3 shortest paths from node 1:

  Node   Dist   Prev
     2      1      1
     3     10      1
     4     15      3


Graph 4 shortest paths from node 1:

  Node   Dist   Prev
     2      1      1
     3      3      2
     4      8      3


Graph 5 shortest paths from node 1:

  Node   Dist   Prev
     2      5      1
     3      9      1
     4     14      6
     5      4      1
     6      7      5
     7      9      5
     8     14      1
     9     10      5
</Text-field><Text-field layout="Maple Output" style="2D Output"><Equation>NiNJJXRydWVHSSpwcm90ZWN0ZWRHRiQ=</Equation></Text-field></Output></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">draw(G);</Text-field></Input><Output><Text-field layout="Maple Plot"><Plot height="400" plot-scale="1.0" plot-xtrans="0.0" plot-ytrans="0.0" type="two-dimensional" width="400">LSUlUExPVEc2PS0lJ1BPSU5UU0c2IzckJCErKCoqKioqKipcISM1JCIrU1NEZycpRistRiY2IzckJCIiIiIiISRGM0YzLSUlVEVYVEc2JDckJCIyLSsrKysrKz8iISM7JEYzISIiUSIxNiItRiY2IzckJCIrSldXZ3dGKyQiKyg0d3lVJ0YrLUY2NiQ3JCQiK0pXV2cnKkYrRkVRIjJGPy1GJjYjNyQkIit0PFtPPEYrJCIrSnYyWykqRistRjY2JDckJCIrdDxbT1BGK0ZSUSIzRj8tRjY2JDckJCErKCoqKioqKipwRiskIiovYS1tKSEiKlEiNEY/LUYmNiM3JCQhKzJpI3BSKkYrJCIrTjk/P01GKy1GNjYkNyQkIStARXBSNkZbbyQiMi8rK11WLC1VJCEjPFEiNUY/LUYmNiM3JCQhKzNpI3BSKkYrJCErTDk/P01GKy1GNjYkNyRGZ29GYnBRIjZGPy1GJjYjNyQkISspKioqKioqKlxGKyQhK1JTRGcnKUYrLUY2NiQ3JCQhKykqKioqKioqcEYrRl1xUSI3Rj8tRiY2IzckJCIrInkiW088RiskIStIdjJbKSpGKy1GNjYkNyQkIisieSJbT1BGK0ZqcVEiOEY/LUYmNiM3JCQiK0hXV2d3RiskISsraCh5VSdGKy1GNjYkNyQkIitIV1dnJypGKyQhMSsrKytoKHlVJ0Y7USI5Rj8tJSdDVVJWRVNHNiQ3JEYwRkItJSZDT0xPUkc2JiUkUkdCR0Y8JCIjNUY9RjwtRmJzNiQ3JEYwRk9GZXMtRmJzNiQ3JEYoRl9wRmVzLUZiczYkNyRGMEZfb0Zlcy1GYnM2JDckRl9vRl9wRmVzLUZiczYkNyRGX29GanBGZXMtRmJzNiQ3JEYwRmdxRmVzLUZiczYkNyRGX29GZHJGZXMtJSpBWEVTU1RZTEVHNiMlJU5PTkVH</Plot></Text-field></Output></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input"/></Input></Group><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/></Worksheet>