Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/dijkstras-algorithm.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / dijkstras-algorithm.wiki`
154 lines ยท 2813 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text tarrow " -> "
10 text tcost " cost "
11 text tpath " path"
12 text tnone " unreachable"
13 global wt long 36
14 global dist long 6
15 global prev ulong 6
16 global done uchar 6
17 global stk ulong 6
18 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
19 fn emit a view uchar * to void
20 o = ulong 0
21 while lt o len a bound 4096
22 k = write 1 a o sub len a o
23 if le k 0
24 exit 1
25 end
26 o = add o ulong k
27 end
28 end
29 note emitu writes n in decimal; 20 digits hold every ulong
30 fn emitu n ulong to void
31 d = new uchar 20 0
32 i = ulong 20
33 v = n
34 while true bound 20
35 i = sub i 1
36 put d i uchar add rem v 10 48
37 v = div v 10
38 if eq v 0
39 break
40 end
41 end
42 emit part d i sub 20 i
43 end
44 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
45 fn emiti n long to void
46 if lt n 0
47 emit minus
48 emitu add bitcast ulong not n 1
49 else
50 emitu ulong n
51 end
52 end
53 note emitb writes the single byte c
54 fn emitb c ulong to void
55 d = new uchar 1 0
56 put d 0 uchar c
57 emit d
58 end
59 note edge records a directed edge u -> v of weight w; vertices a..f are 0..5, weight 0 means no edge
60 fn edge u ulong v ulong w long to void
61 put wt add mul u 6 v w
62 end
63 note dijkstra from s: repeatedly settle the unsettled vertex of least known distance (-1 is infinity)
64 note and relax its out-edges; 6 rounds settle every reachable vertex
65 fn dijkstra s ulong to void
66 for i ulong 6
67 put dist i -1
68 put prev i 6
69 put done i 0
70 end
71 put dist s 0
72 for r ulong 6
73 u = ulong 6
74 for i ulong 6
75 if and eq get done i 0 ge get dist i 0
76 if eq u 6
77 u = i
78 else
79 if lt get dist i get dist u
80 u = i
81 end
82 end
83 end
84 end
85 if eq u 6
86 ret
87 end
88 put done u 1
89 for v ulong 6
90 relax u v
91 end
92 end
93 end
94 fn relax u ulong v ulong to void
95 w = get wt add mul u 6 v
96 if gt w 0
97 d = add get dist u w
98 if or lt get dist v 0 lt d get dist v
99 put dist v d
100 put prev v u
101 end
102 end
103 end
104 note route prints the path to t by following prev back to the source through a small stack
105 fn route t ulong to void
106 k = ulong 0
107 v = t
108 while lt v 6 bound 6
109 put stk k v
110 k = add k 1
111 v = get prev v
112 end
113 emit tpath
114 for x ulong k
115 emit sp
116 emitb add 97 get stk sub sub k x 1
117 end
118 end
119 fn main to int
120 edge 0 1 7
121 edge 0 2 9
122 edge 0 5 14
123 edge 1 2 10
124 edge 1 3 15
125 edge 2 3 11
126 edge 2 5 2
127 edge 3 4 6
128 edge 4 5 9
129 dijkstra 0
130 for t ulong 5
131 v = add t 1
132 emitb 97
133 emit tarrow
134 emitb add 97 v
135 if lt get dist v 0
136 emit tnone
137 else
138 emit tcost
139 emiti get dist v
140 route v
141 end
142 emit nl
143 end
144 ret 0
145 end
146
147 {{out}}
148
149 a -> b cost 7 path a b
150 a -> c cost 9 path a c
151 a -> d cost 20 path a c d
152 a -> e cost 26 path a c d e
153 a -> f cost 11 path a c f
154