7QiF9pQ1oe3J5KgTEKwDII changeset

Changeset393735356538 (b)
ParentNone (a)
ab
0+(
0+ (fn tour?
0+   ([g] (some boolean (map (partial tour? g) (set (flatten g))))) ; try each node as starting point
0+   ([g r] ; is the graph visitable from the "r" root node?
0+        (let [connected? (fn [n [x y]] (cond (= n x) y (= n y) y)) ; return the connected node, if any
0+          connected (keep-indexed ; return the nodes connected to the root, and their index
0+                     #(let [conn (connected? r %2)]
0+                        (if conn [conn %1])) g)
0+          candidates (map (fn [[n i]] [(concat (take (dec i) g) (drop i g)) n]) connected)]
0+         (or (empty? g)
0+             (and (not (empty? connected))
0+                  (some boolean (map #(apply tour? %) candidates)))))))
0+[[:a :b] [:a :c] [:c :b] [:a :e]
0+              [:b :e] [:a :d] [:b :d] [:c :e]
0+              [:d :e] [:c :f] [:d :f]])
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
--- Revision None
+++ Revision 393735356538
@@ -0,0 +1,15 @@
+(
+ (fn tour?
+ ([g] (some boolean (map (partial tour? g) (set (flatten g))))) ; try each node as starting point
+ ([g r] ; is the graph visitable from the "r" root node?
+ (let [connected? (fn [n [x y]] (cond (= n x) y (= n y) y)) ; return the connected node, if any
+ connected (keep-indexed ; return the nodes connected to the root, and their index
+ #(let [conn (connected? r %2)]
+ (if conn [conn %1])) g)
+ candidates (map (fn [[n i]] [(concat (take (dec i) g) (drop i g)) n]) connected)]
+ (or (empty? g)
+ (and (not (empty? connected))
+ (some boolean (map #(apply tour? %) candidates)))))))
+[[:a :b] [:a :c] [:c :b] [:a :e]
+ [:b :e] [:a :d] [:b :d] [:c :e]
+ [:d :e] [:c :f] [:d :f]])