An Optimal Labeling Scheme for Ancestry QueriesThis research is supported in part by the ANR projects ALADDIN and PROSE, and by the INRIA project GANG.

# An Optimal Labeling Scheme for Ancestry Queries††thanks: This research is supported in part by the ANR projects ALADDIN and PROSE, and by the INRIA project GANG.

Pierre Fraigniaud
CNRS and Univ. Paris Diderot
pierre.fraigniaud@liafa.jussieu.fr
Amos Korman
CNRS and Univ. Paris Diderot
amos.korman@liafa.jussieu.fr
###### Abstract

An ancestry labeling scheme assigns labels (bit strings) to the nodes of rooted trees such that ancestry queries between any two nodes in a tree can be answered merely by looking at their corresponding labels. The quality of an ancestry labeling scheme is measured by its label size, that is the maximal number of bits in a label of a tree node.

In addition to its theoretical appeal, the design of efficient ancestry labeling schemes is motivated by applications in web search engines. For this purpose, even small improvements in the label size are important. In fact, the literature about this topic is interested in the exact label size rather than just its order of magnitude. As a result, following the proposal of a simple interval-based ancestry scheme with label size bits (Kannan et al., STOC ’88), a considerable amount of work was devoted to improve the bound on the size of a label. The current state of the art upper bound is bits (Abiteboul et al., SODA ’02) which is still far from the known bits lower bound (Alstrup et al., SODA’03).

In this paper we close the gap between the known lower and upper bounds, by constructing an ancestry labeling scheme with label size bits. In addition to the optimal label size, our scheme assigns the labels in linear time and can support any ancestry query in constant time.

## 1 Introduction

### 1.1 Background

In this paper we consider the following problem. Given an -node rooted tree , label the nodes of in the most compact way such that given any pair of nodes and , one can determine whether is an ancestor of in by merely inspecting the labels of and . The main quality measure used to evaluate such an ancestry labeling scheme is the label size, that is, the maximum number of bits stored in a label of a node, taken over all nodes in all possible -node rooted trees.

Among other things, the above elegant problem is not only of fundamental interest but is also useful for performance enhancement of XML search engines. In the context of this application, each indexed document is a tree, and the labels of all trees are maintained in the main memory111Details on XML search engines and their relation to ancestry labeling schemes can be found, e.g., [1, 2, 8].. Therefore, even small improvements in the label size are important, and, in fact, the literature about this topic is interested in the exact label size rather than just its order of magnitude (e.g., label size bits is considered significantly better than label size bits222All logarithms in this paper are taken in base 2.).

Ancestry schemes which are currently being used by actual systems are variants of the following simple interval-based ancestry labeling scheme [16] (see also [18]). Given an -node tree , perform a DFS traversal in starting at the root, and provide each node with a DFS number in the range . Then the label of a node is simply the interval , where is the descendant of with largest DFS number. An ancestry query then amounts to an interval containment query between the corresponding labels: a node is an ancestor of a node if and only if . Clearly, the label size of this scheme is bits.

An elegant lower bound of bits on the label size is given in  [3]. This lower bound holds even for a very restricted family of trees, each composed of equal length simple paths hanging down from the root.

In the other direction, a considerable amount of research has been devoted to improve the upper bound on the label size as much as possible beyond the trivial bound [1, 2, 6, 8, 15, 20]. Specifically, [2] gave a first non-trivial upper bound of bits. This was improved the year after to [6], which is the current best upper bound (that scheme is described in detail in the joint journal publication [1]). In addition to its relatively small label size, the scheme in [6] also assigns labels in linear time and can answer any ancestry query in constant time. Independently of that work, an ancestry labeling scheme with larger label size of was given in [20].

Following the above results, two other works were published, which focused on particular types of trees. Specifically, an experimental comparison of different ancestry labeling schemes on XML trees that appear in real life can be found in [15]. Recently, [8] gave an ancestry labeling scheme which is efficient for trees of small depth; specifically, for -node trees with depth , their scheme uses labels of size .

### 1.2 Our results

In this paper we close the gap between the known lower and upper bounds, by constructing an ancestry labeling scheme for general rooted -node trees with label size . This solves one of the main open problems in the field of informative labeling schemes. In addition to the optimal label size, our scheme assigns the labels to the nodes of a tree in linear time, and guarantees that any ancestry query can be answered in constant time.

### 1.3 Related work

As explained in [16], the names of nodes in traditional graph representations reveal no information about the graph structure and hence memory is wasted. Moreover, typical representations are usually global in nature, i.e., in order to derive useful information, one must access a global data structure representing the entire network, even if the sought information is local, pertaining to only a few nodes. In contrast, the notion of informative labeling schemes, introduced in [16], involves an informative method for assigning labels to nodes. Specifically, the assignment is made in a way that allows one to infer information regarding any two nodes directly from their labels, without using any additional information sources. Hence in essence, this method bases the entire representation on the set of labels alone. This method was illustrated in [16], by giving two elegant and simple labeling schemes for -node trees: one supporting adjacency queries and the other supporting ancestry queries. Both schemes incur label size.

As mentioned earlier, ancestry labeling schemes were further investigated in [1, 3, 2, 6, 8, 15, 20], and the current state of the art upper and lower bounds are and , respectively. Adjacency labeling schemes on trees were also further investigated in an attempt to optimize the label size beyond the simple bound of [16]. The current state of the art upper bound [5] for that problem is .

Labeling schemes were also proposed for other decision problems on graphs, including distance [3, 10, 19], routing [7, 13, 20], flow [14, 11], vertex connectivity [12, 11], nearest common ancestor [4, 17], and various other tree functions, such as center, separation level, and Steiner weight of a given subset of vertices [17]. See [9] for a partial survey on labeling schemes.

## 2 Preliminaries

Let be a tree rooted at some node referred as the root of . For two nodes and in , we say that is an ancestor of if and is one of the nodes on the shortest path connecting and in . For every non-root node , let denote the parent of , i.e., the ancestor of at distance 1 from it. A node is a descendant of if and only if is an ancestor of .

The depth of a node is defined as the distance from to the root of , i.e., the number of edge traversals from to the root. In particular, the depth of the root is 0. The size of , denoted by , is the number of nodes in . The weight of a node , denoted by , is defined as 1 plus the number of descendants of , i.e., is the size of the subtree hanging down from . In particular, the weight of the root is . Let denote the family of all rooted trees of size at most .

An ancestry labeling scheme for the family of trees is composed of the following two components:

1. A marker algorithm that, given a tree , assigns labels (i.e., bit strings) to its nodes.

2. A decoder algorithm that, given any two labels and in the output domain of , returns a boolean .

These components must satisfy that if and denote the labels assigned by the marker to two nodes and in some rooted tree , then

 D(L(u),L(v))=1⟺u is an ancestor of v in T.

It is important to note that the decoder is independent of the tree . That is, given the labels of two nodes, the decoder decides the ancestry relationship between the corresponding nodes without knowing to which tree in they belong to.

The common complexity measure used to evaluate the quality of an ancestry labeling scheme is the label size, that is the maximum number of bits in a label assigned by the marker algorithm to any node in any tree .

When considering the query time of the decoder, we use the RAM model of computation, and assume that the length of a computer word is bits. Similarly to [6], our decoder algorithm uses only the basic and fast RAM operations such as addition, substraction, left/right shifts and less-than comparisons. Our scheme avoids the sometimes more costly operations such as multiplication, division or non-standard operations which are pre-computed and stored in a pre-computed table.

##### Notations.

For every two nodes and in , let denote the shortest path connecting and in the tree (including and ), and let .

For two integers , let denote the set of integers . We refer to this set as an interval. For two intervals and , we say that if . The size of an interval , denoted by , is the number of integers in , i.e, .

## 3 Modifying the interval containment test

Our scheme is inspired by the scheme in [8] which was designed for trees of bounded depth. Given a rooted tree , the label assigned to each node by the scheme in [8] is a pointer to some interval, and an ancestry query between any two given nodes is answered by a simple interval containment test between the corresponding intervals. The underlying idea of that scheme consists in proving that, if is of small depth, then one can choose the intervals from a small set of intervals in which the intervals are well nested within themselves. A pointer to an interval in can be encoded using bits, and thus, since is relatively small, the scheme uses short labels. Unfortunately, this approach is no longer efficient when the tree has long paths. Indeed, in that case, the set of nested intervals becomes too large.

Informally, enforcing the decoder to be merely an interval containment test imposes a strong constraint on the way the intervals must be organized in . For arbitrary trees, we could not find a way to bypass this constraint while keeping small. Instead, we introduce a decoder which, on the one hand, makes the ancestry test somewhat more complicated than when using the interval containment test (yet the test remains very simple), but, on the other hand, enables to organize and nest the intervals in such a way that labels become very small. Our new decoder exploits the fact that intervals can be partially ordered not only by the containment relation, but also by the relation introduced in the previous section.

Given any node of some rooted tree , we associate with an interval , and with a supervisor node, denoted by , which is either itself or one of its ancestors. For this purpose, we first mark each node as either heavy or light as follows. For every non-leaf node of , let be the set of children of that satisfy for every child of . Among the nodes in , select an arbitrary node, and call it heavy. A node which is not heavy is called light. (In particular, the root is light).

For each node , define the supervisor of , denoted by , as the light node of largest depth on the path connecting to the root . Note that if is light then is itself; in particular, , and . Observe also that if is an ancestor of , then either , or is an ancestor of . See Figure 1.

As we will show later, the basic rule of our decoder relies on the following definition which is a modification of the interval containment test used in several previous schemes.

###### Definition 1

Let us consider a set of intervals for a tree . We assume that all intervals in the set are distinct, i.e., for any two distinct nodes and . We say that the decoding conditions hold at w.r.t. if and only if

• d1: , and

• d2: or .

Given the labels and of two nodes in a rooted tree, our boolean decoder outputs 1 if and only if the decoding conditions hold at w.r.t. . Our marker algorithm will then guarantee the following:

• The decoder is correct, i.e., the intervals associated with each node are selected such that, for any two nodes and , the decoding conditions hold at w.r.t. if and only if is an ancestor of ;

• given a tree , the interval and labels can be assigned to all nodes in in linear time;

• given a label , the intervals and can be computed in constant time;

• each label is encoded using bits.

## 4 The logn+O(loglogn) ancestry labeling scheme

We are now ready to prove our main result, that is:

###### Theorem 1

There is an ancestry labeling scheme for with label size and constant query time. Moreover, given a tree , the labels can be assigned to the nodes of in linear time.

We prove Theorem 1 by constructing an ancestry labeling scheme with the desired properties.

### 4.1 The marker algorithm M

For simplicity of presentation assume that is a power of 2, and let us fix a tree . Our marker algorithm first assigns an interval to each node in a way such that is an ancestor of if and only the decoding conditions hold at w.r.t. . For this purpose, we first show that it is sufficient to provide an assignment of intervals that satisfies a more “local” condition.

#### 4.1.1 The local partial order conditions

Let us first assign numbers from 0 to to the nodes according to a DFS traversal that starts at the root, and visits light children first. We denote by the DFS number of . Let . We define the local quasi-ancestors of , denoted by , as all nodes in , together with their light children, but removing , , and all nodes that have DFS numbers higher than . See Figure 1 for an example. Note that the local quasi-ancestors of a node may not form a connected subtree of .

###### Definition 2

Let us consider a set of pairwise distinct intervals for a tree . For every node , we say that satisfies the local partial order (lpo) conditions if the two conditions below are satisfied:

• lpo1: for every non root node ;

• lpo2: for every local quasi-ancestor .

###### Claim 1

If every node satisfies the local partial order condition lpo1, then for any light node , and any descendent of , we have .

Proof.  The claim is established by induction on the distance between and . If then the claim holds by lpo1. Assume that the claim holds for for , and assume . If then the claim follows by lpo1. Thus assume . If then again the claim follows by lpo1. Thus assume also . In this case there exists a light node on the shortest path connecting to . The claim then follows by induction.

