Longest Zig-Zag Subsequence - GeeksforGeeks



Longest Zig-Zag Subsequence - GeeksforGeeks

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     x2 > 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.  

We strongly recommend you to minimize your browser and try this yourself first.

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.


Read full article from Longest Zig-Zag Subsequence - GeeksforGeeks


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts