You are on page 1of 13

6.042/18.

062JMathematicsforComputerScience April7,2005
SriniDevadasandEricLehman LectureNotes
GeneratingFunctions
Generatingfunctionsareoneofthemostsurprising,useful,andcleverinventionsin
discrete math. Roughly speaking, generating functions transform problems about se-
quences into problems about functions. This is great because weve got piles of mathe-
maticalmachineryformanipulatingfunctions. Thankstogeneratingfunctions, wecan
applyallthatmachinerytoproblemsaboutsequences.Inthisway,wecanusegenerating
functionstosolveallsortsofcountingproblems. Thereisahugechunkofmathematics
concerninggeneratingfunctions,sowewillonlygetatasteofthesubject.
Inthislecture,wellputsequencesinanglebracketstomoreclearlydistinguishthem
fromthemanyothermathemticalexpressionsoatingaround.
1 GeneratingFunctions
Theordinarygeneratingfunctionfortheinnitesequenceg
0
, g
1
, g
2
, g
3
. . . istheformal
powerseries:
3
G(x) = g
0
+g
1
x +g
2
x
2
+g
3
x +
A generating function is a formal power series in the sense that we usually regard x
asaplaceholderratherthananumber. Onlyinrarecaseswillweletx bearealnumber
andactuallyevaluateageneratingfunction,sowecanlargelyforgetaboutquestionsof
convergence. Notallgeneratingfunctionsareordinary,butthosearetheonlykindwell
considerhere.
Throughoutthelecture,wellindicatethecorrespondencebetweenasequenceandits
generatingfunctionwithadouble-sidedarrowasfollows:
2 3
+g
3
x g
0
, g
1
, g
2
, g
3
, . . . g
0
+g
1
x +g
2
x +
Forexample,herearesomesequencesandtheirgeneratingfunctions:
3
0, 0, 0, 0, . . . 0 +0x +0x
2
+0x = 0 +
3
1, 0, 0, 0, . . . 1 +0x +0x
2
+0x = 1 +
3
3, 2, 1, 0, . . . 3 +2x +1x
2
+0x = 3 +2x +x
2
+
Thepatternhereissimple:thei-thterminthesequence(indexingfrom0)isthecoefcient
ofx
i
inthegeneratingfunction.
Recallthatthesumofaninnitegeometricseriesis:
1
3
1 +z +z
2
+z = +
1 z
2 GeneratingFunctions
Thisequationdoesnotholdwhen z 1,butonceagainwewontworryaboutconver- | |
genceissues. Thisformulagivesclosed-formgeneratingfunctionsforawholerangeof
sequences.Forexample:
1
, 1, 1, 1, . . . 1 +x +x
2
+x = 1
3
+
1 x
1
4
= 1, 1, 1, 1, . . . x
3
+x 1 x +x
2

1 +x
1
3 3
1, a, a
2
, a , . . . 1 +ax +a
2
x
2
+a x =
1 ax

3
+
1
4
+x
6
1, 0, 1, 0, 1, 0, . . . 1 +x
2
+x =
1 x
2
+
2 OperationsonGeneratingFunctions
Themagicofgeneratingfunctionsisthatwecancarryoutallsortsofmanipulationson
sequences by performing mathematical operations on their associated generating func-
tions. Letsexperimentwithvariousoperationsandcharacterizetheireffectsintermsof
sequences.
2.1 Scaling
Multiplying a generating function by a constant scales every term in the associated se-
quencebythesameconstant.Forexample,wenotedabovethat:
1
4 6
1, 0, 1, 0, 1, 0, . . . 1 +x
2
+x +x =
1 x
2
+
Multiplyingthegeneratingfunctionby2gives
2
= 2 +2x
2
+2x
4
+2x
6
1 x
2
+
whichgeneratesthesequence:
, 0, 2, 0, 2, 0, . . . 2
Rule1(ScalingRule). If
x), f
0
, f
1
, f
2
, . . . F(
then
cf
0
, cf
1
, cf
2
, F(x). . . . c
GeneratingFunctions 3
Proof.
cf
0
, cf
1
, cf
2
, . . . cf
0
+cf
1
x +cf
2
x
2
+
= c (f
0
+f
1
x +f
2
x
2
+)
= cF(x)
2.2 Addition
Addinggeneratingfunctionscorrespondstoaddingthetwosequencestermbyterm.For
example,addingtwoofourearlierexamplesgives:
1
1, 1, 1, 1, 1, 1, . . .
1 x
1
+ 1, 1, 1, 1, 1, 1, . . .
1 +x
1 1
2, 0, 2, 0, 2, 0, . . . +
1 x 1 +x
Wevenowderivedtwodifferentexpressionsthatbothgeneratethesequence 2, 0, 2, 0, . . . .
Notsurprisingly,theyturnouttobeequal:
1 1 (1 +x) +(1 x) 2
+ = =
1 x 1 +x (1 x)(1 +x) 1 x
2
Rule2(AdditionRule). If
f
0
, f
1
, f
2
, . . . F(x), and
g
0
, g
1
, g
2
, . . . G(x),
then
f
0
+g
0
, f
1
+g
1
, f
2
+g
2
, . . . F(x) +G(x).
Proof.

f
0
+g
0
, f
1
+g
1
, f
2
+g
2
, . . . (f
n
+g
n
)x
n
n=0

= f
n
x
n
+ g
n
x
n
n=0 n=0
= F(x) +G(x)


4 GeneratingFunctions
2.3 RightShifting
Letsstartoveragainwithasimplesequenceanditsgeneratingfunction:
1
, 1, 1, 1, . . . 1
1 x
Nowletsright-shiftthesequencebyaddingk leadingzeros:
k+1 k+2 k+3
0, 0, . . . , 0, 1, 1, 1, . . . x
k
+x +x +x

+
k zeroes
k 3
= x (1 +x +x
2
+x + )
k
x
=
1 x
Evidently,addingk leadingzerostothesequencecorrespondstomultiplyingthegener-
atingfunctionbyx
k
.Thisholdstrueingeneral.
Rule3(Right-ShiftRule). Iff
0
, f
1
, f
2
, . . . F(x),then:
0, 0, . . . , 0, f
0
, f
1
, f
2
, . . . x
k
F(x)

k zeroes
Proof.
k zeroes
f
0
x
k
+f
1
x
k+1
+f
2
x
k+2
0, 0, . . . , 0, f
0
, f
1
, f
2
, . . . +
3
= x
k
(f
0
+f
1
x +f
2
x
2
+f
3
x + )
k
= x F(x)
2.4 Differentiation
What happens if we take the derivative of a generating function? As an example, lets
differentiatethenow-familiargeneratingfunctionforaninnitesequenceof1s.
d 1 d
(1 +x +x
2
+x
3
+x
4
=
dx
+ )
dx 1 x
1
3
1 +2x +3x
2
+4x =
2
+
(1 x)
1
1, 2, 3, 4, . . .
(1 x)
2
Wefoundageneratingfunctionforthesequence1, 2, 3, 4, . . . !
Ingeneral,differentiatingageneratingfunctionhastwoeffectsonthecorresponding
sequence: eachtermismultipliedbyitsindexandtheentiresequenceisshiftedleftone
place.
5 GeneratingFunctions
Rule4(DerivativeRule). If
f
0
, f
1
, f
2
, f
3
, . . . F(x),
then

(x). f
1
, 2f
2
, 3f
3
, . . . F
Proof.
f
1
, 2f
2
, 3f
3
, . . . = f
1
+ 2f
2
x + 3f
3
x
2
+
d
(f
0
+ f
1
x + f
2
x
2
+ f
3
x
3
+ =
dx
)
d
= F(x)
dx
The Derivative Rule is very useful. In fact, there is frequent, independent need for
eachofdifferentiationstwoeffects,multiplyingtermsbytheirindexandleft-shiftingone
place. Typically,wewantjustoneeffectandmustsomehowcancelouttheother. Forex-
ample,letstrytondthegeneratingfunctionforthesequenceofsquares,0, 1, 4, 9, 16, . . . .
Ifwecouldstartwiththesequence1, 1, 1, 1, . . . andmultiplyeachtermbyitsindextwo
times,thenwedhavethedesiredresult:
0, 1 1, 2 2, 3 3, . . . = 0, 1, 4, 9, . . . 0
A challenge is that differentiation not only multiplies each term by its index, but also
shifts the whole sequence left one place. However, the Right-Shift Rule 3 tells how to
canceloutthisunwantedleft-shift:multiplythegeneratingfunctionbyx.
Our procedure, therefore, is to begin with the generating function for 1, 1, 1, 1, . . . ,
differentiate,multiplybyx,andthendifferentiateandmultiplybyx oncemore.
1
1, 1, 1, 1, . . .
1 x
d 1 1
= 1, 2, 3, 4, . . .
dx 1 x (1 x)
2
1 x
= 0, 1, 2, 3, . . . x
(1 x)
2
(1 x)
2
d x 1 + x
= 1, 4, 9, 16, . . .
dx (1 x)
2
(1 x)
3
1 + x x(1 + x)
= 0, 1, 4, 9, . . . x
(1 x)
3
(1 x)
3
Thus,thegeneratingfunctionforsquaresis:
x(1 + x)
(1 x)
3
6 GeneratingFunctions
3 TheFibonacciSequence
Sometimes we can nd nice generating functions for more complicated sequences. For
example,hereisageneratingfunctionfortheFibonaccinumbers:
x
0, 1, 1, 2, 3, 5, 8, 13, 21, . . .
1 x x
2
TheFibonaccinumbersareafairlynastybunch,butthegeneratingfunctionissimple!
Weregoingtoderivethisgeneratingfunctionandthenuseittondaclosedformfor
then-Fibonaccinumber.Ofcourse,wealreadyhaveaclosedformforFibonaccinumbers,
obtained from the cookbook procedure for solving linear recurrences. But there are a
couplereasonstocoverthesamegroundagain. First,wellgainsomeinsightintowhy
thecookbookmethodforlinearrecurrencesworks.And,second,thetechniqueswelluse
are applicable to a large class of recurrence equations, including some that we have no
otherwaytotackle.
3.1 FindingaGeneratingFunction
LetsbeginbyrecallingthedenitionoftheFibonaccinumbers:
f
0
= 0
f
1
= 1
f
n
= f
n1
+ f
n2
(forn 2)
Wecanexpandthenalclauseintoaninnitesequenceofequations.Thus,theFibonacci
numbersaredenedby:
f
0
=0
f
1
=1
f
2
=f
1
+ f
0
f
3
=f
2
+ f
1
f
4
=f
3
+ f
2
.
.
.
NowtheoverallplanistodeneafunctionF(x) thatgeneratesthesequenceontheleft
sideoftheequalitysymbols,whicharetheFibonaccinumbers.Thenwederiveafunction
that generates the sequence on the right side. Finally, we equate the two and solve for
F(x).Letstrythis.First,wedene:
3
+ f
4
x
4
F(x) = f
0
+ f
1
x + f
2
x
2
+ f
3
x +


7 GeneratingFunctions
Nowweneedtoderiveageneratingfunctionforthesequence:
, 1, f
1
+f
0
, f
2
+f
1
, f
3
+f
2
, . . . 0
Oneapproachistobreakthisintoasumofthreesequencesforwhichweknowgenerating
functionsandthenapplytheAdditionRule:
0, 1, 0, 0, 0, x . . .
0, f
0
, f
1
, f
2
, f
3
, xF(x) . . .
+ 0, 0, f
0
, f
1
, f
2
, . . . x
2
F(x)
0, 1 +f
0
, f
1
+f
0
, f
2
+f
1
, f
3
+f
2
, . . . x +xF(x) +x
2
F(x)
ThissequenceisalmostidenticaltotherightsidesoftheFibonacciequations. Theone
blemishisthatthesecondtermis1 + f
0
insteadofsimply1. However,thisamountsto
nothing,sincef
0
= 0 anyway.
NowifweequateF(x) withthenewfunctionx+xF(x)+x
2
F(x),thenwereimplicitly
writingdownalloftheequationsthatdenetheFibonaccinumbersinonefellswoop:
F(x) = f
0
+ f
1
x + f
2
x
2
+ f
3
x
3
+ f
4
x
4
+. . .
x +xF(x) +x
2
F(x) = 0 + (1 +f
0
) x + (f
1
+f
0
) x
2
+ (f
2
+f
1
) x
3
+ (f
3
+f
2
) x
4
+
SolvingforF(x) givesthegeneratingfunctionfortheFibonaccisequence:
F(x) = x +xF(x) +x
2
F(x)
x
F(x) =
1 x x
2
Sureenough,thisisthesimplegeneratingfunctionweclaimedattheoutset!
3.2 FindingaClosedForm
Why should one care about the generating function for a sequence? There are several
answers,buthereisone:ifwecanndageneratingfunctionforasequence,thenwecan
oftenndaclosedformforthen-thcoefcientwhichcanbeprettyuseful!Forexample,
aclosedformforthecoefcientofx
n
inthepowerseriesforx/(1 x x
2
) wouldbean
explicitformulaforthen-thFibonaccinumber.
So our next task is to extract coefcients from a generating function. There are sev-
eralapproaches. Forageneratingfunctionthatisaratioofpolynomials,wecanusethe
method of partial fractions, which you learned in calculus. Just as the terms in a par-
tialfractionsexpansionareeasiertointegrate,thecoefcientsofthosetermsareeasyto
compute.
LetstrythisapproachwiththegeneratingfunctionforFibonaccinumbers. First,we
factorthedenominator:
1 x x
2
= (1
1
x)(1
2
x)


8 GeneratingFunctions
1 1
where
1
=
2
(1 +

5) and
2
=
2
(1

5).Next,wendA
1
andA
2
whichsatisfy:
x A
1
A
2
= +
1 x x
2
1
1
x 1
2
x
Wedothisbyplugginginvariousvaluesofx togeneratelinearequationsinA
1
andA
2
.
WecanthenndA
1
andA
2
bysolvingalinearsystem.Thisgives:
1 1
A
1
= =

5
1 1
A
2
= =

5
SubstitutingintotheequationabovegivesthepartialfractionsexpansionofF(x):
x 1 1 1
=
1 x x
2

5
1
1
x

1
2
x
Eachterminthepartialfractionsexpansionhasasimplepowerseriesgivenbythegeo-
metricsumformula:
1
2
= 1 +
1
x +
2
1
x
1
1
x
+
1
2
= 1 +
2
x +
2
2
x
1
2
x
+
Substitutingintheseseriesgivesapowerseriesforthegeneratingfunction:
1 1 1
F(x) =
5
1
1
x

1
2
x
1

2
=
5
(1 +
1
x +
2
2
x
1
x + ) (1 +
2
x +
2
+ )

n
1

n
f
n
=
5
2

5

n n
1 1 +

=
5
2

2
Thisisthesamescaryformulaforthen-thFibonaccinumberthatwefoundusingthe
methodforsolvinglinearrecurrences. Andthisalternateapproachshedssomelighton
thatmethod.Inparticular,thestrangerulesinvolvingrepeatedrootsofthecharacteristic
equationarereectionsoftherulesforndingapartialfractionsexpansion!
4 CountingwithGeneratingFunctions
Generatingfunctionsareparticularlyusefulforsolvingcountingproblems.Inparticular,
problems involving choosing items from a set often lead to nice generating functions.
When generating functions are used in this way, the coefcient of x
n
is the number of
waystochoosen items.

GeneratingFunctions 9
4.1 ChoosingDistinctItemsfromaSet
ThegeneratingfunctionforbinomialcoefcientsfollowsdirectlyfromtheBinomialThe-
orem:

k k k k k k k
2
k
k
, , , . . . , , 0, 0, 0, . . . + x + x + x
0 1 2 k

0 1 2
+
k
= (1 +x)
k
Thus,thecoefcientofx
n
in(1 + x)
k
isthenumberofwaystochoosen distinctitems
k
from a k-element set. For example, the coefcient of x
2
is , the number of ways to
2
choose 2 items from a k-element set. Similarly, the coefcient of x
k+1
is the number of
waystochoosek +1 itemsfromak-elementset,whichiszero.
4.2 BuildingGeneratingFunctionsthatCount
Oftenwecantranslatethedescriptionofacountingproblemdirectlyintoagenerating
function for the solution. For example, we could gure out that (1 + x)
k
generates the
number of ways to select n distinct items from a k-element subset without resorting to
theBinomialTheoremorevenfussingwithbinomialcoefcients!
Hereishow. First,considerasingle-elementset{a
1
}. Thegeneratingfunctionforthe
number of ways to choose n elements from this set is simply 1 + x: we have 1 way to
choosezeroelements,1waytochooseoneelement,and0waystochoosemorethanone
element. Similarly, the number of ways to choose n elements from the set {a
2
} is also
givenbythegeneratingfunction1 + x. Thefactthattheelementsdifferinthetwocases
isirrelevant.
Nowhereisthethemaintrick:thegeneratingfunctionforchoosingelementsfromaunionof
disjointsetsistheproductofthegeneratingfunctionsforchoosingfromeachset.Welljustifythis
inamoment,butletsrstlookatanexample.Accordingtothisprinciple,thegenerating
functionforthenumberofwaystochoosen elementsfromthe{a
1
, a
2
} is:
(1 +x) (1 +x) = (1 +x)
2
= 1 +2x +x
2


OGFfor OGFfor OGFfor
{a
1
, a
2
} {a
1
} {a
2
}
Sureenough,fortheset{a
1
, a
2
},wehave1waytochoosezeroelements,2waystochoose
one element, 1 way to choose two elements, and 0 ways to choose more than two ele-
ments.
Repeated application of this rule gives the generating function for choosing n items
fromak-elementset{a
1
, a
2
, . . . , a
k
}:
(1 +x) (1 +x) (1 +x) = (1 +x)
k




OGFfor OGFfor OGFfor OGFfor
{a
1
, a
2
, . . . , a
k
} {a
1
} {a
2
} {a
k
}

10 GeneratingFunctions
This is the same generating function that we obtained by using the Binomial Theorem.
Butthistimearoundwetranslateddirectlyfromthecountingproblemtothegenerating
function.
Wecanextendtheseideastoageneralprinciple:
Rule5(ConvolutionRule). LetA(x) bethegeneratingfunctionforselectingitemsfromsetA,
andletB(x) bethegeneratingfunctionforselectingitemsfromsetB. IfAandB aredisjoint,
thenthegeneratingfunctionforselectingitemsfromtheunionA B istheproductA(x) B(x).
This rule is rather ambiguous: what exactly are the rules governing the selection of
items from a set? Remarkably, the Convolution Rule remains valid under many inter-
pretations of selection. For example, we could insist that distinct items be selected or
wemightallowthesameitemtobepickedalimitednumberoftimesoranynumberof
times. Informally,theonlyrestrictionsarethat(1)theorderinwhichitemsareselected
isdisregardedand(2)restrictionsontheselectionofitemsfromsetsA andB alsoapply
in selecting items from A B. (Formally, there must be a bijection between n-element
selectionsfromA B andorderedpairsofselectionsfromA andBcontainingatotalof
n elements.)
Proof. Dene:

A(x) = a
n
x
n
, B(x) = b
n
x
n
, C(x) = A( B(x) = c
n
x
n
. x)
n=0 n=0 n=0
LetsrstevaluatetheproductA(x) B(x) andexpressthecoefcientc
n
intermsofthe
a andb coefcients.Wecantabulateallofthetermsinthisproductinatable:
b
0
x
0
b
1
x
1
b
2
x
2
b
3
x
3
. . .
a
0
x
0
a
0
b
0
x
0
a
0
b
1
x
1
a
0
b
2
x
2
a
0
b
3
x
3
. . .
a
1
x
1
a
1
b
0
x
1
a
1
b
1
x
2
a
1
b
2
x
3
. . .
a
2
x
2
a
2
b
0
x
2
a
2
b
1
x
3
. . .
a
3
x
3
a
3
b
0
x
3
. . .
.
.
. . . .
Noticethatalltermsinvolvingthesamepowerofx lieona/-slopeddiagonal.Collecting
thesetermstogether,wendthatthecoefcientofx
n
intheproductis:
c
n
= a
0
b
n
+a
1
b
n1
+a
2
b +a
n
b
0 n2
+
GeneratingFunctions 11
Nowwemustshowthatthisisalsothenumberofwaystoselectn itemsfromA B.
Ingeneral, wecanselectatotalofn itemsfromAB bychoosingj itemsfromAand
n j itemsfromB,wherej isanynumberfrom0ton. Thiscanbedoneina
j
b
nj
ways.
Summingoverallthepossiblevaluesofj givesatotalof
a
0
b
n
+ a
1
b
n1
+ a
2
b
n2
+ + a
n
b
0

waystoselectn itemsfromA B.Thisispreciselythevalueofc
n
computedabove.
Theexpressionc
n
= a
0
b
n
+a
1
b
n1
+a
2
b +a
n
b
0
maybefamiliarfromasignalpro-
n2
+
cessingcourse;thesequencec
0
, c
1
, c
2
, . . . istheconvolutionofsequencesa
0
, a
1
, a
2
, . . .
andb
0
, b
1
, b
2
, . . . .
4.3 ChoosingItemswithRepetition
Therstcountingproblemweconsideredaskedforthenumberofwaystoselectadozen
doughnutswhentherewerevevarietiesavailable. Wecangeneralizethisquestionas
follows: inhowmanywayscanweselectk itemsfromann-elementsetifwereallowed
topickthesameitemmultiplestimes?Intheseterms,thedoughnutproblemasksinhow
manywayswecanselectadozendoughnutsfromtheset:
{chocolate, lemon-lled, sugar, glazed, plain}
ifwereallowedtopickseveraldoughnutsofthesamevariety. Letsapproachthisques-
tionfromageneratingfunctionsperspective.
Suppose we choose n items (with repetition allowed) from a set containing a single
item. Thenthereisonewaytochoosezeroitems,onewaytochooseoneitem,oneway
to choose two items, etc. Thus, the generating function for choosing n elements with
repetitionfroma1-elementsetis:
3
1, 1, 1, 1, . . . 1 + x + x
2
+ x +
1
=
1 x
The Convolution Rule says that the generating function for selecting items from a
unionofdisjointsetsistheproductofthegeneratingfunctionsforselectingitemsfrom
eachset:
1 1 1 1
=
1 x

1 x

1 x (1 x)
n


OGFfor OGFfor OGFfor
OGFfor
{a
1
} {a
2
} {a
n
}
{a
1
, a
2
, . . . , a
n
}
Therefore,thegeneratingfunctionforselectingitemsfroman-elementsetwithrepetition
allowedis1/(1 x)
n
.

12 GeneratingFunctions
Nowweneedtondthecoefcientsofthisgeneratingfunction. Wecouldtrytouse
partial fractions, but (1 x)
n
has a nasty repeated root at 1. An alternative is to use
TaylorsTheorem:
Theorem1(TaylorsTheorem).
f

(0) f

(0) f
(k)
(0)
2 3 k
f(x) = f(0) + f

(0)x + x + x + + x +
2! 3!

k!

Thistheoremsaysthatthek-thcoefcientof1/(1 x)
n
isequaltoitsk-thderivative
evaluatedat0anddividedbyk!. Andcomputingthek-thderivativeturnsoutnottobe
verydifcult.Let
g(x) =
1
(1 x)
n
= (1 x)
n
Thenwehave:
G

(x) = n(1 x)
n1
G

(x) = n(n + 1)(1 x)


n2
G

(x) = n(n + 1)(n + 2)(1 x)


n3
G
(k)
(x) = n(n + 1) (n + k 1)(1 x)
nk
Thus,thecoefcientofx
k
inthegeneratingfunctionis:
G
(k)
(0)/k! =
n(n + 1) (n + k 1)
k!
(n + k 1)!
=
(n 1)! k!
n + k 1
=
k
Therefore, the number of ways to select k items from an n-element set with repetition
allowedis:

n + k 1
k
This makes sense, since there is a bijection between such selections and (n + k 1)-bit
sequenceswithk zeroes(representingtheitems)andn1 ones(separatingthen different
typesofitem).
5 AnImpossibleCountingProblem
Sofareverythingwevedonewithgeneratingfunctionswecouldhavedoneanotherway.
Buthereisanabsurdcountingproblemreallyoverthetop!Inhowmanywayscanwe
llabagwithn fruitssubjecttothefollowingconstraints?
13 GeneratingFunctions
Thenumberofapplesmustbeeven.
Thenumberofbananasmustbeamultipleof5.
Therecanbeatmostfouroranges.
Therecanbeatmostonepear.
Forexample,thereare7waystoformabagwith6fruits:
Apples 6 4 4 2 2 0 0
Bananas 0 0 0 0 0 5 5
Oranges 0 2 1 4 3 1 0
Pears 0 0 1 0 1 0 1
Theseconstraintsaresocomplicatedthattheproblemseemshopeless!Butletsseewhat
generatingfunctionsreveal.
Letsrstconstructageneratingfunctionforselectingapples. Wecanselectasetof
0applesinoneway,asetof1applesinzeroways(sincethenumberofapplesmustbe
even), asetof2appliesinoneway, asetof3applesinzeroways, andsoforth. Sowe
have:
1
4 6
A(x) = 1 +x
2
+x +x + =
1 x
2
Similarly,thegeneratingfunctionforselectingbananasis:
1
10 15
B(x) = 1 +x
5
+x +x + =
1 x
5
Now,wecanselectasetof0orangesinoneway,asetof1orangeinoneways,andsoon.
However,wecannotselectmorethanfouroranges,sowehavethegeneratingfunction:
3 4
O(x) = 1 +x +x
2
+x +x =
1 x
5
1 x
Herewereusingthegeometricsumformula.Finally,wecanselectonlyzerooronepear,
sowehave:
P(x) = 1 +x
TheConvolutionRulesaysthatthegeneratingfunctionforselectingfromamongall
fourkindsoffruitis:
1 1
A(x)B(x)O(x)P(x) =
1 x
5
(1 +x)
1 x
2
1 x
5
1 x
1
=
(1 x)
2
3
= 1 +2x +3x
2
+4x +
Almosteverythingcancels!Wereleftwith1/(1 x)
2
,whichwefoundapowerseriesfor
earlier: thecoefcientofx
n
issimplyn +1. Thus,thenumberofwaystoformabagofn
fruitsisjustn +1.Thisisconsistentwiththeexampleweworkedout,sincetherewere7
differentfruitbagscontaining6fruits.Amazing!

You might also like