MAXIMUM SUBARRAY PROBLEM OPTIMIZATION FOR SPECIFIC DATA

The maximum subarray problem (MSP) is to the find maximum contiguous sum in an array. This paper describes a method of Kadanes algorithm (the state of the art) optimization for specific data (continuous sequences of zeros or negative real numbers). When the data are unfavourable, the modification of the algorithm causes a non significant performance loss (1% > decrease in performance). The modification does not improve time complexity but reduces the number of elementary operations. Various experimental data sets have been used to evaluate possible time efficiency improvement. For the most favourable data sets an increase in efficiency of 25% can be achieved.


Introduction
The maximum subarray problem was described by U. Grenander at Brown Univer sity in 1977 during his study on the pattern recognition field. There are numerous algorithms which solves the problem in range from brute force method with time complexity O(n 2 ) to Kadane's algorithm with time complexity O(n). Maximum subarray problem solutions are used in data mining [10], pattern recognition [5,9], biological sequence analysis [6,1,11], computer vision [2] etc.
The maximum subsequence problem for a one-dimensional array is to find a contiguous subarray which has the largest sum of elements. At least one element of the array should be a positive real number. If the array elements are all non-negative, the solution is the whole array. When all the numbers are negative then the maximum subarray is the empty array.
Example. We are given a zero-indexed array X, such that:   6  5  5  1  3  6  1  4  5 , , , Maximum subarray sum is 10, the beginning of this subarray is on index 4, and the end is on index 8.
Kadane's algorithm [3] finds maximum contiguous subsequence in one-dimensional array. The input is an array X consisting of N real numbers. The output is the maximum sum found in any contiguous subarray of the input array. Its running time is linear (O(n)) The algorithm uses the dynamic programming approach. Kadane's algorithm scans the whole array from left (X[0]) to right (X[N-1]) and keeps track of the maximum sum subarray seen so far. Its pseudocode is given below.
On the right side of pseudocode are numbers (after //) represent the number of elementary operations which are done to execute certain line of code.
Below we describe in detail Kadane's algorithm analysis. The runtime complexity of Kadane's algorithm is O(n). This description focuses on number of elementary operations not asymptotic notation. Elementary operation is defined as one of the arithmetic operations (addition, subtraction, multiplication, division), comparisons (between two real numbers), assignments [7]. Let T (n) be the number of units of time taken for any input array size of n, required to solve the maximum subarray problem. For simplicity's sake, one can assume that all elementary operations take the same time. The analysis is given for the worstcase running time. Second line (for i ← 1 to n) takes 1 + n + (n − 1) due to: 1 for assignment operations (i = 1), n for checking condition (i < n) and (n -1) for incrementing i. According to the above pseudocode one has:

Materials and methods
In this section we describe improvement of Kadane's algorithm and explain for what kind of data is can be effectively used. Then we present test results for random generated data. Finally we demonstrate how much faster can be modified algorithm during searching specific regions on images.

Algorithm optimizing
Idea of optimizing Kadane's algorithm for specific data is based on the observation that the searched interval (containing solution) always starts and ends with a positive real number. All numbers before(after) first(last) positive should be negative or equal to zero.
For example, for a given zero-indexed array B consisting of N=11 real numbers: It can be noticed that the array containing the solution is between 4 (index) and 9 because all real numbers with an index less than 4 and greater than 8 are negative or equal to 0.
We can exclude part of the array which we know that there is no solution. In order to eliminate further calculations, we can searching for the first positive real number from left/right side.
To find the interval with the solution, the algorithm scans the whole array from the left until it finds real number x, where x > 0 and returns its index. This operation works analogously for the other side of the input array. The returned indices determine the range in which the array will be analyzed by the classic Kadane's algorithm. The pseudocode of improved version of the algorithm is shown below.
In order to explain how improvement version works, we should analyze number of elementary operations. Every negative number in series from left/right side decrease number of elementary operations which should be performed by 1. Using the improved version of Kadane's algorithm one can assume that additional code (searching interval which contains solution) takes: n n n n n T Productivity growth relative to the classical algorithm. From line (2) one can conclude that a loop-cycle costs 4 elementary operations, line (3) shows that a reduction in the range of one real number takes 3 elementary operations. Constants are not taken into account because they are negligible for a large n. From this one can conclude that finding a real number, which is not in the solution interval, allows to reduce the number of operations (by 25%) required to solve the MPS problem.
In the worst case (the first and the last real number in the array are positive) the improved version of Kadane's algorithms will perform 10 more elementary operations (for checking the first and last real number and for two assignments) than the classical algorithm. In the most favourable situation (all real numbers in the array are negative) the improved version of Kadane's algorithm will perform 4/3 elementary operations of the classical algorithm.
Correctness of the results over the classical algorithm. The improvement of the classical algorithm does not change the result which can be achieved by running it. Even in the case of ambiguous solutions, for example in a zero indexed array, such that: where there is one result in the situation of the maximum sum which is 3 and there are 3 possible results in the context of intervals ( [1,2], [1,5], [4,5]). The result interval is [1,5] in both versions of the algorithms.

Performance tests
Tests were performed on isolated virtual machine. The hardware used for testing was: Intel(R) Core(TM) i5-3320M, 8GB RAM with Debian 64-bit system. There were two environments used to further confirm the results: compiled and interpreted. The following platforms, on which performance was tested, had identical code in terms of semantics:  C++ (compiler g++ version 4.8.2),  Ruby ( interpreter version 2.1.1p76).
In this section test results for C++ implementation are presented. Running times of two versions of the algorithm on the same data sets are shown on the graph below ( Fig. 1):  mixed datadecrease in performance by 0,37% ,  25% of negative and 75% of positive integersincrease in performance by 8.29%,  50% of negative and 50% of positive integersincrease in performance by 23.37%,  75% of negative and 25% of positive integersincrease in performance by 28.06%. Ruby test results were performed in the same manner. They are presented on Fig 2:  mixed datadecrease in performance by 0,53%,  25% of negative and 75% of positive integersincrease in performance by 6.97%,  50% of negative and 50% of positive integersincrease in performance by 11.98%,  75% of negative and 25% of positive integersincrease in performance by 20.6%.

Fig. 2. Classical and improved algorithm run time (Ruby implementation) for different data set
In relation to the results of the performance tests above, the increase can be estimated as shown on Fig. 3. It also converges with the algorithm analysis.

Result
In previous section we showed that modified version of Kadane's algorithm increase its performance. Data for test were generated to analyze theoretical improvement. In this section we present that there are some applications of proposed improvement. Among others Kadane's algorithm in two dimensional version is used to find brightest region or region with specific features (dominant colorred, green or blue maximum likelihood estimator of a certain kind of pattern in digital image) is time consume problem. Example of that kind of task is finding brightest (stars) regions on the sky or warmest/coolest place on thermo-graphic image.
To find brightest region on an image, it should be represented as a two dimensional array, where each pixel is luminance values. As a luminance one can assume that this is (for 24-bit images) according to formula [4]: The area with maximum sum is row (0,1) and column (1,2).
The best known algorithms which solves MSP is Kadane's algorithm do it in O(n^2) time. In digital images values of pixels are all non negative. The solution of 2D MPS will be the whole array. Before computing the maximum subarray, we should normalize each pixel value by substracting an anchor value. Selection of certain anchor value determinate sensitiveness (size) of region which we want to find.
The whole operation is time consuming, for example for an image 3MP (2,048 pixels  1536 pixels), which is exactly 3145728 pixels, one can assume that is input data n for our algorithm, time of running is O(nm 2 ) (qubic time when m = n), the number of operations which algorithm have to do in approximation is 9.8956046510 12 .
Today personal computer, which has a two-core processor 2 GHz possesses a theoretical power of about 16GFLOPS, which stems from the formula:    what is more than 10 minutes. This calculations are approximate, we assume one operation per pixel which is not precise. They do not count real number of operations, but show the scale size of the problem.

Performance tests for 2D application
We take 3 images from different categories for tests:  astronomy,  thermography,  computed tomography.
All images were 24-bit. In order to find specific (brightest) region on an image, we convert it into gray scale (8 bit) and subtract anchor (about 128) value from each pixel's value. We get two dimensional array with pixels in range (-x to 256 -x, where x is chosen anchor value) as a result of this operation. We can adjust sensitive of searched region by changing parameters: anchor value and luminance formula.
Both versions of Kadane's algorithm were implemeneted in C++ Below we present taken images with selected area which was found and percent of improvement over classical algorithm.

Conclusion
Kadane's algorithm is the state of the art algorithm for solving the maximum subarray problem. One can conclude that the presented improvement can limit the number of elementary operations. The algorithm analysis showed that finding one element of a sequence which does not contain a solution costs 3 units of time and the analysis of the faulty component costs 4 units. The tests prove that with the falling number of elementary operations the performance of the algorithm increases. For the most unfavourable data, the performance is slightly worse in comparison to Kadane's algorithm. In all other situations presented algorithm's runtime is lower. Regardless of the implementation platform (C++, Ruby), a proportional gain in the runtime is achieved. Analogously improvement version of Kadane's algorithm for 2 dimmensional space show that finding the brightest regions can be faster up to 22% then classical Kadane's algorithm.There are applied applications like analysis of DNA sequence or protein sequence, where a significant increase in efficiency is possible. This improvement could be the state of the art for Kadane's algorithm.