Shortest Beer Path Queries in Outerplanar Graphs

A \emph{beer graph} is an undirected graph $G$, in which each edge has a positive weight and some vertices have a beer store. A \emph{beer path} between two vertices $u$ and $v$ in $G$ is any path in $G$ between $u$ and $v$ that visits at least one beer store. We show that any outerplanar beer graph $G$ with $n$ vertices can be preprocessed in $O(n)$ time into a data structure of size $O(n)$, such that for any two query vertices $u$ and $v$, (i) the weight of the shortest beer path between $u$ and $v$ can be reported in $O(\alpha(n))$ time (where $\alpha(n)$ is the inverse Ackermann function), and (ii) the shortest beer path between $u$ and $v$ can be reported in $O(L)$ time, where $L$ is the number of vertices on this path. Both results are optimal, even when $G$ is a beer tree (i.e., a beer graph whose underlying graph is a tree).


Introduction
Imagine that you are going to visit a friend and, not wanting to show up empty handed, you decide to pick up some beer along the way. In this paper we determine the fastest way to go from your place to your friend's place while stopping at a beer store to buy some drinks.
A beer graph is a undirected graph G = (V, E), in which each edge (u, v) has a positive weight ω(u, v) and some of the vertices are beer stores. For two vertices u and v of G, we define the shortest beer path from u to v to be the shortest (potentially non-simple) path that starts at u, ends at v, and visits at least one beer store. We denote this shortest path by SP B (u, v). The beer distance dist B (u, v) between u and v is the weight of the path SP B (u, v), i.e., the sum of the edge weights on SP B (u, v).
Observe that even though the shortest beer path from u to v may be a non-simple path, it is always composed of two simple paths: the shortest path from u to a beer store and

Our Results
We present data structures that can answer shortest beer path queries in outerplanar beer graphs. Recall that a graph G is outerplanar, if G can be embedded in the plane, such that all vertices are on the outer face, and no two edges cross.
Our first result is stated in terms of the inverse Ackermann function. We use the definition as given in [3]: Let A 0 (i) = i + 1 and, for ≥ 0, A +1 (i) = A (i+1) (i + 8), where A (i+1) is the function A iterated i + 1 times. We define α(m, n) to be the smallest value of for which A ( m/n ) > n, and we define α(n) = α(n, n).
For example, straightforward induction proofs show that A 1 (i) > 2i, A 2 (i) > 2 i , and where the number of 2's is equal to i. Thus, if we let log * n denote the number of times the function "log" must be applied, when starting with the value n, until the result is at most 1, then α(n log * n, n) ≤ 3. Let log * * n be the number of times the function "log * " must be applied, again starting with n, until the result is at most 1. Since the function α(m, n) is non-decreasing in m, and since log * * n ≤ log * n, it also follows that α(n log * * n, n) ≤ 3.
Theorem 1 Let G be an outerplanar beer graph with n vertices. For any integer m ≥ n, we can preprocess G in O(m) time into a data structure of size O(m), such that for any two query vertices u and v, both dist(u, v) and dist B (u, v) can be computed in O(α(m, n)) time.
By taking m = n, both the preprocessing time and the space used are O(n), and for any two query vertices u and v, both dist(u, v) and dist B (u, v) can be computed in O(α(n)) time. As another example, by taking m = n log * * n, we obtain a data structure with space and preprocessing time O(n log * * n) that can answer both distance and beer distance queries in O(1) time.
As we mentioned before, beer distance queries have not been considered for any class of graphs. In fact, the only result on (non-beer) distance queries in outerplanar graphs that we are aware of is by Djidjev et al. [5]. They show that an outerplanar graph with n vertices can be preprocessed in O(n log n) time into a data structure of size O(n log n), such that any distance query can be answered in O(log n) time. Our result in Theorem 1 significantly improves their result.
We also show that the result in Theorem 1 is optimal for beer distance queries, even if G is a beer tree (i.e., a beer graph whose underlying graph is a tree). We do not know if the query time is optimal for (non-beer) distance queries.
Our second result is on reporting the shortest beer path between two query vertices.
Theorem 2 Let G be an outerplanar beer graph with n vertices. We can preprocess G in O(n) time into a data structure of size O(n), such that for any two vertices u and v, the shortest beer path from u to v can be reported in O(L) time, where L is the number of vertices on this beer path.
Observe that the query time in Theorem 2 does not depend on the number n of vertices of the graph. Again, we are not aware of any previous work on reporting shortest beer paths. Djidjev et al. [5] show that, after O(n log n) preprocessing and using O(n log n) space, the shortest (non-beer) path between two query vertices can be reported in O(log n + L) time, where L is the number of vertices on the path.

Preliminaries and Organization
Throughout this paper, we only consider outerplanar beer graphs G. The number of vertices of G is denoted by n. It is well known that G has at most 2n − 3 edges. As in [5], we say that G satisfies the generalized triangle inequality, if for every edge (u, v) in G, dist(u, v) = ω(u, v), i.e., the shortest path between u and v is the edge (u, v).
The outerplanar graph G is called maximal, if adding an edge between any two nonadjacent vertices of G results in a graph that is not outerplanar. In this case, the number of edges is equal to 2n − 3. A maximal outerplanar graph G is 2-connected, each internal face of G is a triangle and the outer face of G forms a Hamiltonian cycle. In such a graph, edges on the outer face will be referred to as external edges, where all other edges will be referred to as internal edges.

3
The weak dual of a maximal outerplanar graph G is the graph D(G) whose node set is the set of all internal faces of G, and in which (F, F ) is an edge if and only if the faces F and F share an edge in G; see Figure 1. For simplicity, we will refer to D(G) as the dual of G. Observe that D(G) is a tree with n − 2 nodes, each of which has degree at most three. If H is a subgraph of the beer graph G, and u and v are vertices of H, then dist(u, v, H) and dist B (u, v, H) denote the distance and beer distance between u and v in H, respectively. The shortest beer path in H between u and v must be entirely within H. Observe that we use the shorthand dist(u, v) for dist(u, v, G), and dist B (u, v) for dist B (u, v, G).
It will not be surprising that the algorithms for computing shortest beer paths use the dual D(G). Thus, our algorithms will need some basic data structures on trees. These data structures will be presented in Section 2.
In Section 3, we will prove Theorem 1 for maximal outerplanar beer graphs. We also prove that the result in Theorem 1 is optimal, even for beer trees. The proof of Theorem 2, again for maximal outerplanar beer graphs, will be presented in Section 4. Both Sections 3 and 4 will use the result in Lemma 4, whose detailed proof will be given in Section 5.
The extensions of Theorems 1 and 2 to arbitrary outerplanar beer graphs will be given in Section 6. Finally, Section 7 will present an O(n)-time algorithm for computing the single-source shortest beer path tree for any given source vertex.

Query Problems on Trees
Our algorithms for computing beer shortest paths in an outerplanar graph G will use the dual of G, which is a tree. In order to obtain fast implementations of these algorithms, we need to be able to solve several query problems on this tree. In this section, we present all query problems that will be used in later sections.
Lemma 1 Let T be a tree with n nodes that is rooted at an arbitrary node. We can preprocess T in O(n) time, such that each of the following queries can be answered in O(1) time: 1. Given a node u of T , return its level, denoted by level (u), which is the number of edges on the path from u to the root.
2. Given two nodes u and v of T , report their lowest common ancestor, denoted by LCA(u, v).
3. Given two nodes u and v of T , decide whether or not u is in the subtree rooted at v.

4.
Given two distinct nodes u and v of T , report the second node on the path from u to v.

5.
Given three nodes u, v, and w, decide whether or not w is on the path between u and v.
Proof. The first claim follows from the fact that by performing an O(n)-time pre-order traversal of T , we can compute level (u) for each node u. A proof of the second claim can be found in Harel and Tarjan [6] and Bender and Farach-Colton [2]. The third claim follows from the fact that u is in the subtree rooted at v if and only LCA(u, v) = v. A proof of the fourth claim can be found in Chazelle [4,Lemma 15]. The fifth claim follows from the following observations. Assume that u is in the subtree rooted at v. Then w is on the path between u and v if and only if LCA(u, w) = w and w is in the subtree rooted at v. The case when v is in the subtree rooted at u is symmetric. Assume that LCA(u, v) ∈ {u, v}. Then w is on the path between u and v if and only if w is on the path between u and LCA(u, v) or w is on the path between v and LCA(u, v).

Closest-Color Queries in Trees
Let T be a tree with n nodes and let C be a set of colors. For each color c in C, we are given a path P c in T . Even though these paths may share nodes, each node of T belongs to at most a constant number of paths. This implies that the total size of all paths P c is O(n). We assume that each node u of T stores the set of all colors c such that u is on the path P c . In a closest-color query, we are given two nodes u and v of T , and a color c, such that u is on the path P c . The answer to the query is the node on P c that is closest to v. In other words, when following the path in T from u to v, the answer to the query is the last node on this path that is also on P c . Refer to Figure 2 for an illustration.  Proof. We take an arbitrary node of T and make it the root. Then we preprocess T such that each of the queries in Lemma 1 can be answered in O(1) time.
For each color c, let c 1 and c 2 be the end nodes of the path P c , and let c h be the highest node on P c in the tree (i.e., the node on P c that is closest to the root). With each node of P c , we store pointers to c 1 , c 2 , and c h .
Since each node of T is in a constant number of colored paths, we can compute the pointers for all the colored paths in O(n) total time.
The query algorithm does the following. Let u and v be two nodes of T , and let c be a color such that u is on the c-colored path P c .
If u = v or v is also on P c , then we return the node v. From now on, assume that u = v and v is not on P c . Below, we consider all possible cases, which are illustrated in Figure 3.
1. If LCA(u, v) = v, then u is in the subtree rooted at v. In this case, we return c h , the highest c-colored node.
2. Assume that LCA(u, v) = u. Then v is in the subtree rooted at u.
We only justify the correctness for case (a), because the argument for case (b) is symmetric. Let u be the child of u such that v is in the subtree rooted at u .
First assume that the path P c does not contain any node of the subtree rooted at u . Then LCA(v, c 1 ) = u, which is the correct answer to the query.
Next assume that c 1 is in the subtree rooted at u , but c 2 is not in this subtree. Then LCA(v, c 1 ) is in this subtree and is the correct answer to the query.
If c 1 is not in the subtree rooted at u , but c 2 is in this subtree, then LCA(v, c 2 ) is also in this subtree, whereas LCA(v, c 1 ) is on the path from u to the root of T . Thus, level (LCA(v, c 1 )) < level (LCA(v, c 2 )) and, therefore, this cannot happen.
We finally observe that c 1 and c 2 cannot both be in the subtree rooted at u , because P c is a path that contains u, c 1 , and c 2 .  For the correctness, let u be the child of LCA(u, v) whose subtree contains u.
Then c h is on the path in T from u to u , and c h is the correct answer to the query. Figure 3: Illustrating all possible cases in the proof of Lemma 2. The path P c is red and the blue square indicates the node that is returned by the closest-color query.
For the correctness, let x be the child of c h whose subtree contains u. Then LCA(u, v) is also in this subtree and LCA(u, v) is on P c . Since LCA(u, v) is the only node on the path from v to LCA(u, v) that is on P c , the node LCA(u, v) is the correct answer to the query.
(c) Assume that level (c h ) = level (LCA(u, v)). Observe that exactly one end node of P c is in the subtree rooted at u.
i. If c 1 is in the subtree rooted at u, then we return LCA(v, c 2 ). ii. If c 2 is in the subtree rooted at u, then we return LCA(v, c 1 ).
For the correctness, assume that c 1 is in the subtree rooted at u. Then LCA(u, v) = c h . If c 2 is on the path from v to LCA(u, v), then LCA(v, c 2 ) = c 2 , which is the correct answer to the query. Otherwise, LCA(u, v) has at least three children, and u, v, and c 2 are in different subtrees of LCA(u, v). In this case, LCA(v, c 2 ) = LCA(u, v), which is the correct answer to the query.
Using Lemma 1, each of these case takes O(1) time. Therefore, the entire query algorithm takes O(1) time.

Path-Sum Queries in Trees
Let (W, ⊕) be a semigroup. Thus, W is a set and ⊕ : W × W → W is an associative binary operator. We assume that for any two elements s and s in W , the value of s ⊕ s can be computed in O(1) time.
Let T be a tree with n nodes in which each edge e stores a value s(e), which is an element of W . For any two distinct nodes u and v in T , we define their path-sum PS(u, v) as follows: Let e 1 , e 2 , . . . , e k be the edges on the path in T between u and v. Then we define Chazelle [4] considers the problem of preprocessing the tree T , such that for any two distinct query nodes u and v, the value of PS(u, v) can be reported. (See also Alon and Schieber [1], Thorup [9], and Chan et al. [3].) Chazelle's result is stated in terms of the inverse Ackermann function; see Section 1.1.
Lemma 3 Let T be a tree with n nodes in which each edge stores an element of the semigroup (W, ⊕). For any integer m ≥ n, we can preprocess T in O(m) time into a data structure of size O(m), such that any path-sum query can be answered in O(α(m, n)) time.
Remark 1 Assume that (W, ⊕) is the semigroup, where W is the set of all real numbers and the operator ⊕ takes the minimum of its arguments. In this case, we will refer to a query as a path-minimum query. For this semigroup, the result of Lemma 3 is optimal: Any data structure that can be constructed in O(m) time has worst-case query time Ω(α(m, n)). To prove this, assume that we can answer any query in o(α(m, n)) time. Then the on-line minimum spanning tree verification problem on a tree with n vertices and m ≥ n queries can be solved in o(m · α(m, n)) time, by performing a path-maximum query for the endpoints of each edge e and checking that the weight of e is larger than the path-maximum. This contradicts the lower bound for this problem proved by Pettie [8].

Beer Distance Queries in Maximal Outerplanar Graphs
Let G be a maximal outerplanar beer graph with n vertices that satisfies the generalized triangle inequality. We will show how to preprocess G, such that for any two vertices u and v, the weight, dist B (u, v), of a shortest beer path between u and v can be reported. Our approach will be to define a special semigroup (W, ⊕), such that each element of W "contains" certain distances and beer distances. With each edge of the dual D(G), we will store one element of the set W . As we will see later, a beer distance query can then be 8 reduced to a path-sum query in D(G). Thus, by applying the results of Section 2.2, we will obtain a proof of Theorem 1.
We will need the first claim in the following lemma. The second claim will be used in Section 4.

Lemma 4
Consider the beer graph G as above.
for each edge (u, v) in G. Proof. We choose an arbitrary face R of G and make it the root of D(G). Let (u, v) be any edge of G. This edge divides G into two outerplanar subgraphs, both of which contain (u, v) as an edge. Let G R uv be the subgraph that contains the face R, and let G ¬R uv denote the other subgraph. Note that if (u, v) is an external edge, then G R uv = G and G ¬R uv consists of the single edge (u, v). By the generalized triangle inequality, the shortest beer path between u and v is completely in G R uv or completely in G ¬R uv . The same is true for the shortest beer path from u to itself. Thus, for each edge (u, v) of G,

After an
. By performing a post-order traversal of D(G), we can compute dist B (u, v, G ¬R uv ) and dist B (u, u, G ¬R uv ) for all edges (u, v), in O(n) total time. After these values have been computed, we perform a pre-order traversal of D(G) and obtain dist B (u, v, G R uv ) and dist B (u, u, G R uv ), again for all edges (u, v), in O(n) total time. The details will be given in Section 5.
In the rest of this section, we assume that all beer distances in the first claim of Lemma 4 have been computed.
For any two distinct internal faces F and F of G, let Q F,F be the union of the two sets where the "bits" D and BD indicate whether the tuple represents a distance or a beer distance. In words, Q F,F is the set of all shortest path distances and all shortest beer distances between a vertex in F and a vertex in F . Since each internal face has three vertices, the set Q F,F has exactly 18 elements.
Observation 1 Let u and v be vertices of G, and let F and F be internal faces that contain u and v as vertices, respectively.
2. If F = F and we are given the set Q F,F , then we can determine both dist(u, v) and In the rest of this section, we will show that Lemma 3 can be used to compute the set Q F,F for any two distinct internal faces F and F .

Lemma 5 For any edge
Proof. Let u be a vertex of F and let v be a vertex of F . Consider the subgraph G[F, F ] of G that is induced by the four vertices of F and F ; this subgraph has five edges. By the generalized triangle inequality, ) is an edge of G, then dist B (u, v) has been precomputed. Assume that u = v and (u, v) is not an edge of G. Let w and w be the two vertices that are shared by F and F . Since any path in G between u and v (and, in particular, any shortest beer path between u and v) contains at least one of w and w , dist B (u, v) is the minimum of Since (u, w), (w, v), (u, w ), and (w , v) are edges of G, all terms in these four sums have been precomputed. Therefore, We have shown that each of the 18 elements of Q F,F can be computed in O(1) time. Therefore, this entire set can be computed in O(1) time.
Lemma 6 Let F , F , and F be three pairwise distinct internal faces of G, such that F is on the path in D(G) between F and F . If we are given the sets Q F,F and Q F ,F , then the set Q F,F can be computed in O(1) time.
Proof. Let u be a vertex of F and let v be a vertex of F . Since G is an outerplanar graph, any path in G between u and v must contain at least one vertex of F . It follows that Figure 4: Any beer path from u to v contains at least one vertex of F . In (a), we consider the shortest beer path from u to w, followed by the shortest path from w to v. In (b), we consider the shortest path from u to w, followed by the shortest beer path from w to v.
By a similar argument, dist B (u, v) is equal to (refer to Figure 4) Thus, since each of the 18 elements of Q F,F can be computed in O(1) time, the entire set can be computed in O(1) time.
We define where ⊥ is a special symbol. We define the operator ⊕ : W × W → W in the following way. 3. In all other cases, the operator ⊕ returns ⊥.
It is not difficult to verify that ⊕ is associative, implying that (W, ⊕) is a semigroup. By Lemma 5, we can compute Q F,F for all edges (F, F ) of D(G), in O(n) total time.
Recall from Lemma 1 that, after an O(n)-time preprocessing, we can decide in O(1) time, for any three internal faces F , F , and F of G, whether F is on the path in D(G) between F and F . Therefore, using Lemma 6, the operator ⊕ takes O(1) time to evaluate for any two elements of W .
Finally, let F and F be two distinct internal faces of G, and let F = F 0 , F 1 , F 2 , . . . , F k = F be the path in D(G) between F and F . Then Thus, if we store with each edge of the tree D(G), the corresponding element of the semigroup, then computing Q F,F becomes a path-sum query as in Section 2.2.
To summarize, all conditions to apply Lemma 3 are satisfied. As a result, we have proved Theorem 1 for maximal outerplanar graphs that satisfy the generalized triangle inequality.

The Result in Theorem 1 is Optimal
In Section 2.2, see also Remark 1, we have seen path-minimum queries in a tree, in which each edge e stores a real number s(e). In such a query, we are given two distinct nodes u and v, and have to return the smallest value s(e) among all edges e on the path between u and v. Lemma 3 gives a trade-off between the preprocessing and query times when answering such queries.
Let D be an arbitrary data structure that answers beer distance queries in any beer tree. Let P (n), S(n), and Q(n) denote the preprocessing time, space, and query time of D, respectively, when the beer tree has n nodes. We will show that D can be used to answer path-minimum queries. This reduction, combined with Remark 1, implies that the result of Theorem 1 is optimal (even for beer trees).
Consider an arbitrary tree T with n nodes, such that each edge e stores a real number s(e). We may assume without loss of generality that 0 < s(e) < 1 for each edge e of T .
By making an arbitrary node the root of T , the number of edges on the path in T between two nodes u and v is equal to Thus, by Lemma 1, after an O(n)-time preprocessing, we can compute the number of edges on this path in O(1) time.
We create a beer tree T as follows. Initially, T is a copy of T . For each edge e = (u, v) of T , we introduce a new node x e and replace e by two edges (u, x e ) and (v, x e ); we assign a weight of 1 to each of these two edges. In the current tree T , none of the nodes has a beer store. For every node x e in T , we introduce a new node x e , add the edge (x e , x e ), assign a weight of s(e) to this edge, and make x e a beer store. Finally, we construct the data structure D for the resulting beer tree T . Since T has n + 2(n − 1) = 3n − 2 nodes, it takes P (3n − 2) + O(n) time to construct D from the input tree T . Moreover, the amount of space used is S(3n − 2) + O(n).
Let u and v be two distinct nodes in the original tree T , let π be the path in T between u and v, and let be the number of edges on π. The corresponding path π in T between u and v has weight 2 .
For any edge e of T , let π e be the beer path in T that starts at u, goes to x e , then goes to x e and back to x e , and continues to v.
If e is an edge of π, then the weight of π e is equal to 2 + 2 · s(e), which is less than 2 + 2. On the other hand, if e is an edge of T that is not on π, then the weight of π e is at least 2 + 2 + 2 · s(e), which is larger than 2 + 2. It follows that the shortest beer path in T between u and v visits the beer store x e , where e is the edge on π for which s(e) is minimum.
Thus, by computing and querying D for the beer distance in T between u and v, we obtain the smallest value s(e) among all edges e on the path in T between u and v. The query time is Q(3n − 2) + O(1).
By combining this reduction with Remark 1, it follows that the result of Theorem 1 is optimal.

Reporting Shortest Beer Paths in Maximal Outerplanar Graphs
Let G be a maximal outerplanar beer graph with n vertices that satisfies the generalized triangle inequality. In this section, we show that, after an O(n)-time preprocessing, we can report, for any two query vertices s and t, the shortest beer path SP B (s, t) from s to t, in O(L) time, where L is the number of vertices on this path. As before, D(G) denotes the dual of G. Proof. For any vertex v and any vertex u on ρ v , we store the weight of the path from u to CW(v) along ρ v . Observe that CW(u) Figure 5: A maximal outerplanar graph G. The subgraphs G[P v ] and G[P u ] are shown in red and blue, respectively. Both the v-chain ρ v and the u-chain ρ u are shown in bold. Both paths P v and P u are shown in black. Observe that P u is a single node.
Any exterior edge in G is in exactly one chain and any interior edge in G is in exactly two chains. Thus, the sum of the number of edges on each chain is proportional to the number of edges of G, which is O(n).

Lemma 8
After an O(n)-time preprocessing, we can answer the following query in O(1) time: Given three query vertices v, u, and w, such that both u and w are vertices of G[P v ], report dist(u, w), i.e., the distance between u and w in G.
Proof. We get the following cases; the correctness follows from the generalized triangle inequality: 1. If u = w then dist(u, w) = 0.
2. If u = v then (u, w) is an edge and we return ω(u, w). Similarly if w = v, we return ω(u, w).

Proof. Recall from Lemma 4 that we can compute
Recall that by the generalized triangle inequality, ω(u i , u i+1 ) = dist(u i , u i+1 ). Therefore, A[i] holds the difference between the weights of the shortest path from u i to u i+1 and the shortest beer path from u i to u i+1 . After preprocessing the array A v [ ] in O(N ) time, we can conduct range minimum queries in O(1) time. (Bender and Farach-Colton [2] show that these queries are equivalent to LCAqueries in the Cartesian tree of the array.) Thus, for each v-chain of N nodes, we spend O(N ) time processing the v-chain. Since every edge is in at most two chains, processing all v-chains takes O(n) time and space.
Given two vertices u and w of G[P v ], we determine the beer distance dist B (u, w) as follows: 1. If u = w then dist B (u, w) has already been computed by Lemma 4.
2. If u = v or w = v, then there is an edge from v to the other vertex. Thus, dist B (u, w) has already been computed by Lemma 4.
3. Otherwise, u, w and v are three distinct vertices. Assume without loss of generality that w is clockwise from u on the v-chain. We take the minimum of the following two cases: (a) The shortest beer path from u to w that goes through v. Since a beer store must be visited before or after v, this beer path has a weight of min(dist B (u, v) + ω(v, w), ω(u, v) + dist B (v, w)).
(b) The shortest beer path through the vertices of the v-chain. Note that this beer path will visit each vertex on the v-chain between u and w, but may go off the v-chain to visit a beer store. On SP B (u, w), there is one pair of vertices, u i and u i+1 , such that a beer path is taken between u i and u i+1 , and u i and u i+1 are adjacent on the v-chain; refer to Figure 7. The shortest path is taken between all other pairs of adjacent vertices on the v-chain. From Lemma 7, we can compute dist(u, w, ρ v ) in O(1) time. The shortest beer path through the vertices of the v-chain has a weight of dist(u, w, is the additional distance needed to visit a beer store between u i and u i+1 . Let u be the j th vertex on ρ v and let w be the k th vertex in  Figure 7: Both figures show a shortest beer path from u to w through the vertices on the v-chain. Thicker edges on the blue beer path are edges that are traversed twice; once in each direction.

Answering Shortest Beer Path Queries
Recall that, for any vertex v of G, P v denotes the path in D(G) formed by the faces of G containing v. Moreover, G[P v ] denotes the subgraph of G induced by these faces. Consider two query vertices s and t of G. Our goal is to compute the shortest beer path SP B (s, t).
Let F s and F t be arbitrary faces containing s and t, respectively. If t is in G[P s ] then, by Lemma 10, we can construct SP B (s, t) in O(L) time. For the remainder of this section, we assume that t is not in G[P s ]. To find SP B (s, t), we start by constructing a directed acyclic graph (DAG), H. In this DAG, vertices will be arranged in columns of constant size, and all edges go from left to right between vertices in adjacent columns. In H, each column will contain one vertex that is on SP B (s, t). First we will construct H and then we will show how we can use H to construct SP B (s, t). The entire construction is illustrated in Figure 8. (a, b) of G splits G into two subgraphs such that if s is in one subgraph and t is in the other, then any path in G from s to t must visit at least one of a and b.

Observation 3 Any interior edge
Let P be the unique path between F s and F t in D(G). Consider moving along P from F s to F t . Let F 1 be the node on P s that is closest to F t , and let F 1 be the successor of F 1 on P . Note that, by Lemmas 1 and 2, we can find F 1 and F 1 in O(1) time. 1 Let e 1 = (a 1 , b 1 ) be the edge in G shared by the faces F 1 and F 1 . Since SP B (s, t) must visit both of these faces, by Observation 3, at least one of a 1 or b 1 is on the shortest beer path.
We place s in the first column of H and a 1 and b 1 in the second column of H. We then add two directed edges from s to a 1 , one with weight dist(s, a 1 ) and the other with weight dist B (s, a 1 ). Similarly, we add two directed edges from s to b 1 with weights dist(s, b 1 ) and dist B (s, b 1 ).
When i ≥ 2 we construct the (i + 1) th column of H in the following way. Let e i−1 = (a i−1 , b i−1 ) be the edge shared by the faces F i−1 and F i−1 . The i th column of H contains the vertices a i−1 and b i−1 . Note that F i−1 is in both P b i−1 and P a i−1 . Using Lemma 2, we find the node F b i on P b i−1 that is closest to F t . If the vertex a i−1 is not in F b i , then we let F i = F b i . Otherwise, we let F i be the node on P a i−1 that is closest to F t . If t is not a vertex of F i , then let F i be the node that follows F i on P ; we find F i using Lemma 1. Let e i = (a i , b i ) be the edge of G shared by the faces F i and F i . In the (i + 1) th column, we place a i and b i . For each u ∈ {a i−1 , b i−1 } and each v ∈ {a i , b i } we add two directed edges (u, v) to the DAG, one with weight dist(u, v) and the other with weight dist B (u, v). If F i is in P a i−1 , all these vertices are in G[P a i−1 ]; otherwise, F i is in P b i−1 , and all these vertices are in G[P b i−1 ]. Thus, by Lemmas 8 and 10, we can find the distances and beer distances to assign to these edges in constant time.
If t is in F i , then in the (i + 1) th column we only place the vertex t. In this case, for each u ∈ {a i−1 , b i−1 }, we add two directed edges (u, t) to the DAG with weights dist(u, t) and dist B (u, t). At this point we are done constructing H.
We define a beer edge to be an edge of H that was assigned a weight of a beer path during the construction of H. We find the beer distance from s to t in G using the following dynamic programming approach in H.
The vertices a i−2 and b i−2 occur in the (i−1) th column. Thus, dist B (s, a i−2 ), dist B (s, b i−2 ), dist(s, a i−2 ), and dist(s, b i−2 ) will be computed before computing the values for the i th column. We get dist(a i−2 , u), dist B (a i−2 , u), dist(b i−2 , u) and dist B (b i−2 , u) from the weights of the DAG-edges between the (i − 1) th and i th columns of H.
By keeping track of which expression produced dist B (s, u) and dist(s, u), we can backwards reconstruct the shortest beer path in the DAG. Knowing the shortest beer path in the DAG enables us to construct the corresponding beer path in G as follows.
1. Define P st to be an empty path.
2. For each edge (w, v) of the shortest beer path in the DAG.
(a) If (w, v) was a beer edge, let P wv = SP B (w, v), which can be constructed in time proportional to its number of vertices via Lemma 10.
(b) Otherwise, let P wv = SP(w, v) which can be constructed in time proportional to its number of vertices as seen in Lemma 9.
Let P st = P st ∪ P wv .
3. Return P st , which is equal to SP B (w, v).
Let L denote the number of vertices on SP B (s, t). In order for the above query algorithm to take O(L) time, the size of the DAG must be O(L). The following three lemmas will show this to be true.
Lemma 11 For 2 ≤ i < M − 1, F i contains either a i−1 or b i−1 , but not both.
Proof. Recall that we defined F b i to be the last node on P that is also on P b i−1 . We similarly define F a i to be the last node on P that is also on P a i−1 . From the way we choose F i , F i is either F b i or F a i . We only choose F i = F b i after having checked that a i−1 is not in F b i ; thus in this case we can be sure that F i only contains b i−1 .
Assume for the purpose of contradiction that we choose F i = F a i and b i−1 is also in F i . Let the third vertex of F i be c. Let the face on P immediately following F i be F i . The edge shared by F i and F i is either (b i−1 , c) or (a i−1 , c). If (b i−1 , c) is the shared edge, then F i is a face closer to F t that contains b i−1 and not a i−1 , so we would have chosen F i = F b i , which is a contradiction. Otherwise, (a i−1 , c) is the edge shared by F i and F i , which implies that there is a face containing a i−1 closer to F t in P than F a i , which contradicts the definition of F a i .
Lemma 12 Every vertex of G appears in at most one column of H.
Proof. Since (a 1 , b 1 ) is an edge shared by both the last face of P containing s and the first face of P that does not contain s it is not possible for either of these vertices to be the vertex s. Thus, s will only be represented by the vertex in the first column of H. By stopping the construction of H as soon as we add a vertex representing t, we ensure that H only contains one vertex corresponding to the vertex t in G.
is not a vertex on any of the faces on the path from F i to F t . Thus, subsequent columns of H will not contain vertices representing the vertex a i−1 in G.
If F i = F b i then by Lemma 11, a i−1 is not in F i and since (a i , b i ) is an edge of F i , a i = a i−1 and b i = a i−1 . Because F i is a face on P closer to F t than F i−1 (a face that contains a i−1 ) it follows from Observation 2 that none of the faces on P from F i−1 to F t will have the vertex a i−1 on their face and, thus, a i−1 will not be represented by vertices in subsequent columns of H.

Proof of Lemma 4
Let G be a maximal outerplanar beer graph with n vertices that satisfies the generalized triangle inequality. We will first show how to compute dist B (u, u) for each vertex u of G, and dist B (u, v) for each edge (u, v) of G. Consider again the dual D(G) of G. We choose an arbitrary face of G and make it the root of D(G).
Let (u, v) be any edge of G. This edge divides G into two outerplanar subgraphs, both of which contain (u, v) as an edge. Let G R uv be the subgraph that contains the face represented by the root of D(G), and let G ¬R uv denote the other subgraph. Note that if (u, v) is an external edge, then G R uv = G and G ¬R uv consists of the single edge (u, v). By the generalized triangle inequality, the shortest beer path between u and v is completely in G R uv or completely in G ¬R uv . The same is true for the shortest beer path from u to itself. This implies: Thus, it suffices to first compute dist B (u, v, G ¬R uv ), dist B (u, u, G ¬R uv ), and dist B (v, v, G ¬R uv ) for all edges (u, v), and then compute dist B (u, v, G R uv ), dist B (u, u, G R uv ), and dist B (v, v, G R uv ), again for all edges (u, v).

Recurrences for
Let (u, v) be an edge of G. Item 1. below presents the base cases, whereas item 2. gives the recurrences.
1. Assume that (u, v) is an external edge of G.
(a) If both u and v are beer stores, then dist B (u, v, G ¬R uv ) = ω(u, v), dist B (u, u, G ¬R uv ) = 0, and dist B (v, v, G ¬R uv ) = 0. (b) If exactly one of u and v, say u, is a beer store, 2. Assume that (u, v) is an internal edge of G. Let w be the third vertex of the face of G ¬R uv that contains (u, v) as an edge. All possible cases are illustrated in Figure 9.
is obtained by swapping u and v in i., ii., and iii.
These recurrences express dist B (u, v, G ¬R uv ), dist B (u, u, G ¬R uv ), and dist B (v, v, G ¬R uv ) in terms of values that are "further down" in the tree D(G). Therefore, by performing a post-order traversal of D(G), we obtain all these values, for all edges (u, v) of G, in O(n) total time. ) be an edge of G. Item 1. below presents the base cases, whereas item 2. gives the recurrences.

Recurrences for
1. Assume that (u, v) is an edge of the face representing the root of D(G). Let w be the third vertex of this face.
(a) The value of dist B (u, v, G R uv ) is the minimum of i. dist B (u, u, G ¬R uw ) + ω(u, v), is obtained by swapping u and v in i., ii., and iii.
2. Assume that (u, v) is not an edge of the face represented by the root of D(G). Let w be the third vertex of the face of G R uv that contains (u, v) as an edge. We may assume without loss of generality that (v, w) is an edge of the face represented by the parent of the face representing (u, v, w). All possible cases are illustrated in Figure 10.
These recurrences express dist B (u, v, G R uv ), dist B (u, u, G R uv ), and dist B (v, v, G R uv ) in terms of values that are "higher up" in the tree D(G) and values that involve graphs with a superscript "¬R". These latter values have been computed already. Thus, by performing a pre-order traversal of D(G), we obtain all values dist B (u, v, G R uv ), dist B (u, u, G R uv ), and dist B (v, v, G R uv ), for all edges (u, v) of G, in O(n) total time. This completes the proof of the first claim in Lemma 4.
To prove the second claim in Lemma 4, consider dist B (u, v) where u = v or (u, v) is an edge of G. If u or v is a beer store, then store nil with dist B (u, v).
The values dist B (u, v, G R uv ) and dist B (u, v, G ¬R uv ) are computed as the minimum of a set of path weights from u to v through a vertex x such that x is adjacent to both u and v or x is equal to one of these vertices and adjacent to the other. Either the subpath from u to x is a beer path or the subpath from x to v is a beer path. Whenever we take the minimum of a set of path weights in the above computation, we store with that distance the vertex x and a bit to indicate which subpath is the beer path. When u = v we can arbitrarily choose which subpath is the beer path. After taking the minimum of dist B (u, v, G R uv ) and dist B (u, v, G ¬R uv ), we are left with a vertex, x, on the shortest beer path from u to v and the bit indicating which subpath is a beer path.
We recursively compute SP B (u, v) where either (u, v) is an edge of G or u = v as follows.
Since (a, c) and (b, c) are edges of G, we precomputed dist B (a, c) and dist B (b, c) in (i). Lastly, we computed dist(s, a) and dist(s, b) in (ii), so each of the values listed above can be computed in constant time.
The correctness of this algorithm follows from Observation 3 and the generalized triangle inequality. Since we do a constant amount of work at each face in the traversal of D(G) and the number of interior faces of a maximal outerplanar graph is n − 2, this algorithm takes O(n) time.
Let L be the number of vertices on the shortest beer path from s to v. If dist B (s, v) was found in step 1, then by Lemma 4, SP B (s, v) can be constructed in O(L) time. If this is not the case, then v = c in some iteration of step 2. At this step we store a vertex p(v) such that p(v) = a if (a) or (b) was the minimum of step 2 and p(v) = b otherwise. We also store a bit to indicate if the subpath from s to p(v) is the shortest path (as in cases (a) and (c)) or the shortest beer path (as in cases (b) and (d)). If the subpath from s to p(v) is the shortest path, we use the method described in [7] to find SP(s, p(v)) and use Lemma 4 to find SP B (p(v), v) and then concatenate SP(s, p(v)) and SP B (p(v), v) to get SP B (s, v). Both SP(s, p(v)) and SP B (p(v), v) are found in time proportional to the number of vertices on their paths, so this takes O(L) time. If the subpath from s to p(v) is the shortest beer path, then we recursively find SP B (s, p(v)) and concatenate it with the edge (p(v), v) (which is SP(p(v), v) by the generalized triangle inequality). Each iteration of the recursive step takes time proportional to the number of new vertices of the path found in that step. Thus, we find SP B (s, v) in a total of O(L) time.

Concluding Remarks
We have introduced a variant of the shortest path problem in weighted graphs, in which a subset of the vertices store a facility, say a beer store, and we want to compute the shortest path between two vertices, or the length of this path, that visits at least one facility. For the class of outerplanar beer graphs, we have presented data structures that are optimal in terms of preprocessing time, space usage, and query times.
As a byproduct, we have also obtained improved results for standard shortest path queries in outerplanar graphs. In particular, after an O(m)-time preprocessing of an n-vertex beer outerplanar graph, we can determine the length of a shortest path between any two query vertices in O(α(m, n)) time. We have shown that this is optimal for beer paths, even if the graph is a tree. Unfortunately, our reduction does not work for standard shortest path queries. Observe that, in any tree with n vertices, after O(n) preprocessing, we can report the length dist(u, v) of the path between any two query vertices u and v in O(1) time, because dist(u, v) = dist(u, r) + dist(v, r) − 2 · dist(LCA(u, v), r), where r is an arbitrarily chosen root of the tree.
Open Problem 1 Is there a linear-size data structure that can answer standard distance queries in outerplanar graphs in O(1) time?
Since shortest beer path queries are of relevance for practical applications, such queries should be studied in larger families of graphs. Obvious examples are general planar graphs and graphs of bounded tree width.