Skip to content

Instantly share code, notes, and snippets.

@tomjack
Created August 28, 2011 15:02
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/1d43fb74aaf2d16b0ecf to your computer and use it in GitHub Desktop.
Save tomjack/1d43fb74aaf2d16b0ecf 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]]
(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]))
@Netpilgrim
Copy link

I’ve finally found the time to fully understand your code and it’s really elegant and taught me a lot. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment