Skip to content

Instantly share code, notes, and snippets.

@raek
Created July 18, 2010 18:53
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 raek/480615 to your computer and use it in GitHub Desktop.
Save raek/480615 to your computer and use it in GitHub Desktop.
(defn breadth-first-traversal
([g start]
(breadth-first-traversal g [start] [[] #{}]))
([g queue [path visited]]
(if (seq queue)
(let [node (first queue)
next (remove visited (neighbors g node))]
(lazy-seq (breadth-first-traversal g (into (subvec queue 1) next)
[(conj path node)
(into (conj visited node) next)])))
path)))
(defn breadth-first-traversal
([g start]
(breadth-first-traversal g [start] #{}))
([g queue visited]
(lazy-seq
(if (seq queue)
(let [node (first queue)
next (remove visited (neighbors g node))]
(cons node (breadth-first-traversal g (into (subvec queue 1) next)
(into (conj visited node) next))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment