Given a directed graph, return another directed graph with the
order of the edges reversed.
(defn reverse-graph
"Given a directed graph, return another directed graph with the
order of the edges reversed."
[g]
(let [op (fn [rna idx]
(let [ns (get-neighbors g idx)
am (fn [m val]
(assoc m val (conj (get m val #{}) idx)))]
(reduce am rna ns)))
rn (reduce op {} (:nodes g))]
(struct directed-graph (:nodes g) rn)))
Comments top
No comments for reverse-graph. Log in to add a comment.