Guest User

Untitled

a guest
Oct 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. (ns ideal.tools.graph)
  2.  
  3. (defn add-edge
  4. [graph v w]
  5. (update graph v (fnil conj #{}) w))
  6.  
  7. (defn all-children
  8. [graph key]
  9. (if-let [first-children (get graph key)]
  10. (into #{}
  11. (concat first-children
  12. (mapcat #(all-children graph %) first-children)))
  13. #{}))
  14.  
  15. (defn all-vertices
  16. [graph]
  17. (into #{}
  18. (concat (keys graph)
  19. (apply concat (vals graph)))))
  20.  
  21. (defn all-edges
  22. [graph]
  23. (into #{}
  24. (reduce-kv (fn [acc k vs]
  25. (concat acc (map #(vector k %) vs)))
  26. #{}
  27. graph)))
  28.  
  29. (defn invert
  30. [graph]
  31. (into {}
  32. (reduce-kv (fn [acc k vs]
  33. (reduce #(add-edge %1 %2 k) acc vs))
  34. {}
  35. graph)))
Add Comment
Please, Sign In to add comment