Simplified "Gerrymandering"
 
This should work with recent desktop versions of Chrome or Firefox. It was coded using Vanilla JS.
 
The task of the program is to divide a set of squares into n regions of n squares such that the black (filled) dots "win" more regions than the white dots. (Note that draws are possible if n is even.) A region is defined as a connected set of squares and two squares are connected if they share an edge (and not only a corner).
 
Press one of the keys from 3 to 9 to set the value of n and click on individual squares to flip the dot colors. Press p for a random assignment and c to set all dots to white. Press r to start the algorithm and Space to terminate it. You can use the up/down arrows to change the "margin": A margin of k means that black has to win k more regions than white.
 
The actual algorithm is realized as a web worker running in the background while the main thread shows configurations tried by the program one by one. The algorithm will usually be faster than the display process. You can change the display speed using the left/right arrows and you can skip pending displays using the s key. The square is framed in red while the algorithm is running. If the frame becomes green without the user terminating the program, either a solution is shown or the whole square is colored red because the algorithm couldn't solve the problem. Press w to reset the background to white.
 

 
Copyright (c) 2019, Prof. Dr. Edmund Weitz. Impressum, Datenschutzerklärung.
 
 
Margin: 1