Last active
August 29, 2015 14:04
-
-
Save viperscape/d7a4298477be74feee2c to your computer and use it in GitHub Desktop.
square grid to relationship graph for loom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn gen-graph [s] | |
(graph ;;generate loom graph | |
(reduce merge {} ;; format... | |
(apply concat ;; for loom | |
(for [r (range s)] ;;for each row | |
(for [c (range s)] ;;for each column in the row | |
(let [nl [[r (+ 1 c)] | |
[(- r 1) c] | |
[r (- c 1)] | |
[(+ 1 r) c]]] | |
{[r c] | |
(let [res (for [n nl] | |
(->> n | |
(remove neg?) ;;out of bounds? | |
(remove #(> % (- s 1)))))] ;;out of bounds? | |
(remove #(< (count %) 2) res))}))))))) ;;remove all out of bounds | |
;;example shortest-path using this graph | |
(shortest-path (gen-graph 100) [0 3] [4 5]) ;; ([0 3] [1 3] [2 3] (3 3) [4 3] (4 4) [4 5]) |
updated to a much faster version
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for https://github.com/aysylu/loom