Multivariate Analysis of Orthogonal Range Searching and Graph Distances

We show that the eccentricities, diameter, radius, and Wiener index of an undirected n-vertex graph with nonnegative edge lengths can be computed in time O(n·k+⌈logn⌉k·2klogn)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$O(n\cdot \left( {\begin{array}{c}k+\lceil \log n\rceil \\ k\end{array}}\right) \cdot 2^k \log n)$$\end{document}, where k is linear in the treewidth of the graph. For every ϵ>0\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\epsilon >0$$\end{document}, this bound is n1+ϵexpO(k)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$n^{1+\epsilon }\exp O(k)$$\end{document}, which matches a hardness result of Abboud et al. (in: Proceedings of the Twenty-Seventh Annual ACM-SIAM Symposium on Discrete Algorithms, 2016. 10.1137/1.9781611974331.ch28) and closes an open problem in the multivariate analysis of polynomial-time computation. To this end, we show that the analysis of an algorithm of Cabello and Knauer (Comput Geom 42:815–824, 2009. 10.1016/j.comgeo.2009.02.001) in the regime of non-constant treewidth can be improved by revisiting the analysis of orthogonal range searching, improving bounds of the form logdn\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\log ^d n$$\end{document} to d+⌈logn⌉d\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\left( {\begin{array}{c}d+\lceil \log n\rceil \\ d\end{array}}\right)$$\end{document}, as originally observed by Monier (J Algorithms 1:60–74, 1980. 10.1016/0196-6774(80)90005-X). We also investigate the parameterization by vertex cover number.


Introduction
Pairwise distances in an undirected, unweighted graph can be computed by performing a graph exploration, such as breadth-first search, from every vertex. This straightforward procedure determines the diameter of a given graph with n vertices and m edges in time O (nm). It is surprisingly difficult to improve upon this idea in general. In fact, Roditty and Vassilevska Williams [17] have shown that an algorithm that can distinguish between diameter 2 and 3 in an undirected sparse graph in subquadratic time would refute the Orthogonal Vectors conjecture.
However, for very sparse graphs, the running time becomes linear even for weighted graphs. For instance, the diameter of a star can be computed by finding the two largest edge weights. The diameter of a tree can be computed in linear time O(n) by a folklore result that traverses the graph twice. In other words, for graphs with vertex cover number 1 or treewidth 1, the running time is O(n).
The present paper investigates how these structural parameters influence the complexity of computing several graph distance measures. These measures are the eccentricity of every vertex (its maximum distance to any other vertex), the diameter and radius of the graph (the maximum and minimum eccentricities), and the Wiener index (the sum of the distances between all pairs of vertices); precise definitions are in Sect. 4.1. Throughout this paper we will write For every > 0 , the bounds in both cases are Since tw(G) ≤ vc(G) , the treewidth result is in some sense stronger. However, the vertex cover result is slightly faster, already contains the core algorithmic idea, and avoids many distracting technicalities.
Theorem 1 improves the dependency on the treewidth over the running time of Abboud, Vassilevska Williams, and Wang [1]. Previously, Cabello and Knauer [7] had shown that for constant treewidth k ≥ 3 , the diameter (and other distance parameters) can be computed in time O(n log k−1 n) , where the Landau symbol absorbs the exponential dependency on k as well as the time required for computing a tree n 1+ exp O tw(G) log tw(G) 1 3 decomposition. The bound in Theorem 1 is tight in the following sense. Abboud et al. [1] also showed that under the Strong Exponential Time Hypothesis of Impagliazzo, Paturi, and Zane [13], there can be no algorithm that computes the diameter with running time for k = vc(G) and (therefore also) k = tw(G) . In fact, this holds under the potentially weaker Orthogonal Vectors conjecture, see [20] for an introduction to these arguments. Thus, under this assumption, the dependency on k in Theorem 1 cannot be significantly improved, even if the dependency on n is relaxed from just above linear to just below quadratic. This closes an open question raised in [1]. Our analysis encompasses the Wiener index, an important structural graph parameter left unexplored by [1].
Perhaps surprisingly, the main insight needed to establish Theorem 1 has nothing to do with graph distances or treewidth. Instead, we make-or re-discoverthe following observation about the running time of d-dimensional range trees:

Lemma 2 ([16]) A d-dimensional range tree over n points supporting orthogonal range queries for the aggregate value over a commutative monoid has query time O(2 d B(n, d)) and can be built in time O(nd 2 B(n, d)).
This is a more careful statement than the standard textbook analysis, which gives the query time as O(log d n) and the construction time as O(n log d n) . For many values of d, the asymptotic complexities of these bounds agree-in particular, this is true for constant d and for very large d, which are the main regimes of interest in computational geometry. But crucially, B(n, d) is always n exp O(d) for any > 0 , while log d n is not.
Using known reductions, this implies that the following multivariate lower bound on orthogonal range searching is tight: Theorem 3 (Implicit in [1]) A data structure for the orthogonal range query problem in d dimensions for the monoid ( , max) with construction time n ⋅ q � (n, d) and query time q � (n, d) , where for some > 0 , refutes the Strong Exponential Time hypothesis.
We observe in the appendix that for unweighted graphs, the vertex cover result can be improved without using the techniques advertised in the present paper. (1) for any > 0 , Both of these bounds are n exp O(k) , matching (1). We do not know of a similar simplification for treewidth; the bound in Theorem 1.2, and the full construction behind it, seem to be the best we can do even for unit lengths.

Related Work
Abboud et al. [1] show that given a graph and a tree decomposition of width k, various graph distances can be computed in time O(k 2 n log k−1 n) . This bound is n 1+ exp O(k log k) for any > 0 . It is known how to compute an approximate tree decomposition with k = O(tw(G)) from the input graph G in time n exp O(tw(G)) [6], so from a given graph (without a tree decomposition) the algorithm from [1] works in time n 1+ exp O(tw(G) log tw(G)) , extending the construction of Cabello and Knauer [7] to superconstant treewidth. According to [7], the idea of expressing graph distances as coordinates was first mentioned by Shi [18].
If the diameter in the input graph is constant, the diameter can be computed in time n exp O(tw(G)) [12]. This is tight in both parameters in the sense that [1] rules out the running time (1) even for distinguishing diameter 2 from 3, and every algorithm needs to inspect Ω(n) vertices even for treewidth 1. For non-constant diameter Δ , the bound from [12] deteriorates as n exp O(tw(G) log Δ) . However, the construction cannot be used to compute the Wiener index.
The literature on algorithms for graph distance parameters such as diameter or Wiener index is very rich, and we refer to the introduction of [1] for an overview of results directly relating to the present work. A recent paper by Bentert and Nichterlein [2] gives a comprehensive overview of many other parameterisations.
Orthogonal range searching using a multidimensional range tree was first described by Bentley [3], Lueker [15], Willard [19], and Lee and Wong [14], who showed that this data structure supports query time O(log d n) and construction time O(n log d−1 n) . Several papers have improved this in various ways by factors logarithmic in n; for instance, Chazelle's construction [9] achieves query time O(log d−1 n) . In general, queries that report the points Q within a given range, instead of (like in the present paper) computing sums or maxima, incur an additional O(|Q|) term in the query time.

Discussion
In hindsight, the present result is a somewhat undramatic resolution of an open problem that has been viewed as potentially fruitful by many people [1], including the second author of this paper [12]. In particular, the resolution has led neither to an exciting new technique for showing conditional lower bounds of the form n 2− exp (k) , nor a clever new algorithm for graph diameter. Instead, our solution follows the ideas of Cabello and Knauer [7] for constant treewidth, much like in [1]. All that was needed was a better understanding of the asymptotics of bivariate functions, rediscovering a 40-year old analysis of spatial data structures [16] (see the discussion in Sect. 3.3), and using a recent algorithm for approximate tree decompositions [6].

3
Of course, we can derive some satisfaction from the presentation of asymptotically tight bounds for fundamental graph parameters under a well-studied parameterization. In particular, the surprisingly elegant reductions in [1] cannot be improved. However, as we show in the appendix, when we parameterize by vertex cover number instead of treewidth, we can establish even cleaner and tight bounds without much effort.
Instead, the conceptual value of the present work may be in applying the multivariate perspective on high-dimensional computational geometry, reviving an overlooked analysis for non-constant dimension. To see the difference in perspective, Chazelle's improvement [9] of d-dimensional range queries from log d n to log d−1 n makes a lot of sense for small d, but from the multivariate point of view, both bounds are n exp Ω(d log d) . The range of relationships between d and n where the multivariate perspective on range trees gives some new insight is when d is asymptotically just shy of log n , see Sect. 2.1. Table 1 summaries the known bounds for computing the diameter. It remains open to find an algorithm for diameter with running time n exp O(tw(G)) even for unweighted graphs, or an argument that such an algorithm is unlikely to exist under standard hypotheses. This requires better understanding of the regime d = o(log n).

Asymptotics
We summarise the asymptotic relationships between various functions appearing in the present paper:

Lemma 5
For any > 0, n exp o(vc(G)) Impossible under SETH, even given † and ‡ [1] The first expression shows that B(n, d) is always at least as good a bound as O(log d n) . The next two expressions show that from the perspective of parameterised complexity, the two bounds differ asymptotically: B(n, d) depends single-exponentially on d (no matter how small > 0 is chosen), while log d n does not (no matter how large is chosen). Our proof in fact establishes the stronger bound B(n, d) ≤ n + exp O(d) . Expression (5) just shows that (4)  Using that ↦ 2 log(e(1 + ) −1 ) is positive in the interval 0, 1 2 and tends to 0 for → 0 , we obtain d + h d ≤ n for any sufficiently small .
It remains to consider the case that ≥ c for some positive constant c depending only on . In this case, we have We turn to (4). Let > 0 and consider any function g such that for all n ≥ 1, Then log g(d) ≥ d log log n − log n . In particular, for n = 2 d , we have Finally for (5), we repeat the argument from [1]. If d ≤ log n∕ log log n then log d n = 2 d log log n ≤ n . In particular, if d = o(log n∕ log log n) then log d n ≤ n g(d) .
log d n = n o (1) . Moreover, for d ≥ log 1∕2 n we have log log n ≤ 2 log d and thus log d n = 2 d log log n ≤ 4 d log d . ◻ These calculations also show the regimes in which these considerations are at all interesting. For d = o(log n∕ log log n) both functions are bounded by n o (1) , and the multivariate perspective gives no insight. For d ≥ log n , both bounds exceed n, and we are better off running n BFSs for computing diameters, or passing through the entire point set for range searching.

Model of Computation
We operate in the word RAM, assuming constant-time arithmetic operations on coordinates and edge lengths, as well as constant-time operations in the monoid supported by our range queries. For ease of presentation, edge lengths are assumed to be nonnegative integers; we could work with nonnegative weights instead [7].

Preliminaries
Let P be a set of d-dimensional points. We will view every point p ∈ P as a vector p = (p 1 , … , p d ).
A commutative monoid is a set M with an associative and commutative binary operator ⊕ with identity. The reader is invited to think of M as the integers with −∞ as identity and a ⊕ b = max{a, b}.
Let f ∶ P → M be a function and define for each subset Q ⊆ P with the understanding that f (�) is the identity in M. See Fig. 1 for a small example.

Range Trees
Consider dimension i ∈ {1, … , d} and enumerate the points in Q as q (1) , for instance by ordering after the ith coordinate and breaking ties lexicographically. Define med i (Q) to be the median point q (⌈r∕2⌉) , and similarly min i (Q) = q (1) and max i (Q) = q (r) . Set For i ∈ {1, … , d} , the range tree R i (Q) for Q is a node x with the following associated values: , a reference to range tree T i+1 (Q) , called the secondary, associate, or higher-dimensional structure. Only exists for i < d.

Construction
Constructing a range tree for Q is a straightforward recursive procedure: Algorithm C (Construction). Given integer i ∈ {1, … , d} and a list Q of points, this algorithm constructs the range tree R i (Q) with root x.
Let Q L and Q R as given by (7), note that both are nonempty.
The data structure can be viewed as a collection of binary trees whose nodes x represent various subsets P x of the original point set P. In the interest of analysis, we now introduce a scheme for naming the individual nodes x, and thereby also the subsets P x . Each node x is identified by a string of letters from {L, R, D} as follows. Associate with x a set of points, often called the canonical subset of x, as follows. For the empty string we set P = P . In general, if Q = P x then P xL = Q L , P xR = Q R and P xD = Q . The strings over {L, R, D} can be understood as uniquely describing a path through in the data structure; for instance, L means 'go left, i.e., to the left subtree, the one stored at L[x]' and D means 'go to the next dimension, i.e., to the subtree stored at D [x].' The name of a node now describes the unique path that reaches it. Figure 2 shows (part of) the range tree for the points in Fig. 1.

Lemma 6 Let n = |P| . Algorithm C computes the d-dimensional range tree for P in time linear in nd 2 B(n, d).
Proof We run Algorithm C on input P and i = 1.
Disregarding the recursive calls, the running time of algorithm C on input i and Q is dominated by Steps C2 and C3, i.e., splitting Q into two sets of equal size. It is known that this task can be performed using O(|Q|) many comparisons [5]. Each (lexicographic) comparison can take d steps. Thus, the running time for constructing R i (Q) is linear in d|Q| plus the time spent in recursive calls.
This means that we can bound the running time for constructing T 1 (P) by bounding the sizes of the sets P x associated with every node x in the data structure. If X denotes the set of nodes in the data structure, then we want to bound Thus, we need to determine, for given p ∈ P , the number of subsets P x in which p appears. By construction, there are fewer than d occurrences of D in x. Set h = ⌈log n⌉ . Every L or R corresponds to cutting the current points set in half, so if x contains more than h occurrences that are either L or R then P x is empty. Thus, x has at most h + d letters. For two different strings x and x ′ that agree on the positions of D, the sets P x and P x ′ are disjoint, so p appears in at most one of them. We conclude To ease comprehension, leaf nodes are decorated with their canonical subset, which is a singleton from {p, q, r, s} . The reader can infer the canonical subset for an internal node as the union of leaves of the subtree; for instance, P DR = {r, s} . However, note that these point sets are not explicitly stored in the data structure that the number of sets P x such that p ∈ P x is bounded by the number of ways to arrange fewer than d many Ds and at most h non-Ds. Using the identity a + 0 0 repeatedly, we compute this number as The bound follows from aggregating this contribution over all p ∈ P . In summary, the running time becomes

◻
The running time in the above lemma can be improved with some effort to O(d 2 n log n + dnB(n, d)) , but this would not affect our overall results. Search.
In this section, we fix two sequences of integers l 1 , … , l d and r 1 , … , r d describing the query box B given by Algorithm Q(Query). Given integer i ∈ {1, … , d} , a query box B as above and a range tree R i (Q) with root x for a set of points Q such that every point q ∈ Q satisfies To prove correctness, we show that this algorithm is correct for each point set Q = P x .
Then the query algorithm on input x and i returns f (B ∩ P x ).
Proof We use backwards induction in |x|. d) .
If |x| = h + d then P x is the empty set, in which case the algorithm correctly returns the identity in M.
If the algorithm executes Step Q2 then B is satisfied for all q ∈ P x , in which case the algorithm correctly returns f [x] = f (P x ).
If the algorithm executes Step Q3 then B satisfies the condition in the lemma for i + 1 , and the number of Ds in P xD is i + 1 , and D[x] store the (i + 1) th range tree for P xD . Thus, by induction the algorithm returns f (P xD ∩ B) , which equals f (P x ∩ B) because P xD = P x .
Otherwise, by induction, Proof Correctness follows from the previous lemma. For the running time, we first observe that the query algorithm does constant work in each visited node. Thus it suffices to bound the number of visited nodes as We will show by induction in d that (8) is the correct bound for every call to a d-dimensional range tree for a point set P x , where h = ⌈log �P x �⌉ . In the base case, for d = 1 , we need to show that the number of visited nodes is at most for any height h. But this is just the standard observation that interval searching amounts to traversing two root-leaf paths, each of which contains at most h non-root nodes, and that the height of balanced binary search tree on n values is at most 1 + ⌈log n⌉. We now carefully consider the case for d ≥ 2 . The two easy cases are Q1 and Q2, which incur no additional nodes to be visited, so the number of visited nodes is 1, which is bounded by (8).
Step Q3 leads to a recursive call for a (d − 1)-dimensional range tree over the same point set P xD = P x , and we verify for h ≥ 1: The case h = 0 (i.e., P x is a singleton) is immediate. The interesting case is Step Q4. We need to follow two paths from x to the leaves of the binary tree of x. Consider the leaves l and r in the subtree rooted at x associated with the points min i (P x ) and max i (P x ) as defined in Sect. 3.2. We describe the situation of the path Y from l to x; the other case is symmetrical. At each internal node y ∈ Y , the algorithm chooses Step Q4 (because l i ≥ l[y] ). There are two cases for what happens at yL and yR . If l i ≤ med i (P y ) then P yR satisfies l i ≤ min i (P yR ) ≤ r i , so the call to yR will choose Step  (8). ◻

Discussion
The textbook analysis of range trees, and similar d-dimensional spatial algorithms and data structures sets up a recurrence relation like for the construction and for the query time. One then observes that n log d n and log d n are the solutions to these recurrences. This analysis goes back to Bentley's original paper [3]. Along the lines of the previous section, one can show that the functions n ⋅ B(n, d) and B(n, d) solve these recurrences as well. A detailed derivation can be found in [16], which also contains combinatorial arguments of how to interpret the binomial coefficients in the context of spatial data structures. A later paper of Chan [8] also takes the recurrences as a starting point, and observes asymptotically improved solution for the related question of dominance queries.

Preliminaries
We consider an undirected graph G whose edges have nonnegative integer weights. The set of vertices is V(G) and has size n. For a vertex subset U we write G[U] for the induced subgraph. The neighbourhood N(v) of v are the vertices that share an edge with v.
A path from u to v is called a u, v-path and denoted P. The length of a path, denoted l(P), is the sum of its edge lengths.
The distance from vertex u to vertex v, denoted d(u, v), is the length of a shortest u, v-path, i.e., the minumum of l(P) over all u, v-paths P. The Wiener index of G, denoted wien(G) is (u, v) . The eccentricity of a vertex u, denoted e(u) is given by e

Separated Eccentricities
We follow the construction of [7].
Given a graph G, let S x,w denote the set of shortest x, w-paths. We refine the notion of eccentricity to a subset W of vertices. Formally,

In particular, e(u) = e(u, V(G)) and e(u) = max{e(u, X), e(u, Y)} if X ∪ Y = V(G).
A vertex subset Z separates X and Y if every x, y-path with x ∈ X and y ∈ Y and x ≠ y contains a vertex from Z.
Enumerate Z = {z 1 , … , z k } . For i ∈ {1, … , k} define the ith eccentricity e i (x, Y) as the maximum distance from x to any vertex in Y 'via z i .' Formally, See Fig. 3 for a small example. The points corresponding to y ′ and y ′′ both belong to the rectangle for x ′ , certifying that there are shortest x ′ , y ′ -and x ′ , y ′′ -paths through z 3 . Right: Over R x ′ , the point p y ′ maximises f. We have

Lemma 9 If Z separates X and Y then e(x, Y)
Proof A shortest x, y-path with y ∈ Y must contain a vertex from Z, say z i . Thus, e(x, Y) ≤ e i (x, Y) . Conversely, e(x, Y) ≥ e j (x, Y) for all j ∈ {1, … , k} from the definition. ◻ Now we can write the eccentricity via z i as the distance to z i plus a range query: Lemma 10 Let i ∈ {1, … , k} and assume {z 1 , … , z k } separates X and Y. We will define a set of points { p y ∶ y ∈ Y } and a function f on this set as follows. Define for each y ∈ Y the k-dimensional point Define for each x ∈ X the rectangle Then Proof Consider a shortest x, y-path P containing z i ∈ Z . No other x, y-path is shorter than P, so in particular we have equivalently, which means p y ∈ R x . Moreover, if y is chosen so that P attains the eccentricity e i (x, Y) then e i (x, Y) = l(P) = d(x, z i ) + d(z i , y) and p y maximises f (p y ) = d(z i , y) over the points in R x . ◻ We note that the ith coordinate of p y is always 0 and of R y is always [−∞, 0] , so the reduction is actually to a (k − 1)-dimensional range query instance. However, we are mainly interested in the asymptotic dependency on k, so we avoid the possible (but tedious) improvement that arises from this observation.
We have arrived at the following algorithm:

Algorithm S (Separated Eccentricities). Given an undirected, connected graph G with nonnegative integer weights and vertex subsets X and Y such that V(G) = X ∪ Y and a separator Z of size k, this algorithm computes the eccentricity e(x, Y) of every vertex
for all j ∈ {1, … , k} , for all j ∈ {1, … , k} . d(z, v) for each z ∈ Z, v ∈ V(G) using k applications of Dijkstra's algorithm. Compute e(z, Y) = max y∈Y d(z, y) for each z ∈ Z. S2 [Build range trees.] For each i ∈ {1, … , k} , construct a k-dimensional range tree for the points { p y ∶ y ∈ Y } given by (9) using the monoid ( , max). S3 [Query range trees.] For each x ∈ X and for each i ∈ {1, … , k} query the ith range tree for the rectangle R x given by (10) and add d(x, z i ) . The result is

Algorithm S is correct by the observation in
Step S3 (based on Lemma 10) and Lemma 9. O km log n + n2 k B(n, k) .

Proof
The first term accounts for Step S1. Using Lemma 2, we see that Steps S2 and S3 take time O(|Y|k 2 ⋅ B(|Y|, k)) and O(|X|2 k ⋅ B(|Y|, k)) for each i ∈ {1, … , k} . Since k 2 = O(2 k ) and both |Y| and |X| are at most n, both expressions are asymptotically dominated by the second term. ◻

Parameterization by Vertex Cover Number
Graphs with small vertex cover number allow for a particularly simple application of the construction from Sect. 4.2, because the same small separator (namely, the vertex cover itself) separates every vertex from the rest of the graph. A vertex cover is a vertex subset C of V(G) such that every edge in G has at least one endpoint in C. The smallest k for which a vertex cover of size k exists is the vertex cover number of a graph, denoted vc(G) . The number of edges in such a graph is at most n ⋅ vc(G).

Proof of Theorem 1.1, distances Set
, and Z = C . Clearly, every vertex x is separated from all vertices by its neighbourhood N(x). Since C is a vertex cover, N(x) ⊆ C for all x ∉ C . Thus, Z separates X and Y. Note that we have e(x) = e(x, Y) , so it suffices to run algorithm S to compute all eccentricities. The running time is immediate from Lemma 11 with Z of size k ≤ vc(G) and m ≤ kn . From the eccentricities, the radius and diameter can be computed in linear time using their definition. ◻

Parameterization by Treewidth
In the more general case, not all paths from x need to pass through the separator Z. Therefore we cannot determine e(x) from e(x, Y) alone, as we did in Sect. 4.3. Instead, since e(x) = max{e(x, X), e(x, Y)} , it remains to compute e(x, X). However, e(x, X) is entirely determined by the subgraph G[X] (once we add shortcuts inside Z), so we can handle this recursively. The necessary recursive decomposition is provided by a tree decomposition. We need the approximate treewidth construction of Bodlaender et al. [6]. The analysis of the resulting recurrence for superconstant dimension follows from Abboud et al. [1]. We need a decomposition from [7]. Let k + 1 < n . A skew k-separator tree T of an n-vertex graph G is a binary tree such that each node t of T is associated with a vertex set Z t ⊆ V(G) such that • If L t and R t denote the vertices of G associated with the left and right subtrees of t, respectively, then Z t separates L t and R t and • T remains a skew k-separator even if edges between vertices of Z t are added.
It is known that such a tree can be found from a tree decomposition, and an approximate tree decomposition can be found in single-exponential time. We summarise these results in the following lemma:

Lemma 12 ([7, Lemma 3] with [6, Theorem 1]) For a given n-vertex input graph G, a skew (5tw(G) + 4)-separator tree can be computed in time n exp O(tw(G)).
We are ready for the algorithm.  The graph G has treewidth O(k), so it has O(nk) edges.

Algorithm E (Eccentricities). Given an undirected, connected graph G with nonnegative integer weights and a skew k-separator tree with root t, this algorithm computes the eccentricity e(v) of every vertex
Step E1 consists of n executions of Dijkstra's algorithm with n bounded by O(k 2 log k) . This takes time O(k 5 log 2 k) , which is bounded by O(2 k ) .
Step E3 was analysed in Lemma 11 and n2 k B(n, k)) . Accounting for the recursive calls in Step E4 for both X and Y using |Y| ≤ n − |X| + k , we arrive at the divide-and-conquer recurrence for some non-decreasing function S satisfying S(n, k) = O 2 k B(n, k) . We would expect this recurrence to solve to roughly S(n, k) ⋅ n log n if the partition were perfectly balanced and k were constant, but the dependence on k is not clear, so we give a careful analysis. The lemma is implied by the bound which we will show by strong induction in n for all k. Write s = |X| and r = n − s + k . By induction, we can bound From the bounds (12) on s we have s ≤ nk∕(k + 1) and r ≤ n − n∕(k + 1) + k = (nk∕(k + 1)) + k , so if we set then both s ≤ t and r ≤ t . Thus, we can get rid of s and r in the last term of (14) as Step E1 ensures k(k + 1) ≤ n∕(3 ln n) ≤ 1 3 n , so we get Using the bound ln y ≤ y − 1 for y > 0 , we have Using this in (15), and then going back to (14), we have where the last step uses k ln n ≤ n∕(3(k + 1)) , which is ensured by Step E1. Returning to the recurrence, we can now verify T(n, k) = O(k 5 log 2 k) , if n∕ ln n < 4k(k + 1) ; n ⋅ S(n, k) + T(|X|, k) + T(n − |X| + k, k) , otherwise, T(s, k) + T(r, k) s ln s + r ln r ≤ s ln t + r ln t = s ln t + (n − s + k) ln t = n ln t + k ln t ≤ n ln t + k ln n .

Proof of Thm. 1.2, distances
To compute all eccentricities for a given graph, we find a k-skew separator for k = 5tw(G) + 4 using Lemma 12 in time n exp O(tw(G)) . We then run Algorithm E, using Lemma 13 to bound the running time. From the eccentricities, the radius and diameter can be computed in linear time using their definition. ◻

Extension to Wiener Index
Algorithm E can be modified to compute the Wiener index, as described in [7,Sec. 4], completing the proof of Theorem 1. Instead of repeating those arguments, we content ourselves here with pointing out the necessary modifications in our presentation. The orthogonal range queries for vertex x ∈ X now need to report the sum of distances to every y ∈ Y , rather than just the value of the maximum distance e(x, Y). Such a query can be handled with our data structure and a more careful choice of monoid, but another technical issue appears. While the distance maxima satisfied e(x, Y) = max{e 1 (x, Y), … , e k (x, Y)} according to Lemma 9, no similar expression holds for distance sums. This is simply because there can be shortest x, y-paths via two different z i , and their contribution would lead to overcounting. The solution is to associate the distance d(x, y) with exactly one i ∈ {1, … , k} , namely the smallest i for which a shortest x, y-path passes through z i . This leads to the following (somewhat laborious) construction. For x ∈ X , partition Y = Y 1 ∪ ⋯ ∪ Y k into disjoint sets such that y ∈ Y i if and only if (i) there is a shortest x, y-path through z i and (ii) there is none through z j for j < i . Then define s i (x, Y) as the sum of distances from x to Y i : We observe that s 1 (x, Y) + ⋯ + s k (x, Y) is the sum of distances from x to all vertices in Y.
To compute s i (x, Y) , we modify the construction from Lemma 10 slightly. The coordinates of p y are as before. The rectangle R x associated with Following the proof of Lemma 10, the ' −1 ' above for j < i ensures that p y ∈ R x now also requires In other words, x, y-paths through z j for j < i cannot be shortest paths, so we have avoided overcounting. The domain of the function f in (9) is changed to the monoid of positive integer tuples (a, b) with the operation (a, b) ⊕ (a � , b � ) = (a + a � , b + b � ) with identity element (0, 0). The value associated with vertex p y is changed to f (p(y)) = (1, d(z i , y)). It then holds that the query for R x will return a tuple (N, S) where so that s i (x, Y) can be computed as These changes suffice to establish the Wiener index part of Theorem 1.1, finishing the argument for vertex cover.
To extend these results to the recursive construction for treewidth from Sect. 4.4 now only requires some delicacy regarding how sums of distances cross the separator. This part is carefully argued in [7,Lemma 8], and there is no reason to repeat it here. With the these changes, Theorem 1.2 is established.
Open Access This article is licensed under a Creative Commons Attribution 4.0 International License, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons licence, and indicate if changes were made. The images or other third party material in this article are included in the article's Creative Commons licence, unless indicated otherwise in a credit line to the material. If material is not included in the article's Creative Commons licence and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this licence, visit http://creat iveco mmons .org/licen ses/by/4.0/.

Eccentricities and Wiener Index
In a graph with vertex cover C, all paths from v ∉ C have their second vertex in C. If the graph is unweighted, two vertices v, w ∉ C with N(v) = N(w) have the same distances to the rest of the graph. Since N(v) ⊆ C it suffices to consider all 2 k many subsets of C. The details are given in Algorithm U.
Algorithm U (Unweighted graph). Given a connected, unweighted, undirected graph G and a vertex cover C, this algorithm computes the eccentricity of each vertex and the Wiener index. (v, u) , and remove w from D. Repeat step U3 until D is empty.

Theorem 14
The eccentricities and Wiener index of an unweighted, undirected, connected n-vertex graph and vertex cover number k can be computed in time O(nk2 k ).
Proof It is well known that a minimum vertex cover can be computed in the given time bound [11]. For the running time of Algorithm U, we already observed that for each v ∉ C the neighbourhood N(v) is entirely contained in C. Thus, there are only 2 k different neighbourhoods used as an index to D and we can bound the number of BFS computations in Step U3 by 2 k . (Step U2 incurs another k such computations.) The number of edges in a graph with vertex cover number k is O(nk). Assuming constanttime dictionary operations, the running time of the algorithm is therefore O(nk2 k ).
To see correctness, we need to argue that the distances computed for w ∈ D in Step 4 are correct. First, to argue d(v, z) = d(w, z) for all z ∉ {v, w} consider shortest paths P = vv 2 ⋯ z and Q = ww 2 ⋯ z , possibly with v 2 = z or w 2 = z . The suffix P � = v 2 ⋯ z is itself a shortest path, of length l(P) − 1 . Since N(v) = N(w) , the path wP ′ exists and is a shortest w, z-path as well, and therefore of length l(Q). We conclude that l(Q) = 1 + l(P � ) = 1 + l(P) − 1 = l(P).
It is not true that d(v, z) = d(w, z) for z ∈ {v, w} . Instead, we have d(v, w) = d(w, v) (both equal 2) and d(v, v) = d(w, w) (both equal 0). Thus, the contributions from v and w to W are the same, and the sets d(v, ⋅) and d(w, ⋅) have the same maxima. ◻ One way of implementing the neighbourhood-indexed dictionary within the required time bounds (and without using randomised hashing) is as trie of height k whose 01-labelled root-leaf paths describe (the binary representation of) subsets of C; at the leaves we store vertices in a linked list. Insertion requires k operations and happens n times in U1. The iteration over all vertices in U3 can be performed by traversing all the lists at all the leaves, in constant time per vertex. This establishes the second part of Theorem 4.

Faster Eccentricities
Vertex cover number is an extremely well studied parameter, so it makes sense to look for further algorithmic improvements. The best current algorithm for finding a vertex cover runs in time O(nk + 1.274 k ) [10], so the bound in Theorem 14 is dominated by the distance computation. Thus it may make sense to look for distance computation algorithms with running times of the form nk + g(k) rather than m ⋅ g(k) . We now present such an algorithm for eccentricities. (It does not work for the Wiener index.) First, we observe that if C is a vertex cover, then no path can contain consecutive vertices from V(G) − C . Thus, we can modify the graph by inserting length-2 shortcuts between nonadjacent vertices in C that share a neighbour without changing the pairwise distances in the graph. We can now run Dijkstra restricted to the subgraph G[C ∪ {v}] , noting that the second layer of the shortest path tree is contained in N(v), which is contained in C. Thus, the number of such computations that are different is bounded by 2 k , the number of neighbourhoods. The eccentricity e(v) can be derived from the shortest path tree as follows. Let E(v) denote the eccentric vertices from v in C, i.e., the vertices belonging to C at distance max u∈C d (v, u) . Note that E(v) contains exactly the vertices at the deepest layer of the shortest path tree from v in G[C ∪ {v}] . The only vertices u in G that can be farther away from v than E(v) must have their entire neighbourhood N(u) contained in E(v). See Fig. 4.
The only confusion arises if the only such vertex is v itself. To handle these details we need to determine, for each cover subset S ⊆ C , if the number of u with N(u) ⊆ S is 0, 1, or more. This can be solved by a fast zeta transform in time 2 k k , see [4], or more directly as follows. For each S ⊆ C , let (The third value is an arbitrary placeholder.) Then h(S) can be computed for all S ⊆ C in a bottom-up fashion.
The details are given in the following algorithm.
Algorithm F (Faster Eccentricities Parameterized by Vertex Cover). Given a connected, unweighted, undirected graph G and a vertex cover C, this algorithm computes the eccentricity of each vertex.  Proof If G has only one or two vertices, we calculate all distances naïvely. Otherwise, we run Algorithm F.
Step F1 needs to visit every of the nk edges. The first part of Step F2 visits at most n − k vertices and spends time O(k) at each. There are 2 k subsets of C, bounding the running time of the second part of Step F2 to O(2 k k) .
Step F3 can be performed in time O(2 k k 2 ) (instead of the obvious O(nk 2 ) ) by iterating over w ∈ D and all pairs u, v ∈ N(w) . The shortest path computations in Steps F4 and F5 take time O(k 2 ) each using Dijkstra's algorithm, for a total of O(2 k k 2 ) . The dictionary contains at most n values, so the total time of Step F4 and F5 is O(n + 2 k k 2 ) , assuming that the dictionary allows iteration over the vertices u ∈ D with N(u) = N(v) with constant overhead.
To see correctness, assume that we already performed the shortcut operation in Step F3. We argue for correctness of Step F5, Step F4 is similar. Let u ∈ V(G) and consider an eccentric vertex z and a shortest u, z-path P. We want to show that the algorithm sets e(u) to the length of P.
First assume there exists such an eccentric vertex z belonging to C. Then z ∈ E(u) . Moreover, there can be no vertex w ≠ u with N(w) ⊆ E(u) , because otherwise w would have a shortest path from u through E(u) and therefore be farther away than z. Thus, Step F5 picks the second branch and correctly sets e(u) to d = d(u, z).
Otherwise, none of the eccentric vertices z belong to C. There are a number of cases. If P is just the edge uz then every vertex in G has distance at most 1 to u. If G is a star then C = N(z) = E(u) = {u} and d = 0 . Moreover, some third vertex w has its neighbourhood (namely, {u} ) completely contained in E(u) so Step F5 correctly sets e(u) = d + 1 = 1 . If P = uz and G is not a star then it must contain a triangle, so |C| > 1 (possibly containing u) and the vertices in E(u) (which could be many) are all at distance 1. Moreover, there cannot exist w ≠ u with N(w) ⊆ E(u) because then there would be a shortest u, w-path of length 2. Thus, Step F5 correctly sets e(u) to d = 1.
The remaining case is when the u, z-path P with z ∉ C contains at least 3 vertices. Let w denote the penultimate vertex, so the P is of the form u ⋯ wz . Since z ∉ C , we have w ∈ C . The distance from u to w is therefore d = max w∈C d (u, w) . It remains to argue that the assignment to e(u) in Step F5 picks the second branch. Consider the neighbourhood N(z). Every neighbour x of z must belong to C. Moreover, its distance d(u, x) is d, because if it were longer then w would not belong to E(u), if it where shorter then there would be a shorter path u ⋯ xz than P. Therefore x ∈ E(u) . Thus, we have established that N(z) ⊆ E(u) , so we conclude that Step F5 correctly sets e(u) to d + 1 . ◻ This establishes the first part of Theorem 4.