Dijkstra's algorithm: Difference between revisions
Content added Content deleted
(→{{header|OCaml}}: added translation from c++) |
m (→{{header|REXX}}: corrected comments and DO-END comment labels, changed indentation, simplified logic, added whitespace. -- ~~~~) |
||
Line 1,080:
* elimination of duplications (the cheapest path is chosen)
* a test for a ''no path found'' condition
<lang rexx>/*REXX pgm finds the least costly path between
numeric digits 20 /*must be
$.=copies(9,digits()) /*edge cost; this means
aList='!. @. $. beg fin bestP best$ xx yy' /*"common" variables.*/
@abc='abcdefghijklmnopqrstuvwxyz' /*list of possible vertices. */
verts=0 /*number of vertices. */
edges=0 /*number of edges. */
do jj=1 for length(@abc);
call value translate(_),jj; @@.jj=_
end /*jj*/
Line 1,099:
call def$ d e 6 /*define an edge and its cost. */
call def$ e f 9 /*define an edge and its cost. */
say; say 'number of edges = ' edges
▲fin=e /*the finish vertix. */
say 'number of vertices = '
best$=$.; bestP=
say
if bestP==$. then do; say 'no path found.'; exit; end
say 'best path =' translate(bestP,@abc,123456789) ' cost =' best$
exit /*stick a fork in it, we're done.*/
Line 1,115 ⟶ 1,114:
apath: parse arg pathx 1 p1 2 p2 3; Lp=length(pathx); $=$.p1.p2
if $>=best$ then return
pv=p2;
best$=$; bestP=pathx
return
/*──────────────────────────────────DEF$
def$: parse arg xx yy $ .; if $.xx.yy<$ & $.yy.xx<$ | xx=yy then return
edges=edges+1; verts=verts + ($.xx\==0) + ($.yy\==0)
$.xx=0; $.yy=0; $.xx.yy=$; $.yy.xx=$
say left('',40) "cost of " @@.xx '◄───►' @@.yy " is " $
return
/*──────────────────────────────────PATHS subroutine────────────────────*/
paths: procedure expose (aList);
do kp=1 for xx; _=kp; !.kp=_
end /*kp*/
call .paths 1
return
/*──────────────────────────────────.PATHS subroutine───────────────────*/
.paths: procedure expose (aList);
if ?>yy then do
if
end
else do qq=1 for xx /*build vertex paths recursively.*/
do kp=1 for ?-1
if @.kp==!.qq then iterate qq
end /*kp*/
@.?=!.qq; call .paths ?+1
end /*qq*/
return</lang>
'''output''' when using the input of: <tt> xxx </tt>
|