Skip to content

Instantly share code, notes, and snippets.

@0racle
Last active April 29, 2025 11:33
Show Gist options
  • Save 0racle/69801132ce3178423701a7f93c10ecf9 to your computer and use it in GitHub Desktop.
Save 0racle/69801132ce3178423701a7f93c10ecf9 to your computer and use it in GitHub Desktop.
Greedy Map Coloring
require 'viewmat plot'
colorxy =: colorxy_jzplot_

dm =. ,~ 200                    NB. dimensions
np =. 50                        NB. number of points
ps =. ? np $ ,: dm              NB. points

NB. Voronoi tesselation
Dist =: +/"1@(*:@-"1/)
map =: (i. <./)"1 ((#: i.) dm) Dist ps

NB. Adjacency map
Shift4 =: ((, -) =/~ i. 2)&(|.!.0)
AdjMap =: [ e."1 ([ -.~ ] ~.@#&,~ +./@Shift4@:=)"0 _
NB. Need to avoid 0 when building adjacency map
nodes =. /:~ ~. , map
adj =. nodes AdjMap&:>: nodes i. map

ord =. \: +/"1 adj              NB. degree order
col =. (# nodes) $ < i. 8       NB. possible colors

Prune =: {{ (I. x { m) (-.&({. x {:: y))&.>@:{`[`]} y }}
col =. col ]F..(adj Prune) ord
echo ' colors',~ ": # ~. {.@> col

pal =. 10 colorxy 0.9 0.9 0.1,0.1 0.5 0.5,:0.2 0.0 0.3
pal viewmat (nodes i. map) { {.@> col 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment