Created
January 4, 2018 16:48
-
-
Save ChrisBlom/b8104585ad68c7c803a714c0760352fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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