Largest sum Zigzag sequence in a matrix

Given a matrix of size n x n, find sum of the Zigzag sequence with the largest sum. A zigzag sequence starts from the top and ends at the bottom. Two consecutive elements of sequence cannot belong to same column.
Examples:

Input : mat[][] = 3  1  2
                  4  8  5
                  6  9  7
Output : 18
Zigzag sequence is: 3->8->7
Another such sequence is 2->4->7

Input : mat[][] =  4  2  1
                   3  9  6
                  11  3 15
Output : 28

This problem has Optimal Substructure.

Maximum Zigzag sum starting from arr[i][j] to a 
bottom cell can be written as :
zzs(i, j) = arr[i][j] + max(zzs(i+1, k)), 
               where k = 0, 1, 2 and k != j
zzs(i, j) = arr[i][j], if i = n-1 

We have to find the largest among all as
Result = zzs(0, j) where 0 <= j < n
// C++ program to find the largest sum zigzag sequence
#include <bits/stdc++.h>
using namespace std;

const int MAX = 100;

// Returns largest sum of a Zigzag sequence starting
// from (i, j) and ending at a bottom cell.
int largestZigZagSumRec(int mat[][MAX], int i,
                                int j, int n)
{
   // If we have reached bottom
   if (i == n-1)
     return mat[i][j];

   // Find the largest sum by considering all
   // possible next elements in sequence.
   int zzs = 0;
   for (int k=0; k<n; k++)
     if (k != j)
       zzs = max(zzs, largestZigZagSumRec(mat, i+1, k, n));

   return zzs + mat[i][j];
}

// Returns largest possible sum of a Zizag sequence
// starting from top and ending at bottom.
int largestZigZag(int mat[][MAX], int n)
{
   // Consider all cells of top row as starting point
   int res = 0;
   for (int j=0; j<n; j++)
     res = max(res, largestZigZagSumRec(mat, 0, j, n));

   return res;
}

// Driver program to test above
int main()
{
    int n = 3;
    int  mat[][MAX] = { {4, 2, 1},
                        {3, 9, 6},
	                    {11, 3, 15}};
    cout << "Largest zigzag sum: " << largestZigZag(mat, n);
    return 0;
}

Output:

Largest zigzag sum: 28

Overlapping Subproblems
Considering the above implementation, for a matrix mat[][] of size 3 x 3, to find zigzag sum(zzs) for an element mat(i,j), the following recursion tree is formed.

Recursion tree for cell (0, 0)
             zzs(0,0)                                
           /         \                               
    zzs(1,1)           zzs(1,2)                      
    /     \            /      \                      
zzs(2,0)  zzs(2,2)  zzs(2,0)  zzs(2,1)               


Recursion tree for cell (0, 1)
            zzs(0,1)
           /         \              
    zzs(1,0)          zzs(1,2)
    /     \            /      \    
zzs(2,1)  zzs(2,2)  zzs(2,0)  zzs(2,1)

Recursion tree for cell (0, 2)
             zzs(0,2)
           /         \                                             
    zzs(1,0)           zzs(1,1)                             
    /     \            /      \                             
 zzs(2,1)  zzs(2,2)  zzs(2,0)  zzs(2,2)

We can see that there are many subproblems which are solved again and again. So this problem has Overlapping Substructure property and recomputation of same subproblems can be avoided by either using Memoization or Tabulation. Following is a tabluated implementation for the LIS problem.

// Memoization based C++ program to find the largest
// sum zigzag sequence
#include <bits/stdc++.h>
using namespace std;

const int MAX = 100;
int dp[MAX][MAX];

// Returns largest sum of a Zigzag sequence starting
// from (i, j) and ending at a bottom cell.
int largestZigZagSumRec(int mat[][MAX], int i,
                                int j, int n)
{
   if (dp[i][j] != -1)
      return dp[i][j];

   // If we have reached bottom
   if (i == n-1)
     return (dp[i][j] = mat[i][j]);

   // Find the largest sum by considering all
   // possible next elements in sequence.
   int zzs = 0;
   for (int k=0; k<n; k++)
     if (k != j)
       zzs = max(zzs, largestZigZagSumRec(mat, i+1, k, n));

   return (dp[i][j] = (zzs + mat[i][j]));
}

// Returns largest possible sum of a Zizag sequence
// starting from top and ending at bottom.
int largestZigZag(int mat[][MAX], int n)
{
   memset(dp, -1, sizeof(dp));

   // Consider all cells of top row as starting point
   int res = 0;
   for (int j=0; j<n; j++)
     res = max(res, largestZigZagSumRec(mat, 0, j, n));

   return res;
}

// Driver program to test above
int main()
{
    int n = 3;
    int  mat[][MAX] = { {4, 2, 1},
                        {3, 9, 6},
	                    {11, 3, 15}};
    cout << "Largest zigzag sum: " << largestZigZag(mat, n);
    return 0;
}

Output:

28

References: Asked in Directi

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.