Anda di halaman 1dari 2

1.

7 On-Line versus Off-Line Algorithms

Algorithms can be classified according to the way in which they receive their
input. If the entire input set is provided at the beginning of the algorithm,
we say it is off-line. If input may be supplied during the computations of the
algorithm, it is considered on-line. While most algorithms are off-line,
because it often makes little sense to start solving the problem before all
data are received, numerous problems are inherently on-line. For example,
many algorithms occurring in operating systems are on-line, since an
operating system deals with a dynamic situation where decisions must be
continually made based on the information available at that time; once
additional information is received, updates of the status are necessary. In
general, on-line algorithms tend to be more difficult than off-line ones.

As an example, consider again the computation of the largest element of


some set of integers. We have already seen an algorithm to solve this
problem: the algorithm Max. Revisiting it makes it clear that this is a typical
off-line algorithm. The entire input set V is assumed to be available before
we start carrying out any computations. It is not unreasonable to consider
an on-line algorithm for this purpose. We may have a continuous stream
ofinput and would like to know, upon demand, what the maximum of the
numbers seen up to this point was. It turns out that we can use Max without
much modification; we simply treat each incoming new element as the next
element with which we must compare our current TempMax and, if
necessary, update it. It follows without great difficulty that the time
complexity of this on-line version is still O(n) if at some point we have
received n integers as input. However, ordinarily one tends to report the
time complexity of an on-line algorithm differently. Instead of giving a global
answer (O(n), where n is the number of inputs received), we might report
the amount of work per input integer, because for each input, we have to do
some work, so this amount of work should be attributed to the integer just
received as input. Thus, we would report that per integer received, we must
spend O(1), or a constant amount of work. Also, in some on-line algorithms
the question of how many input elements have been received at a certain
point in time is not germane and might require an (extraneous) counter to
enable us to know this. Another example involves inserting elements into an
ordered linear list with n elements. By adapting the analysis in Scenario 1 of
Section 1.3, we see that one insertion requires on average n/2 probes,
assuming all locations are equally likely. 19 Thus, carrying out m successive
insertions in this way requires a total of n/2 + (n + 1)/2 + …(n + m − 1)/2
probes, or m·n/2 + (m − 1)·m/4 probes. This is the on-line version. If we
were able to batch these

m insertions together, we could instead sort the m numbers (using HeapSort


which requires no more than 3·m·log2(m) comparisons; see Section 3.2.6)
and then merge the two ordered structures (this requires about m + n
comparisons; see Section 3.2.4). Thus, this off-line process takes no more
than n + m·[1 + 3·log2(m)]. Since one probe is essentially one comparison,
the off-line version is significantly more efficient. For example, if m = n = 2k

, then the on-line version requires asymptotically 2k /(4·k) times more


probes; for larger

n, this is a dramatically increased number of probes.20 It should be clear


that the complexity of an optimal on-line algorithm cannever be better than
that of an optimal off-line algorithm. If there were an on-line algorithm more
efficient than the best off-line algorithm, we could simply use it on the data
set of the off-line algorithm to obtain a more efficient off-line algorithm.