2 ALGORITHM TO COMPUTE
MAX/MIN VALUE BASED ON
SLIDING WINDOW
2.1 Basic Idea
Let X = x
1
, x
2
, . .. , x
n
denote the arriving data se-
quence, W denote the width of the sliding window.
We only consider the situation that the window fits
in memory. In a diagram showing sliding window, x-
axis represents time, and y-axis is the value of the data
element. A newly arriving data will be inserted into
the window from right, and step out of the window
from left when expire. Calculation of the Max/Min
value is executed when a new data element arrives or
an old data element steps out of the sliding window,
which are described in more details as follows:
Case 1.New data element arrives
We only need to store the current Max value of
the sliding window. Let Max
n−1
be the current Max
value of the sliding window before a new data ele-
ment x
n
arrive, the new current Max value Max
n
can
be calculated as follows:
Max
n
=
x
n
if x
n
>= Max
n−1
Max
n−1
if x
n
< Max
n−1
(1)
In the same way, we can get the new current Min value
Min
n
:
Min
n
=
x
n
if x
n
<= Min
n−1
Min
n−1
if x
n
> Min
n−1
(2)
We can save Max
n
and Min
n
as historical data.
Case 2. Old data element steps out
When old data element expires, some data ele-
ments should be saved as historical data. First we
need simulate the data sequence in the sliding win-
dow as a broken curve. Figure 1 illustrates the various
cases of removing old data element from the window.
case a: The data sequence in the sliding window
monotonically increases.
Max In order to get the Max value, we only need
to save one data element, the wave crest of the
broken curve, as the historical data.
Min In order to get the Min value, we need to
save all elements in the sliding window(The
data elements in blue).
Max/Min In order to get both Max value and Min
value, we need to save all elements in the slid-
ing window.
case b: The data sequence in the sliding window
monotonically decreases.
Max In order to get the Max value, we need to
save all elements in the sliding window(The
data elements in red).
Min In order to get the Min value, we only need
to save one data element, the wave trough of the
broken curve, as the historical data.
Max/Min In order to get both Max value and Min
value, we need to save all elements in the slid-
ing window.
case c: The data sequence in the sliding window con-
tains an increasing part followed by a decreasing
part. It includes three cases.
• case c1:
Max In order to get the Max value, we
need to save elements between the crest and
the newest data element in the sliding win-
dow(The data elements in red).
Min In order to get the Min value, we only
need to save one data element, the newest data
element(The data element B).
Max/Min In order to get both Max value and
Min value, we need to save elements between
the crest and the newest data element in the
sliding window(The data elements in red).
• case c2:
Max In order to get the Max value, we
need to save elements between the crest and
the newest data element in the sliding win-
dow(The data elements in red).
Min In order to get the Min value, we only
need to save one data element, the newest data
element(The data element B).
Max/Min In order to get both Max value and
Min value, we need to save elements between
the crest and the newest data element in the
sliding window(The data elements in red).
• case c3:
Max In order to get the Max value, we
need to save elements between the crest and
the newest data element in the sliding win-
dow(The data elements in red).
Min In order to get the Min value, we need
to save elements between the trough and
the newest data element in the sliding win-
dow(The data elements in blue).
Max/Min We need to save elements between
C and B for Max value( The data elements in
read), and elements between A and D for Min
value.
case d: The data sequence has multiple peaks and
troughs.
ICSOFT 2007 - International Conference on Software and Data Technologies
124