From e1b343b13cbd47032ee858d733ae50594820d527 Mon Sep 17 00:00:00 2001 From: David Rupp Date: Sat, 13 May 2017 14:20:43 -0400 Subject: [PATCH] give everything meaningful names --- src/graffy/core.clj | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/graffy/core.clj b/src/graffy/core.clj index fcbd014..7f39d48 100644 --- a/src/graffy/core.clj +++ b/src/graffy/core.clj @@ -1,32 +1,35 @@ (ns graffy.core) -(defn add-edge [g s d] - (-> g - (assoc s (or (g s) (sorted-set))) - (assoc d (or (g d) (sorted-set))) - (update-in [s] conj d) - (update-in [d] conj s))) +(defn add-edge + "graph: a map of sources to sets of dests; adds a bi-directional edge" + [graph source dest] + (-> graph + (assoc source (or (graph source) (sorted-set))) + (assoc dest (or (graph dest) (sorted-set))) + (update-in [source] conj dest) + (update-in [dest] conj source))) -(defn vertices [g] - (keys g)) +(defn vertices [graph] + (keys graph)) -(defn neighbors [g n] - (seq (or (g n) +(defn neighbors [graph node] + (seq (or (graph node) (sorted-set)))) -(defn- traverse [g n strategy] +(defn- traverse [graph node strategy] (assert (#{:bf :df} strategy) "Strategy must be one of :bf (breadth-first) or :df (depth-first)") - (loop [acc [] stack (seq [n])] + (loop [acc [] + stack (seq [node])] (if (zero? (count stack)) acc (if (some #{(first stack)} acc) (recur acc (rest stack)) (if (= strategy :df) - (recur (conj acc (first stack)) (concat (neighbors g (first stack)) (rest stack))) - (recur (conj acc (first stack)) (concat (rest stack) (neighbors g (first stack))))))))) + (recur (conj acc (first stack)) (concat (neighbors graph (first stack)) (rest stack))) + (recur (conj acc (first stack)) (concat (rest stack) (neighbors graph (first stack))))))))) -(defn dft [g n] - (traverse g n :df)) +(defn dft [graph node] + (traverse graph node :df)) -(defn bft [g n] - (traverse g n :bf)) +(defn bft [graph node] + (traverse graph node :bf))