A Fast Shortest Path Algorithm on Terrain-like Graphs

Terrain visibility graphs are a well-known graph class in computational geometry. They are closely related to polygon visibility graphs, but a precise graph-theoretical characterization is still unknown. Over the last decade, terrain visibility graphs attracted considerable attention in the context of time series analysis (there called time series visibility graphs) with various practical applications in areas such as physics, geography, and medical sciences. Computing shortest paths in visibility graphs is a common task, for example, in line-of-sight communication. For time series analysis, graph characteristics involving shortest paths lengths (such as centrality measures) have proven useful. In this paper, we devise a fast output-sensitive shortest path algorithm on a superclass of terrain visibility graphs called terrain-like graphs (including all induced subgraphs of terrain visibility graphs). Our algorithm runs in O(d∗logΔ)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$O(d^*\log \varDelta )$$\end{document} time, where d∗\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$d^*$$\end{document} is the length (that is, the number of edges) of the shortest path and Δ\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\varDelta $$\end{document} is the maximum vertex degree. Alternatively, with an O(n2)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$O(n^2)$$\end{document}-time preprocessing our algorithm runs in O(d∗)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$O(d^*)$$\end{document} time.


Introduction
Visibility graphs are a fundamental concept in computational geometry.For a given set of geometrical objects (e.g. points, segments, rectangles, polygons) they encode which objects are visible to each other.To this end, the objects form the vertices of the graph and there is an edge between two vertices if and only if the two corresponding objects "can see each other" (for a specified notion of visibility).Visibility graphs are well studied from a graph-theoretical perspective and find applications in many real-world problems occurring in different fields such as physics [11,12,24], robotics [6], object recognition [27], or medicine [3,15].
In this work, we consider so-called terrain-like graphs [5] which form a superclass of the visibility graphs of 1.5-dimensional terrains, that is, x-monotone polygonal chains (see Fig. 1).Terrain visibility graphs have been studied since the 90's [1,2,9,10], yet a precise graph-theoretical characterization is still open.While certain necessary conditions for being a terrain visibility graph are known (see Sect. 2), it was recently shown that these conditions are not sufficient [4].Terrain-like graphs are only required to satisfy one of these necessary conditions.They include all induced subgraphs of terrain visibility graphs.
Recently, terrain visibility graphs found numerous applications in analyzing and classifying time series [11,12,15,23,24,28].Here, the so-called complex network approach is based on studying graph-theoretic measures of visibility graphs of time series.Examples of graph-theoretic measures used in time series analysis include closeness centrality, betweenness centrality, or average path length [11], all of which involve (unweighted) shortest paths.Hence, a fast algorithm for unweighted shortest path computation might speed up time series analysis tasks.Moreover, understanding the structure of shortest paths in terrain visibility graphs might explain in which cases these measures perform well.
Another natural example for the occurrence of terrain visibility graphs is a network of stations communicating via line-of-sight links (e.g.radio signals).Here, determining the distance between two stations is a common task.If the length of a path is measured Fig. 1 A terrain visibility graph drawn in two different ways (with a corresponding terrain on the left).The shortest path of length three from the second leftmost to the rightmost vertex is drawn with thick lines via Euclidean distance, then simply moving into the direction of the other station as far as possible is optimal.But a more realistic distance measure in the above scenario is the number of edges, as edge travel times can be negligible in comparison to the processing times at the stations.This setting is more difficult since it might now be better to move in the opposite direction first (as in Fig. 1).Moreover, in general there might also be terrain vertices which obstruct communication but are not communication stations themselves.Then the communication network is not a terrain visibility graph itself but an induced subgraph of a terrain visibility graph.
We develop an output-sensitive algorithm computing shortest paths in terrain-like graphs in O(d * log Δ) time, where d * is the length of the shortest path and Δ is the maximum degree (also O(d * ) time is possible with an O(n 2 )-time preprocessing).Our algorithm is based on a detailed analysis of the structure of a shortest path.Outputsensitivity is indeed favorable in cases where many shortest paths are of a length much smaller than the number of vertices (which might be the case in terrain visibility graphs).

