In this post we will discuss Difference Equations.

One could say difference equations are the discrete analogue of differential equations.

Indeed the techniques used to solve them very much resembles the approach one takes for solving ordinary differential equations (ODEs).

An example of a difference equation is the Fibonacci sequence which we will learn how to solve later.

Difference equations are often not as extensively treated in undergraduate education as their continuous counterpart.

They are nevertheless important in all sorts of disciplines.

In probability theory difference equations (or “recurrence relations”) often arise when describing probabilistic transitions between a number of discrete states. In science and economy one is often interested in discretizing a differential equation into a difference equation so one can solve it numerically in a computer. There is a whole research area devoted just to approximating solutions to differential equations in this way so I am not going to delve much deeper into this.

Instead we will look at how to solve certain types of difference equations exactly and apply the theory to some nice puzzles, like Tower of Hanoi or finding out the probability of a mouse surviving in a filthy student corridor with a cat (a metaphor for finding the absorption probability of a discrete Markov Chain).

Let’s start off with some theory for the simplest kind of difference equations:

**First Order Difference Equations:**

The general form of a first order difference equation with constant coefficients is:

where are a constants

To solve this equation we may first consider what happens in the case where , the so called **homogeneous** case.

Unwinding the equation iteratively we get .

This has the general form for some constant .

To find the general solution when this **complementary solution** of the homogeneous equation needs to be translated in some direction. To understand how this should be done we need to find a **particular solution**.

We could try the simplest possible solution, namely for some constant .

Plugging into (*) we get , which is well defined as long as

If we may instead try which after insertion into (*) yields

.

The general solution to (*) thus becomes

We can indeed verify this solution is valid since in the former case (say) we have:

.

Notice also the resemblance to ordinary differential equations where we also look for homogeneous and particular solutions in a similar way.

Moreover if we have an **initial condition** then we can determine the general coefficient .

Let’s move on to an application:

**Problem 1 (Tower of Hanoi):**

The Tower of Hanoi is a puzzle that consists of three vertical poles and number of discs in different size.

Each disc has a hole in the middle so it can be moved between the poles.

Initially the discs are situated on the middle pole, ordered by size, with the largest disc at the bottom.

The discs are only allowed to be moved in such a way that a larger disc never sits on top of a smaller disc. Other than that discs are allowed to be moved freely between the poles.

The puzzle is solved when every disc either sits all on the left pole or all on the right pole.

What is the minimum number of moves required to solve the puzzle?

**Solution:**

Let denote the minimum number of moves required to solve the puzzle.

Clearly .

What about the general case?

In order to move the bottom disc to an empty pole we must first move the remaining discs to a different empty pole. By definition this requires moves.

Once the discs have been moved aside and the bottom disc moved to its finial location, we need to move back the discs on top of the bottom disc to solve the puzzle. This requires another moves.

In total we therefore need to use moves.

This describes a first order difference equation.

According to our earlier discussion it has the general solution .

Our initial condition yields .

Hence the minimum number of moves required to solve the puzzle is .

According to the legend the world will end once monks, fulfilling an ancient prophecy, have finished solving the puzzle for .

According to our analysis the minimum number of moves required is .

Even if they carry out one move per second it will take them approximately years to complete, so I think we are safe for a while longer.

In order to look at remaining applications we need to go over some theory of second order difference equations:

**Second Order Difference Equations:**

A Second Order Difference Equation with constant coefficients have general form:

for where are given and are constants

Here too the general solution depends on finding complementary and particular solutions.

Let’s first consider the homogeneous case where .

With understanding of first order difference equations we might believe a solution of the form can work here too. Lets see how that works out if we assume :

.

Excellent! So if we choose to be a root of the **auxiliary equation** we get that is a solution to for any constant .

Ignoring the case where the auxiliary equation has no real roots we have two cases:

**i)** has two distinct real roots .

You may easily verify that

is a solution to the homogeneous equation for arbitrary constants .

**ii)** has a repeated real root .

You may easily verify that

is a solution to the homogeneous equation for arbitrary constants .

A particular solution may be found in similar fashion to how they were found for first order difference equations, by trying something simple like or .

Complementary and particular solutions are finally added together for a general solution to (*).

The constants can be determined by plugging in values for and solving the resulting equation system in two unknowns .

Again this bears striking resemblance to solutions of second order ordinary differential equations.

A nice direct application of this is that we may compute a closed form expression for the Fibonacci number.

**Problem 2 (Fibonacci Number Formula):**

The Fibonacci sequence is given by .

Solve this difference equation to get a closed formula for the Fibonacci number.

**Solution:**

The Fibonacci recurrence is a homogeneous second order difference equation.

The auxiliary equation is given by:

.

The roots of the quadratic equation are given by

(Golden Ratio) and

.

This yields the general solution .

Since we can determine via the equation system

Solving for we get and .

Hence where .

It is indeed not obvious that this expression needs to be an integer.

Let’s now turn our attention to a probabilistic application:

**Problem 3 (Cat And Mouse):**

A mouse finds its way to a filthy student corridor with rooms as depicted below.

The mouse is small enough to creep through the tiny gap under each door of the corridor.

It initially starts in room and moves every minute to an adjacent room with equal probability (out of fear for an equally afraid student).

Unfortunately for the mouse there is a cat owner in room .

If the mouse enters room it will get instantly killed by the eager cat.

If on the other hand, the mouse reaches past the last room (room ) it will escape out the corridor and survive.

* (i) What is the probability that the mouse will survive?*

**(ii)** How long is it expected to take before we get a resolution to the mouse’s fate?

**Solution:**

**(i)**

Let denote the probability that the mouse will survive given that it is currently in room .

Since the mouse is equally likely to go left or right we get the following homogeneous second order difference equation:

.

Since the mouse is guaranteed to die if it reaches room we get .

Similarly since it will escape if it reaches the end on the opposite side of the corridor we get .

We can now solve this difference equation using the methods we have been discussing.

The auxiliary equation is given by:

.

This time we have a repeated root so the general solution takes the form

.

The initial condition implies .

The condition implies .

Hence .

**(ii)**

To compute the expected duration of the mouse’s activity in the corridor we may begin by introducing some notation.

Let denote the expected duration until death or escape given that the mouse is currently in room .

Since the mouse stays in each room for one minute before switching to an adjacent room (with equal probability) we obtain the non-homogeneous second order difference equation:

.

Moreover since activity ends at either end state.

We have already seen that the homogeneous equation has general solution .

We now need to find a particular solution.

To spare you the trial and error computation, it turns out that both and lead to degenerate solutions, meaning we cannot determine the constant .

The obvious next candidate to try is where is a constant to be determined.

Inserting yields

Therefore the general solution is given by

The initial condition yields .

The second condition gives

Hence we expect the mouse to run around in the corridor for minutes before either getting killed by the cat or running out of the corridor, provided that the mouse starts in room .

Those of you who have studied elementary probability might have recognized already that the problem above was simply a restatement of the Gamblers Ruin Paradox. Instead of a mouse and a cat we have two opponents each holding and dollars respectively. The opponents play a fair game (say they flip a fair coin) and if one opponent loses, he has to give away one dollar to the other opponent. They play the game until one of them holds all dollars. The question is what the probability is of the first opponent losing all his money (i.e the mouse getting eaten by the cat).

The seeming “paradox” here being despite playing a fair game the first player will still lose all his money if he plays a very rich opponent, say a casino. Indeed if we assume for arguments sake that the casino has infinite amount of money, then the probability that the first player loses all his money is i.e the game will (almost surely) not continue forever, despite even chance of winning or losing, no matter what finite amount of chips the first player starts with.

Below are some neat exercises for you to try.

They can all be solved by identifying a difference equation and solving it.

**Exercise 1 (Stair Climbing – Easy):**

In how many different ways can you climb a stair of steps by taking either one or two steps at a time?

For example you can climb a stair of steps in any of the following 5 ways:

(Hint: Do you recognize the difference equation from somewhere?)

**Exercise 2 (Dividing A Pizza – Intermediate):**

What is the maximum number of pieces you can get if you divide a pizza using straight cuts?

Note: pieces need not be of the same size, nor contain any crust

(Hint: What happens if you make a cut crossing all previous cuts, yet not crossing any of the existing intersection points?)

Pingback: 7th CPC-THE REAL TRUTH BEHIND THE DEMAND

Pingback: Puzzles With Handicap | Mathematical Mélange

Pingback: Dynamic Programming | Mathematical Mélange