Вы находитесь на странице: 1из 20

Generating functions

An important idea in mathematics is to establish connections between two fields in order to apply
knowledge in one field to the other field, or at least take a problem in one field and transform it to a problem
in the other field. This idea motivates the idea of a generating function, which establishes a connection
between functions of a real variable and sequences of numbers.

Definition: Given a numeric sequence the

series is called the generating function of the sequence.


To find the generating function for a sequence means to find a closed form formula for f(x), one that has no
ellipses.

Example: The generating function for the constant sequence , has closed

form

This is because the sum of the geometric series is (for all x less than 1
in absolute value).
> f(x) =sum(x^'i','i'=0..infinity);

>

Problem: Find the generating function for

Problem: Find the generating function for

Problem: Find the generating function for


Problem: Suppose f(x) is the generating function for a and g(x) is the generating function for b. Show that
f(x) + g(x) is the generating function for a + b, but that f(x) * g(x) is not the generating function for a*b.

Definition: The convolution of two sequences a and b is the sequence c defined by


Problem : Suppose f(x) is the generating function for a and g(x) is the generating function for b. Show

that is the generating function for the convolution of a and b.


Every function which has derivatives of all orders at 0 is the generating function of its Taylor expansion at
0.
> 1/(1-x)=series(1/(1-x),x,20);
Uses of generating functions
One use for generating functions is get closed formulas for sequences which are defined recursively in terms
of previous terms. The defining equations for such sequences are called recurence relations

Problem: Find a formula for the nth term of the sequence defined by , and .
Investigation. This rule is directly programmable in Maple, using a recursive program (g refers to itself in
its definition).
> g := proc(n) options remember; if n = 0 then 1 else 5*g(n-1) - 3 fi end;

> g(100);
> seq(a[n]=g(n),n=0..10);

>
A Solution using generating functions.

= =
Doing a partial fraction decomposition of the right hand side
> convert((1-4*x)/((1-5*x)*(1-x)),parfrac,x);

f(x) =

f(x) =

So by inspection, we see the formula for


Checking this,
>
> p :=unapply(5^n-3*(sum(5^'i','i'=0..n-1)),n);

> k := n-> (5^n+3)/4;

> seq(k(n),n=0..10);

> seq(g(n),n=0..10);

> seq(p(n),n=0..10);

> f(x) := collect(sum(a[i]*x^'i','i'=0..5),x);


> expand( 5*f(x)*x);

Actually, DeMoivre introduced the notion of generating function in 1730 to solve recurrence relations. Here
is a famous one first defined by Fibonacci in 1200.

Problem: Given that , find a closed formula for


Another use for generating functions is to solve counting problems . Most counting problems lie in a
sequence of counting problems. If we can identify the generating function of that sequence we have a crack
at finding the formula to solve the counting problems in the sequence.
Example. For fixed n, let C(n,r) be the number of combinations of n things taken r at a time, r = 0 to n. The

generating function for this sequence is .

The way to see this is to think of how the coefficient of in the expansion of is made up. The

expansion of has terms before collecting is done. Each term is a product n things some of

which are x's and the rest 1's. The coefficient of is the number of those terms which has exactly r x's.
We can program this directly in Maple.
> binom := proc(n,r)
local x,cof;
if r = 0 or r=n then 1 else
coeff( expand( (1+x)^n),x,r) ;
fi end:
> seq(binom(4,i),i=0..4);

So, for example, the number of ways to choose 10 books from a library of 40 books is
> binom(40,10);

Checking that with the Maple word binomial --


> binomial(40,10);

Same result.
Problem. How many Lotto tickets have only even numbers? prime numbers?

Problem . Show that for all positive integers n and r with r < n, .

Problem. Show that


This is only the tip of the iceberg. The answers to many many counting problems are coefficients of certain
polynomial generating functions.
Problem. A library has 5 black books, 4 red books, and 3 yellow books, all with different titles. How many
ways can a student take home 6 books, 2 of each color?

Solution. Let . When A(x,y,z) is expanded out, it will have


terms each of which the product of 5 x's or 1's with 4 y's or 1's and 3 z's or 1's. Each term can be thought of
as a checkout set of books. 1x111 yy11 1z1 means the 2nd black, 1st and 2nd red and 2nd yellow books
were checked out. With that interpretation, the coefficient of is answer we seek, and this can be
obtained by expanding out and collecting like terms.
> A := (1+x)^5*(1+y)^4*(1+z)^3;

> cf :=coeff(collect(expand(A) ,x),x^2);

> cf :=coeff(collect(cf ,y),y^2);

> cf :=coeff(collect(cf ,z),z^2);

Second Solution: Choose 2 of the 5 blacks, then choose 2 of the 4 reds, and last choose 2 of the 3 yellows.

This gives
> binom(5,2)*binom(4,2)*binom(3,2);

Problem. A library has 5 indistinguishable black books, 4 indistinguishable red books, and 3
indistinguishable yellow books. How many distinguishable ways can a student take home 6 books, 2 of each
color?
Solution 1 . The student chooses 2 black books (only one way to do that since the black books are
indistinguishable) then 2 reds then 2 yellows. The set of things we are counting is the cartesian product of
three sets each with 1 element, so the answer is 1.
Solution 2. Think of choosing the black books first: since they are indistinguishable amonst each other, the
only distinguishing characteristic of the choice is the number of black books chosen. We could represent this

by choosing a term of the polynomial ; thus choosing the term 1 means no


black books are chosen, etc. Similarly choosing the red books would be to choose a term

of and choosing the yellow books would be to choose a term of .

The polynomial when expanded has


6*5*4 = 120 terms (none alike), each of which represents a distinguisable way of taking home a set of books
from the library, including the term 1*1*1 which is taking home the empty set of books. The coefficient

of is 1 which is the number of ways to take home 6 books, two of each color.
Problem. A library has 5 indistinguishable black books, 4 indistinguishable red books, and 3
indistinguishable yellow books. How many distinguishable ways can a student take home 6 books, at least 1
of each color?
Solution 1. Direct count. Make a list. of the triples [x,y,z] of integers satisfying x+y+z = 6, 1<= x <=5, 1 <=
y <= 4, and 1 <= z <= 4. [4,1,1], [3,2,1],[3,1,2], [2,3,1],[2,2,2],[2,1,3], [1,2,3],[1,3,2],[1,4,1]. There are 9.
>
Solution 2. As above, we can form a polynomial p

= , where the term 1 has been left out of each


factor since at least one book of each color is to be chosen. When expanded this polynomial has 5*4*3 = 60
terms, each representing a way for the student to take home a set of books at least 1 of each color. What we
want is the number of terms of degree 6. Those represent the ways a student could take home 6 books, at

least one of each color. If we set x = y = z in p and collect like terms then the coefficient of is the
desired answer.
> p := (x+x^2+x^3+x^4+x^5)*(y+y^2+y^3+y^4)*(z+z^2+z^3);

> q :=subs({y=x,z=x},p);

> coeff(q,x^6);

>
Problem. A library has 5 indistinguishable black books, 4 indistinguishable red books, and 3
indistinguishable yellow books. How many distinguishable ways can a student take home 6 books?
Solution 1. Direct count. Make a list. of the triples [x,y,z] of integers satisfying x+y+z = 6, 0<= x <=5, 0 <=
y <= 4, and 0 <= z <= 4.
Solution 2. Polynomial solution.
> p := (1+x+x^2+x^3+x^4+x^5)*(1+y+y^2+y^3+y^4)*(1+z+z^2+z^3);

> q :=subs({y=x,z=x},p);

>
> coeff(q,x^6);

Partial-Fraction Decomposition:
General Techniques (page 1 of 3)

you have added and simplified rational expressions, such as:

Partial-fraction decomposition is the process of starting with the simplified answer and taking it back apart,
of "decomposing" the final expression into its initial polynomial fractions.

To decompose a fraction, you first factor the denominator. Let's work backwards from the example above.
The denominator is x2 + x, which factors as x(x + 1).

Then you write the fractions with one of the factors for each of the denominators. Of course, you don't know
what the numerators are yet, so you assign variables (usually capital letters) for these unknown values:

Then you set this sum equal to the simplified result:


Multiply through by the common denominator of x(x + 1) gets rid of all of the denominators:

3x + 2 = A(x + 1) + B(x) Copyright Elizabeth Stapel 2006-2011 All Rights Reserved

Multiply things out, and group the x-terms and the constant terms:

3x + 2 = Ax + A1 + Bx
3x + 2 = (A + B)x + (A)1 ADVERTISEMENT
(3)x + (2)1 = (A + B)x + (A)1

For the two sides to be equal, the coefficients of the two


polynomials must be equal. So you "equate the
coefficients" to get:

3=A+B
2=A

This creates a system of equations that you can solve:

A=2
B=1

Then the original fractions were (as we already know) the following:

What are Partial Fractions?

We can do this directly:

Like this:

2x2 + 3x+1 = 2(x+1) + 3(x2)(x2)(x + 1)

Which can be simplified (see Using Rational Expressions) to:

= 2x+2 + 3x6x2+x2x2

= 5x4x2x2

... but how do we go in the opposite direction?


That is what we are going to discover:

How to find the "parts" that make the single fraction


(the "partial fractions").

Why Do We Want Them?

First of all ... why do we want them?

Because the partial fractions are each simpler.

This can help solve the more complicated fraction. For example it is very useful in Integral Calculus.

Partial Fraction Decomposition

So let me show you how to do it.

The method is called "Partial Fraction Decomposition", and goes like this:

Step 1: Factor the bottom

Step 2: Write one partial fraction for each of those factors

Step 3: Multiply through by the bottom so we no longer have fractions

Step 4: Now find the constants A1 and A2

Substituting the roots, or "zeros", of (x2)(x+1) can help:


And we have our answer:

That was easy! ... almost too easy ...

... because it can be a lot harder!

Now we go into detail on each step.

Proper Rational Expressions

Firstly, this only works for Proper Rational Expressions, where the degree of the top is less thanthe bottom.

The degree is the largest exponent the variable has.

Proper: the degree of the top is less than the degree of the bottom.
degree of top is 1
Proper:
degree of bottom is 3

Improper: the degree of the top is greater than, or equal to, the degree of the bottom.
degree of top is 2
Improper:
degree of bottom is 1

If your expression is Improper, then do polynomial long division first.

Factoring the Bottom

It is up to you to factor the bottom polynomial. See Factoring in Algebra.


But don't factor them into complex numbers ... you may need to stop some factors at quadratic (called
irreducible quadratics because any further factoring leads to complex numbers):

Example: (x2-4)(x2+4)

x2-4 can be factored into (x-2)(x+2)


But x2+4 factors into complex numbers, so don't do it

So the best we can do is:

(x-2)(x+2)(x2+4)

So the factors could be a combination of

linear factors
irreducible quadratic factors

When you have a quadratic factor you need to include this partial fraction:

B1x + C1(Your Quadratic)

Factors with Exponents

Sometimes you may get a factor with an exponent, like (x-2)3 ...

You need a partial fraction for each exponent from 1 up.

Like this:

Example:

1(x2)3

Has partial fractions

A1x2 + A2(x2)2 + A3(x2)3

The same thing can also happen to quadratics:

Example:

1(x2+2x+3)2

Has partial fractions:

B1x + C1x2+2x+3 + B2x + C2(x2+2x+3)2


Sometimes Using Roots Does Not Solve It

Even after using the roots (zeros) of the bottom you can end up with unknown constants.

So the next thing to do is:

Gather all powers of x together and then solve it as a system of linear equations.

Oh my gosh! That is a lot to handle! So, on with an example to help you understand:

A Big Example Bringing It All Together

Here is a nice big example for you!

x2+15(x+3)2 (x2+3)

Because (x+3)2 has an exponent of 2, it needs two terms (A1 and A2).
And (x2+3) is a quadratic, so it will need Bx + C:

x2+15(x+3)2(x2+3) = A1x+3 + A2(x+3)2 + Bx + Cx2+3

Now multiply through by (x+3)2(x2+3):

x2+15 = (x+3)(x2+3)A1 + (x2+3)A2 + (x+3)2(Bx + C)

There is a zero at x = -3 (because x+3=0), so let us try that:

(-3)2+15 = 0 + ((-3)2+3)A2 + 0

And simplify it to:

24 = 12A2

so A2=2

Let us replace A2 with 2:

x2+15 = (x+3)(x2+3)A1 + 2x2+6 + (x+3)2(Bx + C)

Now expand the whole thing:

x2+15 = (x3+3x+3x2+9)A1 + 2x2+6 + (x3+6x2+9x)B + (x2+6x+9)C

Gather powers of x together:


x2+15 = x3(A1+B)+x2(3A1+6B+C+2)+x(3A1+9B+6C)+(9A1+6+9C)

Separate the powers and write as a Systems of Linear Equations:

x3: 0 = A1+B
x2: 1 = 3A1+6B+C+2
x: 0 = 3A1+9B+6C
Constants: 15 = 9A1+6+9C

Simplify, and arrange neatly:

0 = A1 + B
-1 = 3A1 + 6B + C
0 = 3A1 + 9B + 6C
1 = A1 + C

Now solve.

You can choose your own way to solve this ... I decided to subtract the 4th equation from the 2nd to begin with:

0 = A1 + B
-2 = 2A1 + 6B
0 = 3A1 + 9B + 6C
1 = A1 + C

Then subtract 2 times the 1st equation from the 2nd:

0 = A1 + B
-2 = 4B
0 = 3A1 + 9B + 6C
1 = A1 + C

Now I know that B = -(1/2).

And from the 1st equation I can figure that A1 = +(1/2).

And from the 4th equation I can figure that C = +(1/2).

Final Result:

A1=1/2 A2=2 B=(1/2) C=1/2

And we can now write our partial fractions:

x2+15(x+3)2(x2+3) = 12(x+3) + 2(x+3)2 + x + 12(x2+3

Recurrence Relations
We talked about recursively-defined sequences earlier.
o Like the Fibonacci Sequence: f0=0, f1=1, fn=fn1+fn2.
o The last part of that, where the next term depends on
previous ones is called a recurrence relation.

That is, a recurrence relation for a sequence {an} is an equation


that expresses an in terms of earlier terms in the sequence.
o We can say that we have a solution to the recurrence
relation if we have a non-recursive way to express the terms.

o The initial conditions give the first term(s) of the sequence,


before the recurrence part can take over.

For example, we can take the sequence defined by the


recurrence relation an=an1+2, and a0=1.
o The terms of the sequence are 1,3,5,7,9,.

o A solution is an=2n1.

Another example: the Towers of Hanoi.


o A game where you have n disks of decreasing size stacked
on a peg. Your job is to move the tower to another peg.

o You can only move one disk at a time, and cannot put a
larger disk on a smaller one.

o For n=4, winning the game looks like this:

From Wikipedia's Tower_of_Hanoi_4.gif

o How many moves does it take to move n disks? Call this


number Hn.

To move n disks, we can win the game with a recursive


description of the algorithm. This moves n disks from the starting
peg from to the peg to, using the extra peg other.
procedure hanoi_move(n, from, to, other)
if n=0:
return
hanoi_move(n-1, from, other, to)
move one disk from the "from" peg to the "to" peg
hanoi_move(n-1, other, to, from)
o Now we can see that we have the recurrence
relation Hn=2Hn1+1. Our initial condition is H1=1.
o The values in the sequence are: 1, 3, 7, 15, 31, 63,
o We can get a solution with a little algebra:

Hn=2Hn1+1=4Hn2+2+1=8Hn3+4+2+1=2n1H1+2n2+2n3++
4+2+1=2n1+2n2+2n3++4+2+1=2n1.

o We could have also looked at the first terms of the


sequence, guessed Hn=2n1 and proved it by induction:

Base case: For n=1, it's true.


Induction case: Assume for induction that Hn1=2n11. Then,

Hn=2Hn1+1=2(2n11)+1=2n1.

Solving Recurrence Relations


It is often useful to have a solution to a recurrence relation.
o Faster calculation, better idea of growth rate, etc.

Some just aren't possible to write a nice formula for.


o e.g. the logistic map, xn+1=rxn(1xn).

o I don't know of any functions that have behaviour like this


as r increases:

From Wikimedia LogisticMap BifurcationDiagram.png


Some recurrence relations have easy to guess-and-prove
solutions, like the above.
But, a few theorems can help us some other types of recurrence
relations.
We will look at recurrence relations in this form:

an=c1an1+c2an2++ckank.

with each ci a real constant, and ck0.

o If you really want to call these something, they're linear


homogeneous recurrence relations of degree k.
o Obviously, we need to give the k first terms as initial
conditions.

For example, the Fibonacci numbers are such a


relation: fn=fn1+fn2.
For a linear homogeneous recurrence relation,

an=c1an1+c2an2++ckank,

Suppose we have a solution that the sequence is


geometric, an=rn for some r.

o That will be true if

rn=an=c1rn1+c2rn2++ck+1rn(k+1)+ckrnk.

o A little algebra gives us

rnc1rn1c2rn2ck+1rn
(k+1)ckrnk=0rkc1rk1c2rk2ck+1rck=0.

o The second line there is the characteristic equation of this


recurrence relation.
o Its solutions are the characteristic roots.

Theorem: Suppose we have a linear homogeneous recurrence


relation

an=c1an1+c2an2++ckank,

and its characteristic equation,

rkc1rk1c2rk2ck+1rck=0.
If the equation has k distinct roots, r1,r2,,rk, then {an} is a
solution to the recurrence relation if and only if

an=d1rn1+d2rn2++dkrnk,

for some constants d1,d2,,dk.

Example: What is the solution to the recurrence


relation an=2an1+3an2, with a0=3 and a1=5?

The characteristic equation for this recurrence


is 0=r22r3=(r3)(r+1), which has roots r1=3 and r2=1. Now we
have a solution in the form an=d13n+d2(1)n, for some d1 and d2.

We can find the constants from the initial values we know:

a0=d130+d2(1)0=d1+d2=3,a1=d131+d2(1)1=3d1d2=5.

Adding these equations, we get 4d1=8, so d1=2. And then from the
first equation, we have d2=1.

Finally, we have a solution: an=23n+(1)n.


o We can calculate the first few terms, either with the
recurrence or the solution: 3, 5, 19, 53, 163, 485.
o I got the same result both ways.

Example: We had a closed-form formula to calculate the


Fibonacci numbers earlier. This theorem will find
it: f0=0, f1=1, fn=fn1+fn2.

The characteristic equation is r2r1=0, which has


roots r1=1+52 and r2=152. Thus we have a solution in the form
fn=d1(1+52)n+d2(152)n.

Again, we can use the initial conditions to solve for the


constants:

f0f1=d1+d2=0,=d11+52+d2152=1.

Solving these equations, we get d1=1/5 and d2=1/5. Thus we


have
fn=15(1+52)n15(152)n.
Solving Non-Linear Relations
The above theorem is great if you have a homogeneous linear
recurrence.
o with distinct roots.

o And the related theorem in the text can handle


characteristic equations with repeated roots.

Suppose we have a recurrence relation in the form

an=c1an1+c2an2++ckank+F(n).

o Suppose we ignore the non-linear part and just look at the


homogeneous part:

hn=c1hn1+c2hn2++ckhnk.

o We can solve that and get a formula for {hn}.


o If we're lucky, we might be able to find a solution
for {an} for some initial conditions, but maybe not the ones
we're interested in

For example, we want to solve an=3an1+2n with a1=3.


o We can use the previous theorem (or just guess-and-prove)
that solutions to hn=3hn1 are in the form hn=d3n.

o If are willing to accept any initial conditions, we might be


able to get a particular solution {pn}.

o We could guess that there might be a linear solution in the


form pn=cn+d for some constants c,d.

o We would be right. There is such a solution iff for all n>1:

pncn+dcn+d0=3pn1+2n=3(c(n1)+d)
+2n=3cn3c+3d+2n=(2c+2)n+(2d3c).

This is true for all n iff 2c+2=0 and 2d3c=0. Solving these, we
get c=1 and d=3/2.

o We have a solution to the recurrence: pn=n3/2.


o That isn't actually a useful solution: we're interested
in a1=3 and that's not what p1 is. We have the wrong initial
conditions.

Theorem: For a recurrence relation in the form


an=c1an1+c2an2++ckank+F(n),

if we have a solution to the homogenous linear part {hn} (as


described above), and a particular solution {pn}, then all solutions
to the recurrence are in the form {hn+pn}.

Proof: Suppose we have any solution to the original


recurrence: {an}. We know that {pn} is also a solution:
an=c1an1+c2an2++ckank+F(n)pn=c1pn1+c2pn2++ckpnk+F(n)

Subtracting these, we get

anpn=c1(an1pn1)+c2(an2pn2)++ck(ankpnk)

Thus, {anpn} is a solution to the homogenous part, {hn}.

So, any solution {an} is can be written in the form {pn+hn} for
some solution to the homogenous recurrence.
Returning to our example, we wanted an=3an1+2n with a1=3 and
had hn=d3n and pn=n3/2.
o The above theorem tells us that all solutions to the
recurrence look like an=d3nn3/2.

o We just have to fill in d:

a1d3113/2d=3=3=11/6.

o We finally have an=11/63nn3/2.


Example: Find a solution for an=2an1+3n with a1=5.
o The homogeneous part of this is hn=2hn1. We could guess,
but let's use the theorem.

o The characteristic equation for this recurrence


is r2=0 which has solution r=2. Thus, solutions are in the
form hn=d2n.

o For a particular solution guess that pn=c3n for some c. We


can confirm the guess by finding a constant c. To do this, we
substituting into the recurrence:

pnc3nc(3n23n1)c(32)c=2pn1+3n=2(c3n1)+3n=3n=3n/3n1=3.

o We have a particular solution (for no particular initial


conditions) of pn=3n+1.
o Now we can satisfy an for our initial condition since all
solutions to {an} are in the form

an5=a15d=d2n+3n+1=d21+32=2d+3=2.

o So, an=2n+1+3n+1.
Example: One more. Find a solution
for an=5an1+6an2+7n with a0=1 and a1=6.
o The homogeneous part has characteristic
equation r25r6=0, so roots r1=6,r2=1.

o So, solutions are in the form hn=d16n+d2(1)n.

o For a particular solution, we should find something in the


form pn=c7n:

pn=c7nc720=5c7n1+6c7n2+7n=5c71+6c70+72=49/8.

o So we have pn=4987n.
o From the theorem,

an=d16n+d2(1)n+4987n.

o Substituting a0 and a1, we get

1=d1+d2+498 and 6=6d1d2+3438,d1=6 and d2=78.

o Thus we have a solution:

an=66n78(1)n+4987n=18(86n+17(1)n+7n+2).

o I definitely wouldn't have come up with that without the


theorem to help.

Divide-and-Conquer Relations
Many recursive algorithms are divide-and-conquer algorithms.
o That is, they split the problem into pieces (divide), and
recurse on those pieces to solve the problem (conquer).

The running time of these algorithms is fundamentally a


recurrence relation: it is the time taken to solve the sub-
problems, plus the time taken in the recursive step.
o Binary search: takes O(1) time in the recursive step, and
recurses on half the list. Its running time is f(n)=f(n/2)+1.
o Mergesort: takes O(n) time in the recursive step, and
recurses on both halves of the list. Its running time
is f(n)=2f(n/2)+n.

o We stated running times for these, but only vaguely


discussed why.

Theorem: (Master Theorem) For an increasing function f with the


recurrence relation

f(n)=af(n/b)+cnd,

with a1, b>1 an integer, c>0, d0, then

f(n) is O(nd)O(ndlogn)O(nlogba)if a<bd,if a=bd,if a>bd,

Example: For binary search, we have a=1,b=2,c=1,d=0. Here, a=bd,


so the algorithm is O(ndlogn)=O(logn).
Example: For mergesort, we have a=2,b=2,c=1,d=1. Again, a=bd, so
the algorithm is O(ndlogn)=O(nlogn).
Example: The text describes an algorithm to find the closest two
pair in a set of (x,y) points. The nave would be to take every pair
of points (C(n,2) of them), calculate the distance, and remember
the smallest. That is O(n2).

The algorithm starts by sorting the points by their x coordinate


(O(nlogn)). The problem is then split in half and solved recursively.
Finding the final solution from that takes 7ncomparisons. That
gives a recurrence relation of f(n)=2f(n/2)+7n.
Now, a=2,b=2,c=7,d=1. Again, a=bd, so the algorithm
is O(ndlogn)=O(nlogn).

Example: Suppose we write a recursive algorithm to find the


smallest element in a list
procedure smallest(list, start, end)
if start end-1
return list[start]
else
mid = (start+end)/2
small1 = smallest(list, start, mid)
small2 = smallest(list, mid+1, end)
return smallest of small1 and small2
Now we have a=2,b=2,c=2,d=0 and a>bd. Thus the running time
is O(nlog22)=O(n).
That is, the divide-and-conquer strategy didn't gain us anything
over the obvious left-to-right version of this algorithm.

Example: Suppose you come up with an algorithm with this


general format:
procedure example(n)
if n 1
return
else
a = example(n/2)
b = example(n/2)
combine a and b using n 2 steps
The running time of this algorithm is f(n)=2f(n/2)+n2.
For the Master Theorem, we have a=2,b=2,c=1,d=2 and a<bd. Thus
the running time is O(n2).
So for large-enough d, the recursion work doesn't matter and the
recursive-step work dominates.

Вам также может понравиться