Skip to content

Instantly share code, notes, and snippets.

@cshepp
Last active August 29, 2015 14:17
Show Gist options
  • Save cshepp/43cb2c600058beba68a1 to your computer and use it in GitHub Desktop.
Save cshepp/43cb2c600058beba68a1 to your computer and use it in GitHub Desktop.
(defn find-cycle
"finds a single cycle in the parents
starting at the seed index"
[a b seed]
(loop [coll (mapv vector a b)
cyl #{}
cur (get-in coll [seed 0])]
(let [idx (first (keep-indexed #(if (= (first %2) cur) %1) coll))]
(if (nil? idx)
(vec cyl)
(recur (vec-remove coll idx)
(into cyl (nth coll idx))
(last (nth coll idx)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment