# Symmetric difference

In mathematics, the symmetric difference of two sets, also known as the disjunctive union and set sum, is the set of elements which are in either of the sets, but not in their intersection. For example, the symmetric difference of the sets $\{1,2,3\}$ and $\{3,4\}$ is $\{1,2,4\}$.
The symmetric difference of the sets A and B is commonly denoted by $A\operatorname {\Delta } B$ (alternatively, $A\operatorname {\vartriangle } B$), $A\oplus B$, or $A\ominus B$. It can be viewed as a form of addition modulo 2.
The power set of any set becomes an abelian group under the operation of symmetric difference, with the empty set as the neutral element of the group and every element in this group being its own inverse. The power set of any set becomes a Boolean ring, with symmetric difference as the addition of the ring and intersection as the multiplication of the ring.

## Properties

The symmetric difference is equivalent to the union of both relative complements, that is:
    $A\,\Delta \,B=\left(A\setminus B\right)\cup \left(B\setminus A\right),$
The symmetric difference can also be expressed using the XOR operation ⊕ on the predicates describing the two sets in set-builder notation:
    $A\mathbin {\Delta } B=\{x:(x\in A)\oplus (x\in B)\}.$
The same fact can be stated as the indicator function (denoted here by $\chi$) of the symmetric difference, being the XOR (or addition mod 2) of the indicator functions of its two arguments: $\chi _{(A\,\Delta \,B)}=\chi _{A}\oplus \chi _{B}$ or using the Iverson bracket notation $[x\in A\,\Delta \,B]=[x\in A]\oplus [x\in B]$.
The symmetric difference can also be expressed as the union of the two sets, minus their intersection:
    $A\,\Delta \,B=(A\cup B)\setminus (A\cap B),$
In particular, $A\mathbin {\Delta } B\subseteq A\cup B$; the equality in this non-strict inclusion occurs if and only if $A$ and $B$ are disjoint sets. Furthermore, denoting $D=A\mathbin {\Delta } B$ and $I=A\cap B$, then $D$ and $I$ are always disjoint, so $D$ and $I$ partition $A\cup B$. Consequently, assuming intersection and symmetric difference as primitive operations, the union of two sets can be well defined in terms of symmetric difference by the right-hand side of the equality
    $A\,\cup \,B=(A\,\Delta \,B)\,\Delta \,(A\cap B)$.
The symmetric difference is commutative and associative:
    ${\begin{aligned}A\,\Delta \,B&=B\,\Delta \,A,\\(A\,\Delta \,B)\,\Delta \,C&=A\,\Delta \,(B\,\Delta \,C).\end{aligned}}$
The empty set is neutral, and every set is its own inverse:
    ${\begin{aligned}A\,\Delta \,\varnothing &=A,\\A\,\Delta \,A&=\varnothing .\end{aligned}}$
Thus, the power set of any set X becomes an abelian group under the symmetric difference operation. (More generally, any field of sets forms a group with the symmetric difference as operation.) A group in which every element is its own inverse (or, equivalently, in which every element has order 2) is sometimes called a Boolean group; the symmetric difference provides a prototypical example of such groups. Sometimes the Boolean group is actually defined as the symmetric difference operation on a set. In the case where X has only two elements, the group thus obtained is the Klein four-group.
Equivalently, a Boolean group is an elementary abelian 2-group. Consequently, the group induced by the symmetric difference is in fact a vector space over the field with 2 elements Z$_2$. If X is finite, then the singletons form a basis of this vector space, and its dimension is therefore equal to the number of elements of X. This construction is used in graph theory, to define the cycle space of a graph.
From the property of the inverses in a Boolean group, it follows that the symmetric difference of two repeated symmetric differences is equivalent to the repeated symmetric difference of the join of the two multisets, where for each double set both can be removed. In particular:
    $(A\,\Delta \,B)\,\Delta \,(B\,\Delta \,C)=A\,\Delta \,C.$
This implies triangle inequality: the symmetric difference of A and C is contained in the union of the symmetric difference of A and B and that of B and C.
Intersection distributes over symmetric difference:
    $A\cap (B\,\Delta \,C)=(A\cap B)\,\Delta \,(A\cap C),$
and this shows that the power set of X becomes a ring, with symmetric difference as addition and intersection as multiplication. This is the prototypical example of a Boolean ring.
Further properties of the symmetric difference include:
* $A\mathbin {\Delta } B=\emptyset$ if and only if $A=B$.
* $A\mathbin {\Delta } B=A^{c}\mathbin {\Delta } B^{c}$, where $A^{c}$, $B^{c}$ is $A$'s complement, $B$'s complement, respectively, relative to any (fixed) set that contains both.
* $\left(\bigcup _{\alpha \in {\mathcal {I}}}A_{\alpha }\right)\Delta \left(\bigcup _{\alpha \in {\mathcal {I}}}B_{\alpha }\right)\subseteq \bigcup _{\alpha \in {\mathcal {I}}}\left(A_{\alpha }\mathbin {\Delta } B_{\alpha }\right)$, where ${\mathcal {I}}$ is an arbitrary non-empty index set.
* If $f:S\rightarrow T$ is any function and $A,B\subseteq T$ are any sets in $f$'s codomain, then $f^{-1}\left(A\mathbin {\Delta } B\right)=f^{-1}\left(A\right)\mathbin {\Delta } f^{-1}\left(B\right).$
The symmetric difference can be defined in any Boolean algebra, by writing
    $x\,\Delta \,y=(x\lor y)\land \lnot (x\land y)=(x\land \lnot y)\lor (y\land \lnot x)=x\oplus y.$
This operation has the same properties as the symmetric difference of sets.

## n-ary symmetric difference

