Guest User

ClojureBKvsBKP

a guest
Apr 14th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; =================================================================
  2. ;       General graph functions (defines graph structure too)
  3. ; =================================================================
  4.  
  5. ; Creates and empty graph of size n
  6. (defn empty-graph [n]
  7.  (vec (repeat n #{})))
  8.  
  9. ; Adds a directed edge to a given graph
  10. (defn add-directed-edge [g n1 n2]
  11.  (let [n1-neighbors (g n1)]
  12.    (assoc g n1 (conj n1-neighbors n2))))
  13.  
  14. ; Credit Fred Annexstein
  15. ; Adds a "full" edge to a graph (both directions)
  16. (defn add-edge [g [n1 n2]]
  17.  (-> g
  18.      (add-directed-edge n1 n2)
  19.      (add-directed-edge n2 n1)))
  20.  
  21. ; Determines if a given graph contains a self loop
  22. (defn contains-self-loop? [graph]
  23.   (loop [curnode 0
  24.          curset (first graph)
  25.          restsets (rest graph)]
  26.     (cond (= 0 (count restsets)) (contains? curset curnode)
  27.           :else
  28.           (if (contains? curset curnode) true
  29.               (recur (inc curnode) (first restsets) (rest restsets))))))
  30.  
  31. ; ========================================
  32. ;       Used to create random graphs
  33. ; ========================================
  34.  
  35. ; Returns a random edge that does not loop to itself
  36. (defn random-no-loop-edge [n]
  37.   (let [n1 (rand-int n)
  38.         n2 (loop [possn2 (rand-int n)]
  39.              (if (not (= n1 possn2))
  40.                possn2
  41.                (recur (rand-int n))))]
  42.     (vector n1 n2)))
  43.  
  44. ; Creates a random graph with n nodes and e edges
  45. (defn random-graph [n e]
  46.     (reduce add-edge (empty-graph n)
  47.        (for [i (range e)] (random-no-loop-edge n))))
  48.  
  49.  
  50. ; ======================================
  51. ;       Start of the project code
  52. ; ======================================
  53.  
  54. ; ===== Bron Kerbosch algorithm functions =====
  55.  
  56. ; Given a vector of sets = cliques = [#{1 2 3} #{4 5 6}]
  57. ; Will return a vector of cliques that have the maximum clique size
  58. (defn get-max-cliques [cliqueset]
  59.   (cond (= 1 (count cliqueset)) [(first cliqueset)]
  60.         :else
  61.         (loop [maxset (list (first cliqueset))
  62.                curset (first (rest cliqueset))
  63.                restsets (rest (rest cliqueset))]
  64.           (let [curcnt (count curset)
  65.                 maxcnt (count (first maxset))
  66.                 newmax (cond (> curcnt maxcnt) (list curset)
  67.                              (= curcnt maxcnt) (cons curset maxset)
  68.                              :else maxset)]
  69.             (cond (= 0 (count restsets)) newmax
  70.                   :else (recur newmax (first restsets) (rest restsets)))))))
  71.  
  72.  
  73. ; Bron-Kerbosch with no pivot
  74. (defn BK [r p x graph]
  75.   (if (and (empty? p) (empty? x))
  76.     [(set r)]
  77.     (loop [p p, x x, cliques []]
  78.       (if (empty? p)
  79.         cliques
  80.         (let [v (first p)
  81.               nv (graph v)
  82.               cliques (into cliques
  83.                             (BK (into r (list v))
  84.                                 (filter nv p)
  85.                                 (filter nv x)
  86.                                 graph))
  87.               p (rest p)
  88.               x (into x (list v))]
  89.           (recur p x cliques))))))
  90.  
  91. ; Helper function to call Bron-Kerbosch non-pivot
  92. (defn get-BK [graph]
  93.   (BK (set '()) (set (doall (range (count graph)))) (set '()) graph))
  94.  
  95. ; Bron-Kerbosch with pivoting
  96. (defn BKP [r p x graph]
  97.   (if (and (empty? p) (empty? x))
  98.     [(set r)]
  99.     (let [u (first (into p x))
  100.           nu (graph u)
  101.           pnu (remove #(some (partial = %) nu) p)]
  102.       (loop [p p, pnu pnu, x x, cliques []]
  103.         (if (empty? pnu)
  104.           cliques
  105.           (let [v (first pnu)
  106.                 nv (graph v)
  107.                 cliques (into cliques
  108.                               (BKP (into r (list v))
  109.                                    (filter nv p)
  110.                                    (filter nv x)
  111.                                    graph))
  112.                 p (rest p)
  113.                 x (into x (list v))]
  114.             (recur p (rest pnu) x cliques)))))))
  115.  
  116. ; Helper function to call Bron-Kerbosch with pivoting
  117. (defn get-BKP [graph]
  118.   (BKP (set '()) (set (doall (range (count graph)))) (set '()) graph))
  119.  
  120.  
  121.  
  122. ; =======================================
  123. ;  Used to test BK versus BKP algorithms
  124. ; =======================================
  125.  
  126. (defn testallbk [graph]
  127.   (println "Starting test...")
  128.   (print "Timing evaluation of graph with BK :")
  129.   (cond (contains-self-loop? graph) (println "\nGraph contains self loop, terminating")
  130.         :else
  131.         (let [bk1 (time (get-BK graph))]
  132.           (print "Timing evaluation of graph with BKP:")
  133.           (let [bk2 (time (get-BKP graph))]
  134.                 (println "BK  cliques count:" (count bk1) " Largest: " (count (first (get-max-cliques bk1))))
  135.                 (println "BKP cliques count:" (count bk2) " Largest: " (count (first (get-max-cliques bk2))))))))
Advertisement
Add Comment
Please, Sign In to add comment