Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a sequence is bitonic?

Tags:

A sequence is bitonic if it monotonically increases and then monotonically de- creases, or if it can be circularly shifted to monotonically increase and then monotonically decrease. For example the sequences (1, 4, 6, 8, 3, −2) , (9, 2, −4, −10, −5) , and (1, 2, 3, 4) are bitonic, but (1, 3, 12, 4, 2, 10) is not bitonic.

How can it be determined if given sequence is bitonic?

I have the following opinion. We can walk till n/2, where n is the length of the array, and check if

(a[i] < a[i + 1]) and (a[n - i - 1] < a[n-1 - (i + 1)]) 

Is this correct?

like image 746
dato datuashvili Avatar asked Jun 12 '10 14:06

dato datuashvili


People also ask

What is the definition of a Bitonic sequence of numbers?

A Bitonic Sequence is a sequence of numbers which is first strictly increasing then after a point strictly decreasing. A Bitonic Point is a point in bitonic sequence before which elements are strictly increasing and after which elements are strictly decreasing.

Is a monotonic sequence bitonic?

A monotonic sequence is one in which the values increase (or decrease) from left-to-right. The sequence a1,a2, ···,an is monotonic if ak < ak+1 for all k<n. are bitonic. The first one increases from 3 to 9, then decreases.

What is bitonic sorting network?

Bitonic sort is a comparison-based sorting algorithm that can be run in parallel. It focuses on converting a random sequence of numbers into a bitonic sequence, one that monotonically increases, then decreases. Rotations of a bitonic sequence are also bitonic.


1 Answers

A bitonic sequence:

 /\ /  \     \/ 

Not a bitonic sequence:

 /\     /  \  / (higher than start)     \/ 

Obviously if the direction changes more than two times we cannot have a bitonic sequence.
If the direction changes less than two times, we must have a bitonic sequence.

If there are two changes in direction, we MAY have a bitonic sequence. Consider the two ascii images above. Clearly a sequence with two changes in direction will match one of the patterns (allowing for a reflection). Thus, we set the initial direction by comparing the first and last elements. Since these can be the same, we use the first element that is not equal to the last element.

Here is an implementation in Java:

public static Boolean bitonic(int[] array) {     if (array == null) return false;     if (array.length < 4) return true;     Boolean dir;// false is decreasing, true is increasing     int pos = 0, switches = 0;     while (pos < array.length) {         if (array[pos] != array[array.length - 1])             break;         pos++;     }     if (pos == array.length) return true;     //pos here is the first element that differs from the last     dir = array[pos] > array[array.length - 1];     while (pos < array.length - 1 && switches <= 2) {         if ((array[pos + 1] != array[pos]) &&            ((array[pos + 1] <= array[pos]) == dir)) {             dir ^= true;             switches++;         }         pos++;     }     return switches <= 2; } 
like image 90
Nabb Avatar answered Sep 30 '22 04:09

Nabb