Created
February 2, 2014 00:48
-
-
Save gdevanla/8761497 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;;Computes all possible paths through a graph (DAG) | |
;;assumes the graph is a DAG | |
;; Clojure | |
(defn all-paths [g] | |
(let [walk (fn walk [g seen n] | |
(cond | |
(empty? (g n)) seen | |
:else (mapcat (fn [m] | |
(walk g (conj seen m) m)) (g n))))] | |
(walk g [:u1] :u1))) | |
;;example | |
(all-paths {:u1 [:u2 :u3] :u2 [:u4] :u3 [:u4 :u6] :u4 [:u5] :u6 [:u5 :u7]}) | |
;;(:u1 :u2 :u4 :u5 :u1 :u3 :u4 :u5 :u1 :u3 :u6 :u5 :u1 :u3 :u6 :u7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment