Number of palindromic paths in a matrix


Palindromic pathways are very useful in solving various problems involving patterns and sequences. It can be used in finding correct path in a maze without reversing, palindromes in a sequence of letters, etc., It can also be used to identify symmetric patterns and structures. In this article, we will discuss about palindromic paths and ways to find such paths in a matrix using C++.

Palindromes are series of letters, numbers or characters which have same starting and ending point. Also, they are same when read from left to right and right to left. A path in a matrix is a series of cells which starts from the first cell (1, 1) to the last cell in the right. Palindromic sequences are more commonly found in symmetric matrices. We will be given a set of matrices in which we have to find maximum number of palindromic paths.

Input Output Scenarios

We are given the matrix. The number of palindromic paths in that matrix is the output.

Input: matrix[3][4] = {{1, 1, 1, 2}, {2, 1, 1, 1}, {1, 2, 2, 1}}
Output: 3
Input: matrix[3][4] = {{1, 1, 1, 2}, {2, 1, 1, 1}, {1, 2, 2, 1}}
Output: 2

Here, in case of matrix[3][4] = {{1, 1, 1, 2}, {2, 1, 1, 1}, {1, 2, 2, 1}}, we have 3 palindromic paths. They are as follows −

  • 111111- (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) -> (1, 3) -> (2, 3)

  • 111111- (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (1, 3) -> (2, 3)

  • 121121- (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) -> (2, 2) -> (2, 3)

Using Recursion

We will use recursive function to find the palindromic paths in the matrix.

  • First, we will create a function which will check whether a string (or path) is palindromic or not.

  • To check this, we will run a while loop from first to last position of string. We check the similarity between first and last character, second and second-last one and so on.

  • Then, we create a function for finding the total paths which are palindromic. First, we find the total number of rows and columns using mat.size() and mat[0].size() respectively. Then, we check whether the path is palindromic or not. If the function is not at the last cell, the function calls itself recursively.

  • To move forward, the recursive function is called by increasing the column by 1 and keeping the row same. To move downwards, the recursive function is called by keeping the column same and increasing the row by 1.

  • The sum of the results of forward and below recursive calls gives the total count of palindromic paths in the given matrix.

Example

#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool palindrome(const string & string) {
   int start = 0, end = string.length() - 1;
   while (start < end) {
      if (string[start] != string[end]) {
         return false;
      }
      start++;
      end--;
   }
   return true;
}
int palindromicPaths(vector < vector < int >> & mat, int i, int j, string path){
   int rows = mat.size();
   int columns = mat[0].size();
   if (i < 0 || i >= rows || j < 0 || j >= columns) {
      return 0;
   }
   // Appending the value of current matrix cell
   path += to_string(mat[i][j]);
   if (i == rows - 1 && j == columns - 1) {
      if (palindrome(path)) {
         return 1;
      }
      return 0;
   }
   int forward = palindromicPaths(mat, i, j + 1, path);
   int below = palindromicPaths(mat, i + 1, j, path);
   int totalCount = forward + below;
   return totalCount;
}
int main() {
   vector<vector<int>> mat = {{1, 1, 1, 2}, {2, 1, 1, 1}, {1, 2, 2, 1}};
   cout << "Number of palindromic paths in the matrix: " <<
   palindromicPaths(mat, 0, 0, "") << endl;
   return 0;
}

Output

Number of palindromic paths in the matrix: 3

Conclusion

We have discussed how to find palindromic paths in a matrix. We have used recursive function to find the possible paths. We can use dynamic programming to make the code efficient. For that, we may store the strings (or paths) in a vector. Use of memorization map decreases the space complexity.

Updated on: 05-Jan-2024

52 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements