Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Inclusion-Exclusion Principle Explorer


How to use
  1. Enter the sizes of the sets and their overlaps in the panel on the right: A|A|, B|B|, the pairwise intersections, and with three sets the triple intersection too.
  2. The 2 sets and 3 sets buttons switch between the three-term formula and the seven-term one.
  3. The large number in each region is how many times that region has been counted so far — not how many elements it holds.
  4. The small grey number underneath is the element count of that region, worked out from the sizes you entered.
  5. Press Next term to apply the terms one at a time, Previous to step back, and Reset to return to zero.
  6. Play steps through the whole formula automatically, and Pause stops it wherever it has reached.
  7. The region being changed by the current term is tinted: green when the term is being added, red when it is being subtracted.
  8. The running total beside the diagram is the value of the formula so far, and the counter reads which term you are on.
  9. The goal is to get every region reading exactly 11. That is the moment the running total equals the size of the union.
  10. A region marked with a cross has a negative element count, which means the sizes you entered cannot describe any real collection of sets.


Times counted

term 0 of 7
ABC011080608040303
added by this termsubtracted by this termalready at 1

Nothing counted yet

Every region reads 00, because no term has been applied.

The big number in each region is how many times that region has been counted, not how many elements it holds. Its element count is the small grey number underneath. The job of the formula is to get every big number to exactly 11: each element counted once, no more and no less.

Press Next term to add A|A|.
Nothing about the sets changes from here — every step moves only the large numbers, each towards exactly one. Learn more about the starting state · Getting started

Sizes

Sizes are cumulative: an intersection field counts every element in that intersection, including any in a bigger one.

The formula

running total
  • +|A|24
  • +|B|20
  • +|C|18
  • |A ∩ B|9
  • |A ∩ C|7
  • |B ∩ C|6
  • +|A ∩ B ∩ C|3
|A ∪ B ∪ C|?






Getting Started

The tool walks the inclusion-exclusion formula one term at a time and shows what each term does to the picture.

Enter the sizes on the right — A|A|, B|B|, and the overlaps — then press Next term repeatedly. Each press applies one term of the formula and updates every region.

Two numbers sit in each region, and mixing them up makes the whole display confusing:

• The large number is how many times that region has been counted so far.
• The small grey number underneath is how many elements the region actually holds.

The large numbers all start at 00 and the entire job of the formula is to get every one of them to exactly 11. When that happens, each element of the union has been counted once and the running total is the answer.

Play runs the whole sequence, Previous steps back, and Reset returns to the beginning — which is the state before any term is applied.

Nothing Counted Yet

Before any term is applied every region reads 00. The picture is drawn, the element counts are known, and the bookkeeping has not started.
ABC011080608040303
Before any term, frozen

Large numbers all zero, small grey element counts already final. Only the bookkeeping changes from here.

This is the state to look at when the two numbers per region are still unfamiliar, because they are maximally different here. The small grey numbers are already final — they are the element counts, fixed by the sizes you entered. The large numbers are all zero, because no term has counted anything.

Nothing about the sets changes from here on. Every step that follows changes only the large numbers, moving each region towards 11.

That framing is what makes the walk-through a proof rather than a demonstration. The claim is not that the formula gives the right answer on these numbers; it is that each region ends at a multiplicity of exactly 11, which forces the total to be the size of the union whatever the sizes happen to be.

Press once and the first term goes in.

Entering the Sizes

The right-hand panel takes the sizes, and with three sets there are seven of them: the three set sizes, the three pairwise intersections, and the triple intersection.

Those seven numbers determine everything else. The tool works backwards from them to the size of each region, which is why the small grey numbers change as soon as you type.

The relationship runs both ways. A region count is what is left after removing the overlaps — the part of AA alone is AABAC+ABC|A| - |A \cap B| - |A \cap C| + |A \cap B \cap C|, which is inclusion-exclusion applied to a single set. The input-mode toggle lets you enter region counts directly instead, and the sizes are then derived from those.

Not every combination of numbers is possible, and the tool says so rather than drawing something incoherent — see sizes that cannot happen.

Set every overlap to zero and the formula has nothing to correct, which is worth seeing on its own: nothing overlaps.

When Nothing Overlaps

Set all the intersections to zero and the sets are pairwise disjoint. Every overlap region holds no elements, and the formula has nothing to correct.
ABC02402000018000000
Every overlap zero, frozen

The three overlap regions hold nothing, so the corrections have nothing to correct and the union is a plain sum.

Here the union is simply the sum: ABC=A+B+C|A \cup B \cup C| = |A| + |B| + |C|. Stepping through confirms it — the three additions do all the work, and the four correction terms each subtract or add zero.

This is worth seeing before the general case, because it isolates what the principle is for. Inclusion-exclusion exists only to repair double counting. With no overlap there is no double counting, and the formula degenerates into the obvious answer.

It also explains why the addition rule for disjoint events in probability needs no correction term while the general one does. Disjointness is exactly the condition that makes the intersections vanish.

Give any one intersection a positive size and the correction terms start doing work — the ordinary case runs from the first addition onward.

Sizes That Cannot Happen

Enter A=10|A| = 10 alongside overlaps that need more room than that, and a region comes out negative. The tool marks it with a cross instead of an element count.
ABC0080608040303
Sizes that cannot happen, frozen

A crossed region has a negative element count — a proof that no collection of sets has these sizes.

A negative region is not a rounding artefact — it is a proof that no collection of sets has the sizes you entered. Region counts are element counts, and no set has a negative number of elements.

Two constraints are being violated, and the second is easy to miss. An overlap can never exceed either set it sits inside, so AB|A \cap B| is at most the smaller of A|A| and B|B|. Beyond that, once the triple overlap is fixed, each pairwise overlap has to leave enough room for what remains — push ABC|A \cap B \cap C| up too far and the pairwise-only slivers go negative while every individual number still looks reasonable.

The crossed region tells you where to start working backwards. This is also why a walk-through can end with regions that are not all 11: the arithmetic is faithful, and it is reporting that the input was impossible — see when the walk-through does not resolve.

Stepping Through the Terms

With three sets the formula has seven terms, applied in a fixed order: the three set sizes first, then the three pairwise intersections, then the triple.

ABC=A+B+CABACBC+ABC|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|


Each press of Next term applies exactly one of these. The regions that term touches are tinted — green while a term is being added, red while one is being subtracted — and their large numbers move by one in that direction.

Watching the order matters, because the story has three distinct phases. The additions build an over-count. The subtractions overshoot in the middle. The final addition repairs the overshoot.

The frozen frames below follow that sequence: a term being added, all the sets in, a term being subtracted, and the two ends, the emptied centre and every region at one.

A Term Being Added

With A|A| and B|B| applied, every region inside those sets has gone up by one. The lens where they meet reads 22, and the tint marks what the current term just touched.
ABC111182608141323
|A| and |B| applied, frozen

The lens reads 2. Elements in both sets were counted once by each, which is the over-count the subtractions exist to fix.

The over-count appears immediately, and it appears for a reason that needs no arithmetic: an element in both AA and BB was counted once when AA was added and once again when BB was added. Nothing was done wrong — adding the sizes is exactly what the first terms are supposed to do.

Regions reading 22 or more are tinted amber to mark them as unfinished. Every subsequent subtraction is aimed at those regions, and at nothing else.

Notice too that regions in exactly one set are already correct at 11, and every later term leaves them alone. The corrections are surgical: each one touches only the regions inside the intersection it names.

Keep pressing and all three sets go in.

All the Sets Are In

Three terms applied, and the running total is too big. Regions in one set read 11, regions in two read 22, and the centre — in all three — reads 33.
ABC111182618242333
All three sets in, frozen

Ones, twos, and a three in the centre: every element counted once per set containing it. The peak of the over-count.

This is the peak of the over-count, and the numbers say exactly how bad it is. Each element has been counted once for every set containing it, so its multiplicity is the number of sets it lies in.

The repair is not a guess. Each region reading 22 lies in exactly one pairwise intersection, so subtracting that intersection brings it to 11. Every over-counted region is reachable by a term the formula already contains, which is why the corrections are the intersections and not something invented.

The centre is the case that does not resolve so neatly. It reads 33 and needs to reach 11, so it needs removing twice — but it lies inside all three pairwise intersections, so the three subtractions will reach it three times. That mismatch is the whole reason the formula has a seventh term.

Watch it happen at a term being subtracted.

A Term Being Subtracted

The first pairwise overlap is being removed. The regions inside ABA \cap B drop by one and are tinted red, and the running total falls.
ABC111181618242323
Subtracting |A ∩ B|, frozen

The A∩B sliver lands on 1 and stops. The centre drops too, from 3 to 2, because it sits inside this overlap as well.

For the region in AA and BB but not CC, this is the end of the story: it was counted twice, one count has been removed, and it now reads 11 and stays there.

For the centre it is not. The centre lies inside ABA \cap B too, so it also drops — from 33 to 22 — even though it needs a different amount of correction than its neighbour. The subtraction cannot distinguish them, because both are inside the intersection being subtracted.

That is the structural problem the formula has to solve. A term aimed at one region unavoidably hits every region contained in it, so corrections overshoot on the deeper overlaps and the overshoot has to be repaired in turn.

Two more subtractions follow, and the centre keeps falling: the centre reaches zero.

The Centre Falls to Zero

All three pairwise subtractions are done. Every region in exactly two sets now reads 11 and is correct — and the centre reads 00.
ABC111181618141303
All three subtractions done, frozen

Counted up three times, removed three times: the centre reads 0, and nothing in all three sets is being counted.

The centre was counted 33 times by the three sets, then removed 33 times by the three pairwise overlaps, because it lies inside every one of them. Three up, three down, and elements in all three sets are currently not being counted at all.

This is the moment that makes the final term inevitable. +ABC+|A \cap B \cap C| is not an adjustment someone found by trial; it is the unique correction that brings a region at 00 back to 11.

It is also where the two most common wrong versions of the formula fail. Stopping here leaves the total short by the size of the triple overlap. Subtracting the triple instead of adding it drives the centre to 1-1, which is visibly impossible.

Both are easy to spot once you are watching the region numbers rather than the algebra — which is the argument for reading the picture at all. One more term and every region reads one.

Every Region Counted Once

The triple overlap is added back and the centre returns to 11. Every region on the diagram now reads exactly 11, and the running total is the size of the union.
ABC111181618141313
All seven terms applied, frozen

Every region reads exactly 1. The running total is now the size of the union, and the proof is finished.

That is the proof, finished. The formula is not a recipe to memorise — it is a bookkeeping correction: add everything, remove what you double counted, put back what you removed too often.

The check is available directly. The element counts of all seven regions sum to the same number the formula produced, because the regions partition the union and each is now counted exactly once.

The pattern continues past three sets without changing character. For four sets you add the four sizes, subtract the six pairs, add the four triples, and subtract the quadruple — alternating by the number of sets in each term, for the reason set out in why the signs alternate.

What generalises is not the list of terms but the requirement they satisfy: every region ends at a multiplicity of one.

Reading the Region Numbers

The large number in a region is its multiplicity — the number of times the terms applied so far have counted it.

Colour tracks what that number means. A region reading exactly 11 is tinted in the accent colour, because it is correct. A region reading 22 or more, or less than 00, is tinted amber, because it is wrong and still needs work. Regions the current term is touching take the term's own colour instead.

Reading the diagram this way turns the formula into a visible process rather than an identity to trust. At any moment you can see which parts of the union are already right and which are still miscounted, and the next term is always aimed at the amber ones.

The small grey number never changes as you step, because the elements do not change — only the bookkeeping does. When a region's element count would be negative the tool prints a cross instead, which is a statement about your input rather than about the step you are on.

Two Sets and Three Sets

The 2 sets and 3 sets buttons switch between two versions of the same idea.

With two sets the formula is short enough to hold in your head:

AB=A+BAB|A \cup B| = |A| + |B| - |A \cap B|


Three terms, one correction, and the whole argument is that the lens in the middle got counted twice and needs removing once.

With three sets the count jumps to seven terms, and a new phenomenon appears that two sets cannot show: the triple overlap is corrected too many times by the pairwise subtractions and has to be added back. That is the smallest case where the alternating signs stop looking arbitrary.

Start on two sets if the pattern is unfamiliar, then switch. The two-set walk-through takes three presses and the three-set one takes seven, and the second makes sense fastest immediately after the first.

Switching sizes is worth doing as well. The shape of the argument does not depend on the numbers, so any sizes that are actually possible will produce the same sequence of over-count and repair — only the running total differs.

What the Principle Says

The inclusion-exclusion principle computes the size of a union from the sizes of the sets and their intersections.

For two sets:

AB=A+BAB|A \cup B| = |A| + |B| - |A \cap B|


For three:

ABC=A+B+CABACBC+ABC|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|


The pattern continues for any number of sets: add the individual sizes, subtract the pairwise intersections, add the triples, subtract the quadruples, alternating by the number of sets in each term.

Simply adding A+B|A| + |B| over-counts whatever lies in both sets, and the corrections exist to undo that. The principle is a bookkeeping statement rather than a deep theorem, but the bookkeeping is delicate enough that the signs are worth deriving rather than memorising.

It is also worth noting what the formula does not require. The sets need not be finite in any structured way, need not be nested, and need not overlap at all — the statement holds in every configuration, including the degenerate ones. For the general statement, see cardinality.

Why the Signs Alternate

The alternating signs are not a convention. They are forced, and the reason is visible in the region numbers.

Take an element lying in exactly kk of the sets. The single-set terms count it kk times, once for each set containing it. The pairwise terms subtract it once for every pair of those sets, which is (k2)\binom{k}{2} times. The triples add it back (k3)\binom{k}{3} times, and so on.

Its total multiplicity is therefore

(k1)(k2)+(k3)\binom{k}{1} - \binom{k}{2} + \binom{k}{3} - \cdots


and that alternating sum equals exactly 11 for every k1k \ge 1. It follows from the binomial theorem applied to (11)k=0(1 - 1)^k = 0, rearranged.

So every element in the union is counted once, whatever number of sets it happens to lie in, and elements in no set are never counted at all. Any other choice of signs would break this.

The walk-through is that argument performed on one example rather than in general. Each region holds the elements lying in one particular collection of sets, and its large number is the running value of that alternating sum. For the coefficients, see binomial coefficients.

Where It Gets Used

Counting a union directly is usually harder than counting its parts, which is what makes the principle worth having.

In probability the same statement holds with measures in place of sizes: P(AB)=P(A)+P(B)P(AB)P(A \cup B) = P(A) + P(B) - P(A \cap B), and the three-event version has the same seven terms. The addition rule for probability is inclusion-exclusion.

In combinatorics it counts arrangements avoiding several forbidden patterns at once — how many permutations leave no element fixed, how many integers below a bound avoid a list of divisors. The sieve of Eratosthenes and Euler's totient function are both inclusion-exclusion in disguise.

In computing, the same reasoning counts records matching at least one of several filters without materialising the union, which matters when the individual counts are cheap and the union is not.

The practical rule of thumb: whenever a question asks how many things satisfy at least one of several conditions, and the conditions can hold together, this is the formula that applies. If the conditions are mutually exclusive, the corrections all vanish and plain addition is enough.

Common Mistakes

Three errors account for most wrong answers, and each one has a signature in the region numbers.

Stopping after the subtractions. With three sets this leaves the centre at 00, so elements in all three sets are not counted at all. The running total comes out too small by exactly ABC|A \cap B \cap C|.

Subtracting the triple overlap instead of adding it. The same mistake with a worse ending: the centre lands on 1-1, which is visibly impossible once you are watching the numbers rather than the algebra.

Entering overlaps larger than the sets they sit inside. An intersection can never exceed either set containing it, and with three sets there is a subtler version — pairwise overlaps that leave no room once the triple overlap is fixed.

All three are easy to catch by the same habit: step through and check that every region lands on 11. The formula is right when the picture is right, and not before.

When the Walk-Through Does Not Resolve

Every term has been applied and at least one region is still not counted exactly once. With valid sizes this cannot happen, so the input is what to check.
ABC1181618141313
All terms applied, still not resolved

The terms are right and the regions still do not land on 1, because the entered sizes were never consistent.

The formula does not fail. What fails is the premise that the entered numbers describe a real collection of sets, and the walk-through is reporting that faithfully rather than hiding it.

This is worth distinguishing from an arithmetic slip. Every term was applied in the right order with the right sign; the multiplicities are correct for the terms; and the regions still do not settle, because the region sizes underlying them were never consistent.

Look for a region flagged with a cross and work outward from it, as at sizes that cannot happen. Reducing an overlap, or increasing the set that contains it, is usually enough.

The general lesson is one worth carrying beyond this tool: a counting formula applied to impossible data returns an answer, and the answer is meaningless. The check that every region lands on 11 is what catches it.