Skip to content

Instantly share code, notes, and snippets.

@tomjack
Created August 28, 2011 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomjack/b6baf208f18d21c2d88e to your computer and use it in GitHub Desktop.
Save tomjack/b6baf208f18d21c2d88e to your computer and use it in GitHub Desktop.
(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