Created
February 15, 2014 18:56
-
-
Save Chouser/9023572 to your computer and use it in GitHub Desktop.
Paths in a graph
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
(defn paths | |
"Returns a lazy sequence of path vectors from path [<start-node>] to | |
target in graph." | |
[graph target path] | |
(let [this-node (peek path)] | |
(if (= this-node target) | |
[path] | |
(->> (get graph this-node) | |
(filter #(not-any? (fn [edge] (= edge [this-node %])) | |
(partition 2 1 path))) | |
(mapcat #(paths graph target (conj path %))))))) | |
n01se.suxml> (paths '{a [b d], b [c], c [x], d [b]} 'x '[a]) | |
([a b c x] [a d b c x]) | |
n01se.suxml> (paths '{a [b a], b [c], c [x]} 'x '[a]) | |
([a b c x] [a a b c x]) | |
n01se.suxml> (paths '{a [b d], b [c], c [x]} 'x '[a]) | |
([a b c x]) | |
n01se.suxml> (paths '{a [b d], b [c], c [x], x [a x]} 'x '[a]) | |
([a b c x]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment