Largest sum Zigzag sequence in a matrix

16 Dec, 2021
Given a matrix of size n x n, find the 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 the 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 an 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++
Java
Python 3
C#
PHP
Javascript
// 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 Zigzag 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 aboveint 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;}
// Java program to find the largest sum// zigzag sequenceimport java.io.*; class GFG {     static int MAX = 100;         // Returns largest sum of a Zigzag    // sequence starting from (i, j)    // and ending at a bottom cell.    static int largestZigZagSumRec(int mat[][],                            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 = Math.max(zzs,               largestZigZagSumRec(mat, i+1, k, n));                 return zzs + mat[i][j];    }         // Returns largest possible sum of a Zigzag    // sequence starting from top and ending    // at bottom.    static int largestZigZag(int mat[][], int n)    {        // Consider all cells of top row as starting        // point        int res = 0;        for (int j=0; j<n; j++)            res = Math.max(res,                   largestZigZagSumRec(mat, 0, j, n));                 return res;    }         // Driver program to test above    public static void main (String[] args)    {        int n = 3;                 int mat[][] = { {4, 2, 1},                        {3, 9, 6},                        {11, 3, 15} };        System.out.println( "Largest zigzag sum: "                       + largestZigZag(mat, n));    }} // This code is contributed by anuj_67.
# Python3 program to find the largest# sum zigzag sequenceMAX = 100 # Returns largest sum of a Zigzag# sequence starting from (i, j) and# ending at a bottom cell.def largestZigZagSumRec( mat, i, j, 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.    zzs = 0    for k in range(n):        if (k != j):            zzs = max(zzs, largestZigZagSumRec(mat, i + 1, k, n))         return zzs + mat[i][j] # Returns largest possible sum of a# Zigzag sequence starting from top# and ending at bottom.def largestZigZag(mat, n):             # Consider all cells of top row as    # starting point    res = 0    for j in range(n):        res = max(res, largestZigZagSumRec(mat, 0, j, n))         return res # Driver Codeif __name__ == "__main__":    n = 3    mat = [ [4, 2, 1],            [3, 9, 6],            [11, 3, 15]]    print("Largest zigzag sum: " ,           largestZigZag(mat, n)) # This code is contributed by ChitraNayal
// C# program to find the largest sum// zigzag sequenceusing System;class GFG {     // static int MAX = 100;         // Returns largest sum of a Zigzag    // sequence starting from (i, j)    // and ending at a bottom cell.    static int largestZigZagSumRec(int [,]mat,                          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 = Math.Max(zzs, largestZigZagSumRec(mat,                                           i + 1, k, n));                 return zzs + mat[i,j];    }         // Returns largest possible    // sum of a Zigzag sequence    // starting from top and ending    // at bottom.    static int largestZigZag(int [,]mat, int n)    {                 // Consider all cells of        // top row as starting        // point        int res = 0;        for (int j = 0; j < n; j++)            res = Math.Max(res,                largestZigZagSumRec(mat, 0, j, n));                 return res;    }         // Driver Code    public static void Main ()    {        int n = 3;        int [,]mat = {{4, 2, 1},                      {3, 9, 6},                      {11, 3, 15}};        Console.WriteLine("Largest zigzag sum: "                           + largestZigZag(mat, n));    }} // This code is contributed by anuj_67.
<?php// PHP program to find the// largest sum zigzag sequence $MAX = 100; // Returns largest sum of a// Zigzag sequence starting// from (i, j) and ending at// a bottom cell.function largestZigZagSumRec($mat, $i,                              $j, $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.    $zzs = 0;    for ($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 Zigzag sequence// starting from top and// ending at bottom.function largestZigZag( $mat, $n){         // Consider all cells of top    // row as starting point    $res = 0;    for ($j = 0; $j < $n; $j++)        $res = max($res, largestZigZagSumRec(                            $mat, 0, $j, $n));         return $res;}     // Driver Code    $n = 3;    $mat = array(array(4, 2, 1),                 array(3, 9, 6),                 array(11, 3, 15));    echo "Largest zigzag sum: " , largestZigZag($mat, $n);     // This code is contributed by anuj_67.?>
<script> // Javascript program to find the largest sum// zigzag sequence     let  MAX = 100;         // Returns largest sum of a Zigzag    // sequence starting from (i, j)    // and ending at a bottom cell.    function largestZigZagSumRec(mat,i,j,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.        let zzs = 0;                   for (let k=0; k<n; k++)            if (k != j)            zzs = Math.max(zzs,               largestZigZagSumRec(mat, i+1, k, n));                   return zzs + mat[i][j];    }              // Returns largest possible sum of a Zigzag    // sequence starting from top and ending    // at bottom.    function largestZigZag(mat,n)    {        // Consider all cells of top row as starting        // point        let res = 0;        for (let j=0; j<n; j++)            res = Math.max(res,                   largestZigZagSumRec(mat, 0, j, n));                   return res;    }         // Driver program to test above    let n = 3;         let mat = [ [4, 2, 1],            [3, 9, 6],            [11, 3, 15]];       document.write("Largest zigzag sum: " +           largestZigZag(mat, n))         // This code is contributed by rag2127     </script>
Output:
Largest zigzag sum: 28
Overlapping Subproblems Considering the above implementation, for a matrix mat[][] of size 3 x 3, to find the 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 that 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 tabulated implementation for the LIS problem. 
C++
Java
Python3
C#
Javascript
// 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 Zigzag 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 aboveint 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;}
// Memoization based Java program to find the largest// sum zigzag sequenceclass GFG{ static int MAX = 100;static int [][]dp = new int[MAX][MAX]; // Returns largest sum of a Zigzag sequence starting// from (i, j) and ending at a bottom cell.static int largestZigZagSumRec(int mat[][], 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 = Math.max(zzs, largestZigZagSumRec(mat,                                    i + 1, k, n));         return (dp[i][j] = (zzs + mat[i][j]));} // Returns largest possible sum of a Zigzag sequence// starting from top and ending at bottom.static int largestZigZag(int mat[][], int n){    for (int i = 0; i < MAX; i++)        for (int k = 0; k < MAX; k++)                dp[i][k] = -1;         // Consider all cells of top row as starting point    int res = 0;    for (int j = 0; j < n; j++)        res = Math.max(res, largestZigZagSumRec(mat,                                           0, j, n));         return res;} // Driver codepublic static void main(String[] args){    int n = 3;    int mat[][] = { {4, 2, 1},                    {3, 9, 6},                    {11, 3, 15}};    System.out.print("Largest zigzag sum: " +                        largestZigZag(mat, n));}} // This code is contributed by PrinciRaj1992
# Memoization based Python3 program to find the largest# sum zigzag sequenceMAX = 100; dp = [[0 for i in range(MAX)] for j in range(MAX)] # Returns largest sum of a Zigzag sequence starting# from (i, j) and ending at a bottom cell.def largestZigZagSumRec(mat, i, j, n):    if (dp[i][j] != -1):        return dp[i][j];     # If we have reached bottom    if (i == n - 1):        dp[i][j] = mat[i][j];        return (dp[i][j]);     # Find the largest sum by considering all    # possible next elements in sequence.    zzs = 0;    for k in range(n):        if (k != j):            zzs = max(zzs, largestZigZagSumRec(mat,                     i + 1, k, n));    dp[i][j] = (zzs + mat[i][j]);    return (dp[i][j]); # Returns largest possible sum of a Zigzag sequence# starting from top and ending at bottom.def largestZigZag(mat, n):    for i in range(MAX):        for k in range(MAX):            dp[i][k] = -1;     # Consider all cells of top row as starting point    res = 0;    for j in range(n):        res = max(res, largestZigZagSumRec(mat, 0, j, n));     return res; # Driver codeif __name__ == '__main__':    n = 3;    mat = [[4, 2, 1], [3, 9, 6], [11, 3, 15]];    print("Largest zigzag sum: ", largestZigZag(mat, n)); # This code is contributed by Rajput-Ji
// Memoization based C# program to find the largest// sum zigzag sequenceusing System; class GFG{ static int MAX = 100;static int [,]dp = new int[MAX, MAX]; // Returns largest sum of a Zigzag sequence starting// from (i, j) and ending at a bottom cell.static int largestZigZagSumRec(int [,]mat, 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 = Math.Max(zzs, largestZigZagSumRec(mat,                                    i + 1, k, n));         return (dp[i, j] = (zzs + mat[i, j]));} // Returns largest possible sum of a Zigzag sequence// starting from top and ending at bottom.static int largestZigZag(int [,]mat, int n){    for (int i = 0; i < MAX; i++)        for (int k = 0; k < MAX; k++)                dp[i, k] = -1;         // Consider all cells of top row as starting point    int res = 0;    for (int j = 0; j < n; j++)        res = Math.Max(res, largestZigZagSumRec(mat,                                        0, j, n));    return res;} // Driver codepublic static void Main(String[] args){    int n = 3;    int [,]mat = { {4, 2, 1},                    {3, 9, 6},                    {11, 3, 15}};    Console.Write("Largest zigzag sum: " +                        largestZigZag(mat, n));}} // This code is contributed by 29AjayKumar
<script>// Memoization based Javascript program to find the largest// sum zigzag sequence         let MAX = 100;    let dp=new Array(MAX);         // Returns largest sum of a Zigzag sequence starting// from (i, j) and ending at a bottom cell.    function largestZigZagSumRec(mat,i,j,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.    let zzs = 0;    for (let k = 0; k < n; k++)        if (k != j)            zzs = Math.max(zzs, largestZigZagSumRec(mat,                                    i + 1, k, n));          return (dp[i][j] = (zzs + mat[i][j]));    }         // Returns largest possible sum of a Zigzag sequence// starting from top and ending at bottom.    function largestZigZag(mat,n)    {        for (let i = 0; i < MAX; i++)        {            dp[i]=new Array(MAX);            for (let k = 0; k < MAX; k++)                dp[i][k] = -1;         }    // Consider all cells of top row as starting point    let res = 0;    for (let j = 0; j < n; j++)        res = Math.max(res, largestZigZagSumRec(mat,                                           0, j, n));          return res;    }         // Driver code    let n = 3;    let mat=[[4, 2, 1],[3, 9, 6],[11, 3, 15]];    document.write("Largest zigzag sum: " +                        largestZigZag(mat, n));              // This code is contributed by avanitrachhadiya2155</script>
Output: 
Largest zigzag sum: 28
 
vt_m
ukasp
princiraj1992
29AjayKumar
Rajput-Ji
rag2127
avanitrachhadiya2155
sumitgumber28
sweetyty
Directi
Dynamic Programming
Matrix
Directi
Dynamic Programming
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Longest Palindromic Substring | Set 1
Floyd Warshall Algorithm | DP-16
Coin Change | DP-7
Matrix Chain Multiplication | DP-8
Sieve of Eratosthenes
Matrix Chain Multiplication | DP-8
Print a given matrix in spiral form
Program to find largest element in an array
Rat in a Maze | Backtracking-2
Sudoku | Backtracking-7