<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://tcs.nju.edu.cn/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=172.21.1.108</id>
	<title>TCS Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://tcs.nju.edu.cn/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=172.21.1.108"/>
	<link rel="alternate" type="text/html" href="https://tcs.nju.edu.cn/wiki/index.php?title=Special:Contributions/172.21.1.108"/>
	<updated>2026-05-02T12:47:57Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Partitions,_sieve_methods&amp;diff=3002</id>
		<title>Combinatorics (Fall 2010)/Partitions, sieve methods</title>
		<link rel="alternate" type="text/html" href="https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Partitions,_sieve_methods&amp;diff=3002"/>
		<updated>2010-09-12T06:33:00Z</updated>

		<summary type="html">&lt;p&gt;172.21.1.108: /* Reference */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Partitions ==&lt;br /&gt;
We count the ways of partitioning &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; &#039;&#039;identical&#039;&#039; objects into &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; &#039;&#039;unordered&#039;&#039; groups. This is equivalent to counting the ways partitioning a number &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; unordered parts.&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;&amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partition&#039;&#039;&#039; of a number &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is a multiset &amp;lt;math&amp;gt;\{x_1,x_2,\ldots,x_k\}&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;x_i\ge 1&amp;lt;/math&amp;gt; for every element &amp;lt;math&amp;gt;x_i&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We define &amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt; as the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partitions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For example, number 7 has the following partitions:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
&amp;amp;\{7\}&lt;br /&gt;
&amp;amp; p_1(7)=1\\&lt;br /&gt;
&amp;amp;\{1,6\},\{2,5\},\{3,4\}&lt;br /&gt;
&amp;amp; p_2(7)=3\\&lt;br /&gt;
&amp;amp;\{1,1,5\}, \{1,2,4\}, \{1,3,3\}, \{2,2,3\} &lt;br /&gt;
&amp;amp; p_3(7)=4\\&lt;br /&gt;
&amp;amp;\{1,1,1,4\},\{1,1,2,3\}, \{1,2,2,2\}&lt;br /&gt;
&amp;amp; p_4(7)=3\\&lt;br /&gt;
&amp;amp;\{1,1,1,1,3\},\{1,1,1,2,2\}&lt;br /&gt;
&amp;amp; p_5(7)=2\\&lt;br /&gt;
&amp;amp;\{1,1,1,1,1,2\}&lt;br /&gt;
&amp;amp; p_6(7)=1\\&lt;br /&gt;
&amp;amp;\{1,1,1,1,1,1,1\}&lt;br /&gt;
&amp;amp; p_7(7)=1&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Equivalently, we can also define that A &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partition of a number &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is a &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-tuple &amp;lt;math&amp;gt;(x_1,x_2,\ldots,x_k)&amp;lt;/math&amp;gt; with:&lt;br /&gt;
* &amp;lt;math&amp;gt;x_1\ge x_2\ge\cdots\ge x_k\ge 1&amp;lt;/math&amp;gt;;&lt;br /&gt;
* &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt; the number of integral solutions to the above system.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;p(n)=\sum_{k=1}^n p_k(n)&amp;lt;/math&amp;gt; be the total number of partitions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. The function &amp;lt;math&amp;gt;p(n)&amp;lt;/math&amp;gt; is called the &#039;&#039;&#039;partition number&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Counting &amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt;===&lt;br /&gt;
We now try to determine &amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt;. Unlike most problems we learned in the last lecture, &amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt; does not have a nice closed form formula. We now give a recurrence for &amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
:&amp;lt;math&amp;gt;p_k(n)=p_{k-1}(n-1)+p_k(n-k)\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
{{Proof|&lt;br /&gt;
Suppose that &amp;lt;math&amp;gt;(x_1,\ldots,x_k)&amp;lt;/math&amp;gt; is a &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partition of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Note that it must hold that&lt;br /&gt;
:&amp;lt;math&amp;gt;x_1\ge x_2\ge \cdots \ge x_k\ge 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
There are two cases: &amp;lt;math&amp;gt;x_k=1&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;x_k&amp;gt;1&amp;lt;/math&amp;gt;.&lt;br /&gt;
;Case 1.&lt;br /&gt;
:If &amp;lt;math&amp;gt;x_k=1&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;(x_1,\cdots,x_{k-1})&amp;lt;/math&amp;gt; is a distinct &amp;lt;math&amp;gt;(k-1)&amp;lt;/math&amp;gt;-partition of &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt;. And every &amp;lt;math&amp;gt;(k-1)&amp;lt;/math&amp;gt;-partition of &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt; can be obtained in this way. Thus the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partitions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; in this case is &amp;lt;math&amp;gt;p_{k-1}(n-1)&amp;lt;/math&amp;gt;. &lt;br /&gt;
;Case 2.&lt;br /&gt;
:If &amp;lt;math&amp;gt;x_k&amp;gt;1&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;(x_1-1,\cdots,x_{k}-1)&amp;lt;/math&amp;gt; is a distinct &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partition of &amp;lt;math&amp;gt;n-k&amp;lt;/math&amp;gt;. And every &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partition of &amp;lt;math&amp;gt;n-k&amp;lt;/math&amp;gt; can be obtained in this way. Thus the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partitions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; in this case is &amp;lt;math&amp;gt;p_{k}(n-k)&amp;lt;/math&amp;gt;. &lt;br /&gt;
In conclusion, the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partitions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;p_{k-1}(n-1)+p_k(n-k)&amp;lt;/math&amp;gt;, i.e.&lt;br /&gt;
:&amp;lt;math&amp;gt;p_k(n)=p_{k-1}(n-1)+p_k(n-k)\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Use the above recurrence, we can compute the &amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt;  for some decent &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; by computer simulation.&lt;br /&gt;
&lt;br /&gt;
If we are not restricted ourselves to the precise estimation of &amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt;, the next theorem gives an asymptotic estimation of &amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt;. Note that it only holds for &#039;&#039;&#039;constant&#039;&#039;&#039; &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, i.e. &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; does not depend on &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
For any fixed &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;,&lt;br /&gt;
:&amp;lt;math&amp;gt;p_k(n)\sim\frac{n^{k-1}}{k!(k-1)!}&amp;lt;/math&amp;gt;,&lt;br /&gt;
as &amp;lt;math&amp;gt;n\rightarrow \infty&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
{{Proof|&lt;br /&gt;
Suppose that &amp;lt;math&amp;gt;(x_1,\ldots,x_k)&amp;lt;/math&amp;gt; is a &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partition of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Then &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_1\ge x_2\ge \cdots \ge x_k\ge 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;math&amp;gt;k!&amp;lt;/math&amp;gt; permutations of &amp;lt;math&amp;gt;(x_1,\ldots,x_k)&amp;lt;/math&amp;gt; yield at most &amp;lt;math&amp;gt;k!&amp;lt;/math&amp;gt; many &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-compositions (the &#039;&#039;ordered&#039;&#039; sum of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; positive integers). There are &amp;lt;math&amp;gt;{n-1\choose k-1}&amp;lt;/math&amp;gt; many &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-compositions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, every one of which can be yielded in this way by permuting a partition. Thus,&lt;br /&gt;
:&amp;lt;math&amp;gt;k!p_k(n)\ge{n-1\choose k-1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;y_i=x_i+k-i&amp;lt;/math&amp;gt;. That is, &amp;lt;math&amp;gt;y_k=x_k, y_{k-1}=x_k+1, y_{k-2}=x_k+2,\ldots, y_{1}=x_k+k-1&amp;lt;/math&amp;gt;. Then, it holds that&lt;br /&gt;
* &amp;lt;math&amp;gt;y_1&amp;gt;y_2&amp;gt;\cdots&amp;gt;y_k\ge 1&amp;lt;/math&amp;gt;; and &lt;br /&gt;
* &amp;lt;math&amp;gt;y_1+y_2+\cdots+y_k=n+\frac{k(k-1)}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
Each permutation of &amp;lt;math&amp;gt;(y_1,y_2,\ldots,y_k)&amp;lt;/math&amp;gt; yields a &#039;&#039;&#039;distinct&#039;&#039;&#039; &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-composition of &amp;lt;math&amp;gt;n+\frac{k(k-1)}{2}&amp;lt;/math&amp;gt;, because all &amp;lt;math&amp;gt;y_i&amp;lt;/math&amp;gt; are distinct.&lt;br /&gt;
Thus, &lt;br /&gt;
:&amp;lt;math&amp;gt;k!p_k(n)\le {n+\frac{k(k-1)}{2}-1\choose k-1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Combining the two inequalities, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{{n-1\choose k-1}}{k!}\le p_k(n)\le \frac{{n+\frac{k(k-1)}{2}-1\choose k-1}}{k!}&amp;lt;/math&amp;gt;.&lt;br /&gt;
The theorem follows.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Ferrers diagram ===&lt;br /&gt;
A partition of a number &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; can be represented as a diagram of dots (or squares), called a &#039;&#039;&#039;Ferrers diagram&#039;&#039;&#039; (the square version of Ferrers diagram is also called a &#039;&#039;&#039;Young diagram&#039;&#039;&#039;, named after a structured called Young tableaux). &lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;(x_1,x_2,\ldots,x_k)&amp;lt;/math&amp;gt; with that &amp;lt;math&amp;gt;x_1\ge x_2\ge \cdots x_k\ge 1&amp;lt;/math&amp;gt; be a partition of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Its Ferrers diagram consists of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; rows, where the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th row contains &amp;lt;math&amp;gt;x_i&amp;lt;/math&amp;gt; dots (or squares).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
{|border=&amp;quot;0&amp;quot;&lt;br /&gt;
|&lt;br /&gt;
{|border=&amp;quot;0&amp;quot;&lt;br /&gt;
|[[File:Chess xot45.svg|22px]]||[[File:Chess xot45.svg|22px]]||[[File:Chess xot45.svg|22px]]||[[File:Chess xot45.svg|22px]]||[[File:Chess xot45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess xot45.svg|22px]]||[[File:Chess xot45.svg|22px]]||[[File:Chess xot45.svg|22px]]||[[File:Chess xot45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess xot45.svg|22px]]||[[File:Chess xot45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess xot45.svg|22px]]&lt;br /&gt;
|}&lt;br /&gt;
|&lt;br /&gt;
[[File:Chess t45.svg|120px]]&lt;br /&gt;
|align=center|&lt;br /&gt;
{|border=&amp;quot;2&amp;quot;  cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]&lt;br /&gt;
|}&lt;br /&gt;
|-&lt;br /&gt;
|align=center|Ferrers diagram (&#039;&#039;dot version&#039;&#039;) of (5,4,2,1)||&lt;br /&gt;
|align=center|Ferrers diagram (&#039;&#039;square version&#039;&#039;) of (5,4,2,1)&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
;Conjugate partition&lt;br /&gt;
The partition we get by reading the Ferrers diagram by column instead of rows is called the &#039;&#039;&#039;conjugate&#039;&#039;&#039; of the original partition.&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
{|border=&amp;quot;0&amp;quot;&lt;br /&gt;
|align=center|&lt;br /&gt;
{|border=&amp;quot;2&amp;quot;  cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]&lt;br /&gt;
|}&lt;br /&gt;
|&lt;br /&gt;
[[File:Chess t45.svg|120px]]&lt;br /&gt;
|align=center|&lt;br /&gt;
{|border=&amp;quot;2&amp;quot;  cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]||[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]&lt;br /&gt;
|-&lt;br /&gt;
|[[File:Chess t45.svg|22px]]&lt;br /&gt;
|}&lt;br /&gt;
|-&lt;br /&gt;
|align=center|&amp;lt;math&amp;gt;(6,4,4,2,1)&amp;lt;/math&amp;gt;||&lt;br /&gt;
|align=center|conjugate: &amp;lt;math&amp;gt;(5,4,3,3,1,1)&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Clearly, &lt;br /&gt;
* different partitions cannot have the same conjugate, and &lt;br /&gt;
* every partition of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is the conjugate of some partition of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;,&lt;br /&gt;
so the conjugation mapping is a permutation on the set of partitions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. This fact is very useful in proving theorems for partitions numbers.&lt;br /&gt;
&lt;br /&gt;
Some theorems of partitions can be easily proved by representing partitions in Ferrers diagrams.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
# The number of partitions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; which have largest summand &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, is &amp;lt;math&amp;gt;p_k(n)&amp;lt;/math&amp;gt;. &lt;br /&gt;
# The number of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; parts equals the number of partitions of &amp;lt;math&amp;gt;n-k&amp;lt;/math&amp;gt; into at most &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; parts. Formally,&lt;br /&gt;
::&amp;lt;math&amp;gt;p_k(n)=\sum_{j=1}^k p_j(n-k)&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
{{Proof|&lt;br /&gt;
# For every &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partition, the conjugate partition has largest part &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;. And vice versa.&lt;br /&gt;
# For a &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partition of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, remove the leftmost cell of every row of the Ferrers diagram. Totally &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; cells are removed and the remaining diagram is a partition of &amp;lt;math&amp;gt;n-k&amp;lt;/math&amp;gt; into at most &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; parts. And for a partition of &amp;lt;math&amp;gt;n-k&amp;lt;/math&amp;gt; into at most &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; parts, add a cell to each of the &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; rows (including the empty ones). This will give us a &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-partition of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. It is easy to see the above mappings are 1-1 correspondences. Thus, the number of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; parts equals the number of partitions of &amp;lt;math&amp;gt;n-k&amp;lt;/math&amp;gt; into at most &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; parts.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Principle of Inclusion-Exclusion ==&lt;br /&gt;
Let &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; be two finite sets. The cardinality of their union is&lt;br /&gt;
:&amp;lt;math&amp;gt;|A\cup B|=|A|+|B|-{\color{Blue}|A\cap B|}&amp;lt;/math&amp;gt;.&lt;br /&gt;
For three sets &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;C&amp;lt;/math&amp;gt;, the cardinality of the union of these three sets is computed as&lt;br /&gt;
:&amp;lt;math&amp;gt;|A\cup B\cup C|=|A|+|B|+|C|-{\color{Blue}|A\cap B|}-{\color{Blue}|A\cap C|}-{\color{Blue}|B\cap C|}+{\color{Red}|A\cap B\cap C|}&amp;lt;/math&amp;gt;.&lt;br /&gt;
This is illustrated by the following figure.&lt;br /&gt;
::[[Image:Inclusion-exclusion.png|200px|border|center]] &lt;br /&gt;
&lt;br /&gt;
Generally, the &#039;&#039;&#039;Principle of Inclusion-Exclusion&#039;&#039;&#039; states the rule for computing the union of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; finite sets &amp;lt;math&amp;gt;A_1,A_2,\ldots,A_n&amp;lt;/math&amp;gt;, such that&lt;br /&gt;
{{Equation|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\left|\bigcup_{i=1}^nA_i\right|&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{I\subseteq\{1,\ldots,n\}}(-1)^{|I|-1}\left|\bigcap_{i\in I}A_i\right|.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
In combinatorial enumeration, the Principle of Inclusion-Exclusion is usually applied in its complement form.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;A_1,A_2,\ldots,A_n\subseteq U&amp;lt;/math&amp;gt; be subsets of some finite set &amp;lt;math&amp;gt;U&amp;lt;/math&amp;gt;. Here &amp;lt;math&amp;gt;U&amp;lt;/math&amp;gt; is some universe of combinatorial objects, whose cardinality is easy to calculate (e.g. all strings, tuples, permutations), and each &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; contains the objects with some specific property (e.g. a &amp;quot;pattern&amp;quot;) which we want to avoid. The problem is to count the number of objects without any of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; properties. We write &amp;lt;math&amp;gt;\bar{A_i}=U-A&amp;lt;/math&amp;gt;. The number of objects without any of the properties &amp;lt;math&amp;gt;A_1,A_2,\ldots,A_n&amp;lt;/math&amp;gt; is&lt;br /&gt;
{{Equation|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\left|\bar{A_1}\cap\bar{A_2}\cap\cdots\cap\bar{A_n}\right|=\left|U-\bigcup_{i=1}^nA_i\right|&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
|U|-\sum_{I\subseteq\{1,\ldots,n\}}(-1)^{|I|}\left|\bigcap_{i\in I}A_i\right|.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
For an &amp;lt;math&amp;gt;I\subseteq\{1,2,\ldots,n\}&amp;lt;/math&amp;gt;, we denote&lt;br /&gt;
:&amp;lt;math&amp;gt;A_I=\bigcap_{i\in I}A_i&amp;lt;/math&amp;gt;&lt;br /&gt;
with the convention that &amp;lt;math&amp;gt;A_\emptyset=U&amp;lt;/math&amp;gt;. The above equation is stated as:&lt;br /&gt;
{{Theorem|Principle of Inclusion-Exclusion|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;A_1,A_2,\ldots,A_n&amp;lt;/math&amp;gt; be a family of subsets of &amp;lt;math&amp;gt;U&amp;lt;/math&amp;gt;. Then the number of elements of &amp;lt;math&amp;gt;U&amp;lt;/math&amp;gt; which lie in none of the subsets &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; is&lt;br /&gt;
::&amp;lt;math&amp;gt;\sum_{I\subseteq\{1,\ldots, n\}}(-1)^{|I|}|A_I|&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;S_k=\sum_{|I|=k}|A_I|\,&amp;lt;/math&amp;gt;. Conventionally, &amp;lt;math&amp;gt;S_0=|A_\emptyset|=|U|&amp;lt;/math&amp;gt;. The principle of inclusion-exclusion can be expressed as&lt;br /&gt;
{{Equation|&amp;lt;math&amp;gt;&lt;br /&gt;
S_0-S_1+S_2+\cdots+(-1)^nS_n.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Surjections ===&lt;br /&gt;
In the twelvefold way, we discuss the counting problems incurred by the mappings &amp;lt;math&amp;gt;f:N\rightarrow M&amp;lt;/math&amp;gt;. The basic case is that elements from both &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt; are distinguishable. In this case, it is easy to count the number of arbitrary mappings (which is &amp;lt;math&amp;gt;m^n&amp;lt;/math&amp;gt;) and the number of injective (one-to-one) mappings (which is &amp;lt;math&amp;gt;(m)_n&amp;lt;/math&amp;gt;), but the number of surjective is difficult. Here we apply the principle of inclusion-exclusion to count the number of surjective (onto) mappings.&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:The number of surjective mappings from an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set to an &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;-set is given by&lt;br /&gt;
::&amp;lt;math&amp;gt;\sum_{k=1}^m(-1)^{m-k}{m\choose k}k^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
{{Proof|&lt;br /&gt;
Let &amp;lt;math&amp;gt;U=\{f:[n]\rightarrow[m]\}&amp;lt;/math&amp;gt; be the set of mappings from &amp;lt;math&amp;gt;[n]&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;[m]&amp;lt;/math&amp;gt;. Then &amp;lt;math&amp;gt;|U|=m^n&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
For &amp;lt;math&amp;gt;i\in[m]&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; be the set of mappings &amp;lt;math&amp;gt;f:[n]\rightarrow[m]&amp;lt;/math&amp;gt; that none of &amp;lt;math&amp;gt;j\in[n]&amp;lt;/math&amp;gt; is mapped to &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, i.e. &amp;lt;math&amp;gt;A_i=\{f:[n]\rightarrow[m]\setminus\{i\}\}&amp;lt;/math&amp;gt;, thus &amp;lt;math&amp;gt;|A_i|=(m-1)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
More generally, for &amp;lt;math&amp;gt;I\subseteq [m]&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;A_I=\bigcap_{i\in I}A_i&amp;lt;/math&amp;gt; contains the mappings &amp;lt;math&amp;gt;f:[n]\rightarrow[m]\setminus I&amp;lt;/math&amp;gt;. And &amp;lt;math&amp;gt;|A_I|=(m-|I|)^n\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A mapping &amp;lt;math&amp;gt;f:[n]\rightarrow[m]&amp;lt;/math&amp;gt; is surjective if &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; lies in none of &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt;. By the principle of inclusion-exclusion, the number of surjective &amp;lt;math&amp;gt;f:[n]\rightarrow[m]&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;\sum_{I\subseteq[m]}(-1)^{|I|}\left|A_I\right|=\sum_{I\subseteq[m]}(-1)^{|I|}(m-|I|)^n=\sum_{j=0}^m(-1)^j{m\choose j}(m-j)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
Let &amp;lt;math&amp;gt;k=m-j&amp;lt;/math&amp;gt;. The theorem is proved.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Recall that, in the twelvefold way, we establish a relation between surjections and partitions.&lt;br /&gt;
&lt;br /&gt;
* Surjection to ordered partition:&lt;br /&gt;
:For a surjective &amp;lt;math&amp;gt;f:[n]\rightarrow[m]&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;(f^{-1}(0),f^{-1}(1),\ldots,f^{-1}(m-1))&amp;lt;/math&amp;gt; is an &#039;&#039;&#039;ordered partition&#039;&#039;&#039; of &amp;lt;math&amp;gt;[n]&amp;lt;/math&amp;gt;.&lt;br /&gt;
* Ordered partition to surjection:&lt;br /&gt;
:For an ordered &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;-partition &amp;lt;math&amp;gt;(B_0,B_1,\ldots, B_{m-1})&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;[n]&amp;lt;/math&amp;gt;, we can define a function &amp;lt;math&amp;gt;f:[n]\rightarrow[m]&amp;lt;/math&amp;gt; by letting &amp;lt;math&amp;gt;f(i)=j&amp;lt;/math&amp;gt; if and only if &amp;lt;math&amp;gt;i\in B_j&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; is surjective since as a partition, none of &amp;lt;math&amp;gt;B_i&amp;lt;/math&amp;gt; is empty.&lt;br /&gt;
&lt;br /&gt;
Therefore, we have a one-to-one correspondence between surjective mappings from an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set to an &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;-set and the ordered &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;-partitions of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set.&lt;br /&gt;
&lt;br /&gt;
The Stirling number of the second kind &amp;lt;math&amp;gt;S(n,m)&amp;lt;/math&amp;gt; is the number of &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;-partitions of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. There are &amp;lt;math&amp;gt;m!&amp;lt;/math&amp;gt; ways to order an &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;-partition, thus the number of surjective mappings &amp;lt;math&amp;gt;f:[n]\rightarrow[m]&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;m! S(n,m)&amp;lt;/math&amp;gt;. Combining with what we have proved for surjections, we give the following result for the Stirling number of the second kind.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
:&amp;lt;math&amp;gt;S(n,m)=\frac{1}{m!}\sum_{k=1}^m(-1)^{m-k}{m\choose k}k^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Derangements ===&lt;br /&gt;
We now count the number of bijections from a set to itself with no fixed points. This is the &#039;&#039;&#039;derangement problem&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
For a permutation &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;\{1,2,\ldots,n\}&amp;lt;/math&amp;gt;, a &#039;&#039;&#039;fixed point&#039;&#039;&#039; is such an &amp;lt;math&amp;gt;i\in\{1,2,\ldots,n\}&amp;lt;/math&amp;gt; that &amp;lt;math&amp;gt;\pi(i)=i&amp;lt;/math&amp;gt;.&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Derangement &#039;&#039;&#039;derangement&#039;&#039;&#039;] of &amp;lt;math&amp;gt;\{1,2,\ldots,n\}&amp;lt;/math&amp;gt; is a permutation of &amp;lt;math&amp;gt;\{1,2,\ldots,n\}&amp;lt;/math&amp;gt; that has no fixed points.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:The number of derangements of &amp;lt;math&amp;gt;\{1,2,\ldots,n\}&amp;lt;/math&amp;gt; given by&lt;br /&gt;
::&amp;lt;math&amp;gt;n!\sum_{k=0}^n\frac{(-1)^k}{k!}\approx \frac{n!}{\mathrm{e}}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
{{Proof|&lt;br /&gt;
Let &amp;lt;math&amp;gt;U&amp;lt;/math&amp;gt; be the set of all permutations of &amp;lt;math&amp;gt;\{1,2,\ldots,n\}&amp;lt;/math&amp;gt;. So &amp;lt;math&amp;gt;|U|=n!&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; be the set of permutations with fixed point &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;; so &amp;lt;math&amp;gt;|A_i|=(n-1)!&amp;lt;/math&amp;gt;. More generally, for any &amp;lt;math&amp;gt;I\subseteq \{1,2,\ldots,n\}&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;A_I=\bigcap_{i\in I}A_i&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;|A_I|=(n-|I|)!&amp;lt;/math&amp;gt;, since permutations in &amp;lt;math&amp;gt;A_I&amp;lt;/math&amp;gt; fix every point in &amp;lt;math&amp;gt;I&amp;lt;/math&amp;gt; and permute the remaining points arbitrarily. A permutation is a derangement if and only if it lies in none of the sets &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt;. So the number of derangements is&lt;br /&gt;
:&amp;lt;math&amp;gt;\sum_{I\subseteq\{1,2,\ldots,n\}}(-1)^{|I|}(n-|I|)!=\sum_{k=0}^n(-1)^k{n\choose k}(n-k)!=n!\sum_{k=0}^n\frac{(-1)^k}{k!}.&amp;lt;/math&amp;gt;&lt;br /&gt;
By Taylor&#039;s series,&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{\mathrm{e}}=\sum_{k=0}^\infty\frac{(-1)^k}{k!}=\sum_{k=0}^n\frac{(-1)^k}{k!}\pm o\left(\frac{1}{n!}\right)&amp;lt;/math&amp;gt;.&lt;br /&gt;
It is not hard to see that &amp;lt;math&amp;gt;n!\sum_{k=0}^n\frac{(-1)^k}{k!}&amp;lt;/math&amp;gt; is the closest integer to &amp;lt;math&amp;gt;\frac{n!}{\mathrm{e}}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Therefore, there are about &amp;lt;math&amp;gt;\frac{1}{\mathrm{e}}&amp;lt;/math&amp;gt; fraction of all permutations with no fixed points.&lt;br /&gt;
&lt;br /&gt;
=== Permutations with restricted positions ===&lt;br /&gt;
We introduce a general theory of counting permutations with restricted positions. In the derangement problem, we count the number of permutations that &amp;lt;math&amp;gt;\pi(i)\neq i&amp;lt;/math&amp;gt;. We now generalize to the problem of counting permutations which avoid a set of arbitrarily specified positions. &lt;br /&gt;
&lt;br /&gt;
It is traditionally described using terminology from the game of chess. Let &amp;lt;math&amp;gt;B\subseteq \{1,\ldots,n\}\times \{1,\ldots,n\}&amp;lt;/math&amp;gt;, called a &#039;&#039;&#039;board&#039;&#039;&#039;.  As illustrated below, we can think of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; as a chess board, with the positions in &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; marked by &amp;quot;&amp;lt;math&amp;gt;\times&amp;lt;/math&amp;gt;&amp;quot;.&lt;br /&gt;
{{Chess diagram small&lt;br /&gt;
| &lt;br /&gt;
| &lt;br /&gt;
|=&lt;br /&gt;
 8 |__|xx|xx|__|xx|__|__|xx|=&lt;br /&gt;
 7 |xx|__|__|xx|__|__|xx|__|=&lt;br /&gt;
 6 |xx|__|xx|xx|__|xx|xx|__|=&lt;br /&gt;
 5 |__|xx|__|__|xx|__|xx|__|=&lt;br /&gt;
 4 |xx|__|__|__|xx|xx|xx|__|=&lt;br /&gt;
 3 |__|xx|__|xx|__|__|__|xx|=&lt;br /&gt;
 2 |__|__|xx|__|xx|__|__|xx|=&lt;br /&gt;
 1 |xx|__|__|xx|__|xx|__|__|=&lt;br /&gt;
 a b c d e f g h&lt;br /&gt;
|&lt;br /&gt;
}}&lt;br /&gt;
For a permutation &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;\{1,\ldots,n\}&amp;lt;/math&amp;gt;, define the &#039;&#039;&#039;graph&#039;&#039;&#039; &amp;lt;math&amp;gt;G_\pi(V,E)&amp;lt;/math&amp;gt; as&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G_\pi &amp;amp;= \{(i,\pi(i))\mid i\in \{1,2,\ldots,n\}\}.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
This can also be viewed as a set of marked positions on a chess board. Each row and each column has only one marked position, because &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; is a permutation. Thus, we can identify each &amp;lt;math&amp;gt;G_\pi&amp;lt;/math&amp;gt; as a placement of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; rooks (“城堡”，规则同中国象棋里的“车”) without attacking each other.&lt;br /&gt;
&lt;br /&gt;
For example, the following is the &amp;lt;math&amp;gt;G_\pi&amp;lt;/math&amp;gt; of such &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; that &amp;lt;math&amp;gt;\pi(i)=i&amp;lt;/math&amp;gt;.&lt;br /&gt;
{{Chess diagram small&lt;br /&gt;
| &lt;br /&gt;
| &lt;br /&gt;
|=&lt;br /&gt;
 8 |rl|__|__|__|__|__|__|__|=&lt;br /&gt;
 7 |__|rl|__|__|__|__|__|__|=&lt;br /&gt;
 6 |__|__|rl|__|__|__|__|__|=&lt;br /&gt;
 5 |__|__|__|rl|__|__|__|__|=&lt;br /&gt;
 4 |__|__|__|__|rl|__|__|__|=&lt;br /&gt;
 3 |__|__|__|__|__|rl|__|__|=&lt;br /&gt;
 2 |__|__|__|__|__|__|rl|__|=&lt;br /&gt;
 1 |__|__|__|__|__|__|__|rl|=&lt;br /&gt;
 a b c d e f g h&lt;br /&gt;
|&lt;br /&gt;
}}&lt;br /&gt;
Now define&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
N_0 &amp;amp;= \left|\left\{\pi\mid B\cap G_\pi=\emptyset\right\}\right|\\&lt;br /&gt;
r_k &amp;amp;= \mbox{number of }k\mbox{-subsets of }B\mbox{ such that no two elements have a common coordinate}\\&lt;br /&gt;
&amp;amp;=\left|\left\{S\in{B\choose k} \,\bigg|\, \forall (i_1,j_1),(i_2,j_2)\in S, i_1\neq i_2, j_1\neq j_2 \right\}\right|&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Interpreted in chess game,&lt;br /&gt;
* &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;: a set of marked positions in an &amp;lt;math&amp;gt;[n]\times [n]&amp;lt;/math&amp;gt; chess board.&lt;br /&gt;
* &amp;lt;math&amp;gt;N_0&amp;lt;/math&amp;gt;: the number of ways of placing &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; non-attacking rooks on the chess board such that none of these rooks lie in &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;.&lt;br /&gt;
* &amp;lt;math&amp;gt;r_k&amp;lt;/math&amp;gt;: number of ways of placing &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; non-attacking rooks on &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Our goal is to count &amp;lt;math&amp;gt;N_0&amp;lt;/math&amp;gt; in terms of &amp;lt;math&amp;gt;r_k&amp;lt;/math&amp;gt;. This gives the number of permutations avoid all positions in a &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:&amp;lt;math&amp;gt;N_0=\sum_{k=0}^n(-1)^kr_k(n-k)!&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
{{Proof|&lt;br /&gt;
For each &amp;lt;math&amp;gt;i\in[n]&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;A_i=\{\pi\mid (i,\pi(i))\in B\}&amp;lt;/math&amp;gt; be the set of permutations &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; whose &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th position is in &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N_0&amp;lt;/math&amp;gt; is the number of permutations avoid all positions in &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;. Thus, our goal is to count the number of permutations &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; in none of &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;i\in [n]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For each &amp;lt;math&amp;gt;I\subseteq [n]&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;A_I=\bigcap_{i\in I}A_i&amp;lt;/math&amp;gt;, which is the set of permutations &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;(i,\pi(i))\in B&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;i\in I&amp;lt;/math&amp;gt;. Due to the principle of inclusion-exclusion,&lt;br /&gt;
:&amp;lt;math&amp;gt;N_0=\sum_{I\subseteq [n]} (-1)^{|I|}|A_I|=\sum_{k=0}^n(-1)^k\sum_{I\in{[n]\choose k}}|A_I|&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The next observation is that &lt;br /&gt;
:&amp;lt;math&amp;gt;\sum_{I\in{[n]\choose k}}|A_I|=r_k(n-k)!&amp;lt;/math&amp;gt;,&lt;br /&gt;
because we can count both sides by first placing &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; non-attacking rooks on &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; and placing &amp;lt;math&amp;gt;n-k&amp;lt;/math&amp;gt; additional non-attacking rooks on &amp;lt;math&amp;gt;[n]\times [n]&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;(n-k)!&amp;lt;/math&amp;gt; ways. &lt;br /&gt;
&lt;br /&gt;
Therefore,&lt;br /&gt;
:&amp;lt;math&amp;gt;N_0=\sum_{k=0}^n(-1)^kr_k(n-k)!&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
====Derangement problem====&lt;br /&gt;
We use the above general method to solve the derange problem again.&lt;br /&gt;
&lt;br /&gt;
Take &amp;lt;math&amp;gt;B=\{(1,1),(2,2),\ldots,(n,n)\}&amp;lt;/math&amp;gt; as the chess board.  A derangement &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; is a placement of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; non-attacking rooks such that none of them is in &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;. &lt;br /&gt;
{{Chess diagram small&lt;br /&gt;
| &lt;br /&gt;
| &lt;br /&gt;
|=&lt;br /&gt;
 8 |xx|__|__|__|__|__|__|__|=&lt;br /&gt;
 7 |__|xx|__|__|__|__|__|__|=&lt;br /&gt;
 6 |__|__|xx|__|__|__|__|__|=&lt;br /&gt;
 5 |__|__|__|xx|__|__|__|__|=&lt;br /&gt;
 4 |__|__|__|__|xx|__|__|__|=&lt;br /&gt;
 3 |__|__|__|__|__|xx|__|__|=&lt;br /&gt;
 2 |__|__|__|__|__|__|xx|__|=&lt;br /&gt;
 1 |__|__|__|__|__|__|__|xx|=&lt;br /&gt;
 a b c d e f g h&lt;br /&gt;
|&lt;br /&gt;
}}&lt;br /&gt;
Clearly, the number of ways of placing &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; non-attacking rooks on &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;r_k={n\choose k}&amp;lt;/math&amp;gt;. We want to count &amp;lt;math&amp;gt;N_0&amp;lt;/math&amp;gt;, which gives the number of ways of placing &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; non-attacking rooks such that none of these rooks lie in &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
By the above theorem&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
N_0=\sum_{k=0}^n(-1)^kr_k(n-k)!=\sum_{k=0}^n(-1)^k{n\choose k}(n-k)!=\sum_{k=0}^n(-1)^k\frac{n!}{k!}=n!\sum_{k=0}^n(-1)^k\frac{1}{k!}\approx\frac{n!}{e}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Problème des ménages====&lt;br /&gt;
Suppose that in a banquet, we want to seat &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; couples at a circular table, satisfying the following constraints:&lt;br /&gt;
* Men and women are in alternate places.&lt;br /&gt;
* No one sits next to his/her spouse.&lt;br /&gt;
&lt;br /&gt;
In how many ways can this be done?&lt;br /&gt;
&lt;br /&gt;
(For convenience, we assume that every seat at the table marked differently so that rotating the seats clockwise or anti-clockwise will end up with a &#039;&#039;&#039;different&#039;&#039;&#039; solution.)&lt;br /&gt;
&lt;br /&gt;
First, let the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; ladies find their seats. They may either sit at the odd numbered seats or even numbered seats, in either case, there are &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt; different orders. Thus, there are &amp;lt;math&amp;gt;2(n!)&amp;lt;/math&amp;gt; ways to seat the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; ladies.&lt;br /&gt;
&lt;br /&gt;
After sitting the wives, we label the remaining &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; places clockwise as &amp;lt;math&amp;gt;0,1,\ldots, n-1&amp;lt;/math&amp;gt;. And a seating of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; husbands is given by a permutation &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;[n]&amp;lt;/math&amp;gt; defined as follows. Let &amp;lt;math&amp;gt;\pi(i)&amp;lt;/math&amp;gt; be the seat of the husband of he lady sitting at the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th place.&lt;br /&gt;
&lt;br /&gt;
It is easy to see that &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; satisfies that &amp;lt;math&amp;gt;\pi(i)\neq i&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\pi(i)\not\equiv i+1\pmod n&amp;lt;/math&amp;gt;, and every permutation &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; with these properties gives a feasible seating of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; husbands. Thus, we only need to count the number of permutations &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;\pi(i)\not\equiv i, i+1\pmod n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Take &amp;lt;math&amp;gt;B=\{(0,0),(1,1),\ldots,(n-1,n-1), (0,1),(1,2),\ldots,(n-2,n-1),(n-1,0)\}&amp;lt;/math&amp;gt; as the chess board.  A permutation &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; which defines a way of seating the husbands, is a placement of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; non-attacking rooks such that none of them is in &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;. &lt;br /&gt;
{{Chess diagram small&lt;br /&gt;
| &lt;br /&gt;
| &lt;br /&gt;
|=&lt;br /&gt;
 8 |xx|xx|__|__|__|__|__|__|=&lt;br /&gt;
 7 |__|xx|xx|__|__|__|__|__|=&lt;br /&gt;
 6 |__|__|xx|xx|__|__|__|__|=&lt;br /&gt;
 5 |__|__|__|xx|xx|__|__|__|=&lt;br /&gt;
 4 |__|__|__|__|xx|xx|__|__|=&lt;br /&gt;
 3 |__|__|__|__|__|xx|xx|__|=&lt;br /&gt;
 2 |__|__|__|__|__|__|xx|xx|=&lt;br /&gt;
 1 |xx|__|__|__|__|__|__|xx|=&lt;br /&gt;
 a b c d e f g h&lt;br /&gt;
|&lt;br /&gt;
}}&lt;br /&gt;
We need to compute &amp;lt;math&amp;gt;r_k&amp;lt;/math&amp;gt;, the number of ways of placing &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; non-attacking rooks on &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;. For our choice of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;r_k&amp;lt;/math&amp;gt; is the number of ways of choosing &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; points, no two consecutive, from a collection of &amp;lt;math&amp;gt;2n&amp;lt;/math&amp;gt; points arranged in a circle.&lt;br /&gt;
&lt;br /&gt;
We first see how to do this in a &#039;&#039;line&#039;&#039;.&lt;br /&gt;
{{Theorem|Lemma|&lt;br /&gt;
:The number of ways of choosing &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; &#039;&#039;non-consecutive&#039;&#039; objects from a collection of &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; objects arranged in a &#039;&#039;line&#039;&#039;, is &amp;lt;math&amp;gt;{m-k+1\choose k}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
{{Proof|&lt;br /&gt;
We draw a line of &amp;lt;math&amp;gt;m-k&amp;lt;/math&amp;gt; black points, and then insert &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; red points into the &amp;lt;math&amp;gt;m-k+1&amp;lt;/math&amp;gt; spaces between the black points (including the beginning and end).&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
&amp;amp;\sqcup \, \bullet \, \sqcup \, \bullet \, \sqcup \, \bullet \, \sqcup \, \bullet \, \sqcup \, \bullet \, \sqcup \, \bullet \, \sqcup \, \bullet \, \sqcup \\&lt;br /&gt;
&amp;amp;\qquad\qquad\qquad\quad\Downarrow\\&lt;br /&gt;
&amp;amp;\sqcup \, \bullet \,\, {\color{Red}\bullet} \, \bullet \,\, {\color{Red}\bullet} \, \bullet \, \sqcup \, \bullet \,\, {\color{Red}\bullet}\, \, \bullet \, \sqcup \, \bullet \, \sqcup \, \bullet \,\, {\color{Red}\bullet}&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
This gives us a line of &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; points, and the red points specifies the chosen objects, which are non-consecutive. The mapping is 1-1 correspondence.&lt;br /&gt;
There are &amp;lt;math&amp;gt;{m-k+1\choose k}&amp;lt;/math&amp;gt; ways of placing &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; red points into &amp;lt;math&amp;gt;m-k+1&amp;lt;/math&amp;gt; spaces.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
The problem of choosing non-consecutive objects in a circle can be reduced to the case that the objects are in a line.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Lemma|&lt;br /&gt;
:The number of ways of choosing &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; &#039;&#039;non-consecutive&#039;&#039; objects from a collection of &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; objects arranged in a &#039;&#039;circle&#039;&#039;, is &amp;lt;math&amp;gt;\frac{m}{m-k}{m-k\choose k}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
{{Proof|&lt;br /&gt;
Let &amp;lt;math&amp;gt;f(m,k)&amp;lt;/math&amp;gt; be the desired number; and let &amp;lt;math&amp;gt;g(m,k)&amp;lt;/math&amp;gt; be the number of ways of choosing &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; non-consecutive points from &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; points arranged in a circle, next coloring the &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; points red, and then coloring one of the uncolored point blue. &lt;br /&gt;
&lt;br /&gt;
Clearly, &amp;lt;math&amp;gt;g(m,k)=(m-k)f(m,k)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
But we can also compute &amp;lt;math&amp;gt;g(m,k)&amp;lt;/math&amp;gt; as follows:&lt;br /&gt;
* Choose one of the &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; points and color it blue. This gives us &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; ways.&lt;br /&gt;
* Cut the circle to make a line of &amp;lt;math&amp;gt;m-1&amp;lt;/math&amp;gt; points by removing the blue point.&lt;br /&gt;
* Choose &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; non-consecutive points from the line of &amp;lt;math&amp;gt;m-1&amp;lt;/math&amp;gt; points and color them red. This gives &amp;lt;math&amp;gt;{m-k\choose k}&amp;lt;/math&amp;gt; ways due to the previous lemma.&lt;br /&gt;
&lt;br /&gt;
Thus, &amp;lt;math&amp;gt;g(m,k)=m{m-k\choose k}&amp;lt;/math&amp;gt;. Therefore we have the desired number &amp;lt;math&amp;gt;f(m,k)=\frac{m}{m-k}{m-k\choose k}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
By the above lemma, we have that &amp;lt;math&amp;gt;r_k=\frac{2n}{2n-k}{2n-k\choose k}&amp;lt;/math&amp;gt;. Then apply the theorem of counting permutations with restricted positions,&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
N_0=\sum_{k=0}^n(-1)^kr_k(n-k)!=\sum_{k=0}^n(-1)^k\frac{2n}{2n-k}{2n-k\choose k}(n-k)!.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This gives the number of ways of seating the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; husbands &#039;&#039;after the ladies are seated&#039;&#039;. Recall that there are &amp;lt;math&amp;gt;2n!&amp;lt;/math&amp;gt; ways of seating the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; ladies. Thus, the total number of ways of seating &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; couples as required by problème des ménages is &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
2n!\sum_{k=0}^n(-1)^k\frac{2n}{2n-k}{2n-k\choose k}(n-k)!.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The Euler totient function === &lt;br /&gt;
Two integers &amp;lt;math&amp;gt;m, n&amp;lt;/math&amp;gt; are said to be &#039;&#039;&#039;relatively prime&#039;&#039;&#039; if their greatest common diviser &amp;lt;math&amp;gt;\mathrm{gcd}(m,n)=1&amp;lt;/math&amp;gt;. For a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;\phi(n)&amp;lt;/math&amp;gt; be the number of positive integers from &amp;lt;math&amp;gt;\{1,2,\ldots,n\}&amp;lt;/math&amp;gt; that are relative prime to &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. This function, called the Euler &amp;lt;math&amp;gt;\phi&amp;lt;/math&amp;gt; function or &#039;&#039;&#039;the Euler totient function&#039;&#039;&#039;, is fundamental in number theory.&lt;br /&gt;
&lt;br /&gt;
We know derive a formula for this function by using the principle of inclusion-exclusion.&lt;br /&gt;
{{Theorem|Theorem (The Euler totient function)|&lt;br /&gt;
Suppose &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is divisible by precisely &amp;lt;math&amp;gt;r&amp;lt;/math&amp;gt; different primes, denoted &amp;lt;math&amp;gt;p_1,\ldots,p_r&amp;lt;/math&amp;gt;. Then&lt;br /&gt;
:&amp;lt;math&amp;gt;\phi(n)=n\prod_{i=1}^r\left(1-\frac{1}{p_i}\right)&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
{{Proof|&lt;br /&gt;
Let &amp;lt;math&amp;gt;U=\{1,2,\ldots,n\}&amp;lt;/math&amp;gt; be the universe. The number of positive integers from &amp;lt;math&amp;gt;U&amp;lt;/math&amp;gt; which is divisible by some &amp;lt;math&amp;gt;p_{i_1},p_{i_2},\ldots,p_{i_s}\in\{p_1,\ldots,p_r\}&amp;lt;/math&amp;gt;, is &amp;lt;math&amp;gt;\frac{n}{p_{i_1}p_{i_2}\cdots p_{i_s}}&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\phi(n)&amp;lt;/math&amp;gt; is the number of integers from &amp;lt;math&amp;gt;U&amp;lt;/math&amp;gt; which is not divisible by any &amp;lt;math&amp;gt;p_1,\ldots,p_r&amp;lt;/math&amp;gt;.&lt;br /&gt;
By principle of inclusion-exclusion,&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\phi(n)&lt;br /&gt;
&amp;amp;=n+\sum_{k=1}^r(-1)^k\sum_{1\le i_1&amp;lt;i_2&amp;lt;\cdots &amp;lt;i_k\le n}\frac{n}{p_{i_1}p_{i_2}\cdots p_{i_k}}\\&lt;br /&gt;
&amp;amp;=n-\sum_{1\le i\le n}\frac{n}{p_i}+\sum_{1\le i&amp;lt;j\le n}\frac{n}{p_i p_j}-\sum_{1\le i&amp;lt;j&amp;lt;k\le n}\frac{n}{p_{i} p_{j} p_{k}}+\cdots + (-1)^r\frac{n}{p_{1}p_{2}\cdots p_{r}}\\&lt;br /&gt;
&amp;amp;=n\left(1-\sum_{1\le i\le n}\frac{1}{p_i}+\sum_{1\le i&amp;lt;j\le n}\frac{1}{p_i p_j}-\sum_{1\le i&amp;lt;j&amp;lt;k\le n}\frac{1}{p_{i} p_{j} p_{k}}+\cdots + (-1)^r\frac{1}{p_{1}p_{2}\cdots p_{r}}\right)\\&lt;br /&gt;
&amp;amp;=n\prod_{i=1}^n\left(1-\frac{1}{p_i}\right).&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
* &#039;&#039;Stanley,&#039;&#039; Enumerative Combinatorics, Volume 1, Chapter 2.&lt;br /&gt;
* &#039;&#039;van Lin and Wilson&#039;&#039;, A course in combinatorics, Chapter 10, 15.&lt;/div&gt;</summary>
		<author><name>172.21.1.108</name></author>
	</entry>
	<entry>
		<id>https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3154</id>
		<title>Combinatorics (Fall 2010)/Generating functions</title>
		<link rel="alternate" type="text/html" href="https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3154"/>
		<updated>2010-09-12T06:32:36Z</updated>

		<summary type="html">&lt;p&gt;172.21.1.108: /* Reference */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Generating Functions ==&lt;br /&gt;
In Stanley&#039;s magnificent book &#039;&#039;Enumerative Combinatorics&#039;&#039;, he comments the generating function as &amp;quot;the most useful but most difficult to understand method (for counting)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The solution to a counting problem is usually represented as some &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; depending a parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Sometimes this &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is called a &#039;&#039;counting function&#039;&#039; as it is a function of the parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.  &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; can also be treated as a infinite series:&lt;br /&gt;
:&amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;ordinary generating function (OGF)&#039;&#039;&#039; defined by &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
G(x)=\sum_{n\ge 0} a_nx^n.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So &amp;lt;math&amp;gt;G(x)=a_0+a_1x+a_2x^2+\cdots&amp;lt;/math&amp;gt;. An expression in this form is called a [http://en.wikipedia.org/wiki/Formal_power_series &#039;&#039;&#039;formal power series&#039;&#039;&#039;], and &amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt; is the sequence of &#039;&#039;&#039;coefficients&#039;&#039;&#039;. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the generating function can be expanded as&lt;br /&gt;
:G(x)=&amp;lt;math&amp;gt;(\underbrace{1+\cdots+1}_{a_0})+(\underbrace{x+\cdots+x}_{a_1})+(\underbrace{x^2+\cdots+x^2}_{a_2})+\cdots+(\underbrace{x^n+\cdots+x^n}_{a_n})+\cdots&amp;lt;/math&amp;gt;&lt;br /&gt;
so it indeed &amp;quot;generates&amp;quot; all the possible instances of the objects we want to count.&lt;br /&gt;
&lt;br /&gt;
Usually, we do not evaluate the generating function &amp;lt;math&amp;gt;GF(x)&amp;lt;/math&amp;gt; on any particular value. &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; remains as a &#039;&#039;&#039;formal variable&#039;&#039;&#039; without assuming any value. The numbers that we want to count are the coefficients carried by the terms in the formal power series. So far the generating function is just another way to represent the sequence&lt;br /&gt;
:&amp;lt;math&amp;gt;(a_0,a_1,a_2,\ldots\ldots)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The true power of generating functions comes from the various algebraic operations that we can perform on these generating functions. We use an example to demonstrate this.&lt;br /&gt;
&lt;br /&gt;
=== Combinations ===&lt;br /&gt;
Suppose we wish to enumerate all subsets of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. To construct a subset, we specifies for every element of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set whether the element is chosen or not. Let us denote the choice to omit an element by &amp;lt;math&amp;gt;x_0&amp;lt;/math&amp;gt;, and the choice to include it by &amp;lt;math&amp;gt;x_1&amp;lt;/math&amp;gt;. Using &amp;quot;&amp;lt;math&amp;gt;+&amp;lt;/math&amp;gt;&amp;quot; to represent &amp;quot;OR&amp;quot;, and using the multiplication to denote &amp;quot;AND&amp;quot;, the choices of subsets of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set are expressed as&lt;br /&gt;
:&amp;lt;math&amp;gt;\underbrace{(x_0+x_1)(x_0+x_1)\cdots (x_0+x_1)}_{n\mbox{ elements}}=(x_0+x_1)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For example, when &amp;lt;math&amp;gt;n=3&amp;lt;/math&amp;gt;, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
(x_0+x_1)^3&lt;br /&gt;
&amp;amp;=x_0x_0x_0+x_0x_0x_1+x_0x_1x_0+x_0x_1x_1\\&lt;br /&gt;
&amp;amp;\quad +x_1x_0x_0+x_1x_0x_1+x_1x_1x_0+x_1x_1x_1&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
So it &amp;quot;generate&amp;quot; all subsets of the 3-set. Writing &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;x_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;x_1&amp;lt;/math&amp;gt;, we have &amp;lt;math&amp;gt;(1+x)^3=1+3x+3x^2+x^3&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^k&amp;lt;/math&amp;gt; is the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-subsets of a 3-element set.&lt;br /&gt;
&lt;br /&gt;
In general, &amp;lt;math&amp;gt;(1+x)^n&amp;lt;/math&amp;gt; has the coefficients which are the number of subsets of fixed sizes of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set.&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
Suppose that we have twelve balls: &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;3 red&amp;lt;/font&amp;gt;, &amp;lt;font color=&amp;quot;blue&amp;quot;&amp;gt;4 blue&amp;lt;/font&amp;gt;, and &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;5 green&amp;lt;/font&amp;gt;. Balls with the same color are indistinguishable.&lt;br /&gt;
&lt;br /&gt;
We want to determine the number of ways to select &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; balls from these twelve balls, for some &amp;lt;math&amp;gt;0\le k\le 12&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The generating function of this sequence is&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
&amp;amp;\quad {\color{Red}(1+x+x^2+x^3)}{\color{Blue}(1+x+x^2+x^3+x^4)}{\color{OliveGreen}(1+x+x^2+x^3+x^4+x^5)}\\&lt;br /&gt;
&amp;amp;=1+3x+6x^2+10x^3+14x^4+17x^5+18x^6+17x^7+14x^8+10x^9+6x^{10}+3x^{11}+x^{12}.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
The coefficient of &amp;lt;math&amp;gt;x^k&amp;lt;/math&amp;gt; gives the number of ways to select &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; balls.&lt;br /&gt;
&lt;br /&gt;
=== Fibonacci numbers  ===&lt;br /&gt;
Consider the following counting problems.&lt;br /&gt;
* Count the number of ways that the nonnegative integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; can be written as a sum of ones and twos (in order).&lt;br /&gt;
: The problem asks for the number of compositions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; with summands from &amp;lt;math&amp;gt;\{1,2\}&amp;lt;/math&amp;gt;. Formally, we are counting the number of tuples &amp;lt;math&amp;gt;(x_1,x_2,\ldots,x_k)&amp;lt;/math&amp;gt; for some &amp;lt;math&amp;gt;k\le n&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;x_i\in\{1,2\}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt;.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. We observe that a composition either starts with a 1, in which case the rest is a composition of &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt;; or starts with a 2, in which case the rest is a composition of &amp;lt;math&amp;gt;n-2&amp;lt;/math&amp;gt;. So we have the recursion for &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; that&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
* Count the ways to completely cover a &amp;lt;math&amp;gt;2\times n&amp;lt;/math&amp;gt; rectangle with &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; dominos without any overlaps.&lt;br /&gt;
: Dominos are identical &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; rectangles, so that only their orientations --- vertical or horizontal matter.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. It also holds that &amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;. The proof is left as an exercise.&lt;br /&gt;
&lt;br /&gt;
In both problems, the solution is given by &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; which satisfies the following recursion.&lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\begin{cases}&lt;br /&gt;
F_{n-1}+F_{n-2} &amp;amp; \mbox{if }n\ge 2,\\&lt;br /&gt;
1 &amp;amp; \mbox{if }n=1\\&lt;br /&gt;
0 &amp;amp; \mbox{if }n=0.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is called the [http://en.wikipedia.org/wiki/Fibonacci_number Fibonacci number].&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)&amp;lt;/math&amp;gt;,&lt;br /&gt;
:where &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
The quantity &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; is the so-called [http://en.wikipedia.org/wiki/Golden_ratio golden ratio], a constant with some significance in mathematics and aesthetics.&lt;br /&gt;
&lt;br /&gt;
We now prove this theorem by using generating functions.&lt;br /&gt;
The ordinary generating function for the Fibonacci number &amp;lt;math&amp;gt;F_{n}&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}F_n x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
We have that &amp;lt;math&amp;gt;F_{n}=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;n\ge 2&amp;lt;/math&amp;gt;, thus&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
G(x) &lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}F_n x^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
x+\sum_{n\ge 2}(F_{n-1}+F_{n-2})x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
For generating functions, there are general ways to generate &amp;lt;math&amp;gt;F_{n-1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F_{n-2}&amp;lt;/math&amp;gt;, or the coefficients with any smaller indices.&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
xG(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+1}=\sum_{n\ge 1}F_{n-1} x^n=\sum_{n\ge 2}F_{n-1} x^n\\&lt;br /&gt;
x^2G(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+2}=\sum_{n\ge 2}F_{n-2} x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we have&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;,&lt;br /&gt;
hence&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
The value of &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in the Taylor series for this formular, which is &amp;lt;math&amp;gt;\frac{G^{(n)}(0)}{n!}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;. Although this expansion works in principle, the detailed calculus is rather painful.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
It is easier to expand the generating function by breaking it into two geometric series.&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. It holds that&lt;br /&gt;
::&amp;lt;math&amp;gt;\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
It is easy to verify the above equation, but to deduce it, we need some (high school) calculation.&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; width=&amp;quot;100%&amp;quot; cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|&lt;br /&gt;
:{|&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;math&amp;gt;1-x-x^2&amp;lt;/math&amp;gt; has two roots &amp;lt;math&amp;gt;\frac{-1\pm\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Denote that &amp;lt;math&amp;gt;\phi=\frac{2}{-1+\sqrt{5}}=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{2}{-1-\sqrt{5}}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Then &amp;lt;math&amp;gt;(1-x-x^2)=(1-\phi x)(1-\hat{\phi}x)&amp;lt;/math&amp;gt;, so we can write &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\frac{x}{1-x-x^2}&lt;br /&gt;
&amp;amp;=\frac{x}{(1-\phi x)(1-\hat{\phi} x)}\\&lt;br /&gt;
&amp;amp;=\frac{\alpha}{(1-\phi x)}+\frac{\beta}{(1-\hat{\phi} x)},&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
where &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta&amp;lt;/math&amp;gt; satisfying that&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{cases}&lt;br /&gt;
\alpha+\beta=0\\&lt;br /&gt;
\alpha\phi+\beta\hat{\phi}= -1.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
Solving this we have that &amp;lt;math&amp;gt;\alpha=\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta=-\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt;. Thus,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
|}&lt;br /&gt;
:&amp;lt;math&amp;gt;\square&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the expression &amp;lt;math&amp;gt;\frac{1}{1-z}&amp;lt;/math&amp;gt; has a well known geometric expansion:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-z}=\sum_{n\ge 0}z^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Therefore, &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; can be expanded as&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}\\&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\phi x)^n-\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\hat{\phi} x)^n\\&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)x^n.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
So the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Fibonacci number is given by &lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Solving recurrences ==&lt;br /&gt;
The following steps describe a general methodology of solving recurrences by generating functions.&lt;br /&gt;
:1. Give a recursion that computes &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;. In the case of Fibonacci sequence&lt;br /&gt;
::&amp;lt;math&amp;gt;a_n=a_{n-1}+a_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:2. Multiply both sides of the equation by &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; and sum over all &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. This gives the generating function&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}a_nx^n=\sum_{n\ge 0}(a_{n-1}+a_{n-2})x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
:: And manipulate the right hand side of the equation so that it becomes some other expression involving &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
:3. Solve the resulting equation to derive an explicit formula for &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:4. Expand &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; into a power series and read off the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt;, which is a closed form for &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The first step is usually established by combinatorial observations, or explicitly given by the problem. The third step is trivial.&lt;br /&gt;
&lt;br /&gt;
The second and the forth steps need some non-trivial analytic techniques.&lt;br /&gt;
&lt;br /&gt;
=== Algebraic operations on generating functions ===&lt;br /&gt;
The second step in the above methodology is somehow tricky. It involves first applying the recurrence to the coefficients of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is easy; and then manipulating the resulting formal power series to express it in terms of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is more difficult (because it works backwards).&lt;br /&gt;
&lt;br /&gt;
We can apply several natural algebraic operations on the formal power series.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Generating function manipulation|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}g_nx^n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F(x)=\sum_{n\ge 0}f_nx^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
x^k G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge k}g_{n-k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\frac{G(x)-\sum_{i=0}^{k-1}g_iz^i}{x^k}&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}g_{n+k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\alpha F(x)+\beta G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} (\alpha f_n+\beta g_n)x^n\\&lt;br /&gt;
F(x)G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0}\sum_{k=0}^nf_kg_{n-k}x^n\\&lt;br /&gt;
G(cx)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} c^ng_n x^n\\&lt;br /&gt;
G&#039;(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}(n+1)g_{n+1}x^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
When manipulating generating functions, these rules are applied backwards; that is, from the right-hand-side to the left-hand-side.&lt;br /&gt;
&lt;br /&gt;
=== Expanding generating functions ===&lt;br /&gt;
The last step of solving recurrences by generating function is expanding the closed form generating function &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; to evaluate its &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th coefficient. In principle, we can always use the [http://en.wikipedia.org/wiki/Taylor_series Taylor series]&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}\frac{G^{(n)}(0)}{n!}x^n&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;G^{(n)}(0)&amp;lt;/math&amp;gt; is the value of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; evaluated at &amp;lt;math&amp;gt;x=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Some interesting special cases are very useful.&lt;br /&gt;
&lt;br /&gt;
====Geometric sequence====&lt;br /&gt;
In the example of Fibonacci numbers, we use the well known geometric series:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-x}=\sum_{n\ge 0}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
It is useful when we can express the generating function in the form of &amp;lt;math&amp;gt;G(x)=\frac{a_1}{1-b_1x}+\frac{a_2}{1-b_2x}+\cdots+\frac{a_k}{1-b_kx}&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in such &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;a_1b_1^n+a_2b_2^n+\cdots+a_kb_k^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Binomial theorem====&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;(1+x)^\alpha&amp;lt;/math&amp;gt; for some real &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; is &lt;br /&gt;
:&amp;lt;math&amp;gt;\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)(1+x)^{\alpha-n}&amp;lt;/math&amp;gt;.&lt;br /&gt;
By Taylor series, we get a generalized version of the binomial theorem known as [http://en.wikipedia.org/wiki/Binomial_coefficient#Newton.27s_binomial_series &#039;&#039;&#039;Newton&#039;s formula&#039;&#039;&#039;]:&lt;br /&gt;
{{Theorem|Newton&#039;s formular (generalized binomial theorem)|&lt;br /&gt;
If &amp;lt;math&amp;gt;|x|&amp;lt;1&amp;lt;/math&amp;gt;, then&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x)^\alpha=\sum_{n\ge 0}{\alpha\choose n}x^{n}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;{\alpha\choose n}&amp;lt;/math&amp;gt; is the &#039;&#039;&#039;generalized binomial coefficient&#039;&#039;&#039; defined by &lt;br /&gt;
:&amp;lt;math&amp;gt;{\alpha\choose n}=\frac{\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)}{n!}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Example: multisets ===&lt;br /&gt;
In the last lecture we gave a combinatorial proof of the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-multisets on an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. Now we give a generating function approach to the problem.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;S=\{x_1,x_2,\ldots,x_n\}&amp;lt;/math&amp;gt; be an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set. We have&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x_1+x_1^2+\cdots)(1+x_2+x_2^2+\cdots)\cdots(1+x_n+x_n^2+\cdots)=\sum_{m:S\rightarrow\mathbb{N}} \prod_{x_i\in S}x_i^{m(x_i)}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where each &amp;lt;math&amp;gt;m:S\rightarrow\mathbb{N}&amp;lt;/math&amp;gt; species a possible multiset on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; with multiplicity function &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let all &amp;lt;math&amp;gt;x_i=x&amp;lt;/math&amp;gt;. Then&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
(1+x+x^2+\cdots)^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{m:S\rightarrow\mathbb{N}}x^{m(x_1)+\cdots+m(x_n)}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{\text{multiset }M\text{ on }S}x^{|M|}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{k\ge 0}\left({n\choose k}\right)x^k.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the the definition of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. Our task is to evaluate &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Due to the geometric sequence and the Newton&#039;s formula&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
(1+x+x^2+\cdots)^n=(1-x)^{-n}=\sum_{k\ge 0}{-n\choose k}(-x)^k.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\left({n\choose k}\right)=(-1)^k{-n\choose k}={n+k-1\choose k}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the definition of the generalized binomial coefficient. We use an analytic (generating function) proof to get the same result of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt; as the combinatorial proof.&lt;br /&gt;
&lt;br /&gt;
=== Example: Quicksort ===&lt;br /&gt;
&lt;br /&gt;
== Catalan Number ==&lt;br /&gt;
We now introduce a class of counting problems, all with the same solution, called [http://en.wikipedia.org/wiki/Catalan_number &#039;&#039;&#039;Catalan number&#039;&#039;&#039;]. &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Catalan number is denoted as &amp;lt;math&amp;gt;C_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
In Volume 2 of Stanley&#039;s &#039;&#039;Enumerative Combinatorics&#039;&#039;, a set of exercises describe 66 different interpretations of the Catalan numbers. We give a few examples, cited from Wikipedia.&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;Dyck words&#039;&#039;&#039; of length 2&#039;&#039;n&#039;&#039;. A Dyck word is a string consisting of &#039;&#039;n&#039;&#039; X&#039;s and &#039;&#039;n&#039;&#039; Y&#039;s such that no initial segment of the string has more Y&#039;s than X&#039;s (see also [http://en.wikipedia.org/wiki/Dyck_language Dyck language]). For example, the following are the Dyck words of length 6:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; XXXYYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXXYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYXYY.&amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; counts the number of expressions containing &#039;&#039;n&#039;&#039; pairs of parentheses which are correctly matched:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; ((())) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()(()) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()()() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (())() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (()()) &amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 factors can be completely parenthesized (or the number of ways of associating &#039;&#039;n&#039;&#039; applications of a &#039;&#039;&#039;binary operator&#039;&#039;&#039;). For &#039;&#039;n&#039;&#039; = 3, for example, we have the following five different parenthesizations of four factors:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;math&amp;gt;((ab)c)d \quad (a(bc))d \quad(ab)(cd) \quad a((bc)d) \quad a(b(cd))&amp;lt;/math&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Successive applications of a binary operator can be represented in terms of a &#039;&#039;&#039;full binary tree&#039;&#039;&#039;. (A rooted binary tree is &#039;&#039;full&#039;&#039; if every vertex has either two children or no children.) It follows that &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of full binary trees with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 leaves:&lt;br /&gt;
[[Image:Catalan number binary tree example.png|center]] &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;monotonic paths&#039;&#039;&#039; along the edges of a grid with &#039;&#039;n&#039;&#039; × &#039;&#039;n&#039;&#039; square cells, which do not pass above the diagonal. A monotonic path is one which starts in the lower left corner, finishes in the upper right corner, and consists entirely of edges pointing rightwards or upwards. Counting such paths is equivalent to counting Dyck words: X stands for &amp;quot;move right&amp;quot; and Y stands for &amp;quot;move up&amp;quot;. The following diagrams show the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan number 4x4 grid example.svg.png|450px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways a [http://en.wikipedia.org/wiki/Convex_polygon &#039;&#039;&#039;convex polygon&#039;&#039;&#039;] with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;2 sides can be cut into &#039;&#039;&#039;triangles&#039;&#039;&#039; by connecting vertices with straight lines. The following hexagons illustrate the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan-Hexagons-example.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of [http://en.wikipedia.org/wiki/Stack_(data_structure) &#039;&#039;&#039;stack&#039;&#039;&#039;]-sortable permutations of {1, ..., &#039;&#039;n&#039;&#039;}. A permutation &#039;&#039;w&#039;&#039; is called &#039;&#039;&#039;stack-sortable&#039;&#039;&#039; if &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;(1,&amp;amp;nbsp;...,&amp;amp;nbsp;&#039;&#039;n&#039;&#039;), where &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) is defined recursively as follows: write &#039;&#039;w&#039;&#039; =&amp;amp;nbsp;&#039;&#039;unv&#039;&#039; where &#039;&#039;n&#039;&#039; is the largest element in &#039;&#039;w&#039;&#039; and &#039;&#039;u&#039;&#039; and &#039;&#039;v&#039;&#039; are shorter sequences, and set &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;&#039;&#039;S&#039;&#039;(&#039;&#039;u&#039;&#039;)&#039;&#039;S&#039;&#039;(&#039;&#039;v&#039;&#039;)&#039;&#039;n&#039;&#039;, with &#039;&#039;S&#039;&#039; being the identity for one-element sequences. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of ways to tile a stairstep shape of height &#039;&#039;n&#039;&#039; with &#039;&#039;n&#039;&#039; rectangles. The following figure illustrates the case &#039;&#039;n&#039;&#039;&amp;amp;nbsp;=&amp;amp;nbsp;4:&lt;br /&gt;
[[Image:Catalan stairsteps 4.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Recurrence relation for Catalan numbers|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;C_1=1&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;n&amp;gt;1&amp;lt;/math&amp;gt;,&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
C_n=\sum_{i=1}^{n-1}C_iC_{n-i}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n&amp;lt;/math&amp;gt; be the generating function. Apply the product rule,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)^2=\sum_{n\ge 0}\sum_{k=0}^{n}C_kC_{n-k}x^n=\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
Due to the recurrence,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n=x+\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n=x+G(x)^2&amp;lt;/math&amp;gt;.&lt;br /&gt;
Solving this, we obtain&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{1\pm(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
Because &amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, it must hold that &amp;lt;math&amp;gt;G(x)=\frac{1-(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;, or otherwise the constant term is not zero. Expanding &amp;lt;math&amp;gt;(1-4x)^{1/2}&amp;lt;/math&amp;gt; by Newton&#039;s formula, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\frac{1-(1-4x)^{1/2}}{2}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
1-\frac{1}{2}\sum_{n\ge 0}{1/2\choose n}(-4x)^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Thus, &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
C_n&lt;br /&gt;
&amp;amp;=-\frac{1}{2}{1/2\choose n}(-4)^n\\&lt;br /&gt;
&amp;amp;=-\frac{1}{2}\cdot\frac{1}{2}\cdot\frac{-1}{2}\cdot\frac{-3}{2}\cdots\frac{-(2n-3)}{2}\cdot(-4)^n/n!\\&lt;br /&gt;
&amp;amp;=\frac{(2n-2)!}{(n-1)!n!}\\&lt;br /&gt;
&amp;amp;=\frac{1}{n}{2n-2\choose n-1}.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we prove the following closed form for Catalan number.&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_n=\frac{1}{n}{2n-2\choose n-1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
* &#039;&#039;Graham, Knuth, and Patashnik&#039;&#039;, Concrete Mathematics: A Foundation for Computer Science, Chapter 7.&lt;br /&gt;
* &#039;&#039;Cameron&#039;&#039;, Combinatorics: Topics, Techniques, Algorithms, Chapter 4.&lt;br /&gt;
* &#039;&#039;van Lin and Wilson&#039;&#039;, A course in combinatorics, Chapter 14.&lt;/div&gt;</summary>
		<author><name>172.21.1.108</name></author>
	</entry>
	<entry>
		<id>https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3153</id>
		<title>Combinatorics (Fall 2010)/Generating functions</title>
		<link rel="alternate" type="text/html" href="https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3153"/>
		<updated>2010-09-12T06:32:05Z</updated>

		<summary type="html">&lt;p&gt;172.21.1.108: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Generating Functions ==&lt;br /&gt;
In Stanley&#039;s magnificent book &#039;&#039;Enumerative Combinatorics&#039;&#039;, he comments the generating function as &amp;quot;the most useful but most difficult to understand method (for counting)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The solution to a counting problem is usually represented as some &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; depending a parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Sometimes this &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is called a &#039;&#039;counting function&#039;&#039; as it is a function of the parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.  &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; can also be treated as a infinite series:&lt;br /&gt;
:&amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;ordinary generating function (OGF)&#039;&#039;&#039; defined by &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
G(x)=\sum_{n\ge 0} a_nx^n.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So &amp;lt;math&amp;gt;G(x)=a_0+a_1x+a_2x^2+\cdots&amp;lt;/math&amp;gt;. An expression in this form is called a [http://en.wikipedia.org/wiki/Formal_power_series &#039;&#039;&#039;formal power series&#039;&#039;&#039;], and &amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt; is the sequence of &#039;&#039;&#039;coefficients&#039;&#039;&#039;. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the generating function can be expanded as&lt;br /&gt;
:G(x)=&amp;lt;math&amp;gt;(\underbrace{1+\cdots+1}_{a_0})+(\underbrace{x+\cdots+x}_{a_1})+(\underbrace{x^2+\cdots+x^2}_{a_2})+\cdots+(\underbrace{x^n+\cdots+x^n}_{a_n})+\cdots&amp;lt;/math&amp;gt;&lt;br /&gt;
so it indeed &amp;quot;generates&amp;quot; all the possible instances of the objects we want to count.&lt;br /&gt;
&lt;br /&gt;
Usually, we do not evaluate the generating function &amp;lt;math&amp;gt;GF(x)&amp;lt;/math&amp;gt; on any particular value. &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; remains as a &#039;&#039;&#039;formal variable&#039;&#039;&#039; without assuming any value. The numbers that we want to count are the coefficients carried by the terms in the formal power series. So far the generating function is just another way to represent the sequence&lt;br /&gt;
:&amp;lt;math&amp;gt;(a_0,a_1,a_2,\ldots\ldots)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The true power of generating functions comes from the various algebraic operations that we can perform on these generating functions. We use an example to demonstrate this.&lt;br /&gt;
&lt;br /&gt;
=== Combinations ===&lt;br /&gt;
Suppose we wish to enumerate all subsets of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. To construct a subset, we specifies for every element of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set whether the element is chosen or not. Let us denote the choice to omit an element by &amp;lt;math&amp;gt;x_0&amp;lt;/math&amp;gt;, and the choice to include it by &amp;lt;math&amp;gt;x_1&amp;lt;/math&amp;gt;. Using &amp;quot;&amp;lt;math&amp;gt;+&amp;lt;/math&amp;gt;&amp;quot; to represent &amp;quot;OR&amp;quot;, and using the multiplication to denote &amp;quot;AND&amp;quot;, the choices of subsets of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set are expressed as&lt;br /&gt;
:&amp;lt;math&amp;gt;\underbrace{(x_0+x_1)(x_0+x_1)\cdots (x_0+x_1)}_{n\mbox{ elements}}=(x_0+x_1)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For example, when &amp;lt;math&amp;gt;n=3&amp;lt;/math&amp;gt;, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
(x_0+x_1)^3&lt;br /&gt;
&amp;amp;=x_0x_0x_0+x_0x_0x_1+x_0x_1x_0+x_0x_1x_1\\&lt;br /&gt;
&amp;amp;\quad +x_1x_0x_0+x_1x_0x_1+x_1x_1x_0+x_1x_1x_1&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
So it &amp;quot;generate&amp;quot; all subsets of the 3-set. Writing &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;x_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;x_1&amp;lt;/math&amp;gt;, we have &amp;lt;math&amp;gt;(1+x)^3=1+3x+3x^2+x^3&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^k&amp;lt;/math&amp;gt; is the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-subsets of a 3-element set.&lt;br /&gt;
&lt;br /&gt;
In general, &amp;lt;math&amp;gt;(1+x)^n&amp;lt;/math&amp;gt; has the coefficients which are the number of subsets of fixed sizes of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set.&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
Suppose that we have twelve balls: &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;3 red&amp;lt;/font&amp;gt;, &amp;lt;font color=&amp;quot;blue&amp;quot;&amp;gt;4 blue&amp;lt;/font&amp;gt;, and &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;5 green&amp;lt;/font&amp;gt;. Balls with the same color are indistinguishable.&lt;br /&gt;
&lt;br /&gt;
We want to determine the number of ways to select &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; balls from these twelve balls, for some &amp;lt;math&amp;gt;0\le k\le 12&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The generating function of this sequence is&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
&amp;amp;\quad {\color{Red}(1+x+x^2+x^3)}{\color{Blue}(1+x+x^2+x^3+x^4)}{\color{OliveGreen}(1+x+x^2+x^3+x^4+x^5)}\\&lt;br /&gt;
&amp;amp;=1+3x+6x^2+10x^3+14x^4+17x^5+18x^6+17x^7+14x^8+10x^9+6x^{10}+3x^{11}+x^{12}.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
The coefficient of &amp;lt;math&amp;gt;x^k&amp;lt;/math&amp;gt; gives the number of ways to select &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; balls.&lt;br /&gt;
&lt;br /&gt;
=== Fibonacci numbers  ===&lt;br /&gt;
Consider the following counting problems.&lt;br /&gt;
* Count the number of ways that the nonnegative integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; can be written as a sum of ones and twos (in order).&lt;br /&gt;
: The problem asks for the number of compositions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; with summands from &amp;lt;math&amp;gt;\{1,2\}&amp;lt;/math&amp;gt;. Formally, we are counting the number of tuples &amp;lt;math&amp;gt;(x_1,x_2,\ldots,x_k)&amp;lt;/math&amp;gt; for some &amp;lt;math&amp;gt;k\le n&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;x_i\in\{1,2\}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt;.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. We observe that a composition either starts with a 1, in which case the rest is a composition of &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt;; or starts with a 2, in which case the rest is a composition of &amp;lt;math&amp;gt;n-2&amp;lt;/math&amp;gt;. So we have the recursion for &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; that&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
* Count the ways to completely cover a &amp;lt;math&amp;gt;2\times n&amp;lt;/math&amp;gt; rectangle with &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; dominos without any overlaps.&lt;br /&gt;
: Dominos are identical &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; rectangles, so that only their orientations --- vertical or horizontal matter.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. It also holds that &amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;. The proof is left as an exercise.&lt;br /&gt;
&lt;br /&gt;
In both problems, the solution is given by &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; which satisfies the following recursion.&lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\begin{cases}&lt;br /&gt;
F_{n-1}+F_{n-2} &amp;amp; \mbox{if }n\ge 2,\\&lt;br /&gt;
1 &amp;amp; \mbox{if }n=1\\&lt;br /&gt;
0 &amp;amp; \mbox{if }n=0.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is called the [http://en.wikipedia.org/wiki/Fibonacci_number Fibonacci number].&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)&amp;lt;/math&amp;gt;,&lt;br /&gt;
:where &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
The quantity &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; is the so-called [http://en.wikipedia.org/wiki/Golden_ratio golden ratio], a constant with some significance in mathematics and aesthetics.&lt;br /&gt;
&lt;br /&gt;
We now prove this theorem by using generating functions.&lt;br /&gt;
The ordinary generating function for the Fibonacci number &amp;lt;math&amp;gt;F_{n}&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}F_n x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
We have that &amp;lt;math&amp;gt;F_{n}=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;n\ge 2&amp;lt;/math&amp;gt;, thus&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
G(x) &lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}F_n x^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
x+\sum_{n\ge 2}(F_{n-1}+F_{n-2})x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
For generating functions, there are general ways to generate &amp;lt;math&amp;gt;F_{n-1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F_{n-2}&amp;lt;/math&amp;gt;, or the coefficients with any smaller indices.&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
xG(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+1}=\sum_{n\ge 1}F_{n-1} x^n=\sum_{n\ge 2}F_{n-1} x^n\\&lt;br /&gt;
x^2G(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+2}=\sum_{n\ge 2}F_{n-2} x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we have&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;,&lt;br /&gt;
hence&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
The value of &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in the Taylor series for this formular, which is &amp;lt;math&amp;gt;\frac{G^{(n)}(0)}{n!}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;. Although this expansion works in principle, the detailed calculus is rather painful.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
It is easier to expand the generating function by breaking it into two geometric series.&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. It holds that&lt;br /&gt;
::&amp;lt;math&amp;gt;\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
It is easy to verify the above equation, but to deduce it, we need some (high school) calculation.&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; width=&amp;quot;100%&amp;quot; cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|&lt;br /&gt;
:{|&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;math&amp;gt;1-x-x^2&amp;lt;/math&amp;gt; has two roots &amp;lt;math&amp;gt;\frac{-1\pm\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Denote that &amp;lt;math&amp;gt;\phi=\frac{2}{-1+\sqrt{5}}=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{2}{-1-\sqrt{5}}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Then &amp;lt;math&amp;gt;(1-x-x^2)=(1-\phi x)(1-\hat{\phi}x)&amp;lt;/math&amp;gt;, so we can write &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\frac{x}{1-x-x^2}&lt;br /&gt;
&amp;amp;=\frac{x}{(1-\phi x)(1-\hat{\phi} x)}\\&lt;br /&gt;
&amp;amp;=\frac{\alpha}{(1-\phi x)}+\frac{\beta}{(1-\hat{\phi} x)},&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
where &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta&amp;lt;/math&amp;gt; satisfying that&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{cases}&lt;br /&gt;
\alpha+\beta=0\\&lt;br /&gt;
\alpha\phi+\beta\hat{\phi}= -1.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
Solving this we have that &amp;lt;math&amp;gt;\alpha=\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta=-\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt;. Thus,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
|}&lt;br /&gt;
:&amp;lt;math&amp;gt;\square&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the expression &amp;lt;math&amp;gt;\frac{1}{1-z}&amp;lt;/math&amp;gt; has a well known geometric expansion:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-z}=\sum_{n\ge 0}z^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Therefore, &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; can be expanded as&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}\\&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\phi x)^n-\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\hat{\phi} x)^n\\&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)x^n.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
So the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Fibonacci number is given by &lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Solving recurrences ==&lt;br /&gt;
The following steps describe a general methodology of solving recurrences by generating functions.&lt;br /&gt;
:1. Give a recursion that computes &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;. In the case of Fibonacci sequence&lt;br /&gt;
::&amp;lt;math&amp;gt;a_n=a_{n-1}+a_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:2. Multiply both sides of the equation by &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; and sum over all &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. This gives the generating function&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}a_nx^n=\sum_{n\ge 0}(a_{n-1}+a_{n-2})x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
:: And manipulate the right hand side of the equation so that it becomes some other expression involving &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
:3. Solve the resulting equation to derive an explicit formula for &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:4. Expand &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; into a power series and read off the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt;, which is a closed form for &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The first step is usually established by combinatorial observations, or explicitly given by the problem. The third step is trivial.&lt;br /&gt;
&lt;br /&gt;
The second and the forth steps need some non-trivial analytic techniques.&lt;br /&gt;
&lt;br /&gt;
=== Algebraic operations on generating functions ===&lt;br /&gt;
The second step in the above methodology is somehow tricky. It involves first applying the recurrence to the coefficients of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is easy; and then manipulating the resulting formal power series to express it in terms of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is more difficult (because it works backwards).&lt;br /&gt;
&lt;br /&gt;
We can apply several natural algebraic operations on the formal power series.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Generating function manipulation|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}g_nx^n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F(x)=\sum_{n\ge 0}f_nx^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
x^k G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge k}g_{n-k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\frac{G(x)-\sum_{i=0}^{k-1}g_iz^i}{x^k}&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}g_{n+k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\alpha F(x)+\beta G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} (\alpha f_n+\beta g_n)x^n\\&lt;br /&gt;
F(x)G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0}\sum_{k=0}^nf_kg_{n-k}x^n\\&lt;br /&gt;
G(cx)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} c^ng_n x^n\\&lt;br /&gt;
G&#039;(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}(n+1)g_{n+1}x^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
When manipulating generating functions, these rules are applied backwards; that is, from the right-hand-side to the left-hand-side.&lt;br /&gt;
&lt;br /&gt;
=== Expanding generating functions ===&lt;br /&gt;
The last step of solving recurrences by generating function is expanding the closed form generating function &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; to evaluate its &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th coefficient. In principle, we can always use the [http://en.wikipedia.org/wiki/Taylor_series Taylor series]&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}\frac{G^{(n)}(0)}{n!}x^n&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;G^{(n)}(0)&amp;lt;/math&amp;gt; is the value of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; evaluated at &amp;lt;math&amp;gt;x=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Some interesting special cases are very useful.&lt;br /&gt;
&lt;br /&gt;
====Geometric sequence====&lt;br /&gt;
In the example of Fibonacci numbers, we use the well known geometric series:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-x}=\sum_{n\ge 0}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
It is useful when we can express the generating function in the form of &amp;lt;math&amp;gt;G(x)=\frac{a_1}{1-b_1x}+\frac{a_2}{1-b_2x}+\cdots+\frac{a_k}{1-b_kx}&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in such &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;a_1b_1^n+a_2b_2^n+\cdots+a_kb_k^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Binomial theorem====&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;(1+x)^\alpha&amp;lt;/math&amp;gt; for some real &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; is &lt;br /&gt;
:&amp;lt;math&amp;gt;\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)(1+x)^{\alpha-n}&amp;lt;/math&amp;gt;.&lt;br /&gt;
By Taylor series, we get a generalized version of the binomial theorem known as [http://en.wikipedia.org/wiki/Binomial_coefficient#Newton.27s_binomial_series &#039;&#039;&#039;Newton&#039;s formula&#039;&#039;&#039;]:&lt;br /&gt;
{{Theorem|Newton&#039;s formular (generalized binomial theorem)|&lt;br /&gt;
If &amp;lt;math&amp;gt;|x|&amp;lt;1&amp;lt;/math&amp;gt;, then&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x)^\alpha=\sum_{n\ge 0}{\alpha\choose n}x^{n}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;{\alpha\choose n}&amp;lt;/math&amp;gt; is the &#039;&#039;&#039;generalized binomial coefficient&#039;&#039;&#039; defined by &lt;br /&gt;
:&amp;lt;math&amp;gt;{\alpha\choose n}=\frac{\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)}{n!}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Example: multisets ===&lt;br /&gt;
In the last lecture we gave a combinatorial proof of the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-multisets on an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. Now we give a generating function approach to the problem.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;S=\{x_1,x_2,\ldots,x_n\}&amp;lt;/math&amp;gt; be an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set. We have&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x_1+x_1^2+\cdots)(1+x_2+x_2^2+\cdots)\cdots(1+x_n+x_n^2+\cdots)=\sum_{m:S\rightarrow\mathbb{N}} \prod_{x_i\in S}x_i^{m(x_i)}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where each &amp;lt;math&amp;gt;m:S\rightarrow\mathbb{N}&amp;lt;/math&amp;gt; species a possible multiset on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; with multiplicity function &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let all &amp;lt;math&amp;gt;x_i=x&amp;lt;/math&amp;gt;. Then&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
(1+x+x^2+\cdots)^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{m:S\rightarrow\mathbb{N}}x^{m(x_1)+\cdots+m(x_n)}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{\text{multiset }M\text{ on }S}x^{|M|}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{k\ge 0}\left({n\choose k}\right)x^k.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the the definition of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. Our task is to evaluate &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Due to the geometric sequence and the Newton&#039;s formula&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
(1+x+x^2+\cdots)^n=(1-x)^{-n}=\sum_{k\ge 0}{-n\choose k}(-x)^k.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\left({n\choose k}\right)=(-1)^k{-n\choose k}={n+k-1\choose k}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the definition of the generalized binomial coefficient. We use an analytic (generating function) proof to get the same result of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt; as the combinatorial proof.&lt;br /&gt;
&lt;br /&gt;
=== Example: Quicksort ===&lt;br /&gt;
&lt;br /&gt;
== Catalan Number ==&lt;br /&gt;
We now introduce a class of counting problems, all with the same solution, called [http://en.wikipedia.org/wiki/Catalan_number &#039;&#039;&#039;Catalan number&#039;&#039;&#039;]. &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Catalan number is denoted as &amp;lt;math&amp;gt;C_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
In Volume 2 of Stanley&#039;s &#039;&#039;Enumerative Combinatorics&#039;&#039;, a set of exercises describe 66 different interpretations of the Catalan numbers. We give a few examples, cited from Wikipedia.&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;Dyck words&#039;&#039;&#039; of length 2&#039;&#039;n&#039;&#039;. A Dyck word is a string consisting of &#039;&#039;n&#039;&#039; X&#039;s and &#039;&#039;n&#039;&#039; Y&#039;s such that no initial segment of the string has more Y&#039;s than X&#039;s (see also [http://en.wikipedia.org/wiki/Dyck_language Dyck language]). For example, the following are the Dyck words of length 6:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; XXXYYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXXYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYXYY.&amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; counts the number of expressions containing &#039;&#039;n&#039;&#039; pairs of parentheses which are correctly matched:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; ((())) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()(()) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()()() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (())() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (()()) &amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 factors can be completely parenthesized (or the number of ways of associating &#039;&#039;n&#039;&#039; applications of a &#039;&#039;&#039;binary operator&#039;&#039;&#039;). For &#039;&#039;n&#039;&#039; = 3, for example, we have the following five different parenthesizations of four factors:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;math&amp;gt;((ab)c)d \quad (a(bc))d \quad(ab)(cd) \quad a((bc)d) \quad a(b(cd))&amp;lt;/math&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Successive applications of a binary operator can be represented in terms of a &#039;&#039;&#039;full binary tree&#039;&#039;&#039;. (A rooted binary tree is &#039;&#039;full&#039;&#039; if every vertex has either two children or no children.) It follows that &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of full binary trees with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 leaves:&lt;br /&gt;
[[Image:Catalan number binary tree example.png|center]] &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;monotonic paths&#039;&#039;&#039; along the edges of a grid with &#039;&#039;n&#039;&#039; × &#039;&#039;n&#039;&#039; square cells, which do not pass above the diagonal. A monotonic path is one which starts in the lower left corner, finishes in the upper right corner, and consists entirely of edges pointing rightwards or upwards. Counting such paths is equivalent to counting Dyck words: X stands for &amp;quot;move right&amp;quot; and Y stands for &amp;quot;move up&amp;quot;. The following diagrams show the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan number 4x4 grid example.svg.png|450px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways a [http://en.wikipedia.org/wiki/Convex_polygon &#039;&#039;&#039;convex polygon&#039;&#039;&#039;] with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;2 sides can be cut into &#039;&#039;&#039;triangles&#039;&#039;&#039; by connecting vertices with straight lines. The following hexagons illustrate the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan-Hexagons-example.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of [http://en.wikipedia.org/wiki/Stack_(data_structure) &#039;&#039;&#039;stack&#039;&#039;&#039;]-sortable permutations of {1, ..., &#039;&#039;n&#039;&#039;}. A permutation &#039;&#039;w&#039;&#039; is called &#039;&#039;&#039;stack-sortable&#039;&#039;&#039; if &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;(1,&amp;amp;nbsp;...,&amp;amp;nbsp;&#039;&#039;n&#039;&#039;), where &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) is defined recursively as follows: write &#039;&#039;w&#039;&#039; =&amp;amp;nbsp;&#039;&#039;unv&#039;&#039; where &#039;&#039;n&#039;&#039; is the largest element in &#039;&#039;w&#039;&#039; and &#039;&#039;u&#039;&#039; and &#039;&#039;v&#039;&#039; are shorter sequences, and set &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;&#039;&#039;S&#039;&#039;(&#039;&#039;u&#039;&#039;)&#039;&#039;S&#039;&#039;(&#039;&#039;v&#039;&#039;)&#039;&#039;n&#039;&#039;, with &#039;&#039;S&#039;&#039; being the identity for one-element sequences. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of ways to tile a stairstep shape of height &#039;&#039;n&#039;&#039; with &#039;&#039;n&#039;&#039; rectangles. The following figure illustrates the case &#039;&#039;n&#039;&#039;&amp;amp;nbsp;=&amp;amp;nbsp;4:&lt;br /&gt;
[[Image:Catalan stairsteps 4.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Recurrence relation for Catalan numbers|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;C_1=1&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;n&amp;gt;1&amp;lt;/math&amp;gt;,&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
C_n=\sum_{i=1}^{n-1}C_iC_{n-i}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n&amp;lt;/math&amp;gt; be the generating function. Apply the product rule,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)^2=\sum_{n\ge 0}\sum_{k=0}^{n}C_kC_{n-k}x^n=\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
Due to the recurrence,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n=x+\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n=x+G(x)^2&amp;lt;/math&amp;gt;.&lt;br /&gt;
Solving this, we obtain&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{1\pm(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
Because &amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, it must hold that &amp;lt;math&amp;gt;G(x)=\frac{1-(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;, or otherwise the constant term is not zero. Expanding &amp;lt;math&amp;gt;(1-4x)^{1/2}&amp;lt;/math&amp;gt; by Newton&#039;s formula, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\frac{1-(1-4x)^{1/2}}{2}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
1-\frac{1}{2}\sum_{n\ge 0}{1/2\choose n}(-4x)^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Thus, &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
C_n&lt;br /&gt;
&amp;amp;=-\frac{1}{2}{1/2\choose n}(-4)^n\\&lt;br /&gt;
&amp;amp;=-\frac{1}{2}\cdot\frac{1}{2}\cdot\frac{-1}{2}\cdot\frac{-3}{2}\cdots\frac{-(2n-3)}{2}\cdot(-4)^n/n!\\&lt;br /&gt;
&amp;amp;=\frac{(2n-2)!}{(n-1)!n!}\\&lt;br /&gt;
&amp;amp;=\frac{1}{n}{2n-2\choose n-1}.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we prove the following closed form for Catalan number.&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_n=\frac{1}{n}{2n-2\choose n-1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
* &#039;&#039;Graham, Knuth, and Patashnik&#039;&#039;, Concrete Mathematics: A Foundation for Computer Science, Chapter 7.&lt;br /&gt;
* &#039;&#039;Cameron&#039;&#039;, Combinatorics: Topics, Techniques, Algorithms, Chapter 4.&lt;br /&gt;
* &amp;quot;van Lin and Wilson&amp;quot;, A course in combinatorics, Chapter 14.&lt;/div&gt;</summary>
		<author><name>172.21.1.108</name></author>
	</entry>
	<entry>
		<id>https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3152</id>
		<title>Combinatorics (Fall 2010)/Generating functions</title>
		<link rel="alternate" type="text/html" href="https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3152"/>
		<updated>2010-09-12T06:24:37Z</updated>

		<summary type="html">&lt;p&gt;172.21.1.108: /* Combinations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Generating Functions ==&lt;br /&gt;
In Stanley&#039;s magnificent book &#039;&#039;Enumerative Combinatorics&#039;&#039;, he comments the generating function as &amp;quot;the most useful but most difficult to understand method (for counting)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The solution to a counting problem is usually represented as some &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; depending a parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Sometimes this &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is called a &#039;&#039;counting function&#039;&#039; as it is a function of the parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.  &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; can also be treated as a infinite series:&lt;br /&gt;
:&amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;ordinary generating function (OGF)&#039;&#039;&#039; defined by &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
G(x)=\sum_{n\ge 0} a_nx^n.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So &amp;lt;math&amp;gt;G(x)=a_0+a_1x+a_2x^2+\cdots&amp;lt;/math&amp;gt;. An expression in this form is called a [http://en.wikipedia.org/wiki/Formal_power_series &#039;&#039;&#039;formal power series&#039;&#039;&#039;], and &amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt; is the sequence of &#039;&#039;&#039;coefficients&#039;&#039;&#039;. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the generating function can be expanded as&lt;br /&gt;
:G(x)=&amp;lt;math&amp;gt;(\underbrace{1+\cdots+1}_{a_0})+(\underbrace{x+\cdots+x}_{a_1})+(\underbrace{x^2+\cdots+x^2}_{a_2})+\cdots+(\underbrace{x^n+\cdots+x^n}_{a_n})+\cdots&amp;lt;/math&amp;gt;&lt;br /&gt;
so it indeed &amp;quot;generates&amp;quot; all the possible instances of the objects we want to count.&lt;br /&gt;
&lt;br /&gt;
Usually, we do not evaluate the generating function &amp;lt;math&amp;gt;GF(x)&amp;lt;/math&amp;gt; on any particular value. &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; remains as a &#039;&#039;&#039;formal variable&#039;&#039;&#039; without assuming any value. The numbers that we want to count are the coefficients carried by the terms in the formal power series. So far the generating function is just another way to represent the sequence&lt;br /&gt;
:&amp;lt;math&amp;gt;(a_0,a_1,a_2,\ldots\ldots)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The true power of generating functions comes from the various algebraic operations that we can perform on these generating functions. We use an example to demonstrate this.&lt;br /&gt;
&lt;br /&gt;
=== Combinations ===&lt;br /&gt;
Suppose we wish to enumerate all subsets of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. To construct a subset, we specifies for every element of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set whether the element is chosen or not. Let us denote the choice to omit an element by &amp;lt;math&amp;gt;x_0&amp;lt;/math&amp;gt;, and the choice to include it by &amp;lt;math&amp;gt;x_1&amp;lt;/math&amp;gt;. Using &amp;quot;&amp;lt;math&amp;gt;+&amp;lt;/math&amp;gt;&amp;quot; to represent &amp;quot;OR&amp;quot;, and using the multiplication to denote &amp;quot;AND&amp;quot;, the choices of subsets of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set are expressed as&lt;br /&gt;
:&amp;lt;math&amp;gt;\underbrace{(x_0+x_1)(x_0+x_1)\cdots (x_0+x_1)}_{n\mbox{ elements}}=(x_0+x_1)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For example, when &amp;lt;math&amp;gt;n=3&amp;lt;/math&amp;gt;, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
(x_0+x_1)^3&lt;br /&gt;
&amp;amp;=x_0x_0x_0+x_0x_0x_1+x_0x_1x_0+x_0x_1x_1\\&lt;br /&gt;
&amp;amp;\quad +x_1x_0x_0+x_1x_0x_1+x_1x_1x_0+x_1x_1x_1&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
So it &amp;quot;generate&amp;quot; all subsets of the 3-set. Writing &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;x_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;x_1&amp;lt;/math&amp;gt;, we have &amp;lt;math&amp;gt;(1+x)^3=1+3x+3x^2+x^3&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^k&amp;lt;/math&amp;gt; is the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-subsets of a 3-element set.&lt;br /&gt;
&lt;br /&gt;
In general, &amp;lt;math&amp;gt;(1+x)^n&amp;lt;/math&amp;gt; has the coefficients which are the number of subsets of fixed sizes of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set.&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
Suppose that we have twelve balls: &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;3 red&amp;lt;/font&amp;gt;, &amp;lt;font color=&amp;quot;blue&amp;quot;&amp;gt;4 blue&amp;lt;/font&amp;gt;, and &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;5 green&amp;lt;/font&amp;gt;. Balls with the same color are indistinguishable.&lt;br /&gt;
&lt;br /&gt;
We want to determine the number of ways to select &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; balls from these twelve balls, for some &amp;lt;math&amp;gt;0\le k\le 12&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The generating function of this sequence is&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
&amp;amp;\quad {\color{Red}(1+x+x^2+x^3)}{\color{Blue}(1+x+x^2+x^3+x^4)}{\color{OliveGreen}(1+x+x^2+x^3+x^4+x^5)}\\&lt;br /&gt;
&amp;amp;=1+3x+6x^2+10x^3+14x^4+17x^5+18x^6+17x^7+14x^8+10x^9+6x^{10}+3x^{11}+x^{12}.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
The coefficient of &amp;lt;math&amp;gt;x^k&amp;lt;/math&amp;gt; gives the number of ways to select &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; balls.&lt;br /&gt;
&lt;br /&gt;
=== Fibonacci numbers  ===&lt;br /&gt;
Consider the following counting problems.&lt;br /&gt;
* Count the number of ways that the nonnegative integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; can be written as a sum of ones and twos (in order).&lt;br /&gt;
: The problem asks for the number of compositions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; with summands from &amp;lt;math&amp;gt;\{1,2\}&amp;lt;/math&amp;gt;. Formally, we are counting the number of tuples &amp;lt;math&amp;gt;(x_1,x_2,\ldots,x_k)&amp;lt;/math&amp;gt; for some &amp;lt;math&amp;gt;k\le n&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;x_i\in\{1,2\}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt;.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. We observe that a composition either starts with a 1, in which case the rest is a composition of &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt;; or starts with a 2, in which case the rest is a composition of &amp;lt;math&amp;gt;n-2&amp;lt;/math&amp;gt;. So we have the recursion for &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; that&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
* Count the ways to completely cover a &amp;lt;math&amp;gt;2\times n&amp;lt;/math&amp;gt; rectangle with &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; dominos without any overlaps.&lt;br /&gt;
: Dominos are identical &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; rectangles, so that only their orientations --- vertical or horizontal matter.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. It also holds that &amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;. The proof is left as an exercise.&lt;br /&gt;
&lt;br /&gt;
In both problems, the solution is given by &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; which satisfies the following recursion.&lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\begin{cases}&lt;br /&gt;
F_{n-1}+F_{n-2} &amp;amp; \mbox{if }n\ge 2,\\&lt;br /&gt;
1 &amp;amp; \mbox{if }n=1\\&lt;br /&gt;
0 &amp;amp; \mbox{if }n=0.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is called the [http://en.wikipedia.org/wiki/Fibonacci_number Fibonacci number].&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)&amp;lt;/math&amp;gt;,&lt;br /&gt;
:where &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
The quantity &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; is the so-called [http://en.wikipedia.org/wiki/Golden_ratio golden ratio], a constant with some significance in mathematics and aesthetics.&lt;br /&gt;
&lt;br /&gt;
We now prove this theorem by using generating functions.&lt;br /&gt;
The ordinary generating function for the Fibonacci number &amp;lt;math&amp;gt;F_{n}&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}F_n x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
We have that &amp;lt;math&amp;gt;F_{n}=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;n\ge 2&amp;lt;/math&amp;gt;, thus&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
G(x) &lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}F_n x^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
x+\sum_{n\ge 2}(F_{n-1}+F_{n-2})x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
For generating functions, there are general ways to generate &amp;lt;math&amp;gt;F_{n-1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F_{n-2}&amp;lt;/math&amp;gt;, or the coefficients with any smaller indices.&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
xG(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+1}=\sum_{n\ge 1}F_{n-1} x^n=\sum_{n\ge 2}F_{n-1} x^n\\&lt;br /&gt;
x^2G(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+2}=\sum_{n\ge 2}F_{n-2} x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we have&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;,&lt;br /&gt;
hence&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
The value of &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in the Taylor series for this formular, which is &amp;lt;math&amp;gt;\frac{G^{(n)}(0)}{n!}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;. Although this expansion works in principle, the detailed calculus is rather painful.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
It is easier to expand the generating function by breaking it into two geometric series.&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. It holds that&lt;br /&gt;
::&amp;lt;math&amp;gt;\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
It is easy to verify the above equation, but to deduce it, we need some (high school) calculation.&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; width=&amp;quot;100%&amp;quot; cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|&lt;br /&gt;
:{|&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;math&amp;gt;1-x-x^2&amp;lt;/math&amp;gt; has two roots &amp;lt;math&amp;gt;\frac{-1\pm\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Denote that &amp;lt;math&amp;gt;\phi=\frac{2}{-1+\sqrt{5}}=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{2}{-1-\sqrt{5}}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Then &amp;lt;math&amp;gt;(1-x-x^2)=(1-\phi x)(1-\hat{\phi}x)&amp;lt;/math&amp;gt;, so we can write &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\frac{x}{1-x-x^2}&lt;br /&gt;
&amp;amp;=\frac{x}{(1-\phi x)(1-\hat{\phi} x)}\\&lt;br /&gt;
&amp;amp;=\frac{\alpha}{(1-\phi x)}+\frac{\beta}{(1-\hat{\phi} x)},&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
where &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta&amp;lt;/math&amp;gt; satisfying that&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{cases}&lt;br /&gt;
\alpha+\beta=0\\&lt;br /&gt;
\alpha\phi+\beta\hat{\phi}= -1.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
Solving this we have that &amp;lt;math&amp;gt;\alpha=\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta=-\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt;. Thus,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
|}&lt;br /&gt;
:&amp;lt;math&amp;gt;\square&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the expression &amp;lt;math&amp;gt;\frac{1}{1-z}&amp;lt;/math&amp;gt; has a well known geometric expansion:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-z}=\sum_{n\ge 0}z^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Therefore, &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; can be expanded as&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}\\&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\phi x)^n-\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\hat{\phi} x)^n\\&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)x^n.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
So the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Fibonacci number is given by &lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Solving recurrences ==&lt;br /&gt;
The following steps describe a general methodology of solving recurrences by generating functions.&lt;br /&gt;
:1. Give a recursion that computes &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;. In the case of Fibonacci sequence&lt;br /&gt;
::&amp;lt;math&amp;gt;a_n=a_{n-1}+a_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:2. Multiply both sides of the equation by &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; and sum over all &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. This gives the generating function&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}a_nx^n=\sum_{n\ge 0}(a_{n-1}+a_{n-2})x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
:: And manipulate the right hand side of the equation so that it becomes some other expression involving &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
:3. Solve the resulting equation to derive an explicit formula for &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:4. Expand &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; into a power series and read off the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt;, which is a closed form for &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The first step is usually established by combinatorial observations, or explicitly given by the problem. The third step is trivial.&lt;br /&gt;
&lt;br /&gt;
The second and the forth steps need some non-trivial analytic techniques.&lt;br /&gt;
&lt;br /&gt;
=== Algebraic operations on generating functions ===&lt;br /&gt;
The second step in the above methodology is somehow tricky. It involves first applying the recurrence to the coefficients of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is easy; and then manipulating the resulting formal power series to express it in terms of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is more difficult (because it works backwards).&lt;br /&gt;
&lt;br /&gt;
We can apply several natural algebraic operations on the formal power series.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Generating function manipulation|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}g_nx^n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F(x)=\sum_{n\ge 0}f_nx^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
x^k G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge k}g_{n-k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\frac{G(x)-\sum_{i=0}^{k-1}g_iz^i}{x^k}&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}g_{n+k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\alpha F(x)+\beta G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} (\alpha f_n+\beta g_n)x^n\\&lt;br /&gt;
F(x)G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0}\sum_{k=0}^nf_kg_{n-k}x^n\\&lt;br /&gt;
G(cx)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} c^ng_n x^n\\&lt;br /&gt;
G&#039;(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}(n+1)g_{n+1}x^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
When manipulating generating functions, these rules are applied backwards; that is, from the right-hand-side to the left-hand-side.&lt;br /&gt;
&lt;br /&gt;
=== Expanding generating functions ===&lt;br /&gt;
The last step of solving recurrences by generating function is expanding the closed form generating function &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; to evaluate its &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th coefficient. In principle, we can always use the [http://en.wikipedia.org/wiki/Taylor_series Taylor series]&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}\frac{G^{(n)}(0)}{n!}x^n&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;G^{(n)}(0)&amp;lt;/math&amp;gt; is the value of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; evaluated at &amp;lt;math&amp;gt;x=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Some interesting special cases are very useful.&lt;br /&gt;
&lt;br /&gt;
====Geometric sequence====&lt;br /&gt;
In the example of Fibonacci numbers, we use the well known geometric series:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-x}=\sum_{n\ge 0}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
It is useful when we can express the generating function in the form of &amp;lt;math&amp;gt;G(x)=\frac{a_1}{1-b_1x}+\frac{a_2}{1-b_2x}+\cdots+\frac{a_k}{1-b_kx}&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in such &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;a_1b_1^n+a_2b_2^n+\cdots+a_kb_k^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Binomial theorem====&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;(1+x)^\alpha&amp;lt;/math&amp;gt; for some real &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; is &lt;br /&gt;
:&amp;lt;math&amp;gt;\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)(1+x)^{\alpha-n}&amp;lt;/math&amp;gt;.&lt;br /&gt;
By Taylor series, we get a generalized version of the binomial theorem known as [http://en.wikipedia.org/wiki/Binomial_coefficient#Newton.27s_binomial_series &#039;&#039;&#039;Newton&#039;s formula&#039;&#039;&#039;]:&lt;br /&gt;
{{Theorem|Newton&#039;s formular (generalized binomial theorem)|&lt;br /&gt;
If &amp;lt;math&amp;gt;|x|&amp;lt;1&amp;lt;/math&amp;gt;, then&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x)^\alpha=\sum_{n\ge 0}{\alpha\choose n}x^{n}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;{\alpha\choose n}&amp;lt;/math&amp;gt; is the &#039;&#039;&#039;generalized binomial coefficient&#039;&#039;&#039; defined by &lt;br /&gt;
:&amp;lt;math&amp;gt;{\alpha\choose n}=\frac{\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)}{n!}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Example: multisets ===&lt;br /&gt;
In the last lecture we gave a combinatorial proof of the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-multisets on an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. Now we give a generating function approach to the problem.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;S=\{x_1,x_2,\ldots,x_n\}&amp;lt;/math&amp;gt; be an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set. We have&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x_1+x_1^2+\cdots)(1+x_2+x_2^2+\cdots)\cdots(1+x_n+x_n^2+\cdots)=\sum_{m:S\rightarrow\mathbb{N}} \prod_{x_i\in S}x_i^{m(x_i)}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where each &amp;lt;math&amp;gt;m:S\rightarrow\mathbb{N}&amp;lt;/math&amp;gt; species a possible multiset on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; with multiplicity function &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let all &amp;lt;math&amp;gt;x_i=x&amp;lt;/math&amp;gt;. Then&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
(1+x+x^2+\cdots)^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{m:S\rightarrow\mathbb{N}}x^{m(x_1)+\cdots+m(x_n)}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{\text{multiset }M\text{ on }S}x^{|M|}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{k\ge 0}\left({n\choose k}\right)x^k.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the the definition of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. Our task is to evaluate &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Due to the geometric sequence and the Newton&#039;s formula&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
(1+x+x^2+\cdots)^n=(1-x)^{-n}=\sum_{k\ge 0}{-n\choose k}(-x)^k.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\left({n\choose k}\right)=(-1)^k{-n\choose k}={n+k-1\choose k}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the definition of the generalized binomial coefficient. We use an analytic (generating function) proof to get the same result of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt; as the combinatorial proof.&lt;br /&gt;
&lt;br /&gt;
=== Example: Quicksort ===&lt;br /&gt;
&lt;br /&gt;
== Catalan Number ==&lt;br /&gt;
We now introduce a class of counting problems, all with the same solution, called [http://en.wikipedia.org/wiki/Catalan_number &#039;&#039;&#039;Catalan number&#039;&#039;&#039;]. &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Catalan number is denoted as &amp;lt;math&amp;gt;C_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
In Volume 2 of Stanley&#039;s &#039;&#039;Enumerative Combinatorics&#039;&#039;, a set of exercises describe 66 different interpretations of the Catalan numbers. We give a few examples, cited from Wikipedia.&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;Dyck words&#039;&#039;&#039; of length 2&#039;&#039;n&#039;&#039;. A Dyck word is a string consisting of &#039;&#039;n&#039;&#039; X&#039;s and &#039;&#039;n&#039;&#039; Y&#039;s such that no initial segment of the string has more Y&#039;s than X&#039;s (see also [http://en.wikipedia.org/wiki/Dyck_language Dyck language]). For example, the following are the Dyck words of length 6:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; XXXYYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXXYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYXYY.&amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; counts the number of expressions containing &#039;&#039;n&#039;&#039; pairs of parentheses which are correctly matched:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; ((())) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()(()) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()()() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (())() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (()()) &amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 factors can be completely parenthesized (or the number of ways of associating &#039;&#039;n&#039;&#039; applications of a &#039;&#039;&#039;binary operator&#039;&#039;&#039;). For &#039;&#039;n&#039;&#039; = 3, for example, we have the following five different parenthesizations of four factors:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;math&amp;gt;((ab)c)d \quad (a(bc))d \quad(ab)(cd) \quad a((bc)d) \quad a(b(cd))&amp;lt;/math&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Successive applications of a binary operator can be represented in terms of a &#039;&#039;&#039;full binary tree&#039;&#039;&#039;. (A rooted binary tree is &#039;&#039;full&#039;&#039; if every vertex has either two children or no children.) It follows that &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of full binary trees with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 leaves:&lt;br /&gt;
[[Image:Catalan number binary tree example.png|center]] &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;monotonic paths&#039;&#039;&#039; along the edges of a grid with &#039;&#039;n&#039;&#039; × &#039;&#039;n&#039;&#039; square cells, which do not pass above the diagonal. A monotonic path is one which starts in the lower left corner, finishes in the upper right corner, and consists entirely of edges pointing rightwards or upwards. Counting such paths is equivalent to counting Dyck words: X stands for &amp;quot;move right&amp;quot; and Y stands for &amp;quot;move up&amp;quot;. The following diagrams show the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan number 4x4 grid example.svg.png|450px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways a [http://en.wikipedia.org/wiki/Convex_polygon &#039;&#039;&#039;convex polygon&#039;&#039;&#039;] with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;2 sides can be cut into &#039;&#039;&#039;triangles&#039;&#039;&#039; by connecting vertices with straight lines. The following hexagons illustrate the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan-Hexagons-example.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of [http://en.wikipedia.org/wiki/Stack_(data_structure) &#039;&#039;&#039;stack&#039;&#039;&#039;]-sortable permutations of {1, ..., &#039;&#039;n&#039;&#039;}. A permutation &#039;&#039;w&#039;&#039; is called &#039;&#039;&#039;stack-sortable&#039;&#039;&#039; if &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;(1,&amp;amp;nbsp;...,&amp;amp;nbsp;&#039;&#039;n&#039;&#039;), where &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) is defined recursively as follows: write &#039;&#039;w&#039;&#039; =&amp;amp;nbsp;&#039;&#039;unv&#039;&#039; where &#039;&#039;n&#039;&#039; is the largest element in &#039;&#039;w&#039;&#039; and &#039;&#039;u&#039;&#039; and &#039;&#039;v&#039;&#039; are shorter sequences, and set &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;&#039;&#039;S&#039;&#039;(&#039;&#039;u&#039;&#039;)&#039;&#039;S&#039;&#039;(&#039;&#039;v&#039;&#039;)&#039;&#039;n&#039;&#039;, with &#039;&#039;S&#039;&#039; being the identity for one-element sequences. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of ways to tile a stairstep shape of height &#039;&#039;n&#039;&#039; with &#039;&#039;n&#039;&#039; rectangles. The following figure illustrates the case &#039;&#039;n&#039;&#039;&amp;amp;nbsp;=&amp;amp;nbsp;4:&lt;br /&gt;
[[Image:Catalan stairsteps 4.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Recurrence relation for Catalan numbers|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;C_1=1&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;n&amp;gt;1&amp;lt;/math&amp;gt;,&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
C_n=\sum_{i=1}^{n-1}C_iC_{n-i}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n&amp;lt;/math&amp;gt; be the generating function. Apply the product rule,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)^2=\sum_{n\ge 0}\sum_{k=0}^{n}C_kC_{n-k}x^n=\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
Due to the recurrence,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n=x+\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n=x+G(x)^2&amp;lt;/math&amp;gt;.&lt;br /&gt;
Solving this, we obtain&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{1\pm(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
Because &amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, it must hold that &amp;lt;math&amp;gt;G(x)=\frac{1-(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;, or otherwise the constant term is not zero. Expanding &amp;lt;math&amp;gt;(1-4x)^{1/2}&amp;lt;/math&amp;gt; by Newton&#039;s formula, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\frac{1-(1-4x)^{1/2}}{2}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
1-\frac{1}{2}\sum_{n\ge 0}{1/2\choose n}(-4x)^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Thus, &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
C_n&lt;br /&gt;
&amp;amp;=-\frac{1}{2}{1/2\choose n}(-4)^n\\&lt;br /&gt;
&amp;amp;=-\frac{1}{2}\cdot\frac{1}{2}\cdot\frac{-1}{2}\cdot\frac{-3}{2}\cdots\frac{-(2n-3)}{2}\cdot(-4)^n/n!\\&lt;br /&gt;
&amp;amp;=\frac{(2n-2)!}{(n-1)!n!}\\&lt;br /&gt;
&amp;amp;=\frac{1}{n}{2n-2\choose n-1}.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we prove the following closed form for Catalan number.&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_n=\frac{1}{n}{2n-2\choose n-1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>172.21.1.108</name></author>
	</entry>
	<entry>
		<id>https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3151</id>
		<title>Combinatorics (Fall 2010)/Generating functions</title>
		<link rel="alternate" type="text/html" href="https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3151"/>
		<updated>2010-09-12T06:19:53Z</updated>

		<summary type="html">&lt;p&gt;172.21.1.108: /* Combinations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Generating Functions ==&lt;br /&gt;
In Stanley&#039;s magnificent book &#039;&#039;Enumerative Combinatorics&#039;&#039;, he comments the generating function as &amp;quot;the most useful but most difficult to understand method (for counting)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The solution to a counting problem is usually represented as some &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; depending a parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Sometimes this &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is called a &#039;&#039;counting function&#039;&#039; as it is a function of the parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.  &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; can also be treated as a infinite series:&lt;br /&gt;
:&amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;ordinary generating function (OGF)&#039;&#039;&#039; defined by &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
G(x)=\sum_{n\ge 0} a_nx^n.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So &amp;lt;math&amp;gt;G(x)=a_0+a_1x+a_2x^2+\cdots&amp;lt;/math&amp;gt;. An expression in this form is called a [http://en.wikipedia.org/wiki/Formal_power_series &#039;&#039;&#039;formal power series&#039;&#039;&#039;], and &amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt; is the sequence of &#039;&#039;&#039;coefficients&#039;&#039;&#039;. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the generating function can be expanded as&lt;br /&gt;
:G(x)=&amp;lt;math&amp;gt;(\underbrace{1+\cdots+1}_{a_0})+(\underbrace{x+\cdots+x}_{a_1})+(\underbrace{x^2+\cdots+x^2}_{a_2})+\cdots+(\underbrace{x^n+\cdots+x^n}_{a_n})+\cdots&amp;lt;/math&amp;gt;&lt;br /&gt;
so it indeed &amp;quot;generates&amp;quot; all the possible instances of the objects we want to count.&lt;br /&gt;
&lt;br /&gt;
Usually, we do not evaluate the generating function &amp;lt;math&amp;gt;GF(x)&amp;lt;/math&amp;gt; on any particular value. &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; remains as a &#039;&#039;&#039;formal variable&#039;&#039;&#039; without assuming any value. The numbers that we want to count are the coefficients carried by the terms in the formal power series. So far the generating function is just another way to represent the sequence&lt;br /&gt;
:&amp;lt;math&amp;gt;(a_0,a_1,a_2,\ldots\ldots)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The true power of generating functions comes from the various algebraic operations that we can perform on these generating functions. We use an example to demonstrate this.&lt;br /&gt;
&lt;br /&gt;
=== Combinations ===&lt;br /&gt;
Suppose we wish to enumerate all subsets of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. To construct a subset, we specifies for every element of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set whether the element is chosen or not. Let us denote the choice to omit an element by &amp;lt;math&amp;gt;x_0&amp;lt;/math&amp;gt;, and the choice to include it by &amp;lt;math&amp;gt;x_1&amp;lt;/math&amp;gt;. Using &amp;quot;&amp;lt;math&amp;gt;+&amp;lt;/math&amp;gt;&amp;quot; to represent &amp;quot;OR&amp;quot;, and using the multiplication to denote &amp;quot;AND&amp;quot;, the choices of subsets of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set are expressed as&lt;br /&gt;
:&amp;lt;math&amp;gt;\underbrace{(x_0+x_1)(x_0+x_1)\cdots (x_0+x_1)}_{n\mbox{ elements}}=(x_0+x_1)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For example, when &amp;lt;math&amp;gt;n=3&amp;lt;/math&amp;gt;, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
(x_0+x_1)^3&lt;br /&gt;
&amp;amp;=x_0x_0x_0+x_0x_0x_1+x_0x_1x_0+x_0x_1x_1\\&lt;br /&gt;
&amp;amp;\quad +x_1x_0x_0+x_1x_0x_1+x_1x_1x_0+x_1x_1x_1&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
So it &amp;quot;generate&amp;quot; all subsets of the 3-set. Writing &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;x_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;x_1&amp;lt;/math&amp;gt;, we have &amp;lt;math&amp;gt;(1+x)^3=1+3x+3x^2+x^3&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^k&amp;lt;/math&amp;gt; is the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-subsets of a 3-element set.&lt;br /&gt;
&lt;br /&gt;
In general, &amp;lt;math&amp;gt;(1+x)^n&amp;lt;/math&amp;gt; has the coefficients which are the number of subsets of fixed sizes of an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set.&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
Suppose that we have twelve balls: &amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;3 red&amp;lt;/font&amp;gt;, &amp;lt;font color=&amp;quot;blue&amp;quot;&amp;gt;4 blue&amp;lt;/font&amp;gt;, and &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;5 green&amp;lt;/font&amp;gt;. Balls with the same color are indistinguishable.&lt;br /&gt;
&lt;br /&gt;
We want to determine the number of ways to select &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; balls from these twelve balls, for some &amp;lt;math&amp;gt;0\le k\le 12&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;{\color{Red}(1+x+x^2+x^3)}{\color{Blue}(1+x+x^2+x^3+x^4)}{\color{OliveGreen}(1+x+x^2+x^3+x^4+x^5)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fibonacci numbers  ===&lt;br /&gt;
Consider the following counting problems.&lt;br /&gt;
* Count the number of ways that the nonnegative integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; can be written as a sum of ones and twos (in order).&lt;br /&gt;
: The problem asks for the number of compositions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; with summands from &amp;lt;math&amp;gt;\{1,2\}&amp;lt;/math&amp;gt;. Formally, we are counting the number of tuples &amp;lt;math&amp;gt;(x_1,x_2,\ldots,x_k)&amp;lt;/math&amp;gt; for some &amp;lt;math&amp;gt;k\le n&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;x_i\in\{1,2\}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt;.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. We observe that a composition either starts with a 1, in which case the rest is a composition of &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt;; or starts with a 2, in which case the rest is a composition of &amp;lt;math&amp;gt;n-2&amp;lt;/math&amp;gt;. So we have the recursion for &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; that&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
* Count the ways to completely cover a &amp;lt;math&amp;gt;2\times n&amp;lt;/math&amp;gt; rectangle with &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; dominos without any overlaps.&lt;br /&gt;
: Dominos are identical &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; rectangles, so that only their orientations --- vertical or horizontal matter.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. It also holds that &amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;. The proof is left as an exercise.&lt;br /&gt;
&lt;br /&gt;
In both problems, the solution is given by &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; which satisfies the following recursion.&lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\begin{cases}&lt;br /&gt;
F_{n-1}+F_{n-2} &amp;amp; \mbox{if }n\ge 2,\\&lt;br /&gt;
1 &amp;amp; \mbox{if }n=1\\&lt;br /&gt;
0 &amp;amp; \mbox{if }n=0.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is called the [http://en.wikipedia.org/wiki/Fibonacci_number Fibonacci number].&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)&amp;lt;/math&amp;gt;,&lt;br /&gt;
:where &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
The quantity &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; is the so-called [http://en.wikipedia.org/wiki/Golden_ratio golden ratio], a constant with some significance in mathematics and aesthetics.&lt;br /&gt;
&lt;br /&gt;
We now prove this theorem by using generating functions.&lt;br /&gt;
The ordinary generating function for the Fibonacci number &amp;lt;math&amp;gt;F_{n}&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}F_n x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
We have that &amp;lt;math&amp;gt;F_{n}=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;n\ge 2&amp;lt;/math&amp;gt;, thus&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
G(x) &lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}F_n x^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
x+\sum_{n\ge 2}(F_{n-1}+F_{n-2})x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
For generating functions, there are general ways to generate &amp;lt;math&amp;gt;F_{n-1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F_{n-2}&amp;lt;/math&amp;gt;, or the coefficients with any smaller indices.&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
xG(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+1}=\sum_{n\ge 1}F_{n-1} x^n=\sum_{n\ge 2}F_{n-1} x^n\\&lt;br /&gt;
x^2G(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+2}=\sum_{n\ge 2}F_{n-2} x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we have&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;,&lt;br /&gt;
hence&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
The value of &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in the Taylor series for this formular, which is &amp;lt;math&amp;gt;\frac{G^{(n)}(0)}{n!}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;. Although this expansion works in principle, the detailed calculus is rather painful.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
It is easier to expand the generating function by breaking it into two geometric series.&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. It holds that&lt;br /&gt;
::&amp;lt;math&amp;gt;\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
It is easy to verify the above equation, but to deduce it, we need some (high school) calculation.&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; width=&amp;quot;100%&amp;quot; cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|&lt;br /&gt;
:{|&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;math&amp;gt;1-x-x^2&amp;lt;/math&amp;gt; has two roots &amp;lt;math&amp;gt;\frac{-1\pm\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Denote that &amp;lt;math&amp;gt;\phi=\frac{2}{-1+\sqrt{5}}=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{2}{-1-\sqrt{5}}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Then &amp;lt;math&amp;gt;(1-x-x^2)=(1-\phi x)(1-\hat{\phi}x)&amp;lt;/math&amp;gt;, so we can write &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\frac{x}{1-x-x^2}&lt;br /&gt;
&amp;amp;=\frac{x}{(1-\phi x)(1-\hat{\phi} x)}\\&lt;br /&gt;
&amp;amp;=\frac{\alpha}{(1-\phi x)}+\frac{\beta}{(1-\hat{\phi} x)},&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
where &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta&amp;lt;/math&amp;gt; satisfying that&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{cases}&lt;br /&gt;
\alpha+\beta=0\\&lt;br /&gt;
\alpha\phi+\beta\hat{\phi}= -1.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
Solving this we have that &amp;lt;math&amp;gt;\alpha=\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta=-\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt;. Thus,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
|}&lt;br /&gt;
:&amp;lt;math&amp;gt;\square&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the expression &amp;lt;math&amp;gt;\frac{1}{1-z}&amp;lt;/math&amp;gt; has a well known geometric expansion:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-z}=\sum_{n\ge 0}z^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Therefore, &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; can be expanded as&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}\\&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\phi x)^n-\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\hat{\phi} x)^n\\&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)x^n.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
So the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Fibonacci number is given by &lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Solving recurrences ==&lt;br /&gt;
The following steps describe a general methodology of solving recurrences by generating functions.&lt;br /&gt;
:1. Give a recursion that computes &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;. In the case of Fibonacci sequence&lt;br /&gt;
::&amp;lt;math&amp;gt;a_n=a_{n-1}+a_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:2. Multiply both sides of the equation by &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; and sum over all &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. This gives the generating function&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}a_nx^n=\sum_{n\ge 0}(a_{n-1}+a_{n-2})x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
:: And manipulate the right hand side of the equation so that it becomes some other expression involving &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
:3. Solve the resulting equation to derive an explicit formula for &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:4. Expand &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; into a power series and read off the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt;, which is a closed form for &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The first step is usually established by combinatorial observations, or explicitly given by the problem. The third step is trivial.&lt;br /&gt;
&lt;br /&gt;
The second and the forth steps need some non-trivial analytic techniques.&lt;br /&gt;
&lt;br /&gt;
=== Algebraic operations on generating functions ===&lt;br /&gt;
The second step in the above methodology is somehow tricky. It involves first applying the recurrence to the coefficients of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is easy; and then manipulating the resulting formal power series to express it in terms of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is more difficult (because it works backwards).&lt;br /&gt;
&lt;br /&gt;
We can apply several natural algebraic operations on the formal power series.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Generating function manipulation|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}g_nx^n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F(x)=\sum_{n\ge 0}f_nx^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
x^k G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge k}g_{n-k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\frac{G(x)-\sum_{i=0}^{k-1}g_iz^i}{x^k}&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}g_{n+k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\alpha F(x)+\beta G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} (\alpha f_n+\beta g_n)x^n\\&lt;br /&gt;
F(x)G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0}\sum_{k=0}^nf_kg_{n-k}x^n\\&lt;br /&gt;
G(cx)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} c^ng_n x^n\\&lt;br /&gt;
G&#039;(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}(n+1)g_{n+1}x^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
When manipulating generating functions, these rules are applied backwards; that is, from the right-hand-side to the left-hand-side.&lt;br /&gt;
&lt;br /&gt;
=== Expanding generating functions ===&lt;br /&gt;
The last step of solving recurrences by generating function is expanding the closed form generating function &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; to evaluate its &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th coefficient. In principle, we can always use the [http://en.wikipedia.org/wiki/Taylor_series Taylor series]&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}\frac{G^{(n)}(0)}{n!}x^n&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;G^{(n)}(0)&amp;lt;/math&amp;gt; is the value of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; evaluated at &amp;lt;math&amp;gt;x=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Some interesting special cases are very useful.&lt;br /&gt;
&lt;br /&gt;
====Geometric sequence====&lt;br /&gt;
In the example of Fibonacci numbers, we use the well known geometric series:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-x}=\sum_{n\ge 0}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
It is useful when we can express the generating function in the form of &amp;lt;math&amp;gt;G(x)=\frac{a_1}{1-b_1x}+\frac{a_2}{1-b_2x}+\cdots+\frac{a_k}{1-b_kx}&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in such &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;a_1b_1^n+a_2b_2^n+\cdots+a_kb_k^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Binomial theorem====&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;(1+x)^\alpha&amp;lt;/math&amp;gt; for some real &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; is &lt;br /&gt;
:&amp;lt;math&amp;gt;\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)(1+x)^{\alpha-n}&amp;lt;/math&amp;gt;.&lt;br /&gt;
By Taylor series, we get a generalized version of the binomial theorem known as [http://en.wikipedia.org/wiki/Binomial_coefficient#Newton.27s_binomial_series &#039;&#039;&#039;Newton&#039;s formula&#039;&#039;&#039;]:&lt;br /&gt;
{{Theorem|Newton&#039;s formular (generalized binomial theorem)|&lt;br /&gt;
If &amp;lt;math&amp;gt;|x|&amp;lt;1&amp;lt;/math&amp;gt;, then&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x)^\alpha=\sum_{n\ge 0}{\alpha\choose n}x^{n}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;{\alpha\choose n}&amp;lt;/math&amp;gt; is the &#039;&#039;&#039;generalized binomial coefficient&#039;&#039;&#039; defined by &lt;br /&gt;
:&amp;lt;math&amp;gt;{\alpha\choose n}=\frac{\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)}{n!}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Example: multisets ===&lt;br /&gt;
In the last lecture we gave a combinatorial proof of the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-multisets on an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. Now we give a generating function approach to the problem.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;S=\{x_1,x_2,\ldots,x_n\}&amp;lt;/math&amp;gt; be an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set. We have&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x_1+x_1^2+\cdots)(1+x_2+x_2^2+\cdots)\cdots(1+x_n+x_n^2+\cdots)=\sum_{m:S\rightarrow\mathbb{N}} \prod_{x_i\in S}x_i^{m(x_i)}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where each &amp;lt;math&amp;gt;m:S\rightarrow\mathbb{N}&amp;lt;/math&amp;gt; species a possible multiset on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; with multiplicity function &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let all &amp;lt;math&amp;gt;x_i=x&amp;lt;/math&amp;gt;. Then&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
(1+x+x^2+\cdots)^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{m:S\rightarrow\mathbb{N}}x^{m(x_1)+\cdots+m(x_n)}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{\text{multiset }M\text{ on }S}x^{|M|}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{k\ge 0}\left({n\choose k}\right)x^k.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the the definition of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. Our task is to evaluate &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Due to the geometric sequence and the Newton&#039;s formula&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
(1+x+x^2+\cdots)^n=(1-x)^{-n}=\sum_{k\ge 0}{-n\choose k}(-x)^k.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\left({n\choose k}\right)=(-1)^k{-n\choose k}={n+k-1\choose k}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the definition of the generalized binomial coefficient. We use an analytic (generating function) proof to get the same result of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt; as the combinatorial proof.&lt;br /&gt;
&lt;br /&gt;
=== Example: Quicksort ===&lt;br /&gt;
&lt;br /&gt;
== Catalan Number ==&lt;br /&gt;
We now introduce a class of counting problems, all with the same solution, called [http://en.wikipedia.org/wiki/Catalan_number &#039;&#039;&#039;Catalan number&#039;&#039;&#039;]. &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Catalan number is denoted as &amp;lt;math&amp;gt;C_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
In Volume 2 of Stanley&#039;s &#039;&#039;Enumerative Combinatorics&#039;&#039;, a set of exercises describe 66 different interpretations of the Catalan numbers. We give a few examples, cited from Wikipedia.&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;Dyck words&#039;&#039;&#039; of length 2&#039;&#039;n&#039;&#039;. A Dyck word is a string consisting of &#039;&#039;n&#039;&#039; X&#039;s and &#039;&#039;n&#039;&#039; Y&#039;s such that no initial segment of the string has more Y&#039;s than X&#039;s (see also [http://en.wikipedia.org/wiki/Dyck_language Dyck language]). For example, the following are the Dyck words of length 6:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; XXXYYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXXYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYXYY.&amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; counts the number of expressions containing &#039;&#039;n&#039;&#039; pairs of parentheses which are correctly matched:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; ((())) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()(()) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()()() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (())() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (()()) &amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 factors can be completely parenthesized (or the number of ways of associating &#039;&#039;n&#039;&#039; applications of a &#039;&#039;&#039;binary operator&#039;&#039;&#039;). For &#039;&#039;n&#039;&#039; = 3, for example, we have the following five different parenthesizations of four factors:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;math&amp;gt;((ab)c)d \quad (a(bc))d \quad(ab)(cd) \quad a((bc)d) \quad a(b(cd))&amp;lt;/math&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Successive applications of a binary operator can be represented in terms of a &#039;&#039;&#039;full binary tree&#039;&#039;&#039;. (A rooted binary tree is &#039;&#039;full&#039;&#039; if every vertex has either two children or no children.) It follows that &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of full binary trees with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 leaves:&lt;br /&gt;
[[Image:Catalan number binary tree example.png|center]] &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;monotonic paths&#039;&#039;&#039; along the edges of a grid with &#039;&#039;n&#039;&#039; × &#039;&#039;n&#039;&#039; square cells, which do not pass above the diagonal. A monotonic path is one which starts in the lower left corner, finishes in the upper right corner, and consists entirely of edges pointing rightwards or upwards. Counting such paths is equivalent to counting Dyck words: X stands for &amp;quot;move right&amp;quot; and Y stands for &amp;quot;move up&amp;quot;. The following diagrams show the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan number 4x4 grid example.svg.png|450px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways a [http://en.wikipedia.org/wiki/Convex_polygon &#039;&#039;&#039;convex polygon&#039;&#039;&#039;] with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;2 sides can be cut into &#039;&#039;&#039;triangles&#039;&#039;&#039; by connecting vertices with straight lines. The following hexagons illustrate the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan-Hexagons-example.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of [http://en.wikipedia.org/wiki/Stack_(data_structure) &#039;&#039;&#039;stack&#039;&#039;&#039;]-sortable permutations of {1, ..., &#039;&#039;n&#039;&#039;}. A permutation &#039;&#039;w&#039;&#039; is called &#039;&#039;&#039;stack-sortable&#039;&#039;&#039; if &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;(1,&amp;amp;nbsp;...,&amp;amp;nbsp;&#039;&#039;n&#039;&#039;), where &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) is defined recursively as follows: write &#039;&#039;w&#039;&#039; =&amp;amp;nbsp;&#039;&#039;unv&#039;&#039; where &#039;&#039;n&#039;&#039; is the largest element in &#039;&#039;w&#039;&#039; and &#039;&#039;u&#039;&#039; and &#039;&#039;v&#039;&#039; are shorter sequences, and set &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;&#039;&#039;S&#039;&#039;(&#039;&#039;u&#039;&#039;)&#039;&#039;S&#039;&#039;(&#039;&#039;v&#039;&#039;)&#039;&#039;n&#039;&#039;, with &#039;&#039;S&#039;&#039; being the identity for one-element sequences. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of ways to tile a stairstep shape of height &#039;&#039;n&#039;&#039; with &#039;&#039;n&#039;&#039; rectangles. The following figure illustrates the case &#039;&#039;n&#039;&#039;&amp;amp;nbsp;=&amp;amp;nbsp;4:&lt;br /&gt;
[[Image:Catalan stairsteps 4.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Recurrence relation for Catalan numbers|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;C_1=1&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;n&amp;gt;1&amp;lt;/math&amp;gt;,&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
C_n=\sum_{i=1}^{n-1}C_iC_{n-i}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n&amp;lt;/math&amp;gt; be the generating function. Apply the product rule,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)^2=\sum_{n\ge 0}\sum_{k=0}^{n}C_kC_{n-k}x^n=\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
Due to the recurrence,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n=x+\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n=x+G(x)^2&amp;lt;/math&amp;gt;.&lt;br /&gt;
Solving this, we obtain&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{1\pm(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
Because &amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, it must hold that &amp;lt;math&amp;gt;G(x)=\frac{1-(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;, or otherwise the constant term is not zero. Expanding &amp;lt;math&amp;gt;(1-4x)^{1/2}&amp;lt;/math&amp;gt; by Newton&#039;s formula, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\frac{1-(1-4x)^{1/2}}{2}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
1-\frac{1}{2}\sum_{n\ge 0}{1/2\choose n}(-4x)^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Thus, &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
C_n&lt;br /&gt;
&amp;amp;=-\frac{1}{2}{1/2\choose n}(-4)^n\\&lt;br /&gt;
&amp;amp;=-\frac{1}{2}\cdot\frac{1}{2}\cdot\frac{-1}{2}\cdot\frac{-3}{2}\cdots\frac{-(2n-3)}{2}\cdot(-4)^n/n!\\&lt;br /&gt;
&amp;amp;=\frac{(2n-2)!}{(n-1)!n!}\\&lt;br /&gt;
&amp;amp;=\frac{1}{n}{2n-2\choose n-1}.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we prove the following closed form for Catalan number.&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_n=\frac{1}{n}{2n-2\choose n-1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>172.21.1.108</name></author>
	</entry>
	<entry>
		<id>https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3150</id>
		<title>Combinatorics (Fall 2010)/Generating functions</title>
		<link rel="alternate" type="text/html" href="https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3150"/>
		<updated>2010-09-12T02:52:32Z</updated>

		<summary type="html">&lt;p&gt;172.21.1.108: /* Example: multisets */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Generating Functions ==&lt;br /&gt;
In Stanley&#039;s magnificent book &#039;&#039;Enumerative Combinatorics&#039;&#039;, he comments the generating function as &amp;quot;the most useful but most difficult to understand method (for counting)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The solution to a counting problem is usually represented as some &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; depending a parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Sometimes this &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is called a &#039;&#039;counting function&#039;&#039; as it is a function of the parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.  &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; can also be treated as a infinite series:&lt;br /&gt;
:&amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;ordinary generating function (OGF)&#039;&#039;&#039; defined by &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
G(x)=\sum_{n\ge 0} a_nx^n.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So &amp;lt;math&amp;gt;G(x)=a_0+a_1x+a_2x^2+\cdots&amp;lt;/math&amp;gt;. An expression in this form is called a [http://en.wikipedia.org/wiki/Formal_power_series &#039;&#039;&#039;formal power series&#039;&#039;&#039;], and &amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt; is the sequence of &#039;&#039;&#039;coefficients&#039;&#039;&#039;. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the generating function can be expanded as&lt;br /&gt;
:G(x)=&amp;lt;math&amp;gt;(\underbrace{1+\cdots+1}_{a_0})+(\underbrace{x+\cdots+x}_{a_1})+(\underbrace{x^2+\cdots+x^2}_{a_2})+\cdots+(\underbrace{x^n+\cdots+x^n}_{a_n})+\cdots&amp;lt;/math&amp;gt;&lt;br /&gt;
so it indeed &amp;quot;generates&amp;quot; all the possible instances of the objects we want to count.&lt;br /&gt;
&lt;br /&gt;
Usually, we do not evaluate the generating function &amp;lt;math&amp;gt;GF(x)&amp;lt;/math&amp;gt; on any particular value. &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; remains as a &#039;&#039;&#039;formal variable&#039;&#039;&#039; without assuming any value. The numbers that we want to count are the coefficients carried by the terms in the formal power series. So far the generating function is just another way to represent the sequence&lt;br /&gt;
:&amp;lt;math&amp;gt;(a_0,a_1,a_2,\ldots\ldots)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The true power of generating functions comes from the various algebraic operations that we can perform on these generating functions. We use an example to demonstrate this.&lt;br /&gt;
&lt;br /&gt;
=== Combinations ===&lt;br /&gt;
&lt;br /&gt;
=== Fibonacci numbers  ===&lt;br /&gt;
Consider the following counting problems.&lt;br /&gt;
* Count the number of ways that the nonnegative integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; can be written as a sum of ones and twos (in order).&lt;br /&gt;
: The problem asks for the number of compositions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; with summands from &amp;lt;math&amp;gt;\{1,2\}&amp;lt;/math&amp;gt;. Formally, we are counting the number of tuples &amp;lt;math&amp;gt;(x_1,x_2,\ldots,x_k)&amp;lt;/math&amp;gt; for some &amp;lt;math&amp;gt;k\le n&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;x_i\in\{1,2\}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt;.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. We observe that a composition either starts with a 1, in which case the rest is a composition of &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt;; or starts with a 2, in which case the rest is a composition of &amp;lt;math&amp;gt;n-2&amp;lt;/math&amp;gt;. So we have the recursion for &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; that&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
* Count the ways to completely cover a &amp;lt;math&amp;gt;2\times n&amp;lt;/math&amp;gt; rectangle with &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; dominos without any overlaps.&lt;br /&gt;
: Dominos are identical &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; rectangles, so that only their orientations --- vertical or horizontal matter.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. It also holds that &amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;. The proof is left as an exercise.&lt;br /&gt;
&lt;br /&gt;
In both problems, the solution is given by &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; which satisfies the following recursion.&lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\begin{cases}&lt;br /&gt;
F_{n-1}+F_{n-2} &amp;amp; \mbox{if }n\ge 2,\\&lt;br /&gt;
1 &amp;amp; \mbox{if }n=1\\&lt;br /&gt;
0 &amp;amp; \mbox{if }n=0.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is called the [http://en.wikipedia.org/wiki/Fibonacci_number Fibonacci number].&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)&amp;lt;/math&amp;gt;,&lt;br /&gt;
:where &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
The quantity &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; is the so-called [http://en.wikipedia.org/wiki/Golden_ratio golden ratio], a constant with some significance in mathematics and aesthetics.&lt;br /&gt;
&lt;br /&gt;
We now prove this theorem by using generating functions.&lt;br /&gt;
The ordinary generating function for the Fibonacci number &amp;lt;math&amp;gt;F_{n}&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}F_n x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
We have that &amp;lt;math&amp;gt;F_{n}=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;n\ge 2&amp;lt;/math&amp;gt;, thus&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
G(x) &lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}F_n x^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
x+\sum_{n\ge 2}(F_{n-1}+F_{n-2})x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
For generating functions, there are general ways to generate &amp;lt;math&amp;gt;F_{n-1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F_{n-2}&amp;lt;/math&amp;gt;, or the coefficients with any smaller indices.&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
xG(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+1}=\sum_{n\ge 1}F_{n-1} x^n=\sum_{n\ge 2}F_{n-1} x^n\\&lt;br /&gt;
x^2G(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+2}=\sum_{n\ge 2}F_{n-2} x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we have&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;,&lt;br /&gt;
hence&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
The value of &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in the Taylor series for this formular, which is &amp;lt;math&amp;gt;\frac{G^{(n)}(0)}{n!}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;. Although this expansion works in principle, the detailed calculus is rather painful.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
It is easier to expand the generating function by breaking it into two geometric series.&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. It holds that&lt;br /&gt;
::&amp;lt;math&amp;gt;\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
It is easy to verify the above equation, but to deduce it, we need some (high school) calculation.&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; width=&amp;quot;100%&amp;quot; cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|&lt;br /&gt;
:{|&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;math&amp;gt;1-x-x^2&amp;lt;/math&amp;gt; has two roots &amp;lt;math&amp;gt;\frac{-1\pm\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Denote that &amp;lt;math&amp;gt;\phi=\frac{2}{-1+\sqrt{5}}=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{2}{-1-\sqrt{5}}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Then &amp;lt;math&amp;gt;(1-x-x^2)=(1-\phi x)(1-\hat{\phi}x)&amp;lt;/math&amp;gt;, so we can write &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\frac{x}{1-x-x^2}&lt;br /&gt;
&amp;amp;=\frac{x}{(1-\phi x)(1-\hat{\phi} x)}\\&lt;br /&gt;
&amp;amp;=\frac{\alpha}{(1-\phi x)}+\frac{\beta}{(1-\hat{\phi} x)},&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
where &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta&amp;lt;/math&amp;gt; satisfying that&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{cases}&lt;br /&gt;
\alpha+\beta=0\\&lt;br /&gt;
\alpha\phi+\beta\hat{\phi}= -1.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
Solving this we have that &amp;lt;math&amp;gt;\alpha=\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta=-\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt;. Thus,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
|}&lt;br /&gt;
:&amp;lt;math&amp;gt;\square&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the expression &amp;lt;math&amp;gt;\frac{1}{1-z}&amp;lt;/math&amp;gt; has a well known geometric expansion:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-z}=\sum_{n\ge 0}z^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Therefore, &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; can be expanded as&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}\\&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\phi x)^n-\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\hat{\phi} x)^n\\&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)x^n.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
So the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Fibonacci number is given by &lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Solving recurrences ==&lt;br /&gt;
The following steps describe a general methodology of solving recurrences by generating functions.&lt;br /&gt;
:1. Give a recursion that computes &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;. In the case of Fibonacci sequence&lt;br /&gt;
::&amp;lt;math&amp;gt;a_n=a_{n-1}+a_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:2. Multiply both sides of the equation by &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; and sum over all &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. This gives the generating function&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}a_nx^n=\sum_{n\ge 0}(a_{n-1}+a_{n-2})x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
:: And manipulate the right hand side of the equation so that it becomes some other expression involving &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
:3. Solve the resulting equation to derive an explicit formula for &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:4. Expand &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; into a power series and read off the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt;, which is a closed form for &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The first step is usually established by combinatorial observations, or explicitly given by the problem. The third step is trivial.&lt;br /&gt;
&lt;br /&gt;
The second and the forth steps need some non-trivial analytic techniques.&lt;br /&gt;
&lt;br /&gt;
=== Algebraic operations on generating functions ===&lt;br /&gt;
The second step in the above methodology is somehow tricky. It involves first applying the recurrence to the coefficients of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is easy; and then manipulating the resulting formal power series to express it in terms of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is more difficult (because it works backwards).&lt;br /&gt;
&lt;br /&gt;
We can apply several natural algebraic operations on the formal power series.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Generating function manipulation|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}g_nx^n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F(x)=\sum_{n\ge 0}f_nx^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
x^k G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge k}g_{n-k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\frac{G(x)-\sum_{i=0}^{k-1}g_iz^i}{x^k}&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}g_{n+k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\alpha F(x)+\beta G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} (\alpha f_n+\beta g_n)x^n\\&lt;br /&gt;
F(x)G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0}\sum_{k=0}^nf_kg_{n-k}x^n\\&lt;br /&gt;
G(cx)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} c^ng_n x^n\\&lt;br /&gt;
G&#039;(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}(n+1)g_{n+1}x^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
When manipulating generating functions, these rules are applied backwards; that is, from the right-hand-side to the left-hand-side.&lt;br /&gt;
&lt;br /&gt;
=== Expanding generating functions ===&lt;br /&gt;
The last step of solving recurrences by generating function is expanding the closed form generating function &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; to evaluate its &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th coefficient. In principle, we can always use the [http://en.wikipedia.org/wiki/Taylor_series Taylor series]&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}\frac{G^{(n)}(0)}{n!}x^n&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;G^{(n)}(0)&amp;lt;/math&amp;gt; is the value of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; evaluated at &amp;lt;math&amp;gt;x=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Some interesting special cases are very useful.&lt;br /&gt;
&lt;br /&gt;
====Geometric sequence====&lt;br /&gt;
In the example of Fibonacci numbers, we use the well known geometric series:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-x}=\sum_{n\ge 0}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
It is useful when we can express the generating function in the form of &amp;lt;math&amp;gt;G(x)=\frac{a_1}{1-b_1x}+\frac{a_2}{1-b_2x}+\cdots+\frac{a_k}{1-b_kx}&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in such &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;a_1b_1^n+a_2b_2^n+\cdots+a_kb_k^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Binomial theorem====&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;(1+x)^\alpha&amp;lt;/math&amp;gt; for some real &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; is &lt;br /&gt;
:&amp;lt;math&amp;gt;\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)(1+x)^{\alpha-n}&amp;lt;/math&amp;gt;.&lt;br /&gt;
By Taylor series, we get a generalized version of the binomial theorem known as [http://en.wikipedia.org/wiki/Binomial_coefficient#Newton.27s_binomial_series &#039;&#039;&#039;Newton&#039;s formula&#039;&#039;&#039;]:&lt;br /&gt;
{{Theorem|Newton&#039;s formular (generalized binomial theorem)|&lt;br /&gt;
If &amp;lt;math&amp;gt;|x|&amp;lt;1&amp;lt;/math&amp;gt;, then&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x)^\alpha=\sum_{n\ge 0}{\alpha\choose n}x^{n}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;{\alpha\choose n}&amp;lt;/math&amp;gt; is the &#039;&#039;&#039;generalized binomial coefficient&#039;&#039;&#039; defined by &lt;br /&gt;
:&amp;lt;math&amp;gt;{\alpha\choose n}=\frac{\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)}{n!}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Example: multisets ===&lt;br /&gt;
In the last lecture we gave a combinatorial proof of the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-multisets on an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. Now we give a generating function approach to the problem.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;S=\{x_1,x_2,\ldots,x_n\}&amp;lt;/math&amp;gt; be an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set. We have&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x_1+x_1^2+\cdots)(1+x_2+x_2^2+\cdots)\cdots(1+x_n+x_n^2+\cdots)=\sum_{m:S\rightarrow\mathbb{N}} \prod_{x_i\in S}x_i^{m(x_i)}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where each &amp;lt;math&amp;gt;m:S\rightarrow\mathbb{N}&amp;lt;/math&amp;gt; species a possible multiset on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; with multiplicity function &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let all &amp;lt;math&amp;gt;x_i=x&amp;lt;/math&amp;gt;. Then&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
(1+x+x^2+\cdots)^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{m:S\rightarrow\mathbb{N}}x^{m(x_1)+\cdots+m(x_n)}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{\text{multiset }M\text{ on }S}x^{|M|}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{k\ge 0}\left({n\choose k}\right)x^k.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the the definition of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. Our task is to evaluate &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Due to the geometric sequence and the Newton&#039;s formula&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
(1+x+x^2+\cdots)^n=(1-x)^{-n}=\sum_{k\ge 0}{-n\choose k}(-x)^k.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\left({n\choose k}\right)=(-1)^k{-n\choose k}={n+k-1\choose k}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the definition of the generalized binomial coefficient. We use an analytic (generating function) proof to get the same result of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt; as the combinatorial proof.&lt;br /&gt;
&lt;br /&gt;
=== Example: Quicksort ===&lt;br /&gt;
&lt;br /&gt;
== Catalan Number ==&lt;br /&gt;
We now introduce a class of counting problems, all with the same solution, called [http://en.wikipedia.org/wiki/Catalan_number &#039;&#039;&#039;Catalan number&#039;&#039;&#039;]. &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Catalan number is denoted as &amp;lt;math&amp;gt;C_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
In Volume 2 of Stanley&#039;s &#039;&#039;Enumerative Combinatorics&#039;&#039;, a set of exercises describe 66 different interpretations of the Catalan numbers. We give a few examples, cited from Wikipedia.&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;Dyck words&#039;&#039;&#039; of length 2&#039;&#039;n&#039;&#039;. A Dyck word is a string consisting of &#039;&#039;n&#039;&#039; X&#039;s and &#039;&#039;n&#039;&#039; Y&#039;s such that no initial segment of the string has more Y&#039;s than X&#039;s (see also [http://en.wikipedia.org/wiki/Dyck_language Dyck language]). For example, the following are the Dyck words of length 6:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; XXXYYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXXYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYXYY.&amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; counts the number of expressions containing &#039;&#039;n&#039;&#039; pairs of parentheses which are correctly matched:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; ((())) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()(()) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()()() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (())() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (()()) &amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 factors can be completely parenthesized (or the number of ways of associating &#039;&#039;n&#039;&#039; applications of a &#039;&#039;&#039;binary operator&#039;&#039;&#039;). For &#039;&#039;n&#039;&#039; = 3, for example, we have the following five different parenthesizations of four factors:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;math&amp;gt;((ab)c)d \quad (a(bc))d \quad(ab)(cd) \quad a((bc)d) \quad a(b(cd))&amp;lt;/math&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Successive applications of a binary operator can be represented in terms of a &#039;&#039;&#039;full binary tree&#039;&#039;&#039;. (A rooted binary tree is &#039;&#039;full&#039;&#039; if every vertex has either two children or no children.) It follows that &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of full binary trees with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 leaves:&lt;br /&gt;
[[Image:Catalan number binary tree example.png|center]] &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;monotonic paths&#039;&#039;&#039; along the edges of a grid with &#039;&#039;n&#039;&#039; × &#039;&#039;n&#039;&#039; square cells, which do not pass above the diagonal. A monotonic path is one which starts in the lower left corner, finishes in the upper right corner, and consists entirely of edges pointing rightwards or upwards. Counting such paths is equivalent to counting Dyck words: X stands for &amp;quot;move right&amp;quot; and Y stands for &amp;quot;move up&amp;quot;. The following diagrams show the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan number 4x4 grid example.svg.png|450px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways a [http://en.wikipedia.org/wiki/Convex_polygon &#039;&#039;&#039;convex polygon&#039;&#039;&#039;] with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;2 sides can be cut into &#039;&#039;&#039;triangles&#039;&#039;&#039; by connecting vertices with straight lines. The following hexagons illustrate the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan-Hexagons-example.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of [http://en.wikipedia.org/wiki/Stack_(data_structure) &#039;&#039;&#039;stack&#039;&#039;&#039;]-sortable permutations of {1, ..., &#039;&#039;n&#039;&#039;}. A permutation &#039;&#039;w&#039;&#039; is called &#039;&#039;&#039;stack-sortable&#039;&#039;&#039; if &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;(1,&amp;amp;nbsp;...,&amp;amp;nbsp;&#039;&#039;n&#039;&#039;), where &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) is defined recursively as follows: write &#039;&#039;w&#039;&#039; =&amp;amp;nbsp;&#039;&#039;unv&#039;&#039; where &#039;&#039;n&#039;&#039; is the largest element in &#039;&#039;w&#039;&#039; and &#039;&#039;u&#039;&#039; and &#039;&#039;v&#039;&#039; are shorter sequences, and set &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;&#039;&#039;S&#039;&#039;(&#039;&#039;u&#039;&#039;)&#039;&#039;S&#039;&#039;(&#039;&#039;v&#039;&#039;)&#039;&#039;n&#039;&#039;, with &#039;&#039;S&#039;&#039; being the identity for one-element sequences. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of ways to tile a stairstep shape of height &#039;&#039;n&#039;&#039; with &#039;&#039;n&#039;&#039; rectangles. The following figure illustrates the case &#039;&#039;n&#039;&#039;&amp;amp;nbsp;=&amp;amp;nbsp;4:&lt;br /&gt;
[[Image:Catalan stairsteps 4.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Recurrence relation for Catalan numbers|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;C_1=1&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;n&amp;gt;1&amp;lt;/math&amp;gt;,&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
C_n=\sum_{i=1}^{n-1}C_iC_{n-i}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n&amp;lt;/math&amp;gt; be the generating function. Apply the product rule,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)^2=\sum_{n\ge 0}\sum_{k=0}^{n}C_kC_{n-k}x^n=\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
Due to the recurrence,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n=x+\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n=x+G(x)^2&amp;lt;/math&amp;gt;.&lt;br /&gt;
Solving this, we obtain&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{1\pm(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
Because &amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, it must hold that &amp;lt;math&amp;gt;G(x)=\frac{1-(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;, or otherwise the constant term is not zero. Expanding &amp;lt;math&amp;gt;(1-4x)^{1/2}&amp;lt;/math&amp;gt; by Newton&#039;s formula, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\frac{1-(1-4x)^{1/2}}{2}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
1-\frac{1}{2}\sum_{n\ge 0}{1/2\choose n}(-4x)^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Thus, &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
C_n&lt;br /&gt;
&amp;amp;=-\frac{1}{2}{1/2\choose n}(-4)^n\\&lt;br /&gt;
&amp;amp;=-\frac{1}{2}\cdot\frac{1}{2}\cdot\frac{-1}{2}\cdot\frac{-3}{2}\cdots\frac{-(2n-3)}{2}\cdot(-4)^n/n!\\&lt;br /&gt;
&amp;amp;=\frac{(2n-2)!}{(n-1)!n!}\\&lt;br /&gt;
&amp;amp;=\frac{1}{n}{2n-2\choose n-1}.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we prove the following closed form for Catalan number.&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_n=\frac{1}{n}{2n-2\choose n-1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>172.21.1.108</name></author>
	</entry>
	<entry>
		<id>https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3149</id>
		<title>Combinatorics (Fall 2010)/Generating functions</title>
		<link rel="alternate" type="text/html" href="https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3149"/>
		<updated>2010-09-12T02:52:17Z</updated>

		<summary type="html">&lt;p&gt;172.21.1.108: /* Example: multisets */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Generating Functions ==&lt;br /&gt;
In Stanley&#039;s magnificent book &#039;&#039;Enumerative Combinatorics&#039;&#039;, he comments the generating function as &amp;quot;the most useful but most difficult to understand method (for counting)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The solution to a counting problem is usually represented as some &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; depending a parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Sometimes this &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is called a &#039;&#039;counting function&#039;&#039; as it is a function of the parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.  &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; can also be treated as a infinite series:&lt;br /&gt;
:&amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;ordinary generating function (OGF)&#039;&#039;&#039; defined by &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
G(x)=\sum_{n\ge 0} a_nx^n.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So &amp;lt;math&amp;gt;G(x)=a_0+a_1x+a_2x^2+\cdots&amp;lt;/math&amp;gt;. An expression in this form is called a [http://en.wikipedia.org/wiki/Formal_power_series &#039;&#039;&#039;formal power series&#039;&#039;&#039;], and &amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt; is the sequence of &#039;&#039;&#039;coefficients&#039;&#039;&#039;. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the generating function can be expanded as&lt;br /&gt;
:G(x)=&amp;lt;math&amp;gt;(\underbrace{1+\cdots+1}_{a_0})+(\underbrace{x+\cdots+x}_{a_1})+(\underbrace{x^2+\cdots+x^2}_{a_2})+\cdots+(\underbrace{x^n+\cdots+x^n}_{a_n})+\cdots&amp;lt;/math&amp;gt;&lt;br /&gt;
so it indeed &amp;quot;generates&amp;quot; all the possible instances of the objects we want to count.&lt;br /&gt;
&lt;br /&gt;
Usually, we do not evaluate the generating function &amp;lt;math&amp;gt;GF(x)&amp;lt;/math&amp;gt; on any particular value. &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; remains as a &#039;&#039;&#039;formal variable&#039;&#039;&#039; without assuming any value. The numbers that we want to count are the coefficients carried by the terms in the formal power series. So far the generating function is just another way to represent the sequence&lt;br /&gt;
:&amp;lt;math&amp;gt;(a_0,a_1,a_2,\ldots\ldots)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The true power of generating functions comes from the various algebraic operations that we can perform on these generating functions. We use an example to demonstrate this.&lt;br /&gt;
&lt;br /&gt;
=== Combinations ===&lt;br /&gt;
&lt;br /&gt;
=== Fibonacci numbers  ===&lt;br /&gt;
Consider the following counting problems.&lt;br /&gt;
* Count the number of ways that the nonnegative integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; can be written as a sum of ones and twos (in order).&lt;br /&gt;
: The problem asks for the number of compositions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; with summands from &amp;lt;math&amp;gt;\{1,2\}&amp;lt;/math&amp;gt;. Formally, we are counting the number of tuples &amp;lt;math&amp;gt;(x_1,x_2,\ldots,x_k)&amp;lt;/math&amp;gt; for some &amp;lt;math&amp;gt;k\le n&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;x_i\in\{1,2\}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt;.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. We observe that a composition either starts with a 1, in which case the rest is a composition of &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt;; or starts with a 2, in which case the rest is a composition of &amp;lt;math&amp;gt;n-2&amp;lt;/math&amp;gt;. So we have the recursion for &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; that&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
* Count the ways to completely cover a &amp;lt;math&amp;gt;2\times n&amp;lt;/math&amp;gt; rectangle with &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; dominos without any overlaps.&lt;br /&gt;
: Dominos are identical &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; rectangles, so that only their orientations --- vertical or horizontal matter.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. It also holds that &amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;. The proof is left as an exercise.&lt;br /&gt;
&lt;br /&gt;
In both problems, the solution is given by &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; which satisfies the following recursion.&lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\begin{cases}&lt;br /&gt;
F_{n-1}+F_{n-2} &amp;amp; \mbox{if }n\ge 2,\\&lt;br /&gt;
1 &amp;amp; \mbox{if }n=1\\&lt;br /&gt;
0 &amp;amp; \mbox{if }n=0.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is called the [http://en.wikipedia.org/wiki/Fibonacci_number Fibonacci number].&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)&amp;lt;/math&amp;gt;,&lt;br /&gt;
:where &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
The quantity &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; is the so-called [http://en.wikipedia.org/wiki/Golden_ratio golden ratio], a constant with some significance in mathematics and aesthetics.&lt;br /&gt;
&lt;br /&gt;
We now prove this theorem by using generating functions.&lt;br /&gt;
The ordinary generating function for the Fibonacci number &amp;lt;math&amp;gt;F_{n}&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}F_n x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
We have that &amp;lt;math&amp;gt;F_{n}=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;n\ge 2&amp;lt;/math&amp;gt;, thus&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
G(x) &lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}F_n x^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
x+\sum_{n\ge 2}(F_{n-1}+F_{n-2})x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
For generating functions, there are general ways to generate &amp;lt;math&amp;gt;F_{n-1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F_{n-2}&amp;lt;/math&amp;gt;, or the coefficients with any smaller indices.&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
xG(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+1}=\sum_{n\ge 1}F_{n-1} x^n=\sum_{n\ge 2}F_{n-1} x^n\\&lt;br /&gt;
x^2G(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+2}=\sum_{n\ge 2}F_{n-2} x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we have&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;,&lt;br /&gt;
hence&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
The value of &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in the Taylor series for this formular, which is &amp;lt;math&amp;gt;\frac{G^{(n)}(0)}{n!}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;. Although this expansion works in principle, the detailed calculus is rather painful.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
It is easier to expand the generating function by breaking it into two geometric series.&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. It holds that&lt;br /&gt;
::&amp;lt;math&amp;gt;\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
It is easy to verify the above equation, but to deduce it, we need some (high school) calculation.&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; width=&amp;quot;100%&amp;quot; cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|&lt;br /&gt;
:{|&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;math&amp;gt;1-x-x^2&amp;lt;/math&amp;gt; has two roots &amp;lt;math&amp;gt;\frac{-1\pm\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Denote that &amp;lt;math&amp;gt;\phi=\frac{2}{-1+\sqrt{5}}=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{2}{-1-\sqrt{5}}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Then &amp;lt;math&amp;gt;(1-x-x^2)=(1-\phi x)(1-\hat{\phi}x)&amp;lt;/math&amp;gt;, so we can write &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\frac{x}{1-x-x^2}&lt;br /&gt;
&amp;amp;=\frac{x}{(1-\phi x)(1-\hat{\phi} x)}\\&lt;br /&gt;
&amp;amp;=\frac{\alpha}{(1-\phi x)}+\frac{\beta}{(1-\hat{\phi} x)},&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
where &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta&amp;lt;/math&amp;gt; satisfying that&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{cases}&lt;br /&gt;
\alpha+\beta=0\\&lt;br /&gt;
\alpha\phi+\beta\hat{\phi}= -1.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
Solving this we have that &amp;lt;math&amp;gt;\alpha=\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta=-\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt;. Thus,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
|}&lt;br /&gt;
:&amp;lt;math&amp;gt;\square&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the expression &amp;lt;math&amp;gt;\frac{1}{1-z}&amp;lt;/math&amp;gt; has a well known geometric expansion:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-z}=\sum_{n\ge 0}z^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Therefore, &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; can be expanded as&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}\\&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\phi x)^n-\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\hat{\phi} x)^n\\&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)x^n.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
So the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Fibonacci number is given by &lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Solving recurrences ==&lt;br /&gt;
The following steps describe a general methodology of solving recurrences by generating functions.&lt;br /&gt;
:1. Give a recursion that computes &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;. In the case of Fibonacci sequence&lt;br /&gt;
::&amp;lt;math&amp;gt;a_n=a_{n-1}+a_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:2. Multiply both sides of the equation by &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; and sum over all &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. This gives the generating function&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}a_nx^n=\sum_{n\ge 0}(a_{n-1}+a_{n-2})x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
:: And manipulate the right hand side of the equation so that it becomes some other expression involving &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
:3. Solve the resulting equation to derive an explicit formula for &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:4. Expand &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; into a power series and read off the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt;, which is a closed form for &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The first step is usually established by combinatorial observations, or explicitly given by the problem. The third step is trivial.&lt;br /&gt;
&lt;br /&gt;
The second and the forth steps need some non-trivial analytic techniques.&lt;br /&gt;
&lt;br /&gt;
=== Algebraic operations on generating functions ===&lt;br /&gt;
The second step in the above methodology is somehow tricky. It involves first applying the recurrence to the coefficients of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is easy; and then manipulating the resulting formal power series to express it in terms of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is more difficult (because it works backwards).&lt;br /&gt;
&lt;br /&gt;
We can apply several natural algebraic operations on the formal power series.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Generating function manipulation|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}g_nx^n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F(x)=\sum_{n\ge 0}f_nx^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
x^k G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge k}g_{n-k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\frac{G(x)-\sum_{i=0}^{k-1}g_iz^i}{x^k}&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}g_{n+k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\alpha F(x)+\beta G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} (\alpha f_n+\beta g_n)x^n\\&lt;br /&gt;
F(x)G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0}\sum_{k=0}^nf_kg_{n-k}x^n\\&lt;br /&gt;
G(cx)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} c^ng_n x^n\\&lt;br /&gt;
G&#039;(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}(n+1)g_{n+1}x^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
When manipulating generating functions, these rules are applied backwards; that is, from the right-hand-side to the left-hand-side.&lt;br /&gt;
&lt;br /&gt;
=== Expanding generating functions ===&lt;br /&gt;
The last step of solving recurrences by generating function is expanding the closed form generating function &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; to evaluate its &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th coefficient. In principle, we can always use the [http://en.wikipedia.org/wiki/Taylor_series Taylor series]&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}\frac{G^{(n)}(0)}{n!}x^n&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;G^{(n)}(0)&amp;lt;/math&amp;gt; is the value of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; evaluated at &amp;lt;math&amp;gt;x=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Some interesting special cases are very useful.&lt;br /&gt;
&lt;br /&gt;
====Geometric sequence====&lt;br /&gt;
In the example of Fibonacci numbers, we use the well known geometric series:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-x}=\sum_{n\ge 0}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
It is useful when we can express the generating function in the form of &amp;lt;math&amp;gt;G(x)=\frac{a_1}{1-b_1x}+\frac{a_2}{1-b_2x}+\cdots+\frac{a_k}{1-b_kx}&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in such &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;a_1b_1^n+a_2b_2^n+\cdots+a_kb_k^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Binomial theorem====&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;(1+x)^\alpha&amp;lt;/math&amp;gt; for some real &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; is &lt;br /&gt;
:&amp;lt;math&amp;gt;\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)(1+x)^{\alpha-n}&amp;lt;/math&amp;gt;.&lt;br /&gt;
By Taylor series, we get a generalized version of the binomial theorem known as [http://en.wikipedia.org/wiki/Binomial_coefficient#Newton.27s_binomial_series &#039;&#039;&#039;Newton&#039;s formula&#039;&#039;&#039;]:&lt;br /&gt;
{{Theorem|Newton&#039;s formular (generalized binomial theorem)|&lt;br /&gt;
If &amp;lt;math&amp;gt;|x|&amp;lt;1&amp;lt;/math&amp;gt;, then&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x)^\alpha=\sum_{n\ge 0}{\alpha\choose n}x^{n}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;{\alpha\choose n}&amp;lt;/math&amp;gt; is the &#039;&#039;&#039;generalized binomial coefficient&#039;&#039;&#039; defined by &lt;br /&gt;
:&amp;lt;math&amp;gt;{\alpha\choose n}=\frac{\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)}{n!}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Example: multisets ===&lt;br /&gt;
In the last lecture we gave a combinatorial proof of the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-multisets on an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. Now we give a generating function approach to the problem.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;S=\{x_1,x_2,\ldots,x_n\}&amp;lt;/math&amp;gt; be an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set. We have&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x_1+x_1^2+\cdots)(1+x_2+x_2^2+\cdots)\cdots(1+x_n+x_n^2+\cdots)=\sum_{m:S\rightarrow\mathbb{N}} \prod_{x_i\in S}x_i^{m(x_i)}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where each &amp;lt;math&amp;gt;m:S\rightarrow\mathbb{N}&amp;lt;/math&amp;gt; species a possible multiset on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; with multiplicity function &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let all &amp;lt;math&amp;gt;x_i=x&amp;lt;/math&amp;gt;. Then&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
(1+x+x^2+\cdots)^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{m:S\rightarrow\mathbb{N}}x^{m(x_1)+\cdots+m(x_n)}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{\text{multiset }M\text{ on }S}x^{|M|}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{k\ge 0}\left({n\choose k}\right)x^k.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the the definition of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. Our task is to evaluate &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Due to the geometric sequence and the Newton&#039;s formula&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
(1+x+x^2+\cdots)^n=(1-x)^{-n}=\sum_{k\ge 0}{-n\choose k}(-x)^k.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\left({n\choose k}\right)=(-1)^k{-n\choose k}={n+k-1\choose k}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the definition of the generalized binomial coefficient. We use an analytic (generating function) proof to get the same result of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt; as the combinatorial proof.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example: Quicksort ===&lt;br /&gt;
&lt;br /&gt;
== Catalan Number ==&lt;br /&gt;
We now introduce a class of counting problems, all with the same solution, called [http://en.wikipedia.org/wiki/Catalan_number &#039;&#039;&#039;Catalan number&#039;&#039;&#039;]. &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Catalan number is denoted as &amp;lt;math&amp;gt;C_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
In Volume 2 of Stanley&#039;s &#039;&#039;Enumerative Combinatorics&#039;&#039;, a set of exercises describe 66 different interpretations of the Catalan numbers. We give a few examples, cited from Wikipedia.&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;Dyck words&#039;&#039;&#039; of length 2&#039;&#039;n&#039;&#039;. A Dyck word is a string consisting of &#039;&#039;n&#039;&#039; X&#039;s and &#039;&#039;n&#039;&#039; Y&#039;s such that no initial segment of the string has more Y&#039;s than X&#039;s (see also [http://en.wikipedia.org/wiki/Dyck_language Dyck language]). For example, the following are the Dyck words of length 6:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; XXXYYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXXYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYXYY.&amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; counts the number of expressions containing &#039;&#039;n&#039;&#039; pairs of parentheses which are correctly matched:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; ((())) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()(()) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()()() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (())() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (()()) &amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 factors can be completely parenthesized (or the number of ways of associating &#039;&#039;n&#039;&#039; applications of a &#039;&#039;&#039;binary operator&#039;&#039;&#039;). For &#039;&#039;n&#039;&#039; = 3, for example, we have the following five different parenthesizations of four factors:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;math&amp;gt;((ab)c)d \quad (a(bc))d \quad(ab)(cd) \quad a((bc)d) \quad a(b(cd))&amp;lt;/math&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Successive applications of a binary operator can be represented in terms of a &#039;&#039;&#039;full binary tree&#039;&#039;&#039;. (A rooted binary tree is &#039;&#039;full&#039;&#039; if every vertex has either two children or no children.) It follows that &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of full binary trees with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 leaves:&lt;br /&gt;
[[Image:Catalan number binary tree example.png|center]] &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;monotonic paths&#039;&#039;&#039; along the edges of a grid with &#039;&#039;n&#039;&#039; × &#039;&#039;n&#039;&#039; square cells, which do not pass above the diagonal. A monotonic path is one which starts in the lower left corner, finishes in the upper right corner, and consists entirely of edges pointing rightwards or upwards. Counting such paths is equivalent to counting Dyck words: X stands for &amp;quot;move right&amp;quot; and Y stands for &amp;quot;move up&amp;quot;. The following diagrams show the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan number 4x4 grid example.svg.png|450px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways a [http://en.wikipedia.org/wiki/Convex_polygon &#039;&#039;&#039;convex polygon&#039;&#039;&#039;] with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;2 sides can be cut into &#039;&#039;&#039;triangles&#039;&#039;&#039; by connecting vertices with straight lines. The following hexagons illustrate the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan-Hexagons-example.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of [http://en.wikipedia.org/wiki/Stack_(data_structure) &#039;&#039;&#039;stack&#039;&#039;&#039;]-sortable permutations of {1, ..., &#039;&#039;n&#039;&#039;}. A permutation &#039;&#039;w&#039;&#039; is called &#039;&#039;&#039;stack-sortable&#039;&#039;&#039; if &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;(1,&amp;amp;nbsp;...,&amp;amp;nbsp;&#039;&#039;n&#039;&#039;), where &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) is defined recursively as follows: write &#039;&#039;w&#039;&#039; =&amp;amp;nbsp;&#039;&#039;unv&#039;&#039; where &#039;&#039;n&#039;&#039; is the largest element in &#039;&#039;w&#039;&#039; and &#039;&#039;u&#039;&#039; and &#039;&#039;v&#039;&#039; are shorter sequences, and set &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;&#039;&#039;S&#039;&#039;(&#039;&#039;u&#039;&#039;)&#039;&#039;S&#039;&#039;(&#039;&#039;v&#039;&#039;)&#039;&#039;n&#039;&#039;, with &#039;&#039;S&#039;&#039; being the identity for one-element sequences. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of ways to tile a stairstep shape of height &#039;&#039;n&#039;&#039; with &#039;&#039;n&#039;&#039; rectangles. The following figure illustrates the case &#039;&#039;n&#039;&#039;&amp;amp;nbsp;=&amp;amp;nbsp;4:&lt;br /&gt;
[[Image:Catalan stairsteps 4.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Recurrence relation for Catalan numbers|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;C_1=1&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;n&amp;gt;1&amp;lt;/math&amp;gt;,&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
C_n=\sum_{i=1}^{n-1}C_iC_{n-i}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n&amp;lt;/math&amp;gt; be the generating function. Apply the product rule,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)^2=\sum_{n\ge 0}\sum_{k=0}^{n}C_kC_{n-k}x^n=\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
Due to the recurrence,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n=x+\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n=x+G(x)^2&amp;lt;/math&amp;gt;.&lt;br /&gt;
Solving this, we obtain&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{1\pm(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
Because &amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, it must hold that &amp;lt;math&amp;gt;G(x)=\frac{1-(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;, or otherwise the constant term is not zero. Expanding &amp;lt;math&amp;gt;(1-4x)^{1/2}&amp;lt;/math&amp;gt; by Newton&#039;s formula, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\frac{1-(1-4x)^{1/2}}{2}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
1-\frac{1}{2}\sum_{n\ge 0}{1/2\choose n}(-4x)^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Thus, &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
C_n&lt;br /&gt;
&amp;amp;=-\frac{1}{2}{1/2\choose n}(-4)^n\\&lt;br /&gt;
&amp;amp;=-\frac{1}{2}\cdot\frac{1}{2}\cdot\frac{-1}{2}\cdot\frac{-3}{2}\cdots\frac{-(2n-3)}{2}\cdot(-4)^n/n!\\&lt;br /&gt;
&amp;amp;=\frac{(2n-2)!}{(n-1)!n!}\\&lt;br /&gt;
&amp;amp;=\frac{1}{n}{2n-2\choose n-1}.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we prove the following closed form for Catalan number.&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_n=\frac{1}{n}{2n-2\choose n-1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>172.21.1.108</name></author>
	</entry>
	<entry>
		<id>https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3148</id>
		<title>Combinatorics (Fall 2010)/Generating functions</title>
		<link rel="alternate" type="text/html" href="https://tcs.nju.edu.cn/wiki/index.php?title=Combinatorics_(Fall_2010)/Generating_functions&amp;diff=3148"/>
		<updated>2010-09-12T02:46:22Z</updated>

		<summary type="html">&lt;p&gt;172.21.1.108: /* Pólya&amp;#039;s problem of changing money */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Generating Functions ==&lt;br /&gt;
In Stanley&#039;s magnificent book &#039;&#039;Enumerative Combinatorics&#039;&#039;, he comments the generating function as &amp;quot;the most useful but most difficult to understand method (for counting)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The solution to a counting problem is usually represented as some &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; depending a parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. Sometimes this &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is called a &#039;&#039;counting function&#039;&#039; as it is a function of the parameter &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.  &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; can also be treated as a infinite series:&lt;br /&gt;
:&amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;ordinary generating function (OGF)&#039;&#039;&#039; defined by &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
G(x)=\sum_{n\ge 0} a_nx^n.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So &amp;lt;math&amp;gt;G(x)=a_0+a_1x+a_2x^2+\cdots&amp;lt;/math&amp;gt;. An expression in this form is called a [http://en.wikipedia.org/wiki/Formal_power_series &#039;&#039;&#039;formal power series&#039;&#039;&#039;], and &amp;lt;math&amp;gt;a_0,a_1,a_2,\ldots&amp;lt;/math&amp;gt; is the sequence of &#039;&#039;&#039;coefficients&#039;&#039;&#039;. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the generating function can be expanded as&lt;br /&gt;
:G(x)=&amp;lt;math&amp;gt;(\underbrace{1+\cdots+1}_{a_0})+(\underbrace{x+\cdots+x}_{a_1})+(\underbrace{x^2+\cdots+x^2}_{a_2})+\cdots+(\underbrace{x^n+\cdots+x^n}_{a_n})+\cdots&amp;lt;/math&amp;gt;&lt;br /&gt;
so it indeed &amp;quot;generates&amp;quot; all the possible instances of the objects we want to count.&lt;br /&gt;
&lt;br /&gt;
Usually, we do not evaluate the generating function &amp;lt;math&amp;gt;GF(x)&amp;lt;/math&amp;gt; on any particular value. &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; remains as a &#039;&#039;&#039;formal variable&#039;&#039;&#039; without assuming any value. The numbers that we want to count are the coefficients carried by the terms in the formal power series. So far the generating function is just another way to represent the sequence&lt;br /&gt;
:&amp;lt;math&amp;gt;(a_0,a_1,a_2,\ldots\ldots)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The true power of generating functions comes from the various algebraic operations that we can perform on these generating functions. We use an example to demonstrate this.&lt;br /&gt;
&lt;br /&gt;
=== Combinations ===&lt;br /&gt;
&lt;br /&gt;
=== Fibonacci numbers  ===&lt;br /&gt;
Consider the following counting problems.&lt;br /&gt;
* Count the number of ways that the nonnegative integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; can be written as a sum of ones and twos (in order).&lt;br /&gt;
: The problem asks for the number of compositions of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; with summands from &amp;lt;math&amp;gt;\{1,2\}&amp;lt;/math&amp;gt;. Formally, we are counting the number of tuples &amp;lt;math&amp;gt;(x_1,x_2,\ldots,x_k)&amp;lt;/math&amp;gt; for some &amp;lt;math&amp;gt;k\le n&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;x_i\in\{1,2\}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_1+x_2+\cdots+x_k=n&amp;lt;/math&amp;gt;.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. We observe that a composition either starts with a 1, in which case the rest is a composition of &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt;; or starts with a 2, in which case the rest is a composition of &amp;lt;math&amp;gt;n-2&amp;lt;/math&amp;gt;. So we have the recursion for &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; that&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
* Count the ways to completely cover a &amp;lt;math&amp;gt;2\times n&amp;lt;/math&amp;gt; rectangle with &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; dominos without any overlaps.&lt;br /&gt;
: Dominos are identical &amp;lt;math&amp;gt;2\times 1&amp;lt;/math&amp;gt; rectangles, so that only their orientations --- vertical or horizontal matter.&lt;br /&gt;
: Let &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; be the solution. It also holds that &amp;lt;math&amp;gt;F_n=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt;. The proof is left as an exercise.&lt;br /&gt;
&lt;br /&gt;
In both problems, the solution is given by &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; which satisfies the following recursion.&lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\begin{cases}&lt;br /&gt;
F_{n-1}+F_{n-2} &amp;amp; \mbox{if }n\ge 2,\\&lt;br /&gt;
1 &amp;amp; \mbox{if }n=1\\&lt;br /&gt;
0 &amp;amp; \mbox{if }n=0.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is called the [http://en.wikipedia.org/wiki/Fibonacci_number Fibonacci number].&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
::&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)&amp;lt;/math&amp;gt;,&lt;br /&gt;
:where &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
The quantity &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; is the so-called [http://en.wikipedia.org/wiki/Golden_ratio golden ratio], a constant with some significance in mathematics and aesthetics.&lt;br /&gt;
&lt;br /&gt;
We now prove this theorem by using generating functions.&lt;br /&gt;
The ordinary generating function for the Fibonacci number &amp;lt;math&amp;gt;F_{n}&amp;lt;/math&amp;gt; is&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}F_n x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
We have that &amp;lt;math&amp;gt;F_{n}=F_{n-1}+F_{n-2}&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;n\ge 2&amp;lt;/math&amp;gt;, thus&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{align}&lt;br /&gt;
G(x) &lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}F_n x^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
x+\sum_{n\ge 2}(F_{n-1}+F_{n-2})x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
For generating functions, there are general ways to generate &amp;lt;math&amp;gt;F_{n-1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F_{n-2}&amp;lt;/math&amp;gt;, or the coefficients with any smaller indices.&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
xG(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+1}=\sum_{n\ge 1}F_{n-1} x^n=\sum_{n\ge 2}F_{n-1} x^n\\&lt;br /&gt;
x^2G(x)&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}F_n x^{n+2}=\sum_{n\ge 2}F_{n-2} x^n.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we have&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;,&lt;br /&gt;
hence&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
The value of &amp;lt;math&amp;gt;F_n&amp;lt;/math&amp;gt; is the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in the Taylor series for this formular, which is &amp;lt;math&amp;gt;\frac{G^{(n)}(0)}{n!}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;. Although this expansion works in principle, the detailed calculus is rather painful.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
It is easier to expand the generating function by breaking it into two geometric series.&lt;br /&gt;
{{Theorem|Proposition|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;\phi=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. It holds that&lt;br /&gt;
::&amp;lt;math&amp;gt;\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
It is easy to verify the above equation, but to deduce it, we need some (high school) calculation.&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; width=&amp;quot;100%&amp;quot; cellspacing=&amp;quot;4&amp;quot; cellpadding=&amp;quot;3&amp;quot; rules=&amp;quot;all&amp;quot; style=&amp;quot;margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse;empty-cells:show;&amp;quot;&lt;br /&gt;
|&lt;br /&gt;
:{|&lt;br /&gt;
|&lt;br /&gt;
&amp;lt;math&amp;gt;1-x-x^2&amp;lt;/math&amp;gt; has two roots &amp;lt;math&amp;gt;\frac{-1\pm\sqrt{5}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Denote that &amp;lt;math&amp;gt;\phi=\frac{2}{-1+\sqrt{5}}=\frac{1+\sqrt{5}}{2}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\hat{\phi}=\frac{2}{-1-\sqrt{5}}=\frac{1-\sqrt{5}}{2}&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Then &amp;lt;math&amp;gt;(1-x-x^2)=(1-\phi x)(1-\hat{\phi}x)&amp;lt;/math&amp;gt;, so we can write &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
\frac{x}{1-x-x^2}&lt;br /&gt;
&amp;amp;=\frac{x}{(1-\phi x)(1-\hat{\phi} x)}\\&lt;br /&gt;
&amp;amp;=\frac{\alpha}{(1-\phi x)}+\frac{\beta}{(1-\hat{\phi} x)},&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
where &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta&amp;lt;/math&amp;gt; satisfying that&lt;br /&gt;
:&amp;lt;math&amp;gt;\begin{cases}&lt;br /&gt;
\alpha+\beta=0\\&lt;br /&gt;
\alpha\phi+\beta\hat{\phi}= -1.&lt;br /&gt;
\end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
Solving this we have that &amp;lt;math&amp;gt;\alpha=\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\beta=-\frac{1}{\sqrt{5}}&amp;lt;/math&amp;gt;. Thus,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}&amp;lt;/math&amp;gt;.&lt;br /&gt;
|}&lt;br /&gt;
:&amp;lt;math&amp;gt;\square&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the expression &amp;lt;math&amp;gt;\frac{1}{1-z}&amp;lt;/math&amp;gt; has a well known geometric expansion:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-z}=\sum_{n\ge 0}z^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Therefore, &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; can be expanded as&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\phi x}-\frac{1}{\sqrt{5}}\cdot\frac{1}{1-\hat{\phi} x}\\&lt;br /&gt;
&amp;amp;=\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\phi x)^n-\frac{1}{\sqrt{5}}\sum_{n\ge 0}(\hat{\phi} x)^n\\&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)x^n.&lt;br /&gt;
\end{align}&amp;lt;/math&amp;gt;&lt;br /&gt;
So the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Fibonacci number is given by &lt;br /&gt;
:&amp;lt;math&amp;gt;F_n=\frac{1}{\sqrt{5}}\left(\phi^n-\hat{\phi}^n\right)=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Solving recurrences ==&lt;br /&gt;
The following steps describe a general methodology of solving recurrences by generating functions.&lt;br /&gt;
:1. Give a recursion that computes &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;. In the case of Fibonacci sequence&lt;br /&gt;
::&amp;lt;math&amp;gt;a_n=a_{n-1}+a_{n-2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:2. Multiply both sides of the equation by &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; and sum over all &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. This gives the generating function&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}a_nx^n=\sum_{n\ge 0}(a_{n-1}+a_{n-2})x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
:: And manipulate the right hand side of the equation so that it becomes some other expression involving &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=x+(x+x^2)G(x)\,&amp;lt;/math&amp;gt;.&lt;br /&gt;
:3. Solve the resulting equation to derive an explicit formula for &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;.&lt;br /&gt;
::&amp;lt;math&amp;gt;G(x)=\frac{x}{1-x-x^2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
:4. Expand &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; into a power series and read off the coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt;, which is a closed form for &amp;lt;math&amp;gt;a_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The first step is usually established by combinatorial observations, or explicitly given by the problem. The third step is trivial.&lt;br /&gt;
&lt;br /&gt;
The second and the forth steps need some non-trivial analytic techniques.&lt;br /&gt;
&lt;br /&gt;
=== Algebraic operations on generating functions ===&lt;br /&gt;
The second step in the above methodology is somehow tricky. It involves first applying the recurrence to the coefficients of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is easy; and then manipulating the resulting formal power series to express it in terms of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt;, which is more difficult (because it works backwards).&lt;br /&gt;
&lt;br /&gt;
We can apply several natural algebraic operations on the formal power series.&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Generating function manipulation|&lt;br /&gt;
:Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}g_nx^n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;F(x)=\sum_{n\ge 0}f_nx^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
x^k G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge k}g_{n-k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\frac{G(x)-\sum_{i=0}^{k-1}g_iz^i}{x^k}&lt;br /&gt;
&amp;amp;=\sum_{n\ge 0}g_{n+k}x^n, &amp;amp;\qquad (\mbox{integer }k\ge 0)\\&lt;br /&gt;
\alpha F(x)+\beta G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} (\alpha f_n+\beta g_n)x^n\\&lt;br /&gt;
F(x)G(x)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0}\sum_{k=0}^nf_kg_{n-k}x^n\\&lt;br /&gt;
G(cx)&lt;br /&gt;
&amp;amp;= \sum_{n\ge 0} c^ng_n x^n\\&lt;br /&gt;
G&#039;(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{n\ge 0}(n+1)g_{n+1}x^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
When manipulating generating functions, these rules are applied backwards; that is, from the right-hand-side to the left-hand-side.&lt;br /&gt;
&lt;br /&gt;
=== Expanding generating functions ===&lt;br /&gt;
The last step of solving recurrences by generating function is expanding the closed form generating function &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; to evaluate its &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th coefficient. In principle, we can always use the [http://en.wikipedia.org/wiki/Taylor_series Taylor series]&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}\frac{G^{(n)}(0)}{n!}x^n&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;G^{(n)}(0)&amp;lt;/math&amp;gt; is the value of the &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; evaluated at &amp;lt;math&amp;gt;x=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Some interesting special cases are very useful.&lt;br /&gt;
&lt;br /&gt;
====Geometric sequence====&lt;br /&gt;
In the example of Fibonacci numbers, we use the well known geometric series:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{1-x}=\sum_{n\ge 0}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
It is useful when we can express the generating function in the form of &amp;lt;math&amp;gt;G(x)=\frac{a_1}{1-b_1x}+\frac{a_2}{1-b_2x}+\cdots+\frac{a_k}{1-b_kx}&amp;lt;/math&amp;gt;. The coefficient of &amp;lt;math&amp;gt;x^n&amp;lt;/math&amp;gt; in such &amp;lt;math&amp;gt;G(x)&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;a_1b_1^n+a_2b_2^n+\cdots+a_kb_k^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Binomial theorem====&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-th derivative of &amp;lt;math&amp;gt;(1+x)^\alpha&amp;lt;/math&amp;gt; for some real &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; is &lt;br /&gt;
:&amp;lt;math&amp;gt;\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)(1+x)^{\alpha-n}&amp;lt;/math&amp;gt;.&lt;br /&gt;
By Taylor series, we get a generalized version of the binomial theorem known as [http://en.wikipedia.org/wiki/Binomial_coefficient#Newton.27s_binomial_series &#039;&#039;&#039;Newton&#039;s formula&#039;&#039;&#039;]:&lt;br /&gt;
{{Theorem|Newton&#039;s formular (generalized binomial theorem)|&lt;br /&gt;
If &amp;lt;math&amp;gt;|x|&amp;lt;1&amp;lt;/math&amp;gt;, then&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x)^\alpha=\sum_{n\ge 0}{\alpha\choose n}x^{n}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where &amp;lt;math&amp;gt;{\alpha\choose n}&amp;lt;/math&amp;gt; is the &#039;&#039;&#039;generalized binomial coefficient&#039;&#039;&#039; defined by &lt;br /&gt;
:&amp;lt;math&amp;gt;{\alpha\choose n}=\frac{\alpha(\alpha-1)(\alpha-2)\cdots(\alpha-n+1)}{n!}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=== Example: multisets ===&lt;br /&gt;
In the last lecture we gave a combinatorial proof of the number of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-multisets on an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-set. Now we give a generating function approach to the problem.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;S=\{x_1,x_2,\ldots,x_n\}&amp;lt;/math&amp;gt; be an &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;-element set. We have&lt;br /&gt;
:&amp;lt;math&amp;gt;(1+x_1+x_1^2+\cdots)(1+x_2+x_2^2+\cdots)\cdots(1+x_n+x_n^2+\cdots)=\sum_{m:S\rightarrow\mathbb{N}} \prod_{x_i\in S}x_i^{m(x_i)}&amp;lt;/math&amp;gt;,&lt;br /&gt;
where each &amp;lt;math&amp;gt;m:S\rightarrow\mathbb{N}&amp;lt;/math&amp;gt; species a possible multiset on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; with multiplicity function &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let all &amp;lt;math&amp;gt;x_i=x&amp;lt;/math&amp;gt;. Then&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
(1+x+x^2+\cdots)^n&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{m:S\rightarrow\mathbb{N}}x^{m(x_1)+\cdots+m(x_n)}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{\text{multiset }M\text{ on }S}x^{|M|}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\sum_{k\ge 0}\left({n\choose k}\right)x^k.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the the definition of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. Our task is to evaluate &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Due to the geometric sequence and the Newton&#039;s formula&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
(1+x+x^2+\cdots)^n=(1-x)^{-n}=\sum_{k\ge 0}{-n\choose k}(-x)^k.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\left({n\choose k}\right)=(-1)^k{-n\choose k}={n+k-1\choose k}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
The last equation is due to the definition of the generalized binomial coefficient. We use an analytic (generating function) proof to get the same result of &amp;lt;math&amp;gt;\left({n\choose k}\right)&amp;lt;/math&amp;gt; as the combinatorial proof.&lt;br /&gt;
&lt;br /&gt;
== Catalan Number ==&lt;br /&gt;
We now introduce a class of counting problems, all with the same solution, called [http://en.wikipedia.org/wiki/Catalan_number &#039;&#039;&#039;Catalan number&#039;&#039;&#039;]. &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;th Catalan number is denoted as &amp;lt;math&amp;gt;C_n&amp;lt;/math&amp;gt;.&lt;br /&gt;
In Volume 2 of Stanley&#039;s &#039;&#039;Enumerative Combinatorics&#039;&#039;, a set of exercises describe 66 different interpretations of the Catalan numbers. We give a few examples, cited from Wikipedia.&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;Dyck words&#039;&#039;&#039; of length 2&#039;&#039;n&#039;&#039;. A Dyck word is a string consisting of &#039;&#039;n&#039;&#039; X&#039;s and &#039;&#039;n&#039;&#039; Y&#039;s such that no initial segment of the string has more Y&#039;s than X&#039;s (see also [http://en.wikipedia.org/wiki/Dyck_language Dyck language]). For example, the following are the Dyck words of length 6:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; XXXYYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXXYY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XYXYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYYXY &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; XXYXYY.&amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; counts the number of expressions containing &#039;&#039;n&#039;&#039; pairs of parentheses which are correctly matched:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;big&amp;gt; ((())) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()(()) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ()()() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (())() &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; (()()) &amp;lt;/big&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 factors can be completely parenthesized (or the number of ways of associating &#039;&#039;n&#039;&#039; applications of a &#039;&#039;&#039;binary operator&#039;&#039;&#039;). For &#039;&#039;n&#039;&#039; = 3, for example, we have the following five different parenthesizations of four factors:&lt;br /&gt;
&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;math&amp;gt;((ab)c)d \quad (a(bc))d \quad(ab)(cd) \quad a((bc)d) \quad a(b(cd))&amp;lt;/math&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Successive applications of a binary operator can be represented in terms of a &#039;&#039;&#039;full binary tree&#039;&#039;&#039;. (A rooted binary tree is &#039;&#039;full&#039;&#039; if every vertex has either two children or no children.) It follows that &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of full binary trees with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;1 leaves:&lt;br /&gt;
[[Image:Catalan number binary tree example.png|center]] &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of &#039;&#039;&#039;monotonic paths&#039;&#039;&#039; along the edges of a grid with &#039;&#039;n&#039;&#039; × &#039;&#039;n&#039;&#039; square cells, which do not pass above the diagonal. A monotonic path is one which starts in the lower left corner, finishes in the upper right corner, and consists entirely of edges pointing rightwards or upwards. Counting such paths is equivalent to counting Dyck words: X stands for &amp;quot;move right&amp;quot; and Y stands for &amp;quot;move up&amp;quot;. The following diagrams show the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan number 4x4 grid example.svg.png|450px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of different ways a [http://en.wikipedia.org/wiki/Convex_polygon &#039;&#039;&#039;convex polygon&#039;&#039;&#039;] with &#039;&#039;n&#039;&#039;&amp;amp;nbsp;+&amp;amp;nbsp;2 sides can be cut into &#039;&#039;&#039;triangles&#039;&#039;&#039; by connecting vertices with straight lines. The following hexagons illustrate the case &#039;&#039;n&#039;&#039; = 4:&lt;br /&gt;
[[Image:Catalan-Hexagons-example.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of [http://en.wikipedia.org/wiki/Stack_(data_structure) &#039;&#039;&#039;stack&#039;&#039;&#039;]-sortable permutations of {1, ..., &#039;&#039;n&#039;&#039;}. A permutation &#039;&#039;w&#039;&#039; is called &#039;&#039;&#039;stack-sortable&#039;&#039;&#039; if &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;(1,&amp;amp;nbsp;...,&amp;amp;nbsp;&#039;&#039;n&#039;&#039;), where &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) is defined recursively as follows: write &#039;&#039;w&#039;&#039; =&amp;amp;nbsp;&#039;&#039;unv&#039;&#039; where &#039;&#039;n&#039;&#039; is the largest element in &#039;&#039;w&#039;&#039; and &#039;&#039;u&#039;&#039; and &#039;&#039;v&#039;&#039; are shorter sequences, and set &#039;&#039;S&#039;&#039;(&#039;&#039;w&#039;&#039;) =&amp;amp;nbsp;&#039;&#039;S&#039;&#039;(&#039;&#039;u&#039;&#039;)&#039;&#039;S&#039;&#039;(&#039;&#039;v&#039;&#039;)&#039;&#039;n&#039;&#039;, with &#039;&#039;S&#039;&#039; being the identity for one-element sequences. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;C&#039;&#039;&amp;lt;sub&amp;gt;&#039;&#039;n&#039;&#039;&amp;lt;/sub&amp;gt; is the number of ways to tile a stairstep shape of height &#039;&#039;n&#039;&#039; with &#039;&#039;n&#039;&#039; rectangles. The following figure illustrates the case &#039;&#039;n&#039;&#039;&amp;amp;nbsp;=&amp;amp;nbsp;4:&lt;br /&gt;
[[Image:Catalan stairsteps 4.png|400px|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Theorem|Recurrence relation for Catalan numbers|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;C_1=1&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;n&amp;gt;1&amp;lt;/math&amp;gt;,&lt;br /&gt;
::&amp;lt;math&amp;gt;&lt;br /&gt;
C_n=\sum_{i=1}^{n-1}C_iC_{n-i}.&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n&amp;lt;/math&amp;gt; be the generating function. Apply the product rule,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)^2=\sum_{n\ge 0}\sum_{k=0}^{n}C_kC_{n-k}x^n=\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n&amp;lt;/math&amp;gt;.&lt;br /&gt;
Due to the recurrence,&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\sum_{n\ge 0}C_nx^n=x+\sum_{n\ge 2}\sum_{k=1}^{n-1}C_kC_{n-k}x^n=x+G(x)^2&amp;lt;/math&amp;gt;.&lt;br /&gt;
Solving this, we obtain&lt;br /&gt;
:&amp;lt;math&amp;gt;G(x)=\frac{1\pm(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
Because &amp;lt;math&amp;gt;C_0=0&amp;lt;/math&amp;gt;, it must hold that &amp;lt;math&amp;gt;G(x)=\frac{1-(1-4x)^{1/2}}{2}&amp;lt;/math&amp;gt;, or otherwise the constant term is not zero. Expanding &amp;lt;math&amp;gt;(1-4x)^{1/2}&amp;lt;/math&amp;gt; by Newton&#039;s formula, we have&lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
G(x)&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
\frac{1-(1-4x)^{1/2}}{2}\\&lt;br /&gt;
&amp;amp;=&lt;br /&gt;
1-\frac{1}{2}\sum_{n\ge 0}{1/2\choose n}(-4x)^n&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Thus, &lt;br /&gt;
:&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
C_n&lt;br /&gt;
&amp;amp;=-\frac{1}{2}{1/2\choose n}(-4)^n\\&lt;br /&gt;
&amp;amp;=-\frac{1}{2}\cdot\frac{1}{2}\cdot\frac{-1}{2}\cdot\frac{-3}{2}\cdots\frac{-(2n-3)}{2}\cdot(-4)^n/n!\\&lt;br /&gt;
&amp;amp;=\frac{(2n-2)!}{(n-1)!n!}\\&lt;br /&gt;
&amp;amp;=\frac{1}{n}{2n-2\choose n-1}.&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
So we prove the following closed form for Catalan number.&lt;br /&gt;
{{Theorem|Theorem|&lt;br /&gt;
:&amp;lt;math&amp;gt;C_n=\frac{1}{n}{2n-2\choose n-1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>172.21.1.108</name></author>
	</entry>
</feed>