Permalink
Browse files

Transitive Closure (p84) solution.

  • Loading branch information...
1 parent 924f6da commit c4bd7a36520f2b1a56b31265d742a433666715a7 @amcnamara committed Aug 28, 2011
Showing with 33 additions and 0 deletions.
  1. +33 −0 4Clojure/084.clj
View
@@ -0,0 +1,33 @@
+;; See http://www.4clojure.com/problem/84
+
+;; Write a function which generates the transitive closure of a binary relation.
+;; The relation will be represented as a set of 2 item vectors.
+
+;; NOTE: This solution doesn't check loops in the transitive graph in order to
+;; save space in the solution. To catch this case, simply break the loop
+;; if val is already contained in the return collection.
+
+(fn [coll]
+ (letfn [(map-set [set] (reduce into (map #(apply hash-map %) set)))]
+ (set
+ (reduce into
+ (for [node coll]
+ (map #(vector (first node) %)
+ (loop [r [] val (first node)]
+ (let [next-node (get (map-set coll) val)]
+ (if next-node
+ (recur (conj r next-node) next-node)
+ r)))))))))
+
+
+;; Golf score: 147 (minified)
+(fn [c]
+ (set
+ (reduce into
+ (for [i c]
+ (map #(vector (first i) %)
+ (loop [r [] n (first i)]
+ (let [j (get (reduce into (map #(apply hash-map %) c)) n)]
+ (if j
+ (recur (conj r j) j)
+ r))))))))

0 comments on commit c4bd7a3

Please sign in to comment.