The following claim relates the decoding conditions to the local partial order conditions.

###### Claim 2

If every node satisfies the local partial order conditions lpo1 and lpo2, then for every two different nodes and , is an ancestor of if and only if the decoding conditions d1 and d2 hold at w.r.t. .

Proof.  Assume that every node satisfies the local partial order conditions. Consider first the case that is an ancestor of . Since is a descendent of , either or is a descendent of . Thus, by Claim 1, . Since , , i.e., d1 follows. If is light then the fact that and satisfy d2 follows trivially from the fact that, in this case, . So assume now that is heavy. If , then d2 follows from lpo2. Otherwise, if , then there exists a light node that is an ancestor of and such that . d2 follows by combining lpo2 with Claim 1.

Consider now the case that is not an ancestor of . We need to show that either d1 or d2 does not hold. Let be the light node of largest depth on the path which is an ancestor of . If is itself then is either or a descendant of . Therefore, by Claim 1, we have , and thus d1 is not satisfied. In the remaining proof we thus assume that .

For a node which is a descendant of , and which satisfies , let be the light node of smallest depth on the path . Assume first that . If also then , and thus, by lpo2, . Since , we have , and therefore d2 is not satisfied. If, on the other hand, then we have and . Similarly to the previous case, this implies that d2 is not satisfied.

Assume now that . We have . If is an ancestor of then . Consequently, and thus d1 does not hold. On the other hand, if is not an ancestor of then we are left with two cases: and . If then since . Thus d1 does not hold. Finally, if then either or . Since and , it follows that d1 does not hold.

#### 4.1.2 The interval assignment

By Claim 2, one of our goals is to let the marker assign intervals that satisfy the local partial order conditions at each node. For integers and , let

 Ik,a,b=[2ka,2k(a+b)].

For , define the set of intervals:

 Ik={Ii,a,b∣i∈[1,k],a∈[1,4nlogn2i],~{% }and~{}b∈[1,4logn]}.

Let .

###### Definition 3

Let . We say that a mapping is a legal interval-mapping if the mapping is one-to-one, and satisfies the local partial order conditions at each node of .

In order to show that there exists a legal interval-mapping from every tree in into , we use the following notation. For any interval , and, for any , , let

 Ik(J)={Ii,a,b∈Ik∣Ii,a,b⊆J}.
###### Lemma 1

For every , every tree , and every interval , such that , there exists an legal interval-mapping of into . Moreover this mapping can be computed in time.

Proof.  We prove the lemma by induction on . For , the lemma holds trivially. Assume now that the claim holds for with , and let us show that it also holds for . Clearly, if then we are done by induction.

Consider now the case where is of size , and let be an interval, such that . Our goal is to show that there exists a legal-interval mapping of into .

We make use of the following decomposition of . Let be the path from the root of to a leaf of such that every non-root node in is heavy. Let be the nodes of , ordered top-down, i.e., is the root of , is a leaf of , and for every , is the parent of .

For every , let be the rooted trees hanging down from the light children of . (If does not have any light child, which is the case, for example, for , then this set of trees is empty, or, in other words, ). One important property of these trees is that, for every and , , we have .

We now group the nodes in in disjoint trees , where as follows. A tree is either a single heavy node , for , or a subtree hanging from a light child of some , . Moreover, the trees are enumerated according to the DFS numbers of their roots as follows. Recall denotes the DFS number of node in . The trees are ordered such that if denotes the root of then for all . See Figure 2.

Consider now the interval such that , and express it as for some integer . Let be the smallest integer such that , and let be the smallest integer such that .

First, we assign the root to the interval . We now show that indeed . By definition of and , we have

 (a+b)2k+1=2k+2+((a−1)+(b−1))2k+1≤2k+2+α+4k|T|−2.

Since , we get that

 (a+b)2k+1<α+(4k+4)|T|−1.

Thus

 J′=[a2k,(a+b)2k+1]⊂[α,α+(4k+4)|T|−1]=J.

Therefore, since , and , we obtain that .

The rest of the nodes in are mapped as follows. First note that , and recall that . We break into consecutive intervals such that, for each , we have and . For each , since , we can use the induction hypothesis to map the nodes in to via a legal interval-mapping.

The fact that the above recursive mapping can be performed in linear time is obvious. It remains to show that the above mapping of into is indeed a legal interval-mapping. That is, we have to show that the set of intervals satisfies the local partial order conditions lpo1 and lpo2 at each node . The conditions hold trivially at the root of . The fact that the conditions hold for every node in , follows from the induction hypothesis, and because both , , and are all contained in . Finally, consider the root of . (Note that if is heavy then ). lpo1 holds trivially for because contains , and, by induction, the interval assigned to is contained in the interval . To establish that lpo2 holds, first observe that . On the other hand, for every , , and thus . Hence lpo2 holds for as well. Our mapping is thus a legal interval-mapping of into . This completes the proof of the lemma.

By taking in the above lemma, we obtain the following.

###### Corollary 1

Let . There exists a legal interval-mapping of into .

#### 4.1.3 The label assignment

We are now ready to describe the label assigned to every node by our marker algorithm . Given a rooted tree , the marker first marks each node as either heavy or light, and then assigns the dfs numbers. This clearly takes linear time. Then the marker maps the nodes of into using the legal-interval mapping given in Corollary 1. Again, this step takes linear time.

Given a node , the marker uses the first least significant bits of to encode the interval . This can be done explicitly as is of the form for some , and .

Finally, the marker aims at encoding in the label of . However, using the method above to encode would consume yet another bits, which is obviously undesired. Instead, we use the following trick. Let , and be such that . (Note that if then we simply have , and ). Clearly, bits suffice to encode both and . To encode , the marker acts as follows. Let , and let be the largest integer such that . Recall that by definition . We have, because . Since , we also have . Thus . Finally, again since , we have , and thus . Combining the above inequalities, we get that . The marker now encodes the integer such that . This is done in consuming another bits. Hence, the following follows by construction:

###### Lemma 2

Given a tree , the marker assigns labels to the nodes of in linear time, and each label is encoded using bits.

### 4.2 The decoder D

Now, we describe our decoder . Given the labels and assigned by to two different nodes in some tree , the decoder needs to find whether is an ancestor of in . (Observe that since each node receives a distinct label, the decoder can easily find out if and are in fact the same node, and, in this trivial case, it simply outputs 0.)

The decoder inspects the first least significant bits of to extract (recall that is encoded by storing explicitly the three parameters , , and ). Then, once has been reconstructed from , the decoder aims at extracting . For this purpose, it first reconstructs and that have been explicitly encoded in the next bits. Then, it computes the largest integer such that . The decoder then proceeds by extracting , and computes . At this point, have reconstructed both and . Similarly, extracts by inspecting .

Finally, the boolean decoder outputs 1 if and only if the two decoding conditions d1 and d2 hold at w.r.t. (see Definition 1).

###### Lemma 3

Let and be two labels assigned by to two nodes in . The decoder performs in constant time, and satisfies if and only if is an ancestor of in .

Proof.  The fact that if and only if is an ancestor of in follows from the fact that the intervals are assigned by the marker via a legal-interval mapping (cf. Corollary 1). Since with all three parameters , , and stored explicitly, computing from can be achieved in constant time. (Note that , for example, can be obtained from but a simple shift of bits.) Similarly, can be extracted from in constant time. Computing just needs a simple substraction and a division by a power of 2, which again amounts to a simple shift operation. The lemma follows.

This completes the proof of Theorem 1.

## 5 Conclusion

Our ancestry labeling scheme is using labels of optimal size bits, to the price of a decoding mechanism based of an interval condition slightly more complex than the simple interval containment condition. Although this has no impact on the decoding time (our decoder still works in constant time), the question of whether there exists an ancestry labeling scheme with labels of size bits, but using solely the interval containment condition, is intriguing.

##### Acknowledgments:

the authors are very thankful to Sundar Vishwanathan and Jean-Sebastien Sereni for helpful discussions.

## References

• [1] S. Abiteboul, S. Alstrup, H. Kaplan, T. Milo and T. Rauhe. Compact labeling schemes for ancestor queries. SIAM Journal on Computing 35, (2006), 1295–1309.
• [2] Abiteboul, S., Kaplan, H., and Milo, T.: Compact labeling schemes for ancestor queries. In Proc. 12th ACM-SIAM Symp. on Discrete Algorithms (SODA), 2001.
• [3] S. Alstrup, P. Bille and T. Rauhe. Labeling Schemes for Small Distances in Trees. In Proc. 14th ACM-SIAM Symp. on Discrete Algorithms (SODA), 2003.
• [4] S. Alstrup, C. Gavoille, H. Kaplan and T. Rauhe. Nearest Common Ancestors: A Survey and a new Distributed Algorithm. Theory of Computing Systems 37, (2004), 441–456.
• [5] S. Alstrup and T. Rauhe. Small induced-universal graphs and compact implicit graph representations. In Proc. 43rd IEEE Symp. on Foundations of Computer Science (FOCS), 2002.
• [6] S. Alstrup and T. Rauhe. Improved labeling scheme for ancestor queries. In Proc. 13th ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 947-953, 2002.
• [7] P. Fraigniaud and C. Gavoille. Routing in trees. In Proc. 28th Int. Colloq. on Automata, Languages & Prog. (ICALP), LNCS 2076, pages 757–772, Springer, 2001.
• [8] P. Fraigniaud and A. Korman. Compact Ancestry Labeling Schemes for XML Trees. In Proc. 21st ACM-SIAM Symp. on Discrete Algorithms (SODA), 2010.
• [9] C. Gavoille and D. Peleg. Compact and Localized Distributed Data Structures. Distributed Computing 16, (2003), 111–120.
• [10] C. Gavoille, D. Peleg, S. Pérennes and R. Raz. Distance labeling in graphs. In Proc. 12th ACM-SIAM Symp. on Discrete Algorithms (SODA), pages 210–219, 2001.
• [11] M. Katz, N.A. Katz, A. Korman, and D. Peleg. Labeling schemes for flow and connectivity. SIAM Journal on Computing 34 (2004),23–40.
• [12] A. Korman. Labeling Schemes for Vertex Connectivity. ACM Transactions on Algorithms, to appear.
• [13] A. Korman. Improved Compact Routing Schemes for Dynamic Trees In Proc. 27th ACM Symp. on Principles of Distributed Computing (PODC), 2008.
• [14] A. Korman and S. Kutten. Distributed Verification of Minimum Spanning Trees. Distributed Computing 20(4): 253-266 (2007).
• [15] H. Kaplan, T. Milo and R. Shabo. A Comparison of Labeling Schemes for Ancestor Queries. In Proc. 19th ACM-SIAM Symp. on Discrete Algorithms (SODA), 2002.
• [16] S. Kannan, M. Naor, and S. Rudich. Implicit representation of graphs. SIAM J. on Discrete Math 5, (1992), 596–603.
• [17] D. Peleg. Informative labeling schemes for graphs. In Proc. 25th Symp. on Mathematical Foundations of Computer Science (MFCS), LNCS 1893, pages 579–588. Springer, 2000.
• [18] N. Santoro and R. Khatib. Labelling and implicit routing in networks. The Computer Journal 28, (1985), 5–8.
• [19] M. Thorup. Compact oracles for reachability and approximate distances in planar digraphs. J. of the ACM 51, (2004), 993–1024.
• [20] M. Thorup and U. Zwick. Compact routing schemes. In Proc. 13th ACM Symp. on Parallel Algorithms and Architecture (SPAA), pages 1–10, 2001.
You are adding the first comment!
How to quickly get a good reply:
• Give credit where it’s due by listing out the positive aspects of a paper before getting into which changes should be made.
• Be specific in your critique, and provide supporting evidence with appropriate references to substantiate general statements.
• Your comment should inspire ideas to flow and help the author improves the paper.

The better we are at sharing our knowledge with each other, the faster we move forward.
The feedback must be of minimum 40 characters and the title a minimum of 5 characters