Derivation of formula for the degree of unsaturation Degree of Unsaturation (DU) of an organic compound tells us about the total number of $\pi$ bonds (both double bonds and triple bonds) and number of rings/cyclic chains in the compound.

Degree of unsaturation forms an integral part of the four tools used to identify the structure of an organic compound (some of the others being structure after hydration and ozonolysis (there's one more I don't remember)).

Wikipedia states the formula of degree of unsaturation as:


  $$\text{Rings + $\pi$-bonds} = C - \frac{H}2 - \frac{X}2 + \frac{N}2 + 1$$
  where $C$ = number of carbons, $H$ = number of hydrogens, $X$ = number of halogens and $N$ = number of nitrogens


A more general formula is stated as 


  $$DU = 1 + \frac{1}{2}\sum{n_i(v_i - 2)}$$
  where $n_i$ is the number of atoms with valence $v_i$.


What I'm interested is the underlying concept from which this formula is derived.
Well, that is simple.

A Carbon (C) makes 4 bonds, a Hydrogen (H) or a halogen (X) makes 1 bond and Nitrogen (N) makes three bonds. So in a molecule with $c$ Carbons, $h$ Hydrogens, $x$ halogens and $n$ Nitrogens, we will have $4c+h+x+3n$ "bonding capacity".  

Assuming there are no rings, no double/triple bonds, we must have $c+h+x+n-1$ bonds in between these atoms [think like how many dashes you can draw between $m$ $\ce{O}$'s: $\ce{O-O-O}$ two dashes for three $\ce{O}$'s apparently].

Since each bond uses one "bonding capacity" from each of the two atoms its binding, $c+h+x+n-1$ bonds are using $2(c+h+x+n-1)$ bonding capacity. So the remaining capacity is:

$$(4c+h+x+3n)-2(c+h+x+n-1)=2c-h-x+n+2$$

If this is larger than zero, it means our molecule is unsaturated, i.e. still has a capacity to make bonds. And our assumption that "there are no rings, no double/triple bonds" is wrong. This bonding capacity is used in forming new bonds either in rings or in pi-bonds.  Since each bond uses two capacity one from each side we should have $c-h/2-x/2+n/2+1$ "unsaturated" bonds.