Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created February 15, 2014 18:56
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 Chouser/9023572 to your computer and use it in GitHub Desktop.
Save Chouser/9023572 to your computer and use it in GitHub Desktop.
Paths in a graph
(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