Skip to content

Instantly share code, notes, and snippets.

@Plasmoxy
Forked from loganlinn/dijkstra.clj
Created March 21, 2022 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Plasmoxy/396801fb78dc25fa18088b89a0eec1f3 to your computer and use it in GitHub Desktop.
Save Plasmoxy/396801fb78dc25fa18088b89a0eec1f3 to your computer and use it in GitHub Desktop.
Dijkstra's Algorithm in Clojure
(def ^:private inf (Long/MAX_VALUE))
(defn neighbors
([g n] (get g n {}))
([g n uv] (select-keys (neighbors g n) uv)))
(defn update-costs
[g costs curr unvisited]
(let [curr-cost (costs curr)]
(reduce
(fn [c [nbr nbr-cost]] (update-in c [nbr] (partial min (+ curr-cost nbr-cost))))
costs
(neighbors g curr unvisited))))
(defn dijkstra
[g src & {:keys [target]}]
(loop [costs (assoc (zipmap (keys g) (repeat inf)) src 0)
curr src
unvisited (disj (apply hash-set (keys g)) src)]
(if (or (empty? unvisited) (= inf (costs curr)))
costs
(let [costs' (update-costs g costs curr unvisited)
curr' (first (sort-by costs' unvisited))]
(if (= target curr)
(costs' target)
(recur costs'
curr'
(disj unvisited curr')))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment