Total number of decreasing paths in a matrix
Given a matrix of size N X N of integers. The task is to find the number of decreasing path in the matrix. You are allowed to start from any cell and from the cell (i, j), you are allowed to move to (i + 1, j), (i – 1, j), (i, j + 1) and (i, j – 1) cell.
Examples:
Input : m[][] = { { 1, 2 },
{ 1, 3 } }
Output : 8
Explanation : Decreasing paths are { 1 }, { 1 }, { 2 }, { 3 },
{ 2, 1 }, { 3, 1 }, { 3, 2 }, { 3, 2, 1 }
Input : m[][] = { { 1, 2, 3 },
{ 1, 3, 4 },
{ 1, 5, 6 } }
Output : 41
The idea to solve this problem is to use Dynamic Programming. Declare a dp[][] array, where dp[i][j] stores the number of decreasing path that can be formed from cell (i, j). So, we will define a recursive function to evaluate the number of decreasing paths with parameters, say i, j, the row number and column number of the current cell. Make every possible move from the cell(i,j) and keep a count of the total number of paths.
First, we will check in the function that the number of decreasing paths for input position (i, j) is already calculated or not. If yes, return the value dp[i][j] else find the number of decreasing sequence is allowed four directions and return the value. Meanwhile, we will also store the number of decreasing for intermediate cells. Since DP[i][j] stores the number of decreasing paths for every cell, so the summation of all the cells of DP[][] will answer to count of decreasing paths in the complete matrix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
int CountDecreasingPathsCell( int mat[MAX][MAX], int dp[MAX][MAX],
int n, int x, int y)
{
if (dp[x][y] != -1)
return dp[x][y];
int delta[4][2] = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } };
int newx, newy;
int ans = 1;
for ( int i = 0; i < 4; i++) {
newx = x + delta[i][0];
newy = y + delta[i][1];
if (newx >= 0 && newx < n && newy >= 0
&& newy < n && mat[newx][newy] < mat[x][y]) {
ans += CountDecreasingPathsCell(mat, dp, n, newx, newy);
}
}
return dp[x][y] = ans;
}
int countDecreasingPathsMatrix( int n,
int mat[MAX][MAX])
{
int dp[MAX][MAX];
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
dp[i][j] = -1;
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
sum += CountDecreasingPathsCell(mat, dp, n, i, j);
return sum;
}
int main()
{
int n = 2;
int mat[MAX][MAX] = { { 1, 2 }, { 1, 3 } };
cout << countDecreasingPathsMatrix(n, mat)
<< endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static Scanner scn =
new Scanner(System.in);
public static int CountDecreasingPathsCell( int mat[][], int dp[][],
int n, int x, int y)
{
if (dp[x][y] != - 1 )
return dp[x][y];
int delta[][] = { { 0 , 1 }, { 1 , 0 },
{ - 1 , 0 }, { 0 , - 1 }};
int newx, newy;
int ans = 1 ;
for ( int i = 0 ; i < 4 ; i++)
{
newx = x + delta[i][ 0 ];
newy = y + delta[i][ 1 ];
if (newx >= 0 && newx < n && newy >= 0 &&
newy < n && mat[newx][newy] < mat[x][y])
{
ans += CountDecreasingPathsCell(mat, dp, n,
newx, newy);
}
}
return dp[x][y] = ans;
}
public static int countDecreasingPathsMatrix( int n,
int mat[][])
{
int dp[][] = new int [n][n];
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n; j++)
dp[i][j] = - 1 ;
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n; j++)
sum += CountDecreasingPathsCell(mat, dp,
n, i, j);
return sum;
}
public static void main(String[] args)
{
int n = 2 ;
int mat[][]= {{ 1 , 2 },
{ 1 , 3 }};
System.out.println(countDecreasingPathsMatrix(n, mat));
}
}
|
Python3
MAX = 100
def CountDecreasingPathsCell(mat, dp, n, x, y):
if (dp[x][y] ! = - 1 ):
return dp[x][y]
delta = [[ 0 , 1 ], [ 1 , 0 ],
[ - 1 , 0 ], [ 0 , - 1 ]]
newx, newy = 0 , 0
ans = 1
for i in range ( 4 ):
newx = x + delta[i][ 0 ]
newy = y + delta[i][ 1 ]
if (newx > = 0 and newx < n and newy > = 0 and
newy < n and mat[newx][newy] < mat[x][y]):
ans + = CountDecreasingPathsCell(mat, dp, n,
newx, newy)
dp[x][y] = ans
return dp[x][y]
def countDecreasingPathsMatrix(n,mat):
dp = []
for i in range (n):
l = []
for j in range (n):
l.append( - 1 )
dp.append(l)
sum = 0
for i in range (n):
for j in range (n):
sum + = CountDecreasingPathsCell(mat, dp,
n, i, j)
return sum
n = 2
mat = [[ 1 , 2 ], [ 1 , 3 ]]
print (countDecreasingPathsMatrix(n, mat))
|
C#
using System;
class GFG
{
public static int CountDecreasingPathsCell( int [,] mat, int [,] dp,
int n, int x, int y)
{
if (dp[x, y] != -1)
return dp[x, y];
int [,] delta = {{0, 1}, {1, 0},
{-1, 0},{0, -1}};
int newx, newy;
int ans = 1;
for ( int i = 0; i < 4; i++)
{
newx = x + delta[i,0];
newy = y + delta[i,1];
if (newx >= 0 && newx < n &&
newy >= 0 && newy < n &&
mat[newx,newy] < mat[x,y])
{
ans += CountDecreasingPathsCell(mat, dp, n,
newx, newy);
}
}
return dp[x,y] = ans;
}
public static int countDecreasingPathsMatrix( int n,
int [,] mat)
{
int [,] dp = new int [n, n];
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
dp[i, j] = -1;
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
sum += CountDecreasingPathsCell(mat, dp,
n, i, j);
return sum;
}
static public void Main ()
{
int n = 2;
int [,] mat= {{1, 2},
{1, 3}};
Console.WriteLine(countDecreasingPathsMatrix(n, mat));
}
}
|
Javascript
<script>
var MAX = 100
function CountDecreasingPathsCell(mat, dp, n, x, y)
{
if (dp[x][y] != -1)
return dp[x][y];
var delta = [ [ 0, 1 ], [ 1, 0 ], [ -1, 0 ],
[ 0, -1 ] ];
var newx, newy;
var ans = 1;
for ( var i = 0; i < 4; i++) {
newx = x + delta[i][0];
newy = y + delta[i][1];
if (newx >= 0 && newx < n && newy >= 0
&& newy < n && mat[newx][newy] < mat[x][y])
{
ans += CountDecreasingPathsCell
(mat, dp, n, newx, newy);
}
}
dp[x][y] = ans;
return ans;
}
function countDecreasingPathsMatrix(n, mat)
{
var dp = Array.from(Array(MAX),
()=> Array(MAX));
for ( var i = 0; i < n; i++)
for ( var j = 0; j < n; j++)
dp[i][j] = -1;
var sum = 0;
for ( var i = 0; i < n; i++)
for ( var j = 0; j < n; j++)
sum += CountDecreasingPathsCell
(mat, dp, n, i, j);
return sum;
}
var n = 2;
var mat = [ [ 1, 2 ], [ 1, 3 ] ];
document.write( countDecreasingPathsMatrix(n, mat));
</script>
|
Complexity Analysis:
- Time Complexity : O(N2)
- Auxiliary Space : O(N2)
Last Updated :
09 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...