Total Number of Paths from (1,1) to the given point in a (N*M) Grid

Revision en2, by puneet.tiwari9039, 2020-05-21 08:39:28

We often need to find total number of path from one point to another in a N*M grid. There are different solution for this,like naive approach or using formula but we can not use formula every time... So here we discuss the Dynamic Programming approach to solve the problem...

Let's say we have a grid of Matrix of size N*M..

         ______________________
         |   |    |   |   |   |
         |___|____|___|___|___|
         |   |     |   |  |   |   
         |___|____|___|___|___|
         |   |     |   |   |   |
         |___|____|___|___|___|

Now we start form the upper left Corner and first we traverse only on the first row and put 1 in there as we know there is only one path from (1,1) to any point in the first Row...

Similarly we traverse on the second column and put 1 in there as there is only one path from (1,1) to any point in the first column..

Now the fun begins:)

Let's say we are in the secind row and second column. Ni=ow if we see there are two possible way to come at 
    this place,
either from the top or from the left to the current block i.e. at (2,2) = (1,2) and (2,1)...  we can reach 
    (2,2) from (1,2) and (2,1)...
So the total number of path to (2,2) is the sum of total number of path to (1,2) and total number of path 
    to (2,1)...

So dp[2][2] = dp[2][1] + dp[1][2]

So total number of ways to reach a point (i,j) is given by the formula...

          **dp[i][j] = dp[i][j-1] + dp[i-1][j]**
                    ====================================



                So at last our dp table will look like this...

         ___________________________
         |    |     |    |    |    |
         |__1_|__1__|__1_|__1_|_1__|
         |    |      |  |    |     |     
         |_1__|__2__|_3_ |__4_|_5__|
         |    |      |  |    |     |     
         |__1_|__3__|_6__|_10_|_15_|

So to reach to (3,5) from (1,1),there are total 15 ways to reach  that point...
It is highly recommend to run the given program and use pen and paper to see how it is working... So in 
    that at way you will never forgot to implement in the programming contest...
#include <bits/stdc++.h>
	using namespace std;

	int main()
	{
		int n,m;
		cin>>n>>m;                     // size of the grid

		int dp[n+1][m+1];               // I will consider indexing from 1 

		for(int i=1;i<=m;i++)
			dp[1][i] = 1;              // we will set 1 in the first row as discussed earlier..

		for(int i=1;i<=n;i++)
			dp[i][1] = 1;              // We will set 1 in the first column as discussed

		for(int i=2;i<=n;i++)
		{
			for(int j=2;j<=m;j++)
			{
				dp[i][j] = dp[i][j-1] + dp[i-1][j];
			}
		}


	}

The time complexity of these approach is O(N*M) which is far better than the naive approach...
The space complexity is O(N*M)...

So that's all from my side... If you have any query just write it down in comments...I will be more than happy to answer your Questions...

Tags #dynamic programing, #dynamic_programming, #matrix, #dp, #travel_on_matrix, #path ahead

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English puneet.tiwari9039 2020-05-21 08:39:28 2 Tiny change: 'nt j=2;j<=n;j++)\n ' -> 'nt j=2;j<=m;j++)\n '
en1 English puneet.tiwari9039 2020-05-21 08:37:41 3019 Initial revision (published)