Revision 323637636337 () - Diff

Link to this snippet: https://friendpaste.com/7QiF9pQ1oe3J5KgTEKwDII
Embed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)] ; candidates are "new" root nodes, and the remaining graphs
(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]])