Waypoint Routing on Bounded Treewidth Graphs

In the \textsc{Waypoint Routing Problem} one is given an undirected capacitated and weighted graph $G$, a source-destination pair $s,t\in V(G)$ and a set $W\subseteq V(G)$, of \emph{waypoints}. The task is to find a walk which starts at the source vertex $s$, visits, in any order, all waypoints, ends at the destination vertex $t$, respects edge capacities, that is, traverses each edge at most as many times as is its capacity, and minimizes the cost computed as the sum of costs of traversed edges with multiplicities. We study the problem for graphs of bounded treewidth and present a new algorithm for the problem working in $2^{O(\mathrm{tw})}\cdot n$ time, significantly improving upon the previously known algorithms. We also show that this running time is optimal for the problem under Exponential Time Hypothesis.


Introduction
We study the Waypoint Routing Problem (WRP), which can be formally defined as follows: The input of the problem is an undirected, simple, capacitated and weighted graph G = (V, E, κ, ω), where κ : E → N and ω : E → N represent edge capacities and edge weights, respectively, a source-destination pair s, t ∈ V , and a set W ⊆ V , of waypoints. A solution to the instance is a walk R which starts at the source vertex s, visits, in any order, all waypoints w ∈ W , ends at the destination vertex t, and respects edge capacities, that is, for each edge e ∈ E, the walk R traverses e at most κ(e) times. Note that the order of the waypoints along the walk is not prescribed by the input and may be selected arbitrarily to suit the solution. Among all walks satisfying the condition above, we aim to minimize the cost of R, computed as a sum of weights of all edges in solution with multiplicities.
The problem is motivated by modern networking systems connecting distributed network functions, often composed of middleboxes, possibly virtualized, such as service chaining [1], hybrid software defined networks [2], or segment routing [3].
Study of (this variant of) the Waypoint Routing Problem was initiated by Amiri et al. [4]. Inspired by the result of Rost et al. [5], who showed that many natural network topologies such as backbone transit and wide area networks have small treewidth (i.e., they are in certain sense similar to trees), Amiri et al. [4] analyzed parameterized complexity of the problem with respect to the treewidth (denoted tw) of the input graph. They presented a dynamic programming algorithm with running time n O(tw 2 ) .
As the running time of this algorithm seems rather impractical, it raises a question, whether it can be improved to say n O(tw) or even f (tw) · n O(1) for a suitable computable function f .
Our contribution. In this paper we answer this question in the affirmative. In particular, we show that the Waypoint Routing Problem can be solved in 2 O(tw) · n time, that is, single exponential linear time, significantly improving upon the running time of the algorithm of Amiri et al. [4]. We also show that the running time of our algorithm is asymptotically optimal, assuming the popular Exponential Time Hypothesis of Impagliazzo and Paturi [6].
Related work. Apart from the above mentioned result, Amiri et al. [4] also showed that WRP admit a randomized 2 k · n O(1) -time algorithm, where k is the number of waypoints, which can be turned into a deterministic polynomial time algorithm when k = O((log log n) 1/10 ). They also showed that the problem is NP-hard on any class of graphs of maximum degree 3 on which Hamiltonian Cycle is NP-hard. By a simple padding argument they showed that this also holds when k = O(n 1/r ) for any constant r ≥ 1. We are not aware of any result concerning approximability of the problem.
More research was concerned with the Ordered WRP, where the exact order in which the waypoints must be visited is given as a part of the input [7,8]. Amiri et al. [8] showed that this problem can be solved in polynomial time on outerplanar graphs, while it is NP-hard on graphs of treewidth 3. Furthermore, it is FPT with respect to k and tw combined. They also studied Ordered WRP on special classes of directed graphs.
Amiri et al. [7] extended these results by showing that the Ordered WRP is FPT with respect to k alone and that even the question whether any walk exists, let alone its weight, is NP-hard for an unbounded number of waypoints. They further studied the problem for the case of one waypoint and showed that it is NP-hard on directed graphs, or if the demanded flow changes in the waypoint, or if we pose some other constraints on the solution walk.
The WRP is also somewhat related to the Shortest disjoint paths problem, where the task is to find k disjoint paths between a specified pair of vertices such that the sum of lengths of the paths is minimized. Interestingly, for this problem, even for k = 2, only a randomized polynomial time algorithm is known, but no deterministic [9]. Another well studied related problem is k-Cycle, where the task is to find a cycle through k specified vertices [10]. The problem has also similarities with Subset TSP, where the task is to find the shortest tour through a specified subset of vertices [11].
A somewhat opposite approach of placing the middleboxes or service chains in a software defined network in such a way that both load and routing constraints are fulfilled is also studied [12,13].

Preliminaries
We follow the basic notation of graph theory [14] and parameterized complexity theory [15]. For a set U , we say that a family of its subsets P ⊆ 2 U \ ∅ is a partition of U if we have A∈P A = U and for every two distinct A, B ∈ P we have A ∩ B = ∅.
Graph theory. An undirected multigraph is a pair G = (V, E), where V is a finite set of vertices and E is a set of edges, together with map η : E → [V ] 2 assigning to every edge two vertices -its endpoints. If there are no e 1 , e 2 ∈ E such that e 1 = e 2 and η(e 1 ) = η(e 2 ), i.e., parallel edges, we say that G is a simple graph. To simplify the notation we abbreviate the fact that u, v are endpoints of e to e = {u, v}, despite that the edge is no longer determined uniquely.
Parameterized complexity. Let Σ be a fixed, finite alphabet. A language L ⊆ Σ * × N is called parameterized problem and (x, k) ⊆ Σ * × N, where x is called input and k is a parameter, is an instance of problem L. Moreover an instance (x, k) is called yes-instance if and only if (x, k) ∈ L.
A parameterized problem L is called fixed-parameter tractable (FPT) if there exists algorithm A correctly deciding whether (x, k) ∈ L in time bounded by f (k) · |(x, k)| c , where f : N → N is a computable function and c ∈ N is a constant. The complexity class containing all such problems is called FPT.
Tree decomposition and treewidth. A tree decomposition of a graph G = (V, E) is a pair T = (T, β) where T is a tree and β : V (T ) → 2 V is a function that associates to each node t ∈ V (T ) a vertex subset B t ⊆ V (G), called a bag, such that 3. for each v ∈ V the nodes t such that v ∈ B t induce a connected subtree of T .
The width w(T ) of a tree decomposition T is max t∈V (T ) {|B t | − 1}. The treewidth of a graph G, denoted tw(G), is a minimum width of a decomposition over all tree decompositions of G.
Bodlaender et al. [16] proved that a tree decomposition of a graph G of width O(tw) can be computed in 2 O(tw) · n time.

Graph unification
In this section we present some easy modifications on the input instance in order to simplify it. Many of them are rather folklore for similar problems and were also used by Amiri et al. [4] or Klein and Marx [11].
As a walk can never leave the connected component containing s, we can delete all other connected components of the input graph. If this also deletes t or any waypoint, we can immediately answer no. As this can be performed in linear time, we can assume that the input graph is connected. As adding s and t into W does not change the problem, we assume s, t ∈ W .
If s = t, then we introduce a new vertex v to G and connect it by edges to both s and t. We set both the capacity and the cost of these two edges to 1. It is easy to observe that there is a solution walk of cost w in the original network if and only if there is solution walk from v to v through all waypoints of cost w + 2 in the modified network. This also increases the treewidth of the graph by at most 1, since we can add v to every bag of the decomposition for G. Hence, we assume that s = t.
Next, we interpret the task in terms of searching for appropriate Eulerian subgraphs of a suitable multigraph. Such an approach was first proposed by Cosmadakis and Papadimitriou [17]. Let us first define this multigraph.

Lemma 1.
There is an s-t walk of total cost w in G passing through all waypoints in W and obeying the capacities if and only if there is a connected subgraph (submultigraph) H of G ′ of cost w such that W ⊆ V (H) and H is even (every vertex of H has an even degree in H).
Proof. Given a walk R in G we can transform it into a walk R ′ in G ′ by replacing, for each edge e occurring in R, its first occurrence by e 1 , the second occurrence by e 2 , etc. As R obeys the capacities, this is possible and the cost of R ′ is the same as the cost of R. Note that R ′ traverses each edge at most once, i.e., it is a trail. Now let V (H) and E(H) be the set of vertices and edges traversed by R ′ , respectively. Then R is an Eulerian trail for H, i.e., H is connected and even. As R ′ traverses all vertices of W , we have W ⊆ V (H) and the cost of H is exactly the cost of R ′ which is the cost of R.
For the other direction, if we have the subgraph H, it is enough to take an Eulerian trail for this subgraph starting and ending in s = t ∈ W to obtain the desired walk.
Observe that if H is the desired subgraph of G ′ and it uses some edge more than twice (more than two parallel edges), then if we reduce the multiplicity of the edge by two, we obtain another subgraph with the desired properties. As each edge has a positive cost, this new subgraph will have a lower total cost. It follows, that in an optimal solution, each edge is traversed at most twice. Hence we introduce into G ′ only at most two copies of each edge, without affecting the set of optimal solutions. We call an instance unified, if the (multi)graph is connected, has unary capacities, parallel edges have the same weight, there are at most two parallel edges between any pair of vertices, s = t, and s ∈ W . Henceforward we will assume that the input instance of WRP is unified.
Amiri et al [4] explicitly asked whether WRP can be expressed in Monadic Second Order Logic (MSOL). It is straightforward to express the task of searching for the least cost Eulerian subgraph of a unified instance containing all the waypoints in a suitable extension of MSOL that allows counting the sizes of sets modulo 2 and finding the subset of interest of least cost in an element-weighted universe. As the classic result of Courcelle [18] holds even for such extensions [19], this shows that WRP is FPT when parameterized by treewidth. Nevertheless, the running time of the algorithm implied by this meta-theorem is far from optimal. Therefore, we present a specialized dynamic programming algorithm in the next section.

Dynamic programming algorithm
To simplify the description of the algorithm, we will describe the computation in terms of a nice tree decomposition of the underlying graph G. • Introduce vertex node: an internal node t with exactly one child t ′ such that • Introduce edge node: an internal node t, labeled with an edge {u, v} ∈ E(G) such that u, v ∈ B t , and with exactly one child t ′ such that B t = B t ′ , • Forget node: an internal node t with exactly one child t ′ such that B t = B t ′ \{v} for some v ∈ B t ′ , • Join node: a node t with two children t 1 , t 2 such that B t = B t1 = B t2 .
4. For each edge e there is exactly one node labeled with this edge.
Any tree decomposition can be turned into a nice one of the same width in O(w 2 · n) time, where w is the width [15,Lemma 7.4, see also the discussion on page 168] of the tree decomposition.
Moreover, we assume that the source s = t is contained in all nodes of a nice tree decomposition T and that the root node, just as leaf nodes, contains only s in the corresponding bag.

Partial solutions
We will compute partial solutions for subgraphs corresponding to a node in a tree decomposition by combining partial solutions for subgraphs corresponding to its children. To make the definition more intelligible we introduce the following notation.
Notation 1 (Subgraph induced by a node of a tree decomposition). Let G = (V, E) be a graph and T = (T, β) be a nice tree decomposition for G rooted in node r. For any node x ∈ T we will denote by G x = (V x , E x ) the subgraph induced by a tree decomposition node x with We consider that every x ∈ T is a descendant of itself.
With the necessary preparation, we can finally define a partial solution in subgraphs induced by a tree decomposition.
For a presignature S = (X, L) at x and partition P of X we call (X, L, P) a solution signature at x.
A subgraph H ⊆ G x is a partial solution compatible with solution signature (X, L, P) at x, if all the following conditions hold The signature of a solution allows us to recognize partial solutions that are equivalent from the global perspective.
A standard dynamic programming algorithm would be working in a bottom-up manner and computing for every signature a minimal weight partial solution. The computation runs from leaf nodes to the root node with the assumption that before computing an optimal partial solution of inner node x all child nodes were already processed.
After completion of this procedure we ask for the weight computed for signature ({s}, ∅, {{s}}) at the root r. It is easy to verify that a subgraph of G = G r is compatible with this signature if and only if it is connected, even, and contains all waypoints. By Lemma 1, such a subgraph exists if and only if there is a solution walk of the same cost.
This approach finds an optimal solution, but the running time of this procedure is suboptimal since we have to store the weight of an optimal partial solution for every signature, and the number of partitions is large. As a first step, we store for each presignature only a subset of partitions for which there is a compatible partial solution together with the minimum weight of such a solution. However, this is not sufficient to reduce the number of considered partitions significantly. Hence, we describe the algorithm using the framework of the next subsection, which then allows us to easily speed the algorithm up.

Manipulating Partitions
We use the framework of Bodlaender et al. [20] to improve the algorithm. This allows us, instead of considering all possible partitions, to limit ourselves to the representative sets of a weighted partitions containing all the needed information, as described later. For each presignature (X, L) we store a list of pairs (P, w), where P is a partition of X and w is the weight of an optimal partial solution for solution signature (X, L, P).
Let us first recall some notions concerning partitions. Recall that a partition of a set U is a family of its subsets P ⊆ 2 U \∅ such that A∈P A = U and for every two distinct A, B ∈ P we have A∩B = ∅. We denote by Π(U ) the set of all partitions of U . If P is a partition, then we call A ∈ P a block. For V ⊆ U we denote by U [V ] the partition of U where all blocks are singletons except block V , i.e., For P ∈ Π(U ) we denote by P ↓V the partition obtained by removing from P all x ∈ V , i.e., P ↓V = {A ∩ V | A ∈ P} \ ∅, and in a similar way we let P ↑V be the partition obtained from P by adding singletons for every x ∈ V \ U , i.e, P ↑V = P ∪ {{v} | v ∈ V \ U }. For P, Q ∈ Π(U ) we say that P is a coarsening of Q, denoted Q ⊑ P, if each block of Q is contained in one block of P. The set Π(U ) together with coarsening relation ⊑ forms a lattice. We denote the join operation in this lattice ⊔. In other words, P ⊔ Q is the finest common coarsening of partitions P and Q. Note that the maximum element of the described lattice is {U } and the minimum element is the partition of all x ∈ U as singletons.
A set of weighted partitions is a subset A ⊆ Π(U ) × N. A pair (P, w) is called weighted partition and w its weight. [20]). Let U, U ′ be a sets, A ⊆ Π(U ) × N, B ⊆ Π(U ′ ) × N sets of weighted partitions and rmc(A) a set obtained by removing all duplicates from A while preserving only the one with minimal weight, i.e.,

Definition 3 (Operators on sets of weighted partitions
We define the following operators. Project proj(V, remove every v ∈ V from U and from each partition while discarding each partition where the number of sets is decreased. extend all partitions to the same setÛ and for each pair (p, w 1 ) ∈ A and (q, w 2 ) ∈ B return the result of the ⊔ operation with weight equal to the sum of the weights of the original pairs.

The algorithm
We will now formalize our dynamic programming approach using the operators from Section 4.2 which allows us to later optimize the running time of the algorithm. It is sufficient to describe how the algorithm processes in the different kinds of a nice tree decomposition nodes.
Leaf node x. The computation of the algorithm starts in leaf nodes. The subgraph corresponding to a leaf of the decomposition has single vertex s and no edge at all, so the only valid presignature is ({s}, ∅) with the only possible partition being {{s}}. Thus Introduce vertex v node x with child y. In an introduce vertex v node x of a tree decomposition T the presignature (X, L) either prescribes to use the introduced vertex v or not to use it. In the first case, we can be sure, thanks to the definition of a nice tree decomposition, that there is no edge that starts or ends in v in G x . Hence there is no partial solution in which v has odd degree. Thus, if v ∈ L then the set of weighted partitions is empty. For any presignature (X, L) such that v ∈ X and v / ∈ L the solution signature can be obtained from solution signature (X \ {v}, L, P y ) of child node y as (X, L, P y ∪ {{v}}). For the case v / ∈ X the partial solutions do not change. Hence Introduce edge e = {u, v} node x with child y. When node x introduces edge e = {u, v}, the edge may or may not be added to partial solutions of child node y. The easier part is when we decide to not add the edge to partial solutions, in that case, all the partial solutions are simply copied. On the other hand, if we add edge e into the solution, then for every partial solution of child node y the following happens • the weight ω(e) of edge e is added to the total weight of the partial solution, • if u does not belong to the same block as v, then both blocks are merged, • if u ∈ L, then u is added to L, the same for v, • if u ∈ L, then u is removed from L, the same for v.

In terms of presented operations, it yields
is the symmetric difference of the sets A and B.
Forget vertex v node x with child y. If vertex v is forgotten in node x, then we must eliminate all the partial solutions of child node y where v is a singleton in P because there is no possibility to connect it with the remaining parts, and such partial solutions would not fulfill condition (iv) for partial solutions. By condition (iii) we can also eliminate partial solutions in which v has odd degree. Thus Join node x with children y and z. In join node z we must properly combine the partial solutions of its children, which is exactly the purpose of the join operation. Hence, A x (X, L) = ↓ Ly,Lz L=Ly△Lz join(A y (X, L y ), A z (X, L z )).
Let us now show that the recurrences correctly compute A x (S) for every node x and presignature S. This is captured by the following lemmas.
Lemma 2. For every node x, presignature (X, L), and weighted partition (P, w) ∈ A x (X, L) there is a partial solution H compatible with (X, L, P) at x of cost at most w.
Proof. We prove the lemma by a bottom-up induction on the decomposition tree. For a leaf node the only presignature is ({s}, ∅) with weighted partition ({{s}}, 0) for which ({s}, ∅) is the partial solution. Let us now suppose that the lemma holds for every child node of node x.
If node x with single child y introduces vertex v, then for each presignature where v / ∈ X and v / ∈ L the partial solution compatible with (X, L, P) at y is also compatible with (X, L, P) at x and such a solution exists by the induction hypothesis. When v ∈ X and v / ∈ L, then let H ′ be a partial solution compatible with (X \ {v}, L, P \ {{v}}) at y. Such a solution exists by the induction hypothesis. Then H, created by adding v as an isolated vertex into H ′ , is a partial solution compatible with (X, L, P) at x. Any other presignature is invalid.
When node x with child y introduces edge {u, v} two cases can occur. The first case, when we decide to not add an edge {u, v} to partial solutions valid for node y is obvious. In the other case for solution signature (X, L, P) where u, v ∈ X we obtain a solution by adding edge {u, v} to submultigraph H ′ compatible with presignature (X, L △ {u, v}) in y. The weight of such a partial solution is increased by ω({u, v}).
Let x denote a forget vertex v node with child y and (P, w) ∈ A x (X, L). If (P, w) ∈ A y (X, L), then there is a partial solution of weight at most w compatible with (X, L, P) at y. As it does not contain v, it is also compatible with (X, L, P) at x. If (P, w) ∈ proj(v, A y (X ∪ {v}, L)), then let (P ′ , w) be the weighted partition such that (P, w) ∈ proj(v, {(P ′ , w)}). We know that {v} / ∈ P ′ , as otherwise this partition would be discarded by proj. By the induction hypothesis there is a partial solution H of weight at most w compatible with (X ∪ {v}, L, P ′ ) at y. As the component containing v also contains other vertices of B x , H is also compatible with (X, L, P) at x.
The last not yet discussed case is when node x is a join node with exactly two children y, z. For (P, w) ∈ A x (X, L) let L y , L z be such that (P, w) ∈ join(A y (X, L y ), A z (X, L z )). Furthermore, let (P y , w y ) ∈ A y (X, L y ) and (P z , w z ) ∈ A z (X, L z ) be such that (P, w) ∈ join({(P y , w y )}, {(P z , w z )}). By the induction hypothesis there are partial solutions H y of weight at most w y compatible with (X, L y , P y ) at y and H z of weight at most w z compatible with (X, L z , P z ) at z. Let H = H y ∪ H z . We claim that H is compatible with (X, L, P) at x. Note that, as every edge is introduced exactly once, we have E y ∩ E z = ∅, thus E(H y ) ∩ E(H z ) = ∅ and the weight of H is at most  Let us now suppose that the lemma holds for every child node of node x of the decomposition tree.
When node x with single child node y introduces vertex v, then for any presignature (X, L) and weighted partition P of X such that there is a partial solution H compatible with (X, L, P) we must distinguish two cases. If the introduced vertex v is not part of H, then H ⊆ G y and it is compatible with (X, L, P) at y. Hence, by the induction hypothesis we have (P, w ′ ) ∈ A y (X, L) for some w ′ ≤ w. As v / ∈ X and, thus, A x (X, L) = A y (X, L) in this case, we have also (P, w ′ ) ∈ A y (X, L). If v ∈ V (H), then, since the vertex v is introduced in node x, there is no edge incident with v in G x and also in H which implies that v / ∈ L and v forms a connected component of H, and thus also a singleton partition in P. Therefore H ′ = H \ {v} is compatible with (X \ {v}, L, P ′ ) at y, where P ′ is obtained from P by removing the part {v}. Moreover, H ′ has also weight w, as there are no edges incident on v in H. Thus, by the induction hypothesis, (P ′ , w ′ ) ∈ A y (X \ {v}, L) for some w ′ ≤ w. Since A x (X, L) = ins({v}, A y (X \ {v}, L)) in this case, (P, w ′ ) appears in A x (X, L), as required.
Let x denote introduce edge e = {u, v} node with single child node y and H a partial solution of weight w compatible with solution signature (X, L, P) at x. As in the previous type of tree decomposition node we must analyze several cases. If e ∈ E(H), then there is a partial solution H := (V (H), E(H) \ {e}) of weight w = w − ω(e) compatible with (X, L △ {u, v}, P) at y for some P. Note that all connected components of H are also connected components of H except for the component containing u and v. Therefore, the partition P is such that all parts of P containing neither u nor v also appear in P, if u and v are in the same part of P, then it also appears in P and otherwise, the union of the parts of P containing u and v appears in P. By the induction hypothesis, there is a pair ( P, w ′ ) in A y (X, L △ {u, v}) such that w ′ ≤ w. Therefore, there is a pair (P, w ′′ ) in glue ω (e, A y (X, L △ {u, v})), such that w ′′ = w ′ + ω(e) ≤ w + ω(e) = w. If e / ∈ E(H), then H ⊆ G y and H is compatible with (X, L, P) at y. Therefore, by the induction hypothesis, the pair (P, w ′′ ) is in A y (X, L) for some w ′′ ≤ w. If {u, v} ⊆ X, then in both cases we get that A x (X, L) = A y (X, L) ∪ ↓ glue ω ({u, v}, A y (X, L △ {u, v})) contains a pair (P,w) for somew ≤ w ′′ ≤ w. Otherwise we have A x (X, L) = A y (X, L) and the lemma follows.
Let x be a forget vertex v node with a single child node y. On one hand, when v is not a part of H, then H is also a partial solution compatible with solution signature (X, L, P) at y. Hence A y (X, L) contains a pair (P, w ′ ) for some w ′ ≤ w by the induction hypothesis. On the other hand, if v is a part of H, then v must be in a connected component of H containing some other vertex u of B x . Therefore, H is also compatible with signature (X ∪ {v}, L, P ′ ) at y, where P ′ is obtained from P by adding v to the part containing u. Thus, by the induction hypothesis, the pair (P ′ , w ′ ) is in A y (X ∪ {v}, L) for some w ′ ≤ w. Hence, the pair (P, w ′ ) is in proj(v, A y (X ∪ {v}, L)). In either case, A x (X, L) = A y (X, L) ∪ ↓ proj(v, A y (X ∪ {v}, L)) contains a pair (P,w) for somew ≤ w ′ ≤ w.
The last type of a node in a nice tree decomposition is a join node x with exactly two children y and z. For a given solution signature (X, L, P) and a compatible partial solution H we let H y = H ∩ G y and H z = H ∩ G z and let w y and w z be their weights, respectively. We have that H y and H z are disjoint subgraphs, H = H y ∪ H z , and, thus, w y + w z = w. We let L y (L z ) be the set of vertices of H y (H z ) of odd degree, respectively. For a vertex v ∈ V (H) ∩ (V y \ V z ) all edges of H incident on v are in H y , and, hence, v / ∈ L y . Similarly for a vertex in v ∈ V (H) ∩ (V z \ V y ). Thus L y ⊆ X, L z ⊆ X and L = L y △ L z . Also H y is compatible with (X, L y , P y ) at y for some P y and H z is compatible with (X, L z , P z ) at z for some P z such that P y ⊔ P z = P. Then, by the induction hypothesis, A y (X, L y ) contains a pair (P y , w ′ y ) for some w ′ y ≤ w y and A z (X, L z ) contains a pair (P z , w ′ z ) for some w ′ z ≤ w z . Therefore join(A y (X, L y ), A z (X, L z )) contains a pair (P, w ′ ) for some w ′ ≤ w ′ y + w ′ z ≤ w y + w z = w. Hence, also A x (X, L) = ↓ Ly,Lz L=Ly△Lz join(A y (X, L y ), A z (X, L z )) contains a pair (P, w ′′ ) for some w ′′ ≤ w ′ ≤ w and the lemma holds.

Using Representative Sets
In order to speed up the algorithm, we do not compute the whole set A x for each node x and each presignature, but just its suitable subset. We need the following definitions and theorems.  In addition, we say that a function f : 2 Π(U)×N × Z → 2 Π(U)×N , where Z denotes any combination of further inputs, preserves representation if for every A, A ′ ∈ Π(U ) × N and every z ∈ Z it holds that if A ′ is a representative set for A then f (A ′ , z) remains a representative for f (A, z). Theorem 2 (Bodlaender et al. [20]). There is an algorithm for the reduce operation that given a set of weighted partitions A ⊆ Π(U ) × N produces in time 2 (ω−1)|U| · |U | O(1) · |A|, where ω < 2.373 is a matrix multiplication exponent, a representative subset A ′ ⊆ A such that |A ′ | ≤ 2 |U|−1 .
The above theorem states that a suitably small representative set of weighted partitions can be always found in a reasonable time. This completes the introduction of the used toolbox.
In order to speed up the algorithm, we do not compute the whole set A x for each node x and each presignature, but just its representative subset A ′ x as follows. For each node x and its presignature (X, L) we first compute a set A ′′ x (X, L) based on A ′ y (and possibly A ′ z ), using the recurrences described in the previous section. Then we use algorithm reduce from Theorem 2 to compute representative set A ′ x (X, L) of A ′′ x (X, L), such that |A ′ x (X, L)| ≤ 2 O(tw) . Since in the recurrences we use only operators from Definition 3 which all preserves representation by Theorem 1, set A ′ x (X, L) remains representative for A x (X, L).

Theorem 3.
There exist an algorithm that given an instance (G, κ, ω, s, t, W ) of the Waypoint Routing Problem solves it in 2 O(tw) · n time.
Proof. Our algorithm first preprocesses the instance to obtain a unified one. This increases the number of vertices by at most one and the number of edges is at most 2 more than twice the original number. Also the treewidth is increased by at most 1. Since the number of edges is O(tw ·n) [15,Exercise 7.15], the unification can be performed in O(tw ·n) time. For simplicity, we denote the unified instance also by (G, κ, ω, s, t, W ).
Then we obtain a tree decomposition for G using the algorithm of Bodlaender et al. [16], turn it into a nice one along the lines of [15,Lemma 7.4, see also p. 168], and finally add s to every bag. We obtain a nice tree decomposition of width O(tw) with O(tw O(1) n) nodes and s in every bag in time 2 O(tw) · n. Thereafter the introduced dynamic programming algorithm computes the representative set A ′ x (X, L) of A x (X, L) for every node x of the tree decomposition and every presignature (X, L) at x. By Lemma 2 and Lemma 3, a pair ({{s}}, w) is in A r ({s}, ∅) if and only if there is a solution of weight w. Since in the recurrence we use only operators from Definition 3 which all preserves representation, set A ′ x remains representative for all nodes x including the root node r. Hence A ′ r ({s}, ∅) is representative for A r ({s}, ∅), which necessarily implies A r ({s}, ∅) = A ′ r ({s}, ∅) and the algorithm is correct. Let us discuss the running time of the proposed algorithm. As the width of the used decomposition is O(tw), there are 2 O(tw) presignatures for each bag. We use the reduce procedure to obtain each A ′ x (X, L), hence we have |A ′ x (X, L)| ≤ 2 |X|−1 ≤ 2 O(tw) . In each of the recurrences, we use a constant number of operators from Definition 3. The most time consuming is the computation in join nodes which takes at most |A y (X, L y )| · |A z (X, L z )| · |X| O(1) = 2 |X|−1 · 2 |X|−1 · |X| O(1) = 2 O(tw) time. Therefore the bottleneck of our algorithm is the reduce procedure. Nevertheless, since the size of intermediate sets of weighted partitions never exceeds 2 O(tw) , this procedure also runs in 2 O(tw) time. As the nice tree decomposition has O(tw O(1) n) nodes, we can say that the overall running time of the presented algorithm is 2 O(tw) · n.

ETH Lower Bound
The Exponential Time Hypothesis (ETH for short) introduced by Impagliazzo and Paturi [6] states that there is a constant δ 3 > 0 such that there is no algorithm for 3-SAT with running time 2 δ3n m O(1) , where n is the number of variables and m the total length of the input formula. To prove the optimality of our algorithm, we will use the following ETH implication.
Theorem 4 (Impagliazzo, Paturi, and Zane [21], see also [15,Theorem 14.6]). Unless ETH fails, Hamiltonian Cycle admits no algorithm working in 2 o(n+m) time, where n and m are the number of vertices and edges of the input graph, respectively.
Using the preceding theorem, we are able to easily prove the following. Proof. Assume to the contrary that such an algorithm A exists. Note that, since tw(G) ≤ n−1 for any graph G, we have 2 o(tw(G)) · n O(1) ⊆ 2 o(n) ⊆ 2 o(n+m) . We show that the existence of A contradicts Theorem 4.
Consider the following algorithm. Given an undirected graph G = (V, E)-an instance of Hamiltonian Cycle-let κ(e) = ω(e) = 1 for every e ∈ E, W = V , and s = t be an arbitrary vertex in V . Now run A on (G, κ, ω, s, t, W ). If the returned walk has weight |V |, then accept G, otherwise reject it. As a Hamiltonian cycle in G corresponds to a walk of length |V | visiting each vertex of W exactly once, using each edge at most once, and starting in s = t and vice versa, the answer is correct.
Hence the running time of the algorithm contradicts Theorem 4, finishing the proof.

Conclusion
We presented a deterministic algorithm for the Waypoint Routing Problem in undirected graphs running in 2 O(tw) · n time. An interesting open problem is to determine the complexity of the problem in directed graphs with underlying undirected graphs of small treewidth. While the correspondence with degree constrained submultigraphs via Eulerian trails is still valid in directed graphs, it is no longer true that each edge is traversed at most twice in an optimal walk. It is easy to find instances in which a particular edge must be traversed as many as n − 1 times, where n is the number of vertices, in any feasible walk. Furthermore, while in the final submultigraph the indegree of each vertex must be equal its outdegree, for a partial solution the difference between these two degrees can be arbitrarily large. This makes the problem more challenging in directed graphs.