Related Work
For a general overview on visibility graphs and related problems see the survey by Ghosh and Goswami [18].As regards the origin of terrain visibility graphs, Abello et al. [2] studied visibility graphs of staircase polygons which are closely related to terrain visibility graphs as Colley [9] showed that they are in one-toone correspondence with the core induced subgraphs of staircase polygon visibility graphs.Abello et al. [2] described three necessary properties that are satisfied by every terrain visibility graph and conjectured that these are actually sufficient.Moreover, they showed that one can reconstruct (a possible) slope ordering from the graph with given vertex ordering.Evans and Saeedi [13] gave a simpler proof for the results of Abello et al. [2].Ameer et al. [4] proved that the necessary properties described by Abello et al. [2] are in fact not sufficient.Abello and Egecioglu [1] showed that visibility graphs of staircase polygons with unit step-length can be recognized via linear programming and that not all staircase polygon visibility graphs can be represented with unit steps.Choi et al. [8] studied another subclass of terrain visibility graphs called funnel visibility graphs which is linear-time recognizable (also studied by Colley et al. [10]).Regarding possible induced subgraphs, it is known that terrain visibility graphs can contain arbitrary large holes but no antiholes of size larger than five [14].Ashur et al. [5] introduced terrain-like graphs and studied approximation algorithms computing dominating sets.
Lacasa et al. [23] introduced terrain visibility graphs (under the name of time series visibility graphs) in the context of time series analysis.A variant (called horizontal visibility graphs) where two vertices can only see each other horizontally was later introduced by Luque et al. [26].Horizontal visibility graphs were fully characterized by Gutin et al. [20] who showed that these are exactly the outerplanar graphs with a Hamiltonian path.Moreover, Luque and Lacasa [25] showed that certain canonical horizontal visibility graphs are uniquely determined by their degree sequence (in contrast to terrain visibility graphs [14]).
As regards shortest path computation (see also the book by Ghosh [17]), it is known that Euclidean shortest paths from a fixed source vertex to all other vertices inside a simple (triangulated) polygon can be computed in O(n) time [19].Also, an unweighted shortest path between two vertices of a simple polygon can be computed in O(n) time [16,22,29].Note that these algorithms require the polygon vertices to be given.To the best of our knowledge, no specific algorithms on (arbitrary induced subgraphs of) the visibility graph of a polygon or terrain (with given vertex ordering) have been developed so far.

Preliminaries
We assume the reader to be familiar with basic concepts and classes of graphs (refer to Brandstädt et al. [7] for an overview).
Terrain-like graphs are motivated by terrain visibility graphs, which we introduce first.A (1.5-dimensional) terrain is an x-monotone polygonal chain in the plane defined by a set V of terrain vertices with pairwise different x-coordinates.For two terrain vertices p, q, we write p < q if p is "left" of q, that is, p x < q x , where p x denotes the x-coordinate of p. Furthermore, we define the interval [ p, q] := {x ∈ V | p ≤ x ≤ q}.The corresponding terrain visibility graph is defined on the set of terrain vertices where two vertices p and q are adjacent if and only if they see each other, that is, there is no vertex between them that lies on or above the line segment connecting p and q (see Fig. 1 for an example).Formally, there exists an edge { p, q}, for p < q, if and only if all terrain vertices r with p < r < q satisfy r y < p y + (q y − p y ) r x − p x q x − p x .
Hershberger [21] gave an algorithm to compute the visibility graph of a given terrain with a running time that is linear in the size of the graph.
Clearly, every terrain visibility graph contains a Hamiltonian path along the order of the terrain vertices.Moreover, the following two conditions are necessary for being a terrain visibility graph as shown by Abello et al. [2] (we stick to the names coined by Evans and Saeedi [13]).

X-property:
Let p, q, r , s be four terrain vertices with p < q < r < s.If p sees r and q sees s, then p sees s.
Bar-property: If p, q are two non-consecutive terrain vertices that can see each other, then there is a vertex r between p and q that can see both of them.
It was conjectured that the existence of a vertex ordering satisfying the above two properties fully characterizes terrain visibility graphs.However, Ameer et al. [4] recently disproved the conjecture and gave a counterexample.Now, a terrain-like graph is simply defined to have a vertex ordering fulfilling the X-property [5].Note that every induced subgraph of a terrain visibility graph is terrain-like.Interestingly, the converse is not true since C 6 (that is, the complement of a 6-cycle) can satisfy the X-property (see Fig. 2) but cannot be an induced subgraph of a terrain visibility graph [14,Thm. 3.5].

Fast Shortest Path Computation
In this section we develop an efficient shortest path algorithm on terrain-like graphs.
In the following, let G be an input graph with a (known) vertex ordering satisfying the X-property.Furthermore, let s and t be two vertices of G with s < t and dist(s, t) = d * < ∞, where dist(s, t) denotes the length (that is, the number of edges) of a shortest path from s to t.Two edges { p, q} and {r , s} are said to be crossing if p < r < q < s.
We start with the crucial observation that a shortest path contains at most one pair of crossing edges.
Lemma 3.1 If a shortest s-t path P contains a pair of crossing edges {v, v }, {w, w } with v < w < v < w , then it also contains the edge {v, w }.Furthermore, this is the only pair of crossing edges in P and Proof Let P be a shortest s-t path and {v, v }, {w, w } two crossing edges with v < w < v < w .By the X-property, G contains the edge {v, w }.Since P is a shortest path and thus also an induced path, it must contain that edge (see Fig. 3).We claim that no other edge {x, would also cross {v, w }.Hence, by the X-property, P would have to contain the edge {x, w }, which is not possible since w would be incident to three edges of P.
Otherwise, if v < x < v < x , then P would contain the edge {v, x }, which is again not possible.By symmetry, no other edge of P can cross {w, w }.Consequently, P contains only vertices in [v, w ] since otherwise P would contain an edge from a vertex u / ∈ [v, w ] to a vertex x with v ≤ x ≤ w .Since v, w are both already contained in two edges of P, this would imply v < x < w and thus the edge {u, x} would cross {v, v } or {w, w }.It follows that P cannot contain a second pair of crossing edges since the same argument would apply to that pair.
We denote a shortest s-t path P of length d * by its vertices s = p 0 , p 1 , . . ., p d * = t and define li(P) to be the index of the leftmost vertex of P. Analogously, ri(P) is the index of the rightmost vertex of P. The following can be obtained from Lemma 3.1 (see also Fig. 6 (iii)).Lemma 3.2 Let P be a shortest s-t path.Then, ri(P) < li(P) holds if and only if P contains a pair of crossing edges.Moreover, if P contains a pair of crossing edges, then ri(P) = li(P) − 1 and p i < p j holds for all i < ri(P) < li(P) < j.
Proof If ri(P) < li(P), then P must contain a pair of crossing edges between the subpaths from s to p ri(P) and from p li(P) to t.Conversely, if P contains a pair of crossing edges, then, by Lemma 3.1, the two crossing edges must be { p ri(P)−1 , p ri(P) } and { p li(P) , p li(P)+1 }.This implies that P contains the edge { p ri(P) , p li(P) }, which implies ri(P) = li(P) − 1.Then, for all i < ri(P) < li(P) < j, it holds that p i < p j since P has no other crossing edges and p ri(P)−1 < p li(P)+1 .
Clearly, Lemma 3.2 implies that li(P) < ri(P) if and only if P contains no crossing edges.Moreover, the following holds (see Fig. 6, (i) and (ii)).

Lemma 3.3
Let P be a shortest s-t-path with li(P) < ri(P).Then, p i < p j holds for all i < li(P) < j and all i < ri(P) < j.
Proof We prove both cases simultaneously.Assume towards a contradiction that p j < p i , then we have p li(P) < p j < p i < p ri(P) .Consider the subpath P 1 of P that connects p i with p li(P) and the subpath P 2 of P that connects p j with p ri(P) .These two subpaths are vertex-disjoint and must thus be crossing, meaning that there is a pair of crossing edges e 1 , e 2 where e 1 is an edge of P 1 and e 2 an edge of P 2 .By Lemma 3.2, this implies ri(P) < li(P), which is a contradiction.
In combination, Lemmas 3.2 and 3.3 imply the following corollary.Corollary 3.4 For a shortest s-t path P, it holds that all vertices p i with i < ri(P) satisfy p i < t and all vertices p i with i > li(P) satisfy p i > s.
The results derived above characterize the global structure of a shortest s-t path.Next, we will investigate the local structure.The most important consequence of the X-property is that large steps are usually better than small steps.We formalize this in the following.
Define α s (k) as the leftmost and β s (k) as the rightmost vertex that can be reached from s by a path of length at most k that only uses vertices v ≤ t.We symmetrically define α t (k) as the rightmost and β t (k) as the leftmost vertex reachable from t by a path of length at most k using only vertices v ≥ s.Clearly α s (k) ≤ s ≤ β s (k) ≤ t and s ≤ β t (k) ≤ t ≤ α t (k) for all k ≥ 0 (compare also Fig. 4).
The following lemma shows that there is a shortest path from s to α s (k) that uses only vertices in {α Fig. 4 Example graph with all αand β-vertices labeled.Note that α s (2) = α s (3) = . .., β s (2) = β s (3) = . .., α t (1) = α t (2) = . .., and β t (4) = β t (5) = . . .Furthermore, s = α s (0) = β s (0) and t = α t (0) = β t (0) Proof Clearly, the statement trivially holds for k = 1 and if α s (k) = α s (k − 1).Hence, assume that k ≥ 2 and α s (k) = α s (k − 1).In the following, we only consider vertices to the left of t, which is why we assume that t is the rightmost vertex for the remainder of this proof.Let {x, α s (k)} be the last edge of a shortest path from s to α s (k).By definition, we have ) and we are done.Otherwise, Q contains an edge {y, y } with y < x < y .By the X-property, there is an edge between α s (k) and y , which yields a path of length at most k − 1 between s and α s ) and we are done.Otherwise, Q contains an edge {y, y } with y < x < y .By the X-property, there is an edge between α s (k) and y , which yields a path of length at most k − 1 between s and α s (k), implying α s (k − 1) = α s (k).
Due to symmetry, Lemma 3.5 also holds if one exchanges α and β, and also when s is replaced by t.With the following lemma, we will be able to restrict our search for a shortest s-t path mostly to the α and β vertices.

Lemma 3.6
There is a shortest s-t path P = p 0 , . . ., p d * such that (i) p i ∈ {α s (i), β s (i)} for all i < ri(P), and Proof We begin by showing that there always exists a shortest s-t path satisfying (i) (see Fig. 5 for an illustration).Assume towards a contradiction, that no shortest s-t path satisfies (i).Then, let P = p 0 , p 1 , . . ., p d * be a shortest s-t path chosen so that it maximizes i, where i < ri(P) is the smallest index such that p i / ∈ {α s (i), β s (i)}.Since i < ri(P), we know that all of p 0 , . . ., p i are to the left of t by Corollary 3.4.We assume that s < p i (the case s > p i is similar and uses that p i = α s (i)).Since p i = β s (i), it holds p i < β s (i).Note that also β s (i) < t, since β s (i) = t would contradict dist(s, β s (i)) = i < d * = dist(s, t).By Lemma 3.5, there exists a shortest path Q = q 0 , q 1 , . . ., q i from s = q 0 to β s (i) = q i where q j ∈ {α s ( j), β s ( j)} for Fig. 5 Left: The situation described in the proof of Lemma 3.6.Note that the proof shows that μ = i (that is, q μ = β s (i)) and ν = i + 1 (that is, p ν−1 = p i ).The zigzag line from s to q μ−1 indicates a path of length i − 1, whereas the zigzag line from p ν to t indicates a path of length d * − i − 1. Right: The (similar) situation for the case p i < s.Note that also here μ = i and ν = i + 1 holds (that is, p ν−1 = p i and q μ = α s (i)) all 0 ≤ j ≤ i.Let μ ≤ i be an index such that q μ−1 < p i < q μ .Further, let ν > i be the minimal index for which Then, {q μ−1 , q μ } and { p ν−1 , p ν } are crossing edges.By the X-property, G contains the edge {q μ , p ν }, where either Hence, we obtain an s-t walk P = q 0 , . . ., q μ , p ν , . . ., p d * of length μ + 1 + (d * − ν).Clearly, this length must be at least d * , which implies μ + 1 ≥ ν.Since μ ≤ i and ν ≥ i + 1, this is only possible for μ = μ = i and ν = i + 1.Hence, P has length d * , which means that P is a shortest path from s to t (with i < ri(P )).Recall that, by construction, q j ∈ {α s ( j), β s ( j)} holds for all 0 ≤ j ≤ i, which contradicts the choice of P.This proves the existence of a shortest s-t path satisfying (i).
Before moving on, we show that if the chosen shortest path P in the above argument satisfies (ii), then also P does so.This observation will be helpful for the second part of the proof.To this end, note that we concluded above that μ = μ = i and ν = i + 1, which implies that p i+1 < p i .Assume towards a contradiction that p j < p i+1 for some j < i.Then, there must be a pair of crossing edges from the subpaths from p j to p i and from p i+1 to t.By the X-property, there is now an edge between two non-consecutive vertices of P, contradicting the fact that P is an induced path.Thus, p i+1 must be to the left of p 0 , . . ., p i .This implies i < li(P), and thus, p li(P) is also a vertex of P .Furthermore, all of q 0 , . . ., q i must be to the right of p li(P) because otherwise P would contain a pair of crossing edges from the subpaths from s to q i and from p li(P) to t.Thus, li(P) = li(P ).We conclude that i < li(P) = li(P ), that is, P also satisfies (ii).Now, assume towards a contradiction that no shortest s-t path satisfies both (i) and (ii).Then, let P = p 0 , p 1 , . . ., p d * be a shortest s-t path that satisfies (i) and which is chosen to minimize i > li(P), which is the largest index such that p i / ∈ {α t (d * − i), β t (d * − i)}.Now, if we reverse the vertex ordering of G, swap s and t, and reverse the direction of P, then we obtain a shortest s-t path P satisfying (ii) but not (i).Applying the above arguments to P now yields a contradiction.
Fig. 6 The three cases distinguished by Lemma 3.7 (only the vertices of a shortest path are drawn) Note that the only case in which Lemma 3.6 does not restrict all vertices to be α or β vertices is when ri(P) < li(P), in which case Lemma 3.2 applies.This will allow us to show that, when searching for a shortest s-t path, it is sufficient to check for three possible cases (illustrated in Fig. 6).In the following, we define the left and right horizon of a vertex v as the furthest neighbor in that direction, that is, lhorizon(v) := min(N [v]) and rhorizon(v) := max(N [v]).Remember that d * = dist(s, t).Proof For (i) and (ii), the existence of an s-t path of length at most d follows directly from the definition of α and β.Assume now that (iii) is true.We may also assume s < w since s = w would directly imply d * = τ < d.Define h v := rhorizon(v) and h w := lhorizon(w).If the edges {v, h v } and {w, h w } are crossing, then the existence of an s-t path of length σ + τ + 3 follows from the X-property.Otherwise, let v ≤ h w ≤ s < w (the case v < t ≤ h v ≤ w is symmetric).Let P be a shortest s-v path.Then, P either crosses the edge {w, h w } or contains w or h w .In any case, we obtain an s-w path of length at most σ + 1 and thus an s-t path of length at most σ + τ + 1 < d.If condition (ii) is true, then there are σ and τ with σ + τ = d * − 1 such that α s (σ ) and α t (τ ) are connected by an edge.As soon as k = σ , the vertex α s (σ ) will be added to Q s by UpdateDistances and as soon as k = τ , the vertex α t (τ ) will be added to Q t .Therefore, when k reaches the value max{σ, τ }, then d will be set to d * either in line 14 or line 18 (depending on whether σ ≤ τ ).
Since d ≥ d * is an invariant, the for loop will at some point reach k = d * , and thus, Algorithm 1 outputs exactly d * .It remains to prove the running time.As shown above, the for loop is repeated at most d * + 1 times.Apart from the inner while loops, each iteration only takes constant time.Since at most one vertex is added to Q s and Q t in each of these iterations, the inner while loops are also iterated at most d * + 1 times overall.Note that initializing all d s [v] and d t [v] with ∞ in line 3 is only done for ease of notation.In practice, these distance values only need to be stored once they get updated by UpdateDistances.Note that even though we always assumed dist(s, t) < ∞, Algorithm 1 can easily be extended to handle the case dist(s, t) = ∞ by additionally testing whether any of the vertices α s , β s , α t , β t was updated in ExtendSearchRange.If not, then the algorithm terminates.

