(ns ideal.tools.graph) (defn add-edge [graph v w] (update graph v (fnil conj #{}) w)) (defn all-children [graph key] (if-let [first-children (get graph key)] (into #{} (concat first-children (mapcat #(all-children graph %) first-children))) #{})) (defn all-vertices [graph] (into #{} (concat (keys graph) (apply concat (vals graph))))) (defn all-edges [graph] (into #{} (reduce-kv (fn [acc k vs] (concat acc (map #(vector k %) vs))) #{} graph))) (defn invert [graph] (into {} (reduce-kv (fn [acc k vs] (reduce #(add-edge %1 %2 k) acc vs)) {} graph)))