1 Introduction
One of the applications for online algorithms is optimization problems [15]. The peculiarity is the following. An algorithm reads an input piece by piece and returns an answer piece by piece immediately, even if an answer can depend on future pieces of the input. The algorithm should return an answer for minimizing an objective function (the cost of an output). The most standard method to define the effectiveness is the competitive ratio [18, 13].
Typically, online algorithms have unlimited computational power. At the same time, many different papers consider online algorithms with different restrictions. Some of them are restrictions on memory of an online algorithm [3, 10, 6, 14, 1, 2, 12] another ones are restrictions on time complexity [9, 17].
In this paper, we focus on efficient online algorithms in terms of time complexity. We consider the servers problem on trees [7]. There is an competitive algorithm for this problem, and it is the best competitive ratio. At the same time, the existing implementation has time complexity, where is a number of nodes in a tree. There is a timeefficient algorithm for general graphs [17] that uses mincostmaxflow algorithms, but it is too slow in the case of a tree.
We suggest a new timeefficient implementation of the algorithm from [7]. It has time complexity for preprocessing and for processing a query. It is based on data structures and techniques like a segment tree [16], heavylight decomposition (heavy path decomposition) [19, 11] for a tree and fast algorithms for computing Lowest Common Ancestor (LCA) [5, 4].
2 Preliminaries
2.1 Online algorithms
An online minimization problem consists of a set of inputs and a cost function. Each input is a sequence of requests, where is a length of the input . Furthermore, a set of feasible outputs (or solutions) is associated with each ; an output is a sequence of answers . The cost function assigns a positive real value to and . An optimal solution for is .
Let us define an online algorithm for this problem. A deterministic online algorithm computes the output sequence such that is computed by . We say that is competitive if there exists a constant such that, for every and for any input of size , we have: where is the minimal number that satisfies the inequality. Also we call the competitive ratio of .
2.2 Graph Theory
Let us consider a root tree , where is a set of nodes (vertexes), and is a set of edges. Let be a number of nodes or the size of the tree. Let the root of the tree be st node.
The path is a sequence of nodes that are connected by edges, i.e. for all . Note, that there are no duplicates among . Here is a length of the path. The distance between two nodes and is a length of the path between them.
For each node we can define a parent node , it is such that . Additionally, we can define the set of children .
2.3 servers Problem
We have a root tree . We are also given servers that can move among nodes of . At each time slot, a request appears, and we have to “serve” this request, that is, choose one of our servers and move it to . Other servers are also allowed to move. Our measure of cost is the distance by which we move our servers. In other words, if before the request the positions of servers are and after the request they are , then and the cost of the move is .
The problem is to design a strategy that minimizes the cost of servicing a sequence of requests given online.
3 Tools
In the paper, we use two main tools. The first one is a segment tree with range updates. The good book for the data structure is [16]. The second one is heavylight decomposition (heavy path decomposition) [19, 11] for a tree. Let us describe the main properties of both of them.
3.1 Segment Tree with Range Updates for Coloring Problem
Let us describe the coloring problem and solution using a segment tree data structure. The problem is used as a tool for the main algorithm.
Coloring problem. Assume that we have a sequence of elements . We associate a color with element , where for some positive integer . We should satisfy several queries. Each query can be one of two types:

Update. For three integers (), we should color all elements of segment by , i.e. for .

Request. For an integer (), we should return .

Request Closest Colored. For two integers (), we should return the minimal and maximal indexes of colored elements.
Firstly, let us describe the segment tree data structure. It is the full binary tree of height such that . The data structure works with the sequence of elements of length , but we are care only about the first elements.
Each node of the tree is associated with some segment such that . Each leaf is associated with elements of the sequence or we can say that it is associated with a segment of size . th node of the last level is associated with a segment . Let us consider an inner node and its two children and . Then, is associated with a segment , is associated with a segment , and is associated with a segment for some . Note that because of the structure of the tree, we have
Each node of the segment tree is labeled by color , where . Assume that is associated with segment. If , then it means that segment is not colored at all or it has not a single color. If , then it means that the segment has a single color , i.e. .
Additionally, we add two labels and . is the maximal index of a colored element of the segment. is the minimal index of a colored element of the segment. Initially, ,
For a vertex and associated segment, we use the following notation.

is a left border of the segment.

is a right border of the segment.

is a left child of .

is a right child of .
Let us describe the processing of three types of query and constructing procedure.
3.1.1 Constructing procedure
We can construct a segment tree using a simple recursive procedure. Let be a procedure that returns the root of a segment tree for a segment . We present it in Algorithm 1.
Let us discuss the property of Algorithm 1.
Lemma 1
Time complexity of Algorithm 1 is .
Proof
We construct each node in . A number of nodes on each next level is twice bigger compering to the previous one. The number of nodes on a level is . The number of levels is . So, the total time complexity is .
3.1.2 Request Query.
Assume that we want to get for some . We start with the root node. Assume that we observe a node . If , then we go to the child that is associated with a segment , where . We continue this process until we meet such that or is a leaf. If , then the result is . If and is a leaf, then is not assigned yet. Let us describe this procedure in Algorithm 2.
Let us discuss the property of the algorithm.
Lemma 2
Algorithm 2 works correct with time complexity.
Proof
If the segment tree stores correct colors for segments, then the correctness of the algorithm follows from the description. The algorithm returns a color only if belongs to a segment that has a single color.
On each step we change a vertex to a vertex on the next level. The tree is a full binary tree, therefore, it has levels. Hence, the time complexity is because .
3.1.3 Update Query.
Assume that we want to color a segment in color, where , .
Let us describe two specific cases that are coloring a prefix and coloring a suffix.
Let us have a segment tree with a root node . A segment is associated with node.
Firstly, assume that is a prefix of , i.e. and .
Assume that we observe a node and an associated segment . If is a leaf, then we assign and stop. Otherwise, we continue. We use a variable for an existing color. Initially . If on some step and , then we assign . If or , then we do not change because we already have a color for the segment from an ancestor.
Let be the left child of , and let be the right child of . Firstly, we update , because is a prefix. Secondly, we do the following action.

If , then we go to the left child . Additionally, if , then we color because a segment of has no intersection with and keeps its color .

If , then we go to the right child . Additionally, we color and update , because of is a subsegment of .
Let us describe this procedure in Algorithm 8. It is presented in Appendix 0.A.
Secondly, assume that is a suffix of , i.e. and . The function is similar to the previous one. The difference is the following. Let be the left child of , and let be the right child of . Firstly, we update , because is a suffix. Secondly, we do the following action.

If , then we go to the right child . Additionally, if , then we color because a segment of has no intersection with and we keep its color .

If , then we go to the left child . Additionally, we color and update , because of is a subsegemnt of .
Finally, let us consider a general case for , i.e. and . Assume that we observe a node and an associated segment . If is a leaf, then we assign and stop. Otherwise, we continue. We use a variable for an existing color. Initially . If on some step and , then we assign . If or , then we do not change . We update , .

If , then we go to the right child . Additionally, if , then we color because a segment of has no intersection with and keeps its color .

If , then we go to the left child . Additionally, if , then we color because a segment of has no intersection with and we keep its color .

If , then we split our segment to and . The segment is a suffix of the segment tree with the root . For coloring it, we invoke . The segment is a prefix of the segment tree with the root . For coloring it, we invoke .
Let us describe this procedure in Algorithm 10. It is presented in Appendix 0.A.
Let us discuss properties of the algorithm.
Lemma 3
Algorithm 10 works correct with time complexity.
Proof
If the segment tree stores correct colors for segments, then the correctness of the algorithm follows from the description. The algorithm colors a required segment and keeps the color of the rest part.
Algorithm 8 and Algorithm 9 on each step change a vertex to a vertex on the next level. The tree is full binary tree, therefore, the tree has levels. Hence, the time complexity of these two algorithms is because . Algorithm 10 on each step changes a vertex to a vertex on the next level then stops and invokes Algorithm 8 and Algorithm 9. Its time complexity also . We can say that procedures runs consistently. Therefore, the total time complexity is also .
3.1.4 Request the Closest Colored Element Query
Assume that we want to get the minimal index of a colored element from a segment , where . Let be a segment of the root of the segment tree. Let us describe two specific cases that are requesting from a prefix of and requesting from a suffix of .
Firstly, assume that is a prefix of , i.e. and . Assume that we observe a node and an associated segment . Let be the left child of , and let be the right child of . We do the following action.

If , then then we go to the left child .

If and (i.e there are colored elements in the left child ), then the result is and we stop the process.

If and (i.e there is no colored element in the left child ), then we go to the right child .
If there is no colored elements in , then algorithm returns . Let us describe this procedure in Algorithm 11. It is presented in Appendix 0.A.
Secondly, assume that is a suffix of , i.e. and . Assume that we observe a node and an associated segment . Let be the left child of , and let be the right child of . We do the following action.

If , then then we go to the right child .

If and (i.e there are colored elements in the left child ), then we go to the left child .

If and (i.e there is no colored element in the left child ), then the result is and we stop the process.
If there are no colored elements in , then it returns . Let us describe this procedure in Algorithm 12. It is presented in Appendix 0.A.
Finally, let us consider the general case, i.e. . Assume that we observe a node and an associated segment . Let be the left child of , and let be the right child of . We do the following action.

If , then we go to the right child .

If , then we go to the left child .

If , then we split our segment to and . The segment is a suffix of the segment tree with the root . We invoke . If the result is not , then we return the result of the procedure. If the result is , then we invoke and we return the result of the procedure.
If there are no colored elements in , then the algorithm returns .
We call this function . We can define the function that returns the maximal index of a colored element symmetrically. We call it . Let us duscuss the properties of the procedures.
Lemma 4
and work correct with time complexity.
Proof
The proof is similar to the proof of Lemma 3.
3.2 HeavyLight Decomposition
Heavylight decomposition is a decomposition of a tree of size to a set of paths . The technique is presented in [11, 19]. It has the following properties:

Each vertex of the tree belongs to exactly one path from , i.e. all paths have no intersections and they cover all nodes of the tree.

for any vertex a path from to the root of the tree contains vertexes of at most paths from .

Let us consider a vertex and a path such that . Then, is a vertex that belongs to and has the minimal height.

For a node of the tree, let be a path from that contains .

For a node of the tree, let be an index of an element of a path . For an index of an element of a path , let be a vertex of the tree.

We can construct the set with time complexity.
3.3 Lowest Common Ancestor
We use the Lowest Common Ancestor (LCA) problem in our algorithm.
Lowest Common Ancestor(LCA) problem For two nodes and of a tree, the Lowest Common Ancestor is a node such that is ancestor of and and is closest for and nodes.
4 The Fast Online Algorithm for servers Problem on Trees
Let us describe an competitive algorithm for servers problem from [7].
ChrobakLarmore’s competitive algorithm for servers problem from [7]. Let us have a query on a vertex and servers are in vertexes . Let a server be active if there is no other servers on the path from to . In each phase, we move each active servers to one step towards the vertex . After each phase, the set of active servers can be changed. We repeat phases (moves of servers) until one of the servers reaches the query vertex .
The naive implementation of the algorithm has time complexity for each query. It can be the following. Firstly, we run the Depthfirst search algorithm with time labels [8]. Using it, we can put labels to each node that allows us to check for any two vertexes and , whether is an ancestor of in . After that, we can move each active server to query step by step. Together all active servers cannot visit more than vertexes.
Here we present an effective implementation of ChrobakLarmore’s algorithm. The algorithm contains two parts that are preprocessing and query processing. The preprocessing part is done once and has time complexity (Theorem 4.1). The query processing part is done for each query and has time complexity (Theorem 4.2).
4.1 Preprocessing
We do the following steps for preprocessing:

We construct a Heavylight decomposition for the tree. The properties of decomposition are described in Section 3.2. Assume that we have subroutine for constructing .

For each path we construct a segment tree that will be used for coloring problem that is described in Section 3.1. Assume that we have subroutine for constructing a segment tree for a path . Let be a segment tree for a path .

Additionally, for each vertex we compute a distance from the st (root) node to node. We call it . We can do it using Depthfirst search algorithm [8].
The computing is simple algorithm, but we present it for completeness. Let us consider a vertex and set of children of the vertex . Then, for any , we have . Additionally, . So, the distance computing is presented in recursive Algorithm 3
Finally, we have the following algorithm for preprocessing (Algorithm 4).
Let us discuss the properties of the preprocessing part of the algorithm.
Theorem 4.1
Algorithm 4 for preprocessing has time complexity
Proof
As it was mentioned in Section 3.2 the time complexity of Heavylight decomposition construction is .
Due to Lemma 1, time complexity of is . The total time complexity of constructing all segment trees is because of property of the decomposition.
Time complexity of ComputeDistance is . Therefore, the total time complexity is .
4.2 Query Processing
Let us have a query on a vertex and servers are in vertexes . We do the following steps:
Step 1. Let us sort all servers by the distance to the node . We can compute a distance between a node and a node by the following way. Let be a lowest common ancestor of and . Then, . We can use a HeapSort algorithm [20, 8] or other fast sorting algorithms with time complexity . Let be the sorting procedure. On the following steps we assume that for .
Step 2. The first server from processes the query. We move them to node and color all nodes of a path from to to color . The color of a vertex shows the number of a server that visited the vertex. A detailed description of this step is in Section 4.2.1. Let the coloring process be implemented as a procedure .
Step 3. For we consider a server from . It will be inactive when some other server with a smaller index becomes closer to a query than th server. Let be the index of the server such that the th server becomes inactive because of the th server. For obtaining , we search the closest to colored vertex on the path from to . The color of this vertex is . Let the search of the closest colored vertex be implemented as a procedure . It is described in Section 4.2.2.
Let the obtained vertex be and its color is . The th server reaches the node in steps. After that the th server becomes inactive. So, we should move the server to a vertex to on the path from to . Let the moving process be implemented as a procedure . It is described in Section 4.2.3. Then, we color to color all vertexes on the path from to .
Let us describe the procedure as Algorithm 5
4.2.1 Coloring of a Path
Let us consider a problem of coloring vertexes on a path from a node to a node . The color is .
Let be a LCA of and . Let be paths that contains vertexes of a path from to and let be paths that contains vertexes of a path from to . Let
and
Then, the coloring process is for and for .
The procedure is presented as Algorithm 6.
Let us discuss time complexity of the algorithm
Lemma 6
Time complexity of Algorithm 6 is .
4.2.2 The Search of the Closest Colored Vertex
Let us consider the problem of searching the closest colored vertex on the path from to . The idea is similar to the idea from the previous section.
Let be a LCA of and . Let be paths that contains vertexes of a path from to and let be paths that contains vertexes of a path from to . Let and Then, the searching process is the following. We invoke for . We stop on the minimal such that a result is not . If all of them are , then we continue. We invoke for . We stop on the maximal such that a result is not . The procedure is presented as Algorithm 7.
Let us discuss time complexity of the algorithm
Lemma 7
Time complexity of Algorithm 7 is .
Comments
There are no comments yet.