Skip to content

Instantly share code, notes, and snippets.

@bracki
Created February 22, 2009 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bracki/68690 to your computer and use it in GitHub Desktop.
Save bracki/68690 to your computer and use it in GitHub Desktop.
(use 'clojure.contrib.seq-utils)
(def graph {:a [:b], :b [:a :c], :c [:b :d], :d [:c]})
(defn find-path
"Find a path between a start and an end node."
([g start end]
(if (contains? g start)
(find-path g start end [])))
([g start end path]
(let [p (conj path start)]
(if (= start end)
p
(continue-path g (g start) end p)))))
(defn continue-path [g [n & ns] end path]
(if n
(or (and (not (includes? path n))
(find-path g n end path))
(recur g ns end path))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment