Aug 26, 2013

Number of Paths in Rectangular Grid

Consider a n x m rectangular grid. The problem is to find the number of shortest (or monotonic in this case) paths along the edges of the cell that start at (0,0) and end at (n,m).

A monotonic path is a path always moving towards the goal, i.e. consists of moving up or right but not down or left. Figure 1 illustrates such a path for a grid of size 5 x 3.

2. (Difficult)

Now we need to print all monotonic paths that do not go above the diagonal y = x. Figure 2 shows such a path. Note that the path in figure 1 goes above the diagonal, hence not desirable in this case. How many such paths exist for n x m grid (n >= m)?

Disclaimer:

The first problem is too simple, and the second is very challenging. Have fun :-)

Let W(m,n) be the total number of ways of going from point (0,0) to (m,n) monotonically.

Solution to part 1

Let S(0,n) = 1. (for all n)

Then S(l,n) = Summation from k=1 to k=n (S(l-1,k)).

ie. S(1,n) = n.

S(2,n) = n(n+1)/2.

and so on..

Then W(m,n) = Summation from l=0 to l=m (S(l,n)).

Solution to part 2

If W'(m,n) be total no. of ways then

W'(m,n) = W(m,n) - no. of paths which lie above the diagonal - no. of paths which cut the diagonal at (1,1) while going towards north - no. of paths which cut the diagonal at (2,2) while going towards north - and so on.

Let W(m,n) be the total number of ways of going from point (0,0) to (m,n) monotonically.

Solution to part 1

Let S(0,n) = 1. (for all n)

Then S(l,n) = Summation from k=1 to k=n (S(l-1,k)).

ie. S(1,n) = n.

S(2,n) = n(n+1)/2.

and so on..

Then W(m,n) = Summation from l=0 to l=m (S(l,n)).

Solution to part 2

If W'(m,n) be total no. of ways in which the path doesn't go above the diagonal, then

W'(m,n) = W(m,n) - no. of paths which starts above the diagonal - no. of paths which cut the diagonal at (1,1) while going towards north - no. of paths which cut the diagonal at (2,2) while going towards north - and so on.

I guess the answer to the 2nd part is 47.Let i denote the row number and j denote the column numberLet f(i, j) denote the count of possible number of options to go in the (i+1)th row from the point (i,j) after MANDATORY 1st STEP IN THE NORTH. So,

Correct Options = f(0,0) + f(0,1) + f(0,2) + f(0,3) + f(0,4) + f(0,5)

Now, f(0,0) = 0 since we can't go north on the first step.Now, we can go to points (1,1), (1,2)...(1,5) from (1,1), but not on (0,0) because then we have to take a step back.So,f(0,1) = f(1,1) + f(1,2) + f(1,3) + f(1,4) + f(1,5)Similarly,f(0,2) = f(1,2) + f(1,3) + f(1,4) + f(1,5)f(0,3) = f(1,3) + f(1,4) + f(1,5)f(0,4) = f(1,4) + f(1,5)f(0,5) = f(1,5)

Now f(2,2) = 0And the points (2,3), (2,4) and (2,5) can be treated as normal points since there is no restriction on any three of them as y=x line doesn't interfere for any of these points now.So, f(2,3) = 3C1 = 3f(2,4) = 2C1 = 2f(2,5) = 1C1 = 1

To reach the destination let us consider our journey along y direction. since it is a n*m field we could divide the y journey into a max of n+1 different steps y1,y2,y3...and proceed as follows:

Answer to the first part is number of solutions to the equation y1+y2+y3.....yn+1=m with the condition that yi>=0. It is the coefficient x^m in (1+x+x^2....x^m)^n+1 which is (n+m)Cm. in part 2: n>mfor the second part there are extra constraints on y1 to ym such that y1<=0 y2<=1 y3<=2.....ym<=m-1.number of ways is the coefficent of x^m in 1*(1+x)*(1+x+x^2)....*(1+x+x^2+....x^(m-1))*((1+x+x^2..x^m)^(n-m+1)). I am unable to figure out how to evaluate the coefficient in the above expression by using binomial expansions( infinite series formulae) .

Second part is tricky. Use principal of Reflection to solve this. Ans : (m+n)Cm - (m+n)C(m-1)Solution :Use following graph to visualise this. On the Xnew = X+Y, and Ynew as X-Y.Every step in new axis is 45 degree upward or 45 degree downward. And constraint will be that no point should touch Y=-1 line. Aim is to find #paths from (0,0) to (n+m, n-m).Take the reflection of (0,0) in Y=-1 line i.e. (0,-2).So, #path which touches Y=-1 line will be equivalent to #paths from (0,-2) to (n+m, n-m) (Principle of Reflection)and that will be (m+n)C(m-1).

Assumption made in my previous post was that we can allow points on the diagonal but not above that. If we don't even allow points on diagonal then we can start from (1,1) (in new axis) to get total #paths and from (1,-1) to wrong paths.(i.e. taking reflection along line Y=0). So, solution in this case will be (n+m-1)Cm - (n+m-1)C(m-1).

Consider any path from (0,0) to (m,m) which does not go above diagonal. If we add (n-m) horizontal step to this path it will still gives us a path from (0,0) to (n,m) which does not go above diagonal. Conversely, any such path can be represented as a path from (0,0) to (m,m) with (n-m) horizontal steps added to it. Now number of ways in which you can add (n-m) horizontal steps to a path having m horizontal steps and m vertical steps is nCm. We also know that paths from (0,0) to (m,m) which does not go above diagonal are given by well known Catalan number C_m. So total number of paths = nCm * C_m. So I think the answer should be: (nCm)*(2mCm) /(m+1)

Explanations for 2: flip every step after the first "up" arrow that goes across the diagonal, you will get a bijection to another problem where you will have a 6*2 board, whose solution is C(8,2). Subtract them out is the solution to the original problem.

We don't want to cross diagonal (0,0) to (3,3) here. In this case we count the faulty cases, where we are crossing that diagonal and remove this from total ((n+m)Cm) to get required answer.

to get faulty cases , we can observe whenever we cross (0,0)->(3,3) diagonal , path will touch or cross diagonal (-1,0)->(2,3).Now take a part of path ((0,0) to first cut on the diagonal(-1,0)->(2,3)) and reflect it with respect to diagonal (-1,0)->(2,3) . by doing all this, we can observe point (0,0) will map to (-1,-1) and each path from (0,0) to (5,3) will have a corresponding path from (-1,-1) to (5,3)

so faulty cases = number of paths from (-1,-1) to (5,3) is 10C4 (ANSWER)

We don't want to cross diagonal (0,0) to (3,3) here. In this case we count the faulty cases, where we are crossing that diagonal and remove this from total ((n+m)Cm) to get required answer.

to get faulty cases , we can observe whenever we cross (0,0)->(3,3) diagonal , path will touch or cross diagonal (-1,0)->(2,3).Now take a part of path ((0,0) to first cut on the diagonal(-1,0)->(2,3)) and reflect it with respect to diagonal (-1,0)->(2,3) . by doing all this, we can observe point (0,0) will map to (-1,1) and each path from (0,0) to (5,3) will have a corresponding path from (-1,1) to (5,3)

so faulty cases = number of paths from (-1,1) to (5,3) is 8C2 ANSWER = total - faulty = 8C3 - 8C2

By this approach you can find number of paths even for any kind of restricting path like same problem with barrier (0,0) -> (2,2) instead of (0,0) -> (3,3)

Amazon.com Mathematics Bestsellers

Buy me Coffee / Beer via Paypal/BTC

Pages

Search This Blog

Follow by Email!

About Me

I am an early stage technology investor at Nexus Venture Partners. Prior to this, I was a 3x product entrepreneur. Prior to this, I worked as a private equity analyst at Blackstone and as a quant analyst at Morgan Stanley. I graduated from Department of Computer Science and Engineering of IIT Bombay. I enjoy Economics, Dramatics, Mathematics, Computer Science and Business.

Disclaimer

This is a personal blog. Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity. Any views or opinions are not intended to malign any religion, ethnic group, club, organization, company, or individual.

All content provided on this blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.