-
-
Save tomjack/b6baf208f18d21c2d88e to your computer and use it in GitHub Desktop.
This file contains 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
(def graph {:a [:b :c :g] | |
:b [:e :g] | |
:c [] | |
:d [] | |
:e [:a] | |
:f [:c :d] | |
:g [:e]}) | |
(defn find-paths [graph from to] | |
(let [children (fn [[path visited node]] | |
(->> node | |
graph | |
(remove visited) | |
(map #(vector (conj path %) | |
(conj visited %) | |
%))))] | |
(->> ((juxt vector hash-set identity) from) | |
(tree-seq children children) | |
(filter (comp #{to} peek)) | |
(map first)))) | |
(comment | |
user> (find-paths graph :a :e) | |
([:a :b :e] [:a :b :g :e] [:a :g :e]) | |
user> (find-paths graph :g :d) | |
() | |
user> (find-paths graph :g :a) | |
([:g :e :a])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment