Skip to content

Instantly share code, notes, and snippets.

@viperscape
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viperscape/d7a4298477be74feee2c to your computer and use it in GitHub Desktop.
Save viperscape/d7a4298477be74feee2c to your computer and use it in GitHub Desktop.
square grid to relationship graph for loom
(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])
@viperscape
Copy link
Author

@viperscape
Copy link
Author

updated to a much faster version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment