From b7a39f05b2af22bedf6fc441d536d5d37722b066 Mon Sep 17 00:00:00 2001 From: Chris Bowdon Date: Mon, 31 Dec 2018 18:12:01 +0000 Subject: [PATCH] Optimize degeneracy ordering - calculate the updated degrees and update the node-deg map in a single pass - remove an unused loop parameter --- src/loom/alg.cljc | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/loom/alg.cljc b/src/loom/alg.cljc index b3008b1..a6ff9a1 100644 --- a/src/loom/alg.cljc +++ b/src/loom/alg.cljc @@ -663,25 +663,19 @@ can use these functions." (defn degeneracy-ordering "Returns sequence of vertices in degeneracy order." [g] + ;; The strategy here is to repeatedly remove the node with smallest + ;; degree in the remaining subgraph. (loop [ordered-nodes [] node-degs (->> (zipmap (nodes g) (map (partial out-degree g) (nodes g))) - (into (pm/priority-map))) - k 0] + (into (pm/priority-map)))] (if (empty? node-degs) ordered-nodes - (let [[n deg] (first node-degs) - ;; This will be the adjacent nodes still in node-degs (not in ordered-nodes) decr'd by 1 - updated-degs (->> (map (juxt identity node-degs) (successors g n)) - (filter second) - (map (juxt first (comp dec second))) - (into {}))] + (let [[n _] (first node-degs)] (recur (conj ordered-nodes n) - (reduce (fn [n-ds [n d]] ;; Update this assoc'ing the updated-degs found above - (assoc n-ds n d)) + (reduce (fn [m n'] (if (contains? m n') (update m n' dec) m)) (dissoc node-degs n) - updated-degs) - (max k deg)))))) + (successors g n))))))) (defn- bk-gen [g [r p x] stack] (let [v-pivot (reduce (partial max-key (partial out-degree g)) p)]