Repeated symmetric difference is in a sense equivalent to an operation on a multitude of sets (possibly with multiple appearances of the same set) giving the set of elements which are in an odd number of sets.
The symmetric difference of a collection of sets contains just elements which are in an odd number of the sets in the collection: $\Delta M=\left\{a\in \bigcup M:\left|\{A\in M:a\in A\}\right|{\text{ is odd}}\right\}.$
Evidently, this is well-defined only when each element of the union ⋃M{\textstyle \bigcup M} is contributed by a finite number of elements of $M$.
Suppose $M=\left\{M_{1},M_{2},\ldots ,M_{n}\right\}$ is a multiset and $n\geq 2$. Then there is a formula for $|\Delta M|$, the number of elements in $\Delta M$, given solely in terms of intersections of elements of $M$: $|\Delta M|=\sum _{l=1}^{n}(-2)^{l-1}\sum _{1\leq i_{1}<i_{2}<\ldots <i_{l}\leq n}\left|M_{i_{1}}\cap M_{i_{2}}\cap \ldots \cap M_{i_{l}}\right|.$

## Symmetric difference on measure spaces

As long as there is a notion of "how big" a set is, the symmetric difference between two sets can be considered a measure of how "far apart" they are.
First consider a finite set S and the counting measure on subsets given by their size. Now consider two subsets of S and set their distance apart as the size of their symmetric difference. This distance is in fact a metric, which makes the power set on S a metric space. If S has n elements, then the distance from the empty set to S is n, and this is the maximum distance for any pair of subsets.
Using the ideas of measure theory, the separation of measurable sets can be defined to be the measure of their symmetric difference. If μ is a σ-finite measure defined on a σ-algebra Σ, the function
    $d_{\mu }(X,Y)=\mu (X\,\Delta \,Y)$
is a pseudometric on Σ. d$_μ$ becomes a metric if Σ is considered modulo the equivalence relation X ~ Y if and only if $\mu (X\,\Delta \,Y)=0$. It is sometimes called Fréchet-Nikodym metric. The resulting metric space is separable if and only if L$^2$(μ) is separable.
If $\mu (X),\mu (Y)<\infty$, we have: $|\mu (X)-\mu (Y)|\leq \mu (X\,\Delta \,Y)$. Indeed,
    ${\begin{aligned}|\mu (X)-\mu (Y)|&=\left|\left(\mu \left(X\setminus Y\right)+\mu \left(X\cap Y\right)\right)-\left(\mu \left(X\cap Y\right)+\mu \left(Y\setminus X\right)\right)\right|\\&=\left|\mu \left(X\setminus Y\right)-\mu \left(Y\setminus X\right)\right|\\&\leq \left|\mu \left(X\setminus Y\right)\right|+\left|\mu \left(Y\setminus X\right)\right|\\&=\mu \left(X\setminus Y\right)+\mu \left(Y\setminus X\right)\\&=\mu \left(\left(X\setminus Y\right)\cup \left(Y\setminus X\right)\right)\\&=\mu \left(X\,\Delta \,Y\right)\end{aligned}}$
If $S=\left(\Omega ,{\mathcal {A}},\mu \right)$ is a measure space and $F,G\in {\mathcal {A}}$ are measurable sets, then their symmetric difference is also measurable: $F\Delta G\in {\mathcal {A}}$. One may define an equivalence relation on measurable sets by letting $F$ and $G$ be related if $\mu \left(F\Delta G\right)=0$. This relation is denoted $F=G\left[{\mathcal {A}},\mu \right]$.
Given ${\mathcal {D}},{\mathcal {E}}\subseteq {\mathcal {A}}$, one writes ${\mathcal {D}}\subseteq {\mathcal {E}}\left[{\mathcal {A}},\mu \right]$ if to each $D\in {\mathcal {D}}$ there's some $E\in {\mathcal {E}}$ such that $D=E\left[{\mathcal {A}},\mu \right]$. The relation "$\subseteq \left[{\mathcal {A}},\mu \right]$" is a partial order on the family of subsets of ${\mathcal {A}}$.
We write ${\mathcal {D}}={\mathcal {E}}\left[{\mathcal {A}},\mu \right]$ if ${\mathcal {D}}\subseteq {\mathcal {E}}\left[{\mathcal {A}},\mu \right]$ and ${\mathcal {E}}\subseteq {\mathcal {D}}\left[{\mathcal {A}},\mu \right]$. The relation "$=\left[{\mathcal {A}},\mu \right]$" is an equivalence relationship between the subsets of ${\mathcal {A}}$.
The symmetric closure of ${\mathcal {D}}$ is the collection of all ${\mathcal {A}}$-measurable sets that are $=\left[{\mathcal {A}},\mu \right]$ to some $D\in {\mathcal {D}}$. The symmetric closure of ${\mathcal {D}}$ contains ${\mathcal {D}}$. If ${\mathcal {D}}$ is a sub-$\sigma$-algebra of ${\mathcal {A}}$, so is the symmetric closure of ${\mathcal {D}}$.
$F=G\left[{\mathcal {A}},\mu \right]$ iff $\left|\mathbf {1} _{F}-\mathbf {1} _{G}\right|=0$ $\left[{\mathcal {A}},\mu \right]$ almost everywhere.

## Hausdorff distance vs. symmetric difference

The Hausdorff distance and the (area of the) symmetric difference are both pseudo-metrics on the set of measurable geometric shapes. However, they behave quite differently. The figure at the right shows two sequences of shapes, "Red" and "Red ∪ Green". When the Hausdorff distance between them becomes smaller, the area of the symmetric difference between them becomes larger, and vice versa. By continuing these sequences in both directions, it is possible to get two sequences such that the Hausdorff distance between them converges to 0 and the symmetric distance between them diverges, or vice versa.