Discrete Mathematics and Combinatorics

Counting Lattice Paths and the Ballot Problem

Count routes on a grid, handle blocked points, and use a mirror trick to find the chance a winning candidate leads all night.

Key takeaways

  • East and north paths from (0, 0) to (m, n) number C(m + n, m), because a path is just a word in the letters E and N.
  • Paths through a point are counted by multiplying, and paths avoiding it by subtracting from the total.
  • The reflection principle counts paths that touch a forbidden line by matching them with unrestricted paths from a mirrored start.
  • In a count with a votes against b, where a > b, the winner leads throughout with probability (a − b)/(a + b).

A lattice path is a walk on a grid of points with integer coordinates, taking steps from an allowed list. The number of paths from (0, 0) to (m, n) using only steps one unit east or one unit north is C(m + n, m), the number of ways to choose which m of the m + n steps go east.

The same picture solves the ballot problem. If candidate A receives a votes and candidate B receives b votes, with a > b, and the ballots are counted in random order, the chance that A is strictly ahead throughout is (a − b)/(a + b).

Paths as words

Imagine a city grid where you must walk 4 blocks east and 3 north, never backtracking. Every route is a string of 7 letters with four E's and three N's, such as EENENEN. Choosing the positions of the E's gives

C(7, 4) = 35 routes

Here C(a, b) = a!/(b!(a − b)!) counts the ways to choose b items from a. If you can walk in three dimensions, with i, j and k steps in the three directions, the count becomes (i + j + k)!/(i! j! k!).

Adding along the grid

There is another way to get the same numbers. A route can arrive at a point only from the west or from the south. So the number of routes to a point is the sum of the numbers at those two neighbors.

Write 1 at every point along the bottom edge and the left edge, then fill in by addition:

y=3:   1    4   10   20   35
y=2:   1    3    6   10   15
y=1:   1    2    3    4    5
y=0:   1    1    1    1    1
      x=0  x=1  x=2  x=3  x=4

This is Pascal's triangle tilted onto the grid. The addition method has one big advantage: it still works when some streets are closed. Put 0 at a blocked point and carry on.

Passing through and avoiding

How many of the 35 routes pass through the point (2, 1)? A route splits into two independent legs, so multiply:

(0,0) to (2,1):  C(3, 1) = 3
(2,1) to (4,3):  C(4, 2) = 6
through (2,1):   3 × 6   = 18
avoiding (2,1):  35 − 18 = 17

With several forbidden points, the inclusion-exclusion principle keeps the bookkeeping straight, since a route may hit more than one of them.

With (2, 1) closed, the table becomes:

y=3:   1    4    7   11   17
y=2:   1    3    3    4    6
y=1:   1    2    0    1    2
y=0:   1    1    1    1    1

The corner reads 17, the same as the subtraction. Computers use this style of filling in a table, called dynamic programming, for problems with thousands of irregular obstacles where no formula exists.

Identities you can see

Sorting a set of paths in different ways proves identities without algebra.

Take the 35 routes to (4, 3) and sort them by the height at which the last eastward step is taken. Before that step the walker travels from (0, 0) to (3, h), and afterward goes straight north. So

C(3,0) + C(4,1) + C(5,2) + C(6,3) = 1 + 4 + 10 + 20 = 35

This is often called the hockey stick identity, after the shape it makes in Pascal's triangle.

Now take routes from (0, 0) to (n, n) and sort them by where they cross the diagonal line x + y = n. A route crossing at (k, n − k) has C(n, k) ways to get there and C(n, k) ways to finish. Therefore the sum of C(n, k)² over all k equals C(2n, n). For n = 3 it reads 1 + 9 + 9 + 1 = 20.

The ballot problem

Say A gets 3 votes and B gets 2, and the 5 ballots are opened one at a time in random order. There are C(5, 2) = 10 possible orders. In how many is A strictly ahead after every ballot? Only two: AAABB and AABAB. The probability is 2/10 = 1/5, and (3 − 2)/(3 + 2) agrees.

Draw each A vote as a step up and each B vote as a step down. The height of the path is A's lead. The question becomes how many paths stay strictly above zero after the start.

The problem is associated with Joseph Bertrand, who published it in 1887. William Allen Whitworth had reached an equivalent result some years earlier.

The reflection principle

Count the bad orders, where A is not ahead at some moment after the count begins.

First, every order that starts with B is bad. There are C(a + b − 1, a) of them, since all a of the A votes lie in the remaining a + b − 1 places.

Second, take a bad order that starts with A. The lead must return to zero at some point. Find the first such moment and swap A with B in every ballot up to and including it. The result starts with B. Because the first segment had equal numbers of each, the totals are unchanged, and the swap can be undone.

So the two kinds of bad order are equally numerous.

good = C(a+b, a) − 2 × C(a+b−1, a)
     = C(a+b, a) × (1 − 2b/(a+b))
     = C(a+b, a) × (a − b)/(a + b)
The reflection argument for the ballot problem
1 Draw the count A vote goes up, B vote goes down 2 Bad: starts with B Count these directly 3 Bad: starts with A Mirror up to the first tie 4 Subtract both What remains stays ahead Draw the count A vote goes up, B vote goes down 1 Bad: starts with B Count these directly 2 Bad: starts with A Mirror up to the first tie 3 Subtract both What remains stays ahead 4

When ties are allowed

Now ask only that A never fall behind. A similar reflection, this time across the level one below zero, shows that bad orders number C(a + b, a + 1). For a ≥ b the good orders number

C(a+b, a) − C(a+b, a+1) = C(a+b, a) × (a − b + 1)/(a + 1)

With a = 3 and b = 2 that is 10 × 2/4 = 5 orders.

When a = b = n, the count becomes C(2n, n)/(n + 1), the n-th Catalan number. For n = 3 there are 5 orders in which the lead never goes negative and ends level.

A second proof by rotation

There is a neat alternative for the strict version. Write the a + b ballots around a circle. Reading from each of the a + b starting positions gives a + b linear orders.

The cycle lemma states that, when a > b, exactly a − b of those starting positions give an order in which A is always strictly ahead. One way to see it is to repeatedly delete an adjacent pair reading "A then B" around the circle. No good starting point is ever lost or gained this way, and neither letter of the pair can be one. When all B's are gone, a − b letters A remain, and each is a good start.

Averaging over circles gives the probability (a − b)/(a + b) immediately.

Random walks and other steps

If each step goes up or down by the toss of a fair coin, the lattice path is a random walk. After 10 tosses there are 2¹⁰ = 1,024 equally likely paths, and C(10, 5) = 252 of them end at height zero.

The reflection principle also counts walks that reach a given height at some time, which matters in problems about a gambler's fortune or a fluctuating tally. A surprising consequence is that in a long fair game, one side often stays ahead for most of the duration. See Markov chains for the broader theory of such processes.

Other step sets give other sequences. Allow diagonal steps in addition to east and north, and the counts are called Delannoy numbers. From (0, 0) to (2, 2) there are 13 such paths: 6 with no diagonal, 6 with one and 1 with two.

Paths between any two points

Nothing depends on starting at the origin. From (a, b) to (c, d), with c ≥ a and d ≥ b, you need c − a eastward steps and d − b northward steps, so the count is C((c − a) + (d − b), c − a). If either difference is negative, there are no such paths.

This shift lets you chain several required stops. A route from (0, 0) to (6, 4) that visits (2, 1) and then (3, 3) has 3 × 3 × 4 = 36 possibilities, the three factors being C(3, 1), C(3, 1) and C(4, 1).

Problems to try

  1. Count east and north routes from (0, 0) to (5, 5). The answer is C(10, 5) = 252.
  2. How many of them pass through (2, 3)? Multiply C(5, 2) = 10 by C(5, 3) = 10 to get 100.
  3. A gets 6 votes and B gets 2. The chance that A leads throughout is 4/8 = 1/2. Of the C(8, 2) = 28 orders, 14 qualify.
  4. A line of 8 people each buy a 5 dollar ticket, 4 paying with a 5 dollar bill and 4 with a 10 dollar bill, and the seller starts with no change. In how many patterns of bill types can everyone be served? This is the "never behind" count with a = b = 4, which is 70/5 = 14.

For the last problem, treat each 5 as a step up and each 10 as a step down. The seller fails exactly when the path first dips below zero.

Common mistakes

  • Using C(m + n, m) with the number of grid points instead of the number of steps. A grid 4 blocks wide has 5 vertical streets but needs 4 eastward steps.
  • Forgetting that the ballot formula assumes every order of the ballots is equally likely. Real election reporting is often far from random, so the formula says nothing about actual election nights.
  • Confusing "strictly ahead" with "never behind". Their formulas differ, and so do the answers: 1/5 against 1/2 in the 3 to 2 example.
  • Reflecting the whole path instead of only the portion up to the first touch.

When a new counting problem has a running total that must stay on one side of a limit, drawing it as a path is usually the quickest way in.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic