Algorithmic Redistricting: Elections made-to-order
Video Overview & Insights
If you're a long-time viewer, you'll know that I *really* like random numbers. Today, I'm USING random numbers to generate some distinctly nonrandom maps. I first heard of Monte Carlo simulations in undergraduate computational physics, and have long thought about using it to draw maps with optimized districts. (see "Ising model" for a very similar physics simulation).
Hope everybody likes the video! If anybody has questions about implementing similar algorithms, how the pixel maps I've drawn can't be converted directly to districts because of the realities of the census maps, or how this simulation is actually taking a random walk through a 30,000-dimensional box, ask away!
Corrections:
1ucasvb: I did my napkin statistics very wrong, and the number of ways to give 27921 pixels one of 13 districts is actually 13^27921=10^31102… that’s a LOT
Judah Karesh: The reason some regions on the OpenPrecincts map had no opposing votes was probably a lack of challenging candidate.
Me: Since there are an awful lot of comments taking this video politically, this project was 100% a math/compsci project from my perspective, and in no way should be construed to endorse gerrymandering or our current mechanisms of choosing representatives - the current rules present a fun optimization puzzle I found interesting.
I wrote a program to draw maps of Gerrymandered (or not) congressional districts in the state of North Carolina. I'm not getting into the methods and politics, I just think gerrymandering is a fascinating problem to solve, and the Monte Carlo simulation is great at it! You program in an initial condition, and your "desired election results", and the program handles the rest, adjusting the map pixel-by-pixel until it aligns to your desired metrics (and either a really GOOD map, or a really BAD map).
Sharable GIF:
I posted this on social media in 2021 and here I am in 2026.
https://gfycat.com/illfatedgargantuankusimanse
Extra links for the curious:
1:45 "politicians are really bad at gerrymandering" bro politicians are really bad at everything except convincing the uninformed they know what's best for us 😂 and I mean both parties, every single one of them
https://en.wikipedia.org/wiki/Ising_model
https://en.wikipedia.org/wiki/Markov_chain_Monte_Carlo
As a software engineer that spent way too many years doing Monte Carlo risk analysis software for $100B+ portfolios. The joke lands.
https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm
https://en.wikipedia.org/wiki/Gerrymandering
The perimeter optimization notion that you propose would keep things more honest. I thought of this a few months ago, though not with the mathematical rigor of your examination. I had never heard it proposed before. You did it four years ago! It's good for voters. But bad for politics- so it's unforeseeable that it would happen. Broken system.
Source for election data by precinct (note that precincts and Census tracts are not the same shapes):
openprecincts (update: website link broken now)
I'm here because Virginia tried to enact a ridiculous gerrymandering but fortunately didn't do it legally.
#Gerrymandering #Politics #Math
Music in this video:
Better be careful. Someone will call you racist if they don't like the districts.
I Dunno by grapes is licensed under a Creative Commons Attribution license (https://creativecommons.org/licenses/by/3.0/)
http://ccmixter.org/files/grapes/16626
you know things are going well when this vid gets recommended to you every other year
Arcadia - Wonders by Kevin MacLeod is licensed under a Creative Commons Attribution license (https://creativecommons.org/licenses/by/4.0/)
Source: http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1100326
With recent events, it’s time where this can really be done
Artist: http://incompetech.com/
Dial Up Internet Sound BY SPANAC, Attribution 4.0 International (CC BY 4.0) http://www.freesoundslibrary.com
I was going to write a comment here a while ago about your algorithm ignoring VRA districts, buuuuuut... Well, you method might be legal now!
More User Perspectives
Man the VA Dems didn't even try to be subtle
@jzeppy1I wonder if you can make maps look fair by using how convex the shapes are, taking the points on the border, drawing lines between them, and seeing if the lines are completely on the inside of said shapes. The percentage of lines completely inside the shape can be used as a score for how "convex" the shape is. The one problem I see with this method is that it probably would take a good amount of computing power because it would have to do some edge detection and computing the lines would be intensive. It may also fall into the Perimeter to Area trap, as it may take a few iterations for the ratio to actually improve.
@siddharthbommareddy7954Well, thanks for destroying America
@LlamaLlamaLlamaLlamaChameleonDang bro ur content is sick 🔥. How did u get this level of coding and math modeling experience?
@jiap1164I've been a fan for a long time, and I was super curious when I stumbled upon this video that "approaches politics". You did a really tasteful job of sharing solid and unbiased information on a touchy subject.
Thanks for sharing this with everyone, and taking the risk of your comment section devolving to cavemen.
12:22 does this number include districting that just looks like literal noise--each pixel is a random district, and not necessarily related to its neighbors?
@NobodyYouKnow01bro made a video abt politics and added nothing of value or substance.
@lowerbackpain688question: is it possible to make 13 swing districts? I want chaos
@sirpikapika1129The fairest would be no districts at all. The popular vote should be what counts with a ranked choice system. This is how you solve two parties getting all the votes.
@lonkwuzhere4433How much money has Elon Musk offered you for this code? 🤔
@chairmanbrandoWatching this years later, I LOVE the headphone untangling method and joke. Thank you
@paulpaul1764Hey, I heard that Hyrule Castle Theme in the intro!
@sigmaeagle2551I think "desired election performance" is a better descriptor
@brycechoffi mean at this point it isnt even gerrymandering? its just each individual district voting for x party and the total statewide results not matching the popular vote. and its not even a fptp problem, its just that at the end of the day 1 person has to win the local election.
in a system where people vote for representatives, all of the "gerrymandered" maps you showed are actually fair because they do reflect the politics of the people inside the districts and the districts are sensible
I wonder why that look like Israel country.
@BismarckmistakeHow far can it go if the algorithm doesn't care to make the maps looks reasonable?
@am0rpheusI wonder if something could be done that sort of fixes the electoral college nationally so that the way the electoral college votes always approximates the popular vote as closely as possible, and it's impossible for somebody to lose the popular vote and win the electoral vote.
@KaizenOramaraIt should almost be that you do the state vote, to see how many seats go to each party first. Then you have how many seats eat should have, then you use a program like yours to make a map where as many people are represented by their local seat as possible. The map would happen after the election.. so political maps during the election would be less interesting... but at least it would actually be fair.
@justincoombs9048As I see it there are two core evils with gerrymandering: First of all changing the outcome of an election so it no longer is democratic but maybe equally bad is encouraging balkanization by locking in districts to one part or the other. How can you use the algorithm to prevent both those issues. Looking at your maps it feels like the algorithm may naturally cluster the cities into cities again contributing to a rural/urban balkanisation?
@carlbeethOregon has to be the worst Gerrymandered State of the Union. Criminal theft of voter representation.
@dalegray934I know nothing about statistics and I thought this was an awesome video and I learned a ton! Thanks for making it
@sjoshuasheppardI just realized this was 4 years ago. Can you rerun?
@panthersnbravesAlphaPhoenix just gotta assert his dominance
@someasiandude4797You know in germany we have a two vote system, where you first vote for a party and then a candidate, the party getting seats in respect to their vote share and the candidate directly elected, that alone would pretty much fix the biggest issues of this US electoral system.
@prpr8904My interest in data science and the recent gerrymandering headlines brought me here. Kudos algorithm
@KenB351insanely interresting stuff you did here. It's a problem that is almost complitely exclusiv to America, one of the solution would be multiple member districts with some type of proportional representation. Very unlikely to happen sadly.
@Afifi96Now consider something like city or county borders to try to maintain communities within precincts as much as possible, to make actually administering those elections possible. Algorithms such as you have developed are probably how political majorities would initiate their map creation, then they would apply some creative manipulation to "try" to adjust the maps to make more sense to voters and election administrators while still retaining as much political advantage as possible.
It is sad that something like the modern statistic analysis potential made possible by AI is capable of enhancing the quality of life but is immediately weaponized and turned against the people to exploit them. If technology could only be tamed!
I am just guessing but your map drawing abilities might be much more financially rewarding than explaining science on YouTube. Are you willing to move to Texas or California?
@jeffschroeder4805This is amazing, now do IL.
@beckylight257Dude! Awesome! Last thing to do is make sure you don't draw lines across apartment complexes or literally break up a small neighborhood. It should be a sellable product.
@Off_Duty_StripperThere is no ideal map. No matter what you draw, there's a minority in each district who's vote doesn't count. The only way to represent as many minorities as possible is some variant of rank choice voting for a pool of candidates. So the handful that want Value A can vote for the Value A candidate no matter where they live in the state, and if there are not enough Value A voters then their votes fall to Value B, C, and D candidates. But its nearly impossible to get this because its against the two party's interests.
@DeniseSkidmoreAs interesting as this is, I think my problem with it is the randomness. I do think that algorithmic redistricting is probably the best solution to gerrymandering. We have alot of debates to hold over exact goals and parameters, but the goal should be to have an open source algorithm that if run with the same data and configs always produces the same outcome. That way you know a map was run under those inputs, and not secretly manipulated.
However, I do like alot of the other features of your algorithm and wonder if a clever guy like yourself could figure out how to do them without randomness. Also, one of the objections to algorithmic redistricting in general is that it doesn't respect 'traditional' or 'natural' boundaries. Mind you, it's a fake objection because the new partisan gerrymandered maps don't either.
But I can imagine an algorithm that starts with an initial state where county lines, major highways, city/town/neighborhood boundaries, rivers, etc subdivide the map much more granularly. Then, in order to get to the 'right' number of districts, it starts combining granular pieces that border each other with some priority that tries to prefer to respect varying kinds of boundaries, achieve a linear partisanship curve like you used, and seeks compact shapes, and similar population sizes. I think operating at the 'pixel' level is just too granular.
But I'd love to see you revisit this topic as it really is an INCREDIBLY important topic. The rise of computer aided gerrymandering during the 2000 redistricting cycle is, I believe, a MAJOR factor in what broke our politics. Not the only, but top three for sure. Prior to that, people working with manual maps could only do so much damage with gerrymandering. You claim politicians are bad at gerrymandering. To the contrary, they are outsourcing it to IT consultants already.
But there's a limitation in terms of how much they believe they can get away with in court. That's the current limiting factor. They're WELL aware that they could stuff ALL of their opponents into one district and win the others comfortably. But as reluctant as SCOTUS has been on gerrymandering, state courts have aggressively replaced gerrymandered maps....with their own gerrymandered maps favoring their party instead of the one controlling the legislature. That's why I don't believe judges are the answer. You may get good ones some of the time, but some courts are every bit as craven as legislators.
hi
@glitter_fartcan you use this tool in reverse, feed in political redistricting maps, and get the expected outcome/score of what they did? so people can know just how gerrymandered and where the new voted outcome maps will become?
@theduke49401Great video! I'm assuming this or something similar to it is basically what both parties are using right now in the current fight over control of the house in the 2026 midterms. Maybe it would be worth it for states to shift to the use of a tool like this, under the purview of a transparent and independent districting board, to get politicians out of the re-districting process entirely in every state. Wishful thinking I suppose, but it sounds nice. Maybe then the parties would be forced to start representing the entirety of their constituents instead of concentrating on just a few swing districts and leaving all of the others hanging out to dry. Would potentially reduce the influence of money in political campaigns, as well, as the money would have to spread farther and wider.
@JohnBeaucheminYou have a lot more pixels than the actual number of census districts in NC (2195), if you require contiguous districts, there are even less than 2195^14 maps to consider. You can't have a political map with boundaries that aren't the boundaries of a census district. I love the headphone analogy for annealing, but you might want to use flaking to store your headphones. A super great video, thanx.
@rsaunders5712:20 Your math here is wrong. What you should do is multiply by the 13 disticts for each pixel, giving 13^27921, or about 2.584*10^31102. This number is a little too big for me.
@wilh3lmmusicDemocracy only works when no party has the majority.
@troncooo409I thought you were going to make a simulator to make a histogram of outcomes to test if a proposed map fell within the realm of feasibly random chanced without the intent to gerrymander or improbable to have achieved unless the intent was to gerrymander.
@adamlam3080Behaving.
@msears101