Conclusion
We developed an output-sensitive algorithm for unweighted shortest path computation in terrain-like graphs.We conclude with some open questions.First, a fast algorithm for finding shortest paths with respect to Euclidean distances on terrain-like graphs (or on induced subgraphs of terrain visibility graphs) might be of interest.Second, our algorithm assumes that a vertex ordering satisfying the X-property is known.Is it possible to compute shortest paths without knowing the vertex ordering (or can one compute such an ordering efficiently)?Third, can the algorithm be extended to compute all shortest paths from a single source vertex to all other vertices?Finally, it might be worth to search for more efficient algorithms to compute other graph characteristics used for time series analysis in practice such as clustering coefficients or centrality measures.

Fig. 2 Fig. 3
Fig.2An ordering of the vertices of the graph C 6 that satisfies the X-property.This graph is terrain-like but not a terrain visibility graph
How does the running time change if we incorporate the computation of lclosest a (v) and rclosest a (v)?Algorithm 1 requires knowledge of these values for all a ∈ {s, t} andv ∈ {α b (i), β b (i) | b ∈ {s, t}, i ≤ d * }.This amounts to O(d * ) many computations, each taking O(log(Δ)) time where Δ is the maximal degree of any vertex.The overall running time is thus in O(d • log(Δ)).Alternatively, one can precompute these values for all a, v ∈ V (G) in O(n 2 ) time and then run Algorithm 1 in output-optimal time O(d * ) for any pair s, t.