-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectedGraph.clj
More file actions
34 lines (22 loc) · 1.86 KB
/
Copy pathConnectedGraph.clj
File metadata and controls
34 lines (22 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(ns ConnectedGraph)
(def input1 #{[1 2] [2 3] [3 1] [4 5] [5 6] [6 4]})
;(def candidate (first (first input1)))
;(println candidate)
;(def nodes (into [] (disj (reduce #(into %1 %2) #{} input1) candidate)))
;(println nodes)
;(defn getNext [s x] (into s (filter #(some #{x} %1) input1)))
;(defn nextLevel [level visited] (apply disj (into #{} (flatten (reduce getNext [] level))) (into level visited)))
;(defn connectedPair? [root cand graph] (loop [visited #{cand} next #{cand}] (cond
; (empty? (nextLevel next visited)) false
; (some #{root} (nextLevel next visited)) true
; :default (recur (into visited next) (nextLevel next visited)))))
(defn connected? [graph] (let [candidate (first (first graph))
nodes (into [] (disj (reduce #(into %1 %2) #{} graph) candidate))
getNext (fn [s x] (into s (filter #(some #{x} %1) graph)))
nextLevel (fn [level visited] (apply disj (into #{} (flatten (reduce getNext [] level))) (into level visited)))
connectedPair? (fn [root cand] (loop [visited #{cand} next #{cand}] (cond
(empty? (nextLevel next visited)) false
(some #{root} (nextLevel next visited)) true
:default (recur (into visited next) (nextLevel next visited)))))
] (if (= 1 (count graph)) true (every? #(connectedPair? candidate %1) nodes))))
(println (connected? input1))