### Introduction to online algorithms

• Online and Offline algorithms

• Offline Algorithms:

• And offline algorithm is an algorithm whose input data is given to the algorithm when the algorithm begins to execute

Situation:

• Example:

 Any sorting algorithm The data of a sorting algorithm is given in an input file and it is usually read into an array When the sorting algorithm begins, all input data is available in the array

• Online Algorithms:

• An online algorithm is an algorithm that can process its input piece-by-piece in a serial fashion.

• The algorithm processes the input data as they come in, without having the entire input available from the start.

Situation:

• Some simple online algorithms

• Computing the maximum value in an input stream:

 ``` int max; max = read(input); while ( input ≠ EOS ) { x = read(input); if ( x > max ) max = x; } ```

• Computing the sum of the value in an input stream:

 ``` int sum; sum = 0; while ( input ≠ EOS ) { x = read(input); sum += x; } ```

• Computing the average of the value in an input stream:

 ``` int sum; int N; sum = 0; N = 0; while ( input ≠ EOS ) { x = read(input); sum += x; N++; } average = sum/N; // Compute average ```

• The difficulty of online algorithms

• The main difficulties of online algorithms:

• Unknown input size:

 The number of inputs data is unknown when the algorithm is running We can try to adjust to data structures used to retain the input data (by using linked list) Sometimes, the input size may be too large to be stored completely and the algorithm must run using a partial input (and re-adjust the result when new inputs are red in)

• Incomplete information:

 This is a result from the unknown input size: the algorithm must run using a partial input Because the algorithm does not know the whole input, an online algorithm is forced to make algorithmic decisions that may later turn out not to be optimal. Often, online algorithms will compute solutions that must be non-optimal and the study of online algorithms focuses on the quality of solution opbatined using incomplete information (e.g., max. 1% from the optimum value)