Abstract 1 Introduction 2 Preliminaries 3 Diameter in Graphs with Bounded Treewidth 4 Mean Distance in Graphs with Bounded Treewidth 5 Conclusion References

Algorithms for Distance Problems
in Continuous Graphs

Sergio Cabello ORCID Faculty of Mathematics and Physics, University of Ljubljana, Slovenia
Institute of Mathematics, Physics and Mechanics, Ljubljana, Slovenia
Delia Garijo ORCID University of Seville, Spain Antonia Kalb ORCID Technical University of Dortmund, Germany Fabian Klute ORCID Universitat Politècnica de Catalunya, Barcelona, Spain Irene Parada ORCID Universitat Politècnica de Catalunya, Barcelona, Spain Rodrigo I. Silveira ORCID Universitat Politècnica de Catalunya, Barcelona, Spain
Abstract

We study the problem of computing the diameter and the mean distance of a continuous graph, i.e., a connected graph where all points along the edges, instead of only the vertices, must be taken into account. It is known that for continuous graphs with m edges these values can be computed in roughly O(m2) time. In this paper, we use geometric techniques to obtain subquadratic time algorithms to compute the diameter and the mean distance of a continuous graph for two well-established classes of sparse graphs. We show that the diameter and the mean distance of a continuous graph of treewidth at most k can be computed in O(nlogO(k)n) time, where n is the number of vertices in the graph. We also show that computing the diameter and mean distance of a continuous planar graph with n vertices and F faces takes O(nFlogn) time.

Keywords and phrases:
diameter, mean distance, continuous graph, treewidth, planar graph
Funding:
Sergio Cabello: Funded in part by the Slovenian Research and Innovation Agency (P1-0297, N1-0218, N1-0285). Funded in part by the European Union (ERC, KARST, project number 101071836). Views and opinions expressed are however those of the authors only and do not necessarily reflect those of the European Union or the European Research Council. Neither the European Union nor the granting authority can be held responsible for them.
Delia Garijo: Partially supported by grants PID2019-104129GB-I00 and PID2023-150725NB-I00 funded by MICIU/AEI/10.13039/501100011033.
Fabian Klute: Partially supported by grants PID2019-104129GB-I00 and PID2023-150725NB-I00 funded by MICIU/AEI/10.13039/501100011033.
Irene Parada: Serra Húnter Fellow. Partially supported by grants PID2019-104129GB-I00 and PID2023-150725NB-I00 funded by MICIU/AEI/10.13039/501100011033.
Rodrigo I. Silveira: Partially supported by grants PID2019-104129GB-I00 and PID2023-150725NB-I00 funded by MICIU/AEI/ 10.13039/501100011033.
Copyright and License:
[Uncaptioned image] © Sergio Cabello, Delia Garijo, Antonia Kalb, Fabian Klute, Irene Parada, and Rodrigo I. Silveira; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Design and analysis of algorithms
Related Version:
Full Version: https://arxiv.org/abs/2503.07769 [11]
Editors:
Pat Morin and Eunjin Oh

1 Introduction

Graph parameters dealing with distances provide fundamental information on the graph. The diameter, defined as the maximum distance between any two vertices of a graph, and the mean distance, which gives the average of all those distances, are natural concepts of great importance in real-world applications. While the diameter gives the maximum eccentricity in the graph, the mean distance provides a measure of its compactness, and is closely related to the sum of the pairwise distances of the graph and the well-known Wiener index.111The sum of the pairwise distances of a graph is the sum of distances between all ordered pairs of vertices, and half of this value is the Wiener index. This topological index has been studied extensively.

Computing the diameter and the sum of the pairwise distances of a given graph G is a central problem in algorithmic graph theory. A straightforward algorithm is to perform Dijkstra’s algorithm from each vertex, allowing to compute both parameters in O(nm+n2logn) time, for n and m the number of vertices and edges of G, respectively. Given the high computational cost of this approach, considerable effort has been invested in developing faster algorithms, especially for sparse graphs. It turns out that the general problem is notably difficult. In 2013, it was showed that in sparse graphs, no O(n2ε)-time algorithm can distinguish between diameter 2 and 3, unless the Strong Exponential Time Hypothesis fails [29]. From the proof, one can deduce the same conditional lower bound for computing the sum of the pairwise distances of the graph (see also [9]). This justifies the vast amount of ongoing research on identifying classes of sparse graphs for which these parameters can actually be computed in subquadratic time. Currently, such classes include graphs of bounded treewidth [1, 8, 12], graphs of bounded distance VC dimension [18, 26], median graphs [4, 5], and planar graphs [9, 22].

In this work, we tackle the challenge of subquadratic diameter and mean distance computation for continuous graphs (these objects are also called metric graphs in other areas closer to analysis [27, 19]). Our main motivation arises from geometric graphs. A geometric graph is an undirected graph where each vertex is a two-dimensional point, and each edge is a straight line segment between the corresponding two points. These graphs naturally arise in applications involving geographic information, such as road or river networks. The main characteristic of geometric graphs is that every point on each edge is considered part of the graph. Therefore, the graph can be considered an infinite point set. The diameter of a geometric graph is the maximum distance taken over its infinitely many pairs of points. The class of continuous graphs is actually more general than geometric graphs, and is formally defined in Section 2. Distances in continuous graphs, especially the diameter, have received a lot of interest recently, mainly in the context of augmentation problems [16, 13, 15, 14, 20, 2, 21, 23, 24]; see also [3] for results on the mean distance in the context of geometric analysis. Another well-known related problem about distances in graphs, also with a continuous aspect, is the computation of the absolute center of a graph, originally proposed by Hakimi [25].

The diameter and the mean distance of a continuous graph with m edges can be computed in roughly O(m2) time [13, 16, 21]222The algorithm to compute the diameter in [13] is for plane geometric graphs, but it also applies here.. For the diameter, this follows from the fact that, in a continuous graph, any pair of points attaining the diameter, called diametral pair, consists of either: (i) two vertices, (ii) two points on distinct non-pendant edges,333An edge uvE(G) is pendant if either u or v is a pendant vertex (i.e., has degree 1). or (iii) a pendant vertex and a point on a non-pendant edge [13, Lemma 6] (see Figure 1). Regarding the mean distance, one can show that it is given by a weighted sum of the mean distances of all ordered pairs of edges, which can be obtained in constant time, once the distance matrix of the vertices of the graph has been computed [21].

However, for sparse graphs, one hits again a quadratic running time barrier. Algorithms for diameter in discrete graphs do not carry over to continuous graphs, except in few situations (e.g., if there are only O(1) different edge weights, then O(1) Steiner points can be added to each edge, so that the diameter coincides with that of the continuous graph), and the same conditional lower bound of the discrete setting holds for the continuous case (one can reduce the continuous case to the discrete case by simply adding enough long paths to the graph.)

Figure 1: Types of diametral pairs of a continuous graph.

The main challenge for continuous graphs is that the techniques that have successfully worked to speed-up the computation of the diameter and the sum of the pairwise distances for discrete graphs do not seem to easily extend. The most similar setting to ours is perhaps that of planar graphs, for which recently the first subquadratic algorithms were discovered [9, 22]. These works use Voronoi diagrams in planar graphs to compute those values in the discrete setting. However, it is not clear whether they can be adapted to the continuous setting. More precisely, for a fixed source vertex and a fixed subgraph H of a graph G, they compute the Voronoi diagram of H using some additive weights. As the source moves, the additive weights defining the Voronoi diagram change, and the Voronoi diagrams change. Tracing those changes efficiently seems difficult, especially because the combinatorial structure of the Voronoi diagram may undergo important changes. Moreover, such changes can happen for several different movements of the sources. Thus, to achieve a subquadratic algorithm for planar continuous graphs, it seems that one should be able to treat those parallel changes in groups. The current technology for planar graphs does not seem ready for this.

Contributions.

In this work, we present subquadratic algorithms to compute the diameter and the mean distance for two classes of sparse continuous graphs. In Sections 3 and 4, We study continuous graphs of bounded treewidth and show how to compute, respectively, its diameter and its mean distance in subquadratic time. In fact, we consider the slightly more general framework of computing the diameter diam(,𝒢) and the mean distance mean(,𝒢) of a subgraph of a continuous graph 𝒢 with respect to 𝒢, which are the diameter and mean distance of 𝒢 when =𝒢. This concept appears naturally in our algorithm during the recursion. Theorems 1 and 2 below distinguish whether the treewidth is assumed to be constant, as done in [12], or a parameter, as done in [8].

Theorem 1 (Theorems 11 and 16).

Let k2 be an integer constant. Let G be a graph with n vertices, treewidth at most k, nonnegative edge-lengths, and let 𝒢 be the corresponding continuous graph. Let H be a subgraph of G and let 𝒢 be the corresponding continuous subgraph. We can compute the diameter diam(,𝒢) and the mean distance mean(,𝒢) in O(nlog4k2n) time.

Theorem 2 (Theorems 12 and 17).

Let G be a graph with n vertices, treewidth at most k, nonnegative edge-lengths, and let 𝒢 be the corresponding continuous graph. Let H be a subgraph of G and let 𝒢 be the corresponding continuous subgraph. We can compute the diameter diam(,𝒢) and mean distance mean(,𝒢) in n1+ε2O(k) time, for any fixed ε>0.

Similarly to previous algorithms in the discrete setting to compute the diameter and the sum of the pairwise distances for graphs of bounded treewidth [1, 8, 12], the key technique that we use is orthogonal range searching; see also [17] for a novel application of this technique to compute the eccentricity and the distance-sum of any vertex of a directed weighted graph.

Finally, we investigate planar graphs. For any n-vertex continuous plane graph, we show how to compute the maximum eccentricity (i.e., largest distance from a point) over all points on the boundary of a face in O(nlogn) time. Further, we show that the same approach can be used to compute the mean from all points of a face with respect to the continuous graph in O(nlogn) time. This allows us to compute the diameter and mean of continuous planar graphs in O(nFlogn) time, where F is the number of faces, which is subquadratic, if F=o(nlogn). (By Euler’s formula, F is the same for any embedding of a planar graph.)

Theorem 3.

For a continuous planar graph 𝒢 with n vertices and F faces, we can compute its diameter diam(𝒢) and its mean distance mean(𝒢) in O(nFlogn) time.

Note that some proofs and the section about planar graphs are deferred to the full version.

2 Preliminaries

2.1 Continuous Graphs

Consider an edge-weighted, connected graph G=(V(G),E(G),),where is a function that assigns a length (e)0 to each edge eE(G) (clearly, for our purposes we assume that not all edge lengths are 0). Informally, the continuous graph 𝒢 defined by G is the infinite set of points determined by the vertices and edges of G, where each point on an edge is part of the graph. This idea requires to define precisely what we mean by a point on an edge.

For each edge uv of G, we take a closed segment of length (uv) with the usual metric and measure, and denote it 𝒢(uv). Moreover, for such an edge uv, we arbitrarily select one extreme of the segment 𝒢(uv) and denote it endp(uv,u), and call endp(uv,v) the other endpoint of 𝒢(uv). Finally, for each vertex u of G, we glue (mathematically, we identify) all points endp(uv,u) over all edges uv of G incident to u; we denote by 𝒢(u) the identified point. The continuous graph 𝒢 defined by G=(V(G),E(G),) is the resulting space. The total length (𝒢) of a continuous graph 𝒢 defined by a graph G is defined as uvE(G)(uv).

We observe that one may think of 𝒢 as a 1-dimensional simplicial complex where each edge uv is isometric to a segment of length (uv).

A point p of 𝒢 can be specified by a triple (uv,u,λ)E(G)×V(G)×[0,(uv)], which represents the point of the segment 𝒢(uv) at distance λ (along 𝒢(uv)) from the endpoint endp(uv,u). The triples (uv,u,λ) and (uv,v,(uv)λ) define the same point of 𝒢. Similarly, for any two incident edges uv,uv of G, the triples (uv,u,0), (uv,v,(uv)), (uv,u,0) and (uv,v,(uv)) define the same point, namely 𝒢(u).

We have already set the notation 𝒢(uv) for an edge uv and 𝒢(u) for a vertex u. With a slight abuse of notation, we do not distinguish uv from 𝒢(uv) and u from 𝒢(u).

In general, for any subgraph H of G, we denote by 𝒢(H) the continuous subgraph of 𝒢 defined by the objects of H. This is also the continuous graph defined by H. Also, when H is clear from the context, we denote such an object by and talk about as a subgraph of 𝒢. For a vertex set AV(G), we use G[A] to denote the subgraph of G induced by A.

A walk in 𝒢 between two points p,q𝒢 is a sequence pv1,v1v2,,vk1vk,vkq with v1v2,,vk1vkE(G). If the first and last point coincide, it is closed. The length of a walk is the sum of the length of its pieces, counted with multiplicity. For any two points p,q𝒢, the distance d𝒢(p,q) is the minimum length over all p-to-q walks. A shortest pq-path is a p-to-q walk π(p,q) in 𝒢 such that (π(p,q))=d𝒢(p,q).

We can regard d𝒢(p,q) as the discrete graph-theoretic distance in a graph obtained by subdividing aa with p as a new vertex, subdividing bb with q as a new vertex, and setting (ap)=λ, (pa)=(aa)λ, (bq)=λ, and (qb)=(bb)λ.

2.2 Distance Problems

Consider a continuous graph 𝒢 defined by a graph G and a continuous subgraph 𝒢 defined by a subgraph HG. We are interested in distances between points of using the metric given by 𝒢. One may think of 𝒢 as the ambient space and of as the relevant subset.

The eccentricity of a point p with respect to is ecc(p,,𝒢)=maxqd𝒢(p,q); when =𝒢, we just talk about the eccentricity of p in 𝒢 and write ecc(p,𝒢) for ecc(p,,𝒢). The diameter of with respect to 𝒢 is defined as diam(,𝒢)=maxp,qd𝒢(p,q); when =𝒢, we just talk about the diameter of 𝒢 and write diam(𝒢) for diam(𝒢,𝒢). It is easy to see that diam(,𝒢)=maxpecc(p,,𝒢). The sum of distances of in 𝒢 is the sum of the pairwise distances in , using the metric from 𝒢, that is, sumdist(,𝒢)=p,qd𝒢(p,q)𝑑p𝑑q. The mean distance of in 𝒢 is mean(,𝒢)=1()2sumdist(,𝒢). It is easy to see that mean(,𝒢)=1()pmean(p,,𝒢)𝑑p, where mean(p,,𝒢)=1()qd𝒢(p,q)𝑑q is the mean distance from the point p with respect to in 𝒢. When =𝒢, then we just write mean(𝒢) for mean(𝒢,𝒢) and mean(p,𝒢) for mean(p,𝒢,𝒢).

2.3 Treewidth

The treewidth of a graph is an important parameter in algorithmic graph theory which, roughly speaking, measures how far the graph is from being a tree (a formal definition is given in [11]). Graphs of treewidth k have O(kn) edges [6].

A separation in a graph G is a triple (A,B,S) such that A,B,SV(G), AB=V(G), S=AB, and there is no edge incident to both AB and BA. The elements of S in separation (A,B,S) are called portals. Each path in G from A to B must pass through a portal. Informally, we are interested in separations where A and B have a constant fraction of the vertices and S is small. Such separations of at most k portals exist for graphs of treewidth k, can be computed in linear time, and have the additional property that adding edges between the portals does not increase the treewidth [8, 12]; . For simplicity, we assume that S contains exactly k portals (it may happen that is has fewer). We use the notation [k]={1,,k}. We have two regimes, depending on whether we consider the treewidth constant or a parameter.

2.4 Orthogonal Range Searching

We use the notation B(n,d)=(d+lognd) to bound the performance of some of our data structures. First we note the following asymptotic bounds.

Lemma 4 (Bringmann, Husfeldt, and Magnusson [8]).

B(n,d)=O(logdn) and B(n,d)=nε2O(d) for each ε>0.

A rectangle in d is the Cartesian product of d intervals (whose extremes can be included or not). The analysis of orthogonal range searching performed in [8, Section 3] (see also [28]) leads to the data structure described in the the following theorem. We use the version suggested by Cabello [10] because it adapts better to our needs. (We use to indicate that the union is between pairwise disjoint sets.)

Theorem 5 (Cabello [10]).

Given a set P of n points in d, there is a family of sets 𝒫={PjjJ} and a data structure with the following properties:

  • PjP for each Pj𝒫;

  • all the sets of 𝒫 together have O(ndB(n,d)) points, i.e., Pj𝒫|Pj|=O(ndB(n,d));

  • for each query rectangle Rd, the data structure finds in O(2dB(n,d)) time indices JRJ such that |JR|=O(2dB(n,d)) and PR=jJRPj;

  • the family 𝒫 and the data structure can be computed in O(ndB(n,d)) time.

3 Diameter in Graphs with Bounded Treewidth

In this section, we discuss the computation of the diameter of a continuous graph with treewidth k. Note that computing the diameter of continuous trees (i.e., treewidth 1) can be reduced to the discrete setting, because in trees the diameter is always attained by two vertices. Thus, we restrict our attention to k>1. As in [1, 8, 9, 12], we use orthogonal range searching to work with distance-related problems in graphs of bounded treewidth. However, because we consider continuous graphs, we have to consider pairs of edges instead of pairs of vertices, and the interaction between edges is more complex. We handle this by using more dimensions in the range searching space.

Sometimes we need to consider an orientation for each edge of G, to distinguish its vertices. We orient the edges of G arbitrarily, but keep track of the orientation. We use uv when the orientation is not relevant and (u,v) or (v,u), depending on the orientation, when we consider it oriented. We use E(G) in both cases.

3.1 Characterization of the Diameter via Walks

We start with a characterization of the diameter in the continuous setting that uses the length of walks (compare to Figure 1). For each aa,bbE(G), let W(aa,bb) be a shortest closed walk passing through all the interior points of aa and bb.

 Lemma 6

For each aa,bbE(G), maxpaa,qbbd𝒢(p,q)=(W(aa,bb))2.

The following corollary is an immediate consequence of Lemma 6.

Corollary 7.

If HG defines the continuous subgraph of 𝒢, then diam(,𝒢)=12max{(W(aa,bb))aa,bbE(H)}.

For our computation, we use the following closed formula.

 Lemma 8

For each aa,bbE(G), it holds that

(W(aa,bb))=(aa)+(bb)+min{dG(a,b)+dG(a,b),dG(a,b)+dG(a,b)}.

For each pair of oriented edges (a0,a1),(b0,b1)E(G), Lemma 8 implies that there are two possible values for (W(a0a1,b0b1)). We say that (a0,a1,b0,b1) is of type 1 if

(W(a0a1,b0b1))=(a0a1)+(b0b1)+dG(a0,b0)+dG(a1,b1),

and of type 2 otherwise. For each oriented edge (b0,b1)E(G) and type τ{1,2}, we define

Typeτ(b0,b1) ={(a0,a1)E(G)(a0,a1,b0,b1) is of type τ}.

Therefore, (a0,a1)Type1(b0,b1)dG(a0,b0)+dG(a1,b1)dG(a0,b1)+dG(a1,b0).

3.2 Diameter Across the Portals

Let (A,B,S) be a separation in G with k portals. We fix an enumeration s1,,sk of them. Let EAE(G[A]) be the edge set of the subgraph of G induced by A and EBE(G[B]), respectively; not necessarily disjoint. For each index i[k], each vertex aA, and each vertex bB, let φ(i;a,b) be the logic predicate that holds whenever si is the first portal in the enumeration that lies in some shortest path from a to b. Formally,

φ(i;a,b)= j<i[dG(a,b)<dG(a,sj)+dG(sj,b)][dG(a,b)=dG(a,si)+dG(si,b)].

It is easy to see that, for each (a,b)A×B, there exists a unique index i[k] where φ(i;a,b) holds (in other words, |{i[k]φ(i;a,b)}|=1).

We extend this to the four shortest paths defined by two vertices a0,a1A and two vertices b0,b1B, by defining the following predicate for all κ=(i0,0,i0,1,i1,0,i1,1)[k]4:

Φ(κ;a0,a1,b0,b1)=Φ((i0,0,i0,1,i1,0,i1,1);a0,a1,b0,b1)=(α,β){0,1}2φ(iα,β;aα,bβ).

Therefore, this predicate holds if and only if, for each α,β{0,1}, the index iα,β is the smallest index i with the property that si lies on some shortest path from aα to bβ. As before, for each (a0,a1,b0,b1)A2×B2, there exists a unique 4-tuple κ[k]4 where Φ(κ;a0,a1,b0,b1) holds. For each κ[k]4, each (b0,b1)EB, and each type τ we define

Λτ(κ;b0,b1)=max{(W(a0a1,b0b1))(a0,a1)EATypeτ(b0,b1)Φ(κ;a0,a1,b0,b1)}.

This represents the maximum length over all edges (a0,a1) of a type-τ walk between (b0,b1) and (a0,a1), consistent with the portals in κ. We next discuss how to compute efficiently Λτ(κ;b0,b1) for several edges (b0,b1)EB simultaneously.

Lemma 9.

Consider a fixed type τ{1,2} and indices κ[k]4. In O(m24k3B(m,4k3)) time we can compute the values Λτ(κ;b0,b1) for all (b0,b1)EB.

Proof sketch..

We sketch how to use orthogonal range searching to compute Λτ(κ;b0,b1) for a fixed κ[k]4. The complete proof is in [11].

For each vertex aA and each i[k], we define the point p(i;a)k whose j-th coordinate is pj(i;a)=dG(a,si)dG(a,sj). For each vertex bB and each index i[k], we define the box R(i;b)=I1(i;b)××Ik(i;b)k, where Ij(i;b) is the interval

Ij(i;b)={(,dG(b,sj)dG(b,si))if j<i,if j=i,(,dG(b,sj)dG(b,si)]if j>i.

In p(i;a) and I(i;b) we remove the i-th coordinate, as it does not provide any information. We use the same notation for the resulting objects, now in k1. It has been noted [12, 8, 10] that the point p(i;a) lies in the rectangle R(i;b) if and only if φ(i;a,b) holds.

We do something similar for Φ(κ;a0,a1,b0,b1). For any two vertices a0,a1A, we define the point p(a0,a1) and, for any two vertices b0,b1B, we make a rectangle R(b0,b1) in 4k4, such that p(a0,a1)R(b0,b1) if and only if Φ(κ;a0,a1,b0,b1) holds.

For each edge (a0,a1)EA, we extend p(a0,a1) to p+(a0,a1) by an extra coordinate to distinguish between type 1 and 2. We also introduce the new rectangle R+(b0,b1) such that

(b0,b1)EB:Λτ(κ;b0,b1)=max{(W(a0a1,b0b1))p+(a0,a1)R+(b0,b1)}.

We use orthogonal range searching to handle the right side of the equation. In total, we spend O(24k3B(m,4k3)) time per edge (b0,b1)EB to compute Λτ(κ;b0,b1).

We use Lemma 9 to compute the values Λτ(κ;b0,b1) for each type τ{1,2}, all (b0,b1)EB and indices κ[k]4. This requires applying Lemma 9 a total of O(k4) times, and therefore we spend O(m24k3k4B(m,4k3)) time. Using Lemma 8, we note that

maxp𝒢(EA),q𝒢(EB)d𝒢(p,q)=12max{Λτ(κ;b0,b1)(b0,b1)EB,κ[k]4,τ{1,2}}

(for details check the proof of Lemma 10 in [11]).

Lemma 10.

We can compute maxp𝒢(EA),q𝒢(EB)d𝒢(p,q) in O(m24k3k4B(m,4k3)) time.

 Remark.

We are aware that some log factors can be shaved off, and that for small k one can improve the analysis slightly. However, we prefer to keep this high-level structure to keep it simpler, and parallel to the forthcoming computation of mean distance.

3.3 Global Diameter

Let G be a graph with n vertices and m edges defining the continuous graph 𝒢. Let H be a subgraph of G defining a continuous graph 𝒢. We use a divide-and-conquer approach to compute diam(,𝒢).

Let (A,B,S) be a separation in G. Let G be the graph obtained from G by adding an edge ss with length dG(s,s) for every pair of portals s,sS. (If the edge already exists, we redefine its length.) For X{A,B}, let X and 𝒢X be the continuous graphs defined by H[X]E(G[S]) and G[X], respectively. The edges ss added to G guarantee that d𝒢(p,q)=d𝒢X(p,q) for any points p,qX. (We removed E(G[S]) because for points on edges between portals whose length is redefined, the statement is undefined.) Therefore

diam(,𝒢) =max{maxp,qAd𝒢(p,q),maxp,qBd𝒢(p,q),maxpA,qBd𝒢(p,q)}
=max{maxp,qAd𝒢A(p,q),maxp,qBd𝒢B(p,q),maxpA,qBd𝒢(p,q)}
=max{diam(A,𝒢A),diam(B,𝒢B),maxpA,qBd𝒢(p,q)} (1)

(see Figure 2). The last term can be computed by Lemma 10, which depends on the size of S.

Figure 2: Visualization of the divide-and-conquer approach to compute diam(𝒢) (see Section 3.3).

Now we have two regimes depending on whether we want to assume that the treewidth is constant, as done in [12], or whether we want to consider the treewidth a parameter, as done in [8]. The same distinction was made in [10]. This difference affects the time to find a tree decomposition and the number of portals in a balanced separation. In both cases we use that an n-vertex graph with treewidth k has O(kn) edges [6].

Theorem 11.

Let k2 be an integer constant. Let G be a graph with n vertices, treewidth at most k, nonnegative edge-lengths, and let 𝒢 be the corresponding continuous graph. Let H be a subgraph of G and let 𝒢 be the corresponding continuous subgraph. We can compute the diameter diam(,𝒢) in O(nlog4k2n) time.

Proof.

If G has fewer than 2k=O(1) vertices, we compute diam(,𝒢) in O(1) time. Otherwise, we find in linear time a separation (A,B,S) such that: |S|k, nk+1|A|nkk+1. Such a separation is given, e.g., in [12]. Further, we add an edge ss between all portals s,sS with length dG(s,s). This augmentation costs O(k(m+nlogn))=O(nlogn) time.

By Lemma 10, the value maxpA,qBd𝒢(p,q) is computed in O(m24k3k4B(m,4k3)) time. Using that k is constant, m=O(n), and Lemma 4, this time bound is O(nlog4k3n).

We construct the graphs G, G[A], G[B], H[A]E(G[S]) and H[B]E(G[S]) explicitly, in O(m)=O(n) time. Because adding edges between the portals of S does not increase the treewidth [12][Lemma 3],the graphs G[A] and G[B] have treewidth at most k. The values diam(A,𝒢A) and diam(B,𝒢B) are computed recursively, and we obtain diam(,𝒢) using Section 3.3.

Since nk+1|A|nkk+1 and k is constant, each side of the recursion has a constant fraction of the vertices |A|+|B|=n+k, and the recursion depth is O(logn), leading to a total running time of O(nlog4k2n).

Theorem 12.

Let G be a graph with n vertices, treewidth at most k, nonnegative edge-lengths, and let 𝒢 be the corresponding continuous graph. Let H be a subgraph of G and let 𝒢 be the corresponding continuous subgraph. We can compute the diameter diam(,𝒢) in n1+ε2O(k) time, for any fixed ε>0.

Proof.

We use the same divide-and-conquer strategy as in Theorem 11. The difference is in the properties of the separation. If G has O(k) vertices, we compute diam(,𝒢) in O(k2) time. Otherwise, we proceed as follows.

First, we note that, given a tree decomposition of G of width k, we can obtain in linear time a separation (A,B,S) in G with the following properties: the set S of portals for A has k+1 portals; both A and B=(V(G)A) have Θ(nk) vertices each; adding edges between the vertices of S does not increase the treewidth of the tree decomposition. See for example [6, Theorem 19]; the set S is a bag of the decomposition and thus the tree decomposition keeps being valid with the addition of edges within S.

It is shown in [7] that, for graphs of treewidth at most k, one can find a tree decomposition of width k=3k+4 in 2O(k)nlogn time. From this we obtain the separation (A,B,S) in G mentioned above, where |S|k+1=3k+5. By Lemma 10, the value maxpA,qBd𝒢(p,q) is computed in O(m2O(k)(k+1)4B(m,O(k)) time. Using that m=O(kn) and the estimate of Lemma 4, this time bound becomes

O((kn)2O(k)(k+1)4B(n,O(k))=O(n2O(k)nε2O(k))=n1+ε2O(k),

where ε>0 can be chosen arbitrarily small.

To compute diam(A,𝒢A) and diam(B,𝒢B) recursively, we pass to the subproblems the tree decomposition we have computed, trimmed to the vertices of A and B, respectively. We also can adapt it to keep the tree decompositions of size O(|A|) and O(|B|), respectively. In this way, at any level of the recursion, we always have a set S with k+1=3k+5 portals. Thus, we compute the tree decomposition only once, and then pass it to each subproblem trimmed to the relevant vertices. The recursive calls add a logarithmic factor to the total running time, which is absorbed by the polynomial term n1+ε.

(The reason for passing the tree decomposition to the subproblems is that adding the edges between the portals in S may give a clique of size k, which increases the treewidth of G. Computing an approximate tree decomposition of G anew would increase the width of the decomposition at each level. However, if we pass the tree decomposition to the subproblems, we keep the width of the decomposition bounded by 3k+5 at all levels of the recursion.)

4 Mean Distance in Graphs with Bounded Treewidth

In this section, we discuss the computation of the mean distance in continuous graphs with treewidth k. The case of k=1 corresponds to continuous trees, one of the few graph classes for which linear-time algorithms are known [21]. Thus, we focus on k>1. All our efforts will be on the computation of sumdist(,𝒢), from which mean(,𝒢) can be easily computed. Again, we use orthogonal range searching, but now we need to efficiently retrieve sums of distances. We achieve this by representing the sum of distances between pairs of edges as volumes of collections of triangular prisms, which can be represented in a compact way.

4.1 Mean Distance as a Volume

We compute the sum of all distances in a continuous graph as the volume of a collection of triangular prisms. A truncated triangular prism is formed when a prism is sliced by a plane that is not parallel to its bases; the heights are the lengths of the three edges orthogonal to the basis. The volume of a truncated triangular prism with base and heights h1,h2,h3 is 13area()(h1+h2+h3).

Consider the following setting defined by a complete graph with vertex set {a0,a1,b0,b1} and variable edge lengths, as follows: (i) a0a1 has variable length y>0, (ii) b0b1 has variable length z>0, (iii) for all α,β{0,1}, aαbβ has length xα,β0.

Let K=K(y,z,x0,0,x0,1,x1,0,x1,1) denote this graph, and let 𝒦 denote the corresponding continuous graph. See Figure 3. We say that the 6-tuple (y,z,x0,0,x0,1,x1,0,x1,1) is compliant if, for all α,β{0,1} it holds xα,β=dK(aα,bβ). In our setting we only need to consider compliant cases. (This poses some conditions on the values that the variables can take.)

We want to understand how the total sum of distances between points on a0a1 and b0b1,

ξ(y,z,x0,0,x0,1,x1,0,x1,1)=p𝒦(a0a1),q𝒦(b0b1)d𝒦(p,q)𝑑p𝑑q,

looks like. For each λ[0,y], let p(λ) be the point specified by the triple (a0a1,a0,λ), and, for each μ[0,z], let q(μ) be the point specified by the triple (b0b1,b0,μ). Then

ξ(y,z,x0,0,x0,1,x1,0,x1,1)=(λ,μ)[0,y]×[0,z]d𝒦(p(λ),q(μ))𝑑λ𝑑μ.

Function (λ,μ)d𝒦(p(λ),q(μ)), defined in [0,y]×[0,z], is the lower envelope of four functions

x0,0+λ+μ,x0,1+λ+zμ,x1,0+yλ+μ,x1,1+yλ+zμ.

Following [21], we call the graph of function (λ,μ)d𝒦(p(λ),q(μ)) a roof; the value ξ is the volume below the roof. The minimization diagram of this function consists of convex pieces; see Figure 3. The gradient of each function is of the form (±1,±1). When the variable values are compliant, all four functions appear in the lower envelope, and the minimization diagram has four regions. (Some of them may contain only part of an edge of the domain.)

Figure 3: Concrete example of the minimization diagram of d𝒦(p(λ),q(μ)). In the center, some values of d𝒦(p(λ),q(μ)) are shown in red; on the right, 3D visualization of the roofs.
Lemma 13.

Consider the 4-variable linear function L(x0,0,x0,1,x1,0,x1,1)=x1,0+x0,1x0,0x1,1. There are two 6-variable polynomials ϱ+(),ϱ() of degree at most three with the following property: when (y,z,x0,0,x0,1,x1,0,x1,1) is compliant,

ξ(y,z,x0,0,x0,1,x1,0,x1,1)={ϱ+(y,z,x0,0,x0,1,x1,0,x1,1),if L(x0,0,x0,1,x1,0,x1,1)0,ϱ(y,z,x0,0,x0,1,x1,0,x1,1),otherwise.

4.2 Integral Across the Portals

We reuse much of the notation and ideas from Section 3.2. As before, G is a graph with n vertices and m edges, (A,B,S) is a separation in G with exactly k portals, and we fix an enumeration of the portals as s1,,sk. We also fix an orientation for the edges of G. Let EAE(G[A]) be the edge sets of the subgraph of G induced by A and EBE(G[B]), respectively; not necessarily disjoint. Our objective in this section is to compute the sum of distances between points in EA and EB:

p𝒢(EA),q𝒢(EB)d𝒢(p,q)𝑑p𝑑q=(a0,a1)EA(b0,b1)EBp𝒢(a0a1),q𝒢(b0b1)d𝒢(p,q)𝑑p𝑑q.

Define the graph 𝒦=𝒦((a0a1),(b0b1),dG(a0,b0),dG(a0,b1),dG(a1,b0),dG(a1,b1)), for two edges (a0,a1)EA, (b0,b1)EB.

The continuous edges a0a1 and b0b1 belong to 𝒢 and 𝒦 and, for each p𝒢(a0a1) and q𝒢(b0b1) we have d𝒢(p,q)=d𝒦(p,q). It follows that

p𝒢(a0a1),q𝒢(b0b1)d𝒢(p,q)𝑑p𝑑q =p𝒦(a0a1),q𝒦(b0b1)d𝒦(p,q)𝑑p𝑑q.

It follows that our objective is to compute

(a0,a1)EA(b0,b1)EBξ((a0a1),(b0b1),dG(a0,b0),dG(a0,b1),dG(a1,b0),dG(a1,b1)).

We keep using the predicates φ(i;a,b) and Φ(κ;a0,a1,b0,b1) defined in Section 3.2, where a,a0,a1A, b,b0,b1B, i[k] and κ=(i0,0,i0,1,i1,0,i1,1)[k]4.

Consider the polynomial L of Lemma 13. For each pair (a0,a1),(b0,b1)E(G), we have

(a0,a1,b0,b1) of type 1L(dG(a0,b0),dG(a0,b1),dG(a1,b0),dG(a1,b1))0.

Otherwise, (a0,a1,b0,b1) is of type 2. For each oriented edge (b0,b1)E(G) and type τ{1,2}, we define

Typeτ(b0,b1) ={(a0,a1)E(G)(a0,a1,b0,b1) of type τ}. (2)

For each κ[k]4, each (b0,b1)EB, and each type τ{1,2} we define

Ψτ(κ;b0,b1)=ξ((a0a1),(b0b1),dG(a0,b0),dG(a0,b1),dG(a1,b0),dG(a1,b1)), (3)

where the sum ranges over all oriented edges (a0,a1)EA such that (a0,a1)Typeτ(b0,b1) and Φ(κ;a0,a1,b0,b1) holds.

Next, we show that we can efficiently compute Ψτ(κ;b0,b1) for all edges (b0,b1)EB:

Lemma 14.

Consider a fixed type τ{1,2} and indices κ[k]4. We can compute the values Ψτ(κ;b0,b1) for all (b0,b1)EB in O(m24k3B(m,4k3)) time.

Lemma 15.

We can compute p𝒢(EA),q𝒢(EB)d𝒢(p,q)𝑑p𝑑q in O(m24k3k4B(m,4k3)) time.

4.3 Global Mean

Let G be a graph with n vertices and m edges defining the continuous graph 𝒢. Let H be a subgraph of G defining a continuous graph 𝒢. We use a divide-and-conquer approach to compute sumdist(,𝒢). The approach is very similar to that in Section 3.3 for the diameter, and thus we only emphasize the differences.

Let (A,B,S) be a separation in G. We use the notation defined before Theorem 11 introducing for X{A,B}, and the graphs X and 𝒢X. We have

sumdist(,𝒢)=p𝒢(EA),q𝒢(EB)d𝒢(p,q)𝑑p𝑑qp,q𝒢(H[S])d𝒢(p,q)𝑑p𝑑q+sumdist(A,𝒢A)+sumdist(B,𝒢B). (4)

The first term can be computed using Lemma 15, which depends on |S|. The second term can be computed in O(kmlogn+k2) time because, after computing the distances from S, it is a problem of size O(k2). Dividing sumdist(,𝒢) by ()2, we obtain mean(,𝒢). As in Section 3.3, we have two regimes.

Theorem 16.

Let k2 be an integer constant. Let G be a graph with n vertices, treewidth at most k, nonnegative edge-lengths, and let 𝒢 be the corresponding continuous graph. Let H be a subgraph of G and let 𝒢 be the corresponding continuous subgraph. We can compute mean(,𝒢) in O(nlog4k2n) time.

Theorem 17.

Let G be a graph with n vertices, treewidth at most k, nonnegative edge-lengths, and let 𝒢 be the corresponding continuous graph. Let H be a subgraph of G and let 𝒢 be the corresponding continuous subgraph. We can compute mean(,𝒢) in n1+ε2O(k) time, for any fixed ε>0.

5 Conclusion

We presented the first subquadratic algorithms to compute the diameter and the mean distance in continuous graphs, for two non-trivial graph classes. We expect that the approach for graphs parameterized by the treewidth can be adapted for computing other statistics defined by the distance between two points selected at random in a continuous subgraph 𝒢, like a cumulative density function (CDF) and higher moments:

for given δ, computeCDF(δ,,𝒢)=1()2p,q𝟙[d𝒢(p,q)δ]𝑑p𝑑q,
median distance:sup{δ0CDF(δ,,𝒢)1/2},
higher moments, such as 1()2p,q(d𝒢(p,q))2𝑑p𝑑q.

The main open question stemming from our work is whether our approach can be adapted to work in subquadratic time for arbitrary planar graphs. However, as already mentioned in the introduction, this requires dynamic trees with a set of suitable operations that – at the moment – seem to be out of reach.

References

  • [1] A. Abboud, V. Vassilevska Williams, and J. Wang. Approximation and fixed parameter subquadratic algorithms for radius and diameter in sparse graphs. In Proc. 27th Annu. ACM-SIAM Sympos. Discrete Algorithms (SODA), pages 377–391. SIAM, 2016. doi:10.1137/1.9781611974331.ch28.
  • [2] S. Won Bae, M. De Berg, O. Cheong, J. Gudmundsson, and C. Levcopoulos. Shortcuts for the circle. Comput. Geom. Theory Appl., 79:37–54, 2019. doi:10.1016/J.COMGEO.2019.01.006.
  • [3] L. N. Baptista, J. B. Kennedy, and D. Mugnolo. Mean distance on metric graphs. The Journal of Geometric Analysis, 34(137):1–25, 2024. doi:10.1007/s12220-024-01574-0.
  • [4] P. Bergé, G. Ducoffe, and M. Habib. Subquadratic-time algorithm for the diameter and all eccentricities on median graphs. Theory of Computing Systems, 68(1):144–193, 2024. doi:10.1007/S00224-023-10153-9.
  • [5] P. Bergé, G. Ducoffe, and M. Habib. Quasilinear-time eccentricities computation, and more, on median graphs. In Proc. 36th Annu. ACM-SIAM Sympos. Discrete Algorithms (SODA), pages 1679–1704. SIAM, 2025. doi:10.1137/1.9781611978322.52.
  • [6] H. L. Bodlaender. A partial k-arboretum of graphs with bounded treewidth. Theor. Comput. Sci., 209(1-2):1–45, 1998. doi:10.1016/S0304-3975(97)00228-4.
  • [7] H. L. Bodlaender, P. Grønås Drange, M. S. Dregi, F. V. Fomin, D. Lokshtanov, and M. Pilipczuk. A ckn 5-approximation algorithm for treewidth. SIAM J. Comput., 45(2):317–378, 2016. doi:10.1137/130947374.
  • [8] K. Bringmann, T. Husfeldt, and M. Magnusson. Multivariate analysis of orthogonal range searching and graph distances. Algorithmica, 82:2292–2315, 2020. doi:10.1007/S00453-020-00680-Z.
  • [9] S. Cabello. Subquadratic algorithms for the diameter and the sum of pairwise distances in planar graphs. ACM Trans. Algorithms, 15(2):21:1–21:38, 2019. doi:10.1145/3218821.
  • [10] S. Cabello. Computing the inverse geodesic length in planar graphs and graphs of bounded treewidth. ACM Trans. Algorithms, 18(2):14:1–14:26, 2022. doi:10.1145/3501303.
  • [11] S. Cabello, D. Garijo, A. Kalb, F. Klute, I. Parada, and R. I. Silveira. Algorithms for distance problems in continuous graphs, 2025. arXiv:2503.07769.
  • [12] S. Cabello and C. Knauer. Algorithms for graphs of bounded treewidth via orthogonal range searching. Comput. Geom. Theory Appl., 42(9):815–824, 2009. doi:10.1016/j.comgeo.2009.02.001.
  • [13] J. Cáceres, D. Garijo, A. González, A. Márquez, M. L. Puertas, and P. Ribeiro. Shortcut sets for the locus of plane Euclidean networks. Applied Mathematics and Computation, 334:192–205, 2018. doi:10.1016/j.amc.2018.04.010.
  • [14] J.-L. De Carufel, C. Grimm, A. Maheshwari, S. Schirra, and M. H. M. Smid. Minimizing the continuous diameter when augmenting a geometric tree with a shortcut. Comput. Geom. Theory Appl., 89:101631, 2020. doi:10.1016/J.COMGEO.2020.101631.
  • [15] J.-L. De Carufel, C. Grimm, A. Maheshwari, and M. H. M. Smid. Minimizing the continuous diameter when augmenting paths and cycles with shortcuts. In Proc. 15th Scandinavian Symposium and Workshops on Algorithm Theory (SWAT’16), volume 53 of LIPIcs, pages 27:1–27:14. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2016. doi:10.4230/LIPICS.SWAT.2016.27.
  • [16] C. E. Chen and R. S. Garfinkel. The generalized diameter of a graph. Networks, 12(3):335–340, 1982. doi:10.1002/net.3230120310.
  • [17] G. Ducoffe. Eccentricity queries and beyond using hub labels. Theoretical Computer Science, 930(21):128–141, 2022. doi:10.1016/j.tcs.2022.07.017.
  • [18] G. Ducoffe, M. Habib, and L. Viennot. Diameter, eccentricities and distance oracle computations on h-minor free graphs and graphs of bounded (distance) vapnik–chervonenkis dimension. SIAM Journal on Computing, 51(5):1506–1534, 2022. doi:10.1137/20m136551x.
  • [19] L. Friedlander. Genericity of simple eigenvalues for a metric graph. Israel Journal of Mathematics, 146:149–156, 2005. doi:10.1007/BF02773531.
  • [20] D. Garijo, A. Márquez, N. Rodríguez, and R. I. Silveira. Computing optimal shortcuts for networks. Eur. J. Oper. Res., 279(1):26–37, 2019. doi:10.1016/j.ejor.2019.05.018.
  • [21] D. Garijo, A. Márquez, and R. I. Silveira. Continuous mean distance of a weighted graph. Results in Mathematics, 78(139):1–36, 2023. doi:10.1007/s00025-023-01902-w.
  • [22] P. Gawrychowski, H. Kaplan, S. Mozes, M. Sharir, and O. Weimann. Voronoi diagrams on planar graphs, and computing the diameter in deterministic O(n5/3) time. SIAM J. Comput., 50(2):509–554, 2021. doi:10.1137/18M1193402.
  • [23] J. Gudmundsson and Y. Sha. Augmenting graphs to minimize the radius. Comput. Geom. Theory Appl., 114(101996):1–14, 2023. doi:10.1016/j.comgeo.2023.101996.
  • [24] J. Gudmundsson and S. Wong. Improving the dilation of a metric graph by adding edges. ACM Trans. Algorithms, 18(3-Article 20):1–14, 2022. doi:10.1145/3517807.
  • [25] S. L. Hakimi. Optimum locations of switching centers and the absolute centers and medians of a graph. Operations Research, 12(3):450–459, 1964.
  • [26] H. Le and C. Wulff-Nilsen. Vc set systems in minor-free (di)graphs and applications. In Proc. 35th Annu. ACM-SIAM Sympos. Discrete Algorithms (SODA), pages 5332–5360. SIAM, 2024. doi:10.1137/1.9781611977912.192.
  • [27] G. Lumer. Connecting of local operators and evolution equations on networks. In Potential Theory Copenhagen 1979, pages 219–234. Springer-Verlag, 1980. doi:10.1007/BFb0086338.
  • [28] L. Monier. Combinatorial solutions of multidimensional divide-and-conquer recurrences. Algorithms, 1(1):60–74, 1980. doi:10.1016/0196-6774(80)90005-X.
  • [29] L. Roditty and V. Vassilevska Williams. Fast approximation algorithms for the diameter and radius of sparse graphs. In Proc. 45th Annu. ACM Sympos. Theory Comput. (STOC), pages 515–524. ACM, 2013. doi:10.1145/2488608.2488673.