-
-
Save tomjack/1d43fb74aaf2d16b0ecf 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]] | |
(map (fn [child] | |
[(conj path child) | |
(conj visited child) | |
child]) | |
(remove visited (graph node))))] | |
(map first (filter (comp #{to} peek) | |
(tree-seq children children [[from] #{from} from]))))) | |
(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
I’ve finally found the time to fully understand your code and it’s really elegant and taught me a lot. Thanks.