Dijkstra's algorithm: Difference between revisions
Content added Content deleted
Line 2,706: | Line 2,706: | ||
<pre>shortest path from a to e has cost 20: |
<pre>shortest path from a to e has cost 20: |
||
a -> c -> f -> e</pre> |
a -> c -> f -> e</pre> |
||
=={{header|SAS}}== |
|||
Use network solver in SAS/OR: |
|||
<lang sas>/* create SAS data set */ |
|||
data Edges; |
|||
input Start $ End $ Cost; |
|||
datalines; |
|||
a b 7 |
|||
a c 9 |
|||
a f 14 |
|||
b c 10 |
|||
b d 15 |
|||
c d 11 |
|||
c f 2 |
|||
d e 6 |
|||
e f 9 |
|||
; |
|||
/* call OPTMODEL procedure in SAS/OR */ |
|||
proc optmodel; |
|||
/* declare sets and parameters, and read input data */ |
|||
set <str,str> LINKS; |
|||
num cost {LINKS}; |
|||
read data Edges into LINKS=[start end] cost; |
|||
set NODES = union {<i,j> in LINKS} {i,j}; |
|||
set SOURCES = {'a'}; |
|||
set SINKS = {'e'}; |
|||
/* <source,sink,order,from,to> */ |
|||
set <str,str,num,str,str> PATHS; |
|||
/* call network solver */ |
|||
solve with network / |
|||
shortpath=(source=SOURCES sink=SINKS) links=(weight=cost) out=(sppaths=PATHS); |
|||
/* write shortest path to SAS data set */ |
|||
create data path from [source sink order from to]=PATHS cost[from,to]; |
|||
quit; |
|||
/* print shortest path */ |
|||
proc print data=path; |
|||
run;</lang> |
|||
Output: |
|||
<pre> |
|||
Obs source sink order from to cost |
|||
1 a e 1 a c 9 |
|||
2 a e 2 c f 2 |
|||
3 a e 3 e f 9 |
|||
</pre> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |