![]() |
VOOZH | about |
The longest Zig-Zag subsequence problem is to find length of the longest subsequence of given sequence such that all elements of this are alternating.
If a sequence {x1, x2, .. xn} is alternating sequence then its element satisfy one of the following relation :
x1 < x2 > x3 < x4 > x5 < …. xn or x1 > x2 < x3 > x4 < x5 > …. xn
Examples :
Input: arr[] = {1, 5, 4}
Output: 3
The whole arrays is of the form x1 < x2 > x3
Input: arr[] = {1, 4, 5}
Output: 2
All subsequences of length 2 are either of the form
x1 < x2; or x1 > x2
Input: arr[] = {10, 22, 9, 33, 49, 50, 31, 60}
Output: 6
The subsequences {10, 22, 9, 33, 31, 60} or
{10, 22, 9, 49, 31, 60} or {10, 22, 9, 50, 31, 60}
are longest Zig-Zag of length 6.
This problem is an extension of longest increasing subsequence problem, but requires more thinking for finding optimal substructure property in this.
We will solve this problem by dynamic Programming method, Let A is given array of length n of integers. We define a 2D array Z[n][2] such that Z[i][0] contains longest Zig-Zag subsequence ending at index i and last element is greater than its previous element and Z[i][1] contains longest Zig-Zag subsequence ending at index i and last element is smaller than its previous element, then we have following recurrence relation between them,
Z[i][0] = Length of the longest Zig-Zag subsequence ending at index i and last element is greater than its previous element Z[i][1] = Length of the longest Zig-Zag subsequence ending at index i and last element is smaller than its previous element Recursive Formulation: Z[i][0] = max (Z[i][0], Z[j][1] + 1); for all j < i and A[j] < A[i] Z[i][1] = max (Z[i][1], Z[j][0] + 1); for all j < i and A[j] > A[i]
The first recurrence relation is based on the fact that, If we are at position i and this element has to bigger than its previous element then for this sequence (upto i) to be bigger we will try to choose an element j ( < i) such that A[j] < A[i] i.e. A[j] can become A[i]’s previous element and Z[j][1] + 1 is bigger than Z[i][0] then we will update Z[i][0].
Remember we have chosen Z[j][1] + 1 not Z[j][0] + 1 to satisfy alternate property because in Z[j][0] last element is bigger than its previous one and A[i] is greater than A[j] which will break the alternating property if we update. So above fact derives first recurrence relation, similar argument can be made for second recurrence relation also.
Length of Longest Zig-Zag subsequence is 6
Time Complexity : O(n2)
Auxiliary Space : O(n)
A better approach with time complexity O(n) is explained below:
Let the sequence be stored in an unsorted integer array arr[N].
We shall proceed by comparing the mathematical signs(negative or positive) of the difference of two consecutive elements of arr. To achieve this, we shall store the sign of (arr[i] - arr[i-1]) in a variable, subsequently comparing it with that of (arr[i+1] - arr[i]). If it is different, we shall increment our result. For checking the sign, we shall use a simple Signum Function, which shall determine the sign of a number passed to it. That is,
Considering the fact that we traverse the sequence only once, this becomes an O(n) solution.
The algorithm for the approach discussed above is :
Input integer array seq[N]. Initialize integer lastSign to 0. FOR i in range 1 to N - 1 integer sign = signum(seq[i] - seq[i-1]) IF sign != lastSign AND IF sign != 0 increment length by 1. lastSign = sign. END IF END FOR return length.
Following is the implementation of the above approach:
The maximum length of zig-zag sub-sequence in first sequence is: 3 The maximum length of zig-zag sub-sequence in second sequence is: 4
Time Complexity : O(n)
Auxiliary Space : O(1)