Skip to content

Instantly share code, notes, and snippets.

@ChrisBlom
Created January 4, 2018 16:48
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 ChrisBlom/b8104585ad68c7c803a714c0760352fa to your computer and use it in GitHub Desktop.
Save ChrisBlom/b8104585ad68c7c803a714c0760352fa to your computer and use it in GitHub Desktop.
(defn invert
"inverts a graph represented as a map from source to target nodes"
[graph]
(reduce-kv (fn [acc src trgs]
(if (seq trgs)
(reduce (fn [acc- trg]
(update acc- trg (fnil conj #{}) src))
acc
trgs)
;; to preserve nodes without edges
(assoc acc src #{})))
{}
graph))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment