Skip to content

Instantly share code, notes, and snippets.

@ohAitch
Last active June 22, 2016 18:23
Show Gist options
  • Select an option

  • Save ohAitch/53e221ff6bb5c122234086b62c009a73 to your computer and use it in GitHub Desktop.

Select an option

Save ohAitch/53e221ff6bb5c122234086b62c009a73 to your computer and use it in GitHub Desktop.
:: Rosettacode dijkstra algorithm, translated from LUA
::
:::: /hoon/dijkstra/gen
::
|%
:: Graph type
++ a-graph (map term (map term @u))
--
|%
++ math-huge `(unit @u)`~ :: use actual maybe type
++ lth-u |=({a/@u b/(unit @u)} ?~(b & (lth a u.b))) :: math-huge semantics
::
:: Example graph definition
++ edges
^- a-graph
%- my :~
a+(my b+7 c+9 f+14 ~)
b+(my c+10 d+15 ~)
c+(my d+11 f+2 ~)
d+(my e+6 ~)
e+(my f+9 ~)
==
::
:: Fill in paths in the opposite direction to the stated edges
++ complete
|= graph/a-graph ^+ graph
%+ roll (~(tap by graph))
=+ [[node=*term edges=*(map term @u)] graph=graph] |. ^+ graph
%+ roll (~(tap by edges))
=+ [[edge=*term distance=*@u] graph=graph] |. ^+ graph
%+ ~(put by graph) edge
=+ graph-edge-u=(~(get by graph) edge)
=+ graph-edge=?^(graph-edge-u u.graph-edge-u *(map term @u))
(~(put by graph-edge) node distance)
::
:: Create path string from table of previous nodes
++ follow
|= {trail/(map term term) destination/term}
=+ [path next-step]=[`(list term)`~[destination] (~(get by trail) destination)]
|-
?~ next-step
path
%_ $
path [u.next-step path]
next-step (~(get by trail) u.next-step)
==
::
:: Find the shortest path between the current and destination nodes
++ dijkstra
|= {graph/a-graph current/term destination/term directed/?}
:: ~| [graph (complete graph)]
=. graph ?:(directed graph (complete graph))
=| a/{unvisited/(set term) distance-to/(map term @u) trail/(map term term)} :: first locals
=. a
%+ roll (~(tap by graph))
=+ [*{node/term x-edge-dists/*} a] |.
=+ a=+<+ ^+ a
?: =(node current)
%_ a
distance-to (~(put by distance-to) node 0)
trail (~(del by trail) current)
==
%_ a
distance-to (~(del by distance-to) node) :: set to ~
unvisited (~(put in unvisited) node)
==
=| b/{nearest/(unit @u) next-node/term} :: second locals
|-
=. nearest.b math-huge
=/ c :: loop modifications
%+ roll (~(tap by (~(got by graph) current)))
=+ [[neighbour=*term path-dist=*@u] a b] |.
=+ [a b]=+<+ ^+ [a b]
?. (~(has in unvisited) neighbour) [a b]
=+ tentative=(add (~(got by distance-to) current) path-dist)
:- ?. (lth-u tentative (~(get by distance-to) neighbour))
a
%_ a
distance-to (~(put by distance-to) neighbour tentative)
trail (~(put by trail) neighbour current)
==
?. (lth-u tentative nearest)
b
%_ b
nearest `tentative
next-node neighbour
==
=. unvisited.c (~(del in unvisited.c) current)
=. current next-node.c
?. |(!(~(has in unvisited.c) destination) =(nearest.c math-huge))
$(a -.c, b +.c) :: repeat
[(~(get by distance-to.c) destination) (follow trail.c destination)]
--
::
:::: Main procedure
::
:- directed=(dijkstra edges %a %e &)
undirected=(dijkstra edges %a %e |)
::
:: Mysterious boilerplate square
:::::::::::::
:- %say ::
|= {^ ^} ::
:- %noun ::
+> ::
:::::::::::::
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment