Efficient parameterized algorithms for computing all-pairs shortest paths

Computing all-pairs shortest paths is a fundamental and much-studied problem with many applications. Unfortunately, despite intense study, there are still no significantly faster algorithms for it than the $\mathcal{O}(n^3)$ time algorithm due to Floyd and Warshall (1962). Somewhat faster algorithms exist for the vertex-weighted version if fast matrix multiplication may be used. Yuster (SODA 2009) gave an algorithm running in time $\mathcal{O}(n^{2.842})$, but no combinatorial, truly subcubic algorithm is known. Motivated by the recent framework of efficient parameterized algorithms (or"FPT in P"), we investigate the influence of the graph parameters clique-width ($cw$) and modular-width ($mw$) on the running times of algorithms for solving All-Pairs Shortest Paths. We obtain efficient (and combinatorial) parameterized algorithms on non-negative vertex-weighted graphs of times $\mathcal{O}(cw^2n^2)$, resp. $\mathcal{O}(mw^2n + n^2)$. If fast matrix multiplication is allowed then the latter can be improved to $\mathcal{O}(mw^{1.842}n + n^2)$ using the algorithm of Yuster as a black box. The algorithm relative to modular-width is adaptive, meaning that the running time matches the best unparameterized algorithm for parameter value $mw$ equal to $n$, and they outperform them already for $mw \in \mathcal{O}(n^{1 - \varepsilon})$ for any $\varepsilon>0$.


Introduction
all-pairs shortest paths (APSP) is a fundamental and much-studied problem in the field of algorithmic graph theory.Next to the theoretical interest in the problem, all-pairs shortest paths is important for many practical applications, e.g., it is closely related to several vertex centrality measures in networks (for example, the betweenness centrality of a vertex v is defined as the sum of the fraction of all-pairs shortest paths that pass through v).The all-pairs shortest paths problem is also considered as the core of many routing problems and has applications for example in areas such as routing protocols, driving direction on web mappings, transportation, and traffic assignment problems, and many more.See also the survey of Susmita [25] for more applications.Despite the large interest in all-pairs shortest paths, there are only small improvements known since the well-known O(n 3 )-time algorithm by Floyd and Warshall [9,28] from 1962: Chan [4] as well as Han and Takaoka [15] gave an algorithm running in O(n 3 / log 2 n) (omitting poly(log log n) factors) and Williams [29] gave an randomized algorithm running in time O(n 3 /2 Ω(log n) 1/2 ).While there are no unconditional lower bounds known, it has been conjectured that there is no truly subcubic algorithm for all-pairs shortest paths, i.e., that no algorithm achieves time O(n 3−ε ) for any ε > 0. Using suitable subcubic reductions, this is tightly connected to the existence of subcubic algorithms for several network centrality arXiv:2001.04908v1[cs.DS] 14 Jan 2020

Preliminaries
We follow basic graph notations [8].For a natural number k ∈ N, define [k] = {1, . . ., k}.All graphs are simple, i.e., without loops or multiple edges.In a graph G = (V, E), a path We define by P [vi,vj ] the subpath of P starting in v i and ending in v j for i, j ∈ [n] with i < j.The length of a path is the number of edges in it.In a vertex-weighted graph G = (V, E) with weights ω : V → R ≥0 , the weight (also called cost) of a path P is defined as ω(P ) = n i=1 ω(v i ).Thus, every paths between two distinct vertices u and v has minimum weight ω(v) + ω(u) and a path of length 0 from a vertex v to itself has always weight ω(v).For a graph G = (V, E) and u, v ∈ V , we denote the minimum weight of all paths between u and v as dist G (u, v).For a set of vertices X ⊆ V and a vertex u ∈ V we define dist G (u, X) = min v∈X dist G (u, v) and for two sets of vertices X, Y ⊆ V , we define dist G (X, Y ) = min u∈X,v∈Y (u, v).
For two sets A and B we denote the disjoint union by A ∪B and we say that two sets A and B overlap if A ∩ B = ∅, A \ B = ∅, and B \ A = ∅.

Clique-width and NLC-width
A k-labeled graph is a graph in which each vertex is assigned one out of k labels.Formally, a vertex-labeled graph G is a triple (V, E, lab) with V being the vertex set, E denotes the set of edges, and lab : V → [k] is a function that defines the label for each vertex.For a k-labeled graph G = (V, E, lab) we denote by unlab(G) = (V, E) the underlying unlabeled graph.Intuitively, a graph G has clique-width at most k, if it is the underlying graph of some k-labeled graph that can be constructed by using four operations: (1) Introducing a single labeled vertex, (2) redefining one label to another label, (3) taking the disjoint union of two already created k-labeled graphs, and (4) adding all edges between vertices of label i to vertices of label j for a pair (i, j) of labels.
Definition 1 (Clique-width, [6]).Let k ≥ 2. The class CW k consists of all k-labeled graphs that can be constructed by the following operations: The nullary operation • a , that corresponds to a graph consisting of a single vertex with a label a ∈ [k].
Let G = (V, E, lab) ∈ CW k be a k-labeled graph, and let a, b ∈ Then the disjoint union, defined by The clique-width of a graph G, denoted by cw(G), is the smallest k ≥ 2 such that there is a labeled graph G ∈ CW k with unlab(G ) = G.The expression consisting of the operations defined in Definition 1 is called a (clique-width) k-expression.For a k-expression t, we denote with val(t) the resulting labeled graph and by tree(t) the so called k-expression tree of t, which is the canonical tree representation of t.Clique-width is a strict generalization of modular-width, which will be defined later.In fact, the clique-width of a graph G is equal to the maximum clique-width of any quotient graph of a prime node in the modular decomposition tree of G. On the other hand, modular-width cannot be bounded by a function of clique-width.
Very similar to clique-width, one can define NLC-width, which was introduced by Wanke [27].The main differences are that the join operation η and the disjoint union operation ⊕ are somewhat combined and consecutive relabel operations are compressed into one operation.
Definition 2 (NLC-width).Let k ≥ 1.The class N LC k consists of all k-labeled graphs that can be constructed by the following operations: The nullary operation • a , that corresponds to a graph consisting of a single vertex with a label a ∈ The NLC-width of a graph G, denoted by nlc(G), is the smallest k ≥ 2 such that there is a labeled graph G ∈ N LC k with unlab(G ) = G.As for clique-width, the expression consisting of the operations defined in Definition 2 is called a (NLC-width) k-expression.For a k-expression t, we again denote with val(t) the resulting labeled graph and by tree(t) = T canonical tree representation of t, the so called k-expression tree of t.This means each leaf node of T is marked with • a for some a ∈ [k] and each internal node is either marked with or with × S for some S ⊆ [k] 2 , according to the operations defined in Definition 1 resp.Definition 2. For a node x ∈ V (T ) we denote by G x the labeled graph defined by the k-expression represented by the subtree of T rooted in x and we define by The following lemma shows that we can safely focus on NLC k-expression trees, since the NLC-width and clique-width only differs by a factor of two at most.

Modular-width
A module in a graph G = (V, E) is a set M ⊆ V of vertices such that all vertices outside of M are either connected to none or to all vertices in M , i.e, that M ∩ N (x) = ∅ or M ⊆ N (x) for every vertex x ∈ V \ M .Thus, all vertices in M have the same neighborhood in V \ M .It is easy to see that ∅, V , and {v} for every v ∈ V are modules of any graph G = (V, E); those sets are called trivial modules.If a graph G only admits trivial modules, we call G prime.
Consider a so called modular partition P = {M 1 , M 2 , . . ., M } that is a partition of the vertices of G into modules with ≥ 2. Due to the definition of a module, it holds for any two modules M i and M j of P that every vertex of M i is either connected to all vertices of M j or to none.In the first case we call M i and M j adjacent, in the latter non-adjacent.Thus, for a fixed modular partition P we get a compact representation of the connection between the modules in P by shrinking each module in P to a single vertex.This graph is called the quotient graph of G (together with the modular partition P ).

Definition 4. Let
The quotient graph is defined by G /P = ({q M1 , q M2 , . . ., q M }, E P ) with , the union of the corresponding modules induces a (not necessarily strong) module.In this case the quotient graph G[M ] /P is either a clique or an independent set.In the former case one calls v M a parallel node, in the latter a series node.Another case are so called prime nodes.Here, for no proper subset of the children of v M , the union of the corresponding modules induces a module.In this case the quotient graph of v M is prime.Gallai showed there are no further nodes in M D(G).Theorem 5 ([12]).For any graph G = (V, E) one of the three conditions is satisfied: G and G are connected and the quotient graph G /P , where P is the maximal modular partition of G, is a prime graph.
Theorem 5 implies that M D(G) is unique.The tree M D(G) is called the modular decomposition tree and the modular-width, denoted by mw = mw(G), is the minimum k ≥ 2 such that any prime node in M D(G) has at most k children.Since every node in M D(G) has at least two children and there are exactly n leaves, M D(G) has at most 2n − 1 nodes.It is known that M D(G) can be computed in time O(n + m) [26].We refer to a survey of Habib and Paul [14] for more information.

APSP parameterized by clique-width
Assuming SETH, one cannot solve diameter (and thus, unweighted all-pairs shortest paths) in time 2 o(cw) • n 2−ε [5].In this section, we show how to solve vertex-weighted all-pairs shortest paths in time O(cw 2 n 2 ).
Theorem 6.For every graph G = (V, E), given together with a clique-width k-expression and vertex weights ω : V → R ≥0 , vertex-weighted all-pairs shortest paths can be solved in time O(k 2 n 2 ).
For an input graph G = (V, E), given together with a clique-width k-expression for some k ≥ 2, we transform in a first step the clique-width k-expression to an NLC-width k-expression in linear time as described for example in [18].For the rest of this section, by writing k-expression we always refer to an NLC-width k-expression instead of a clique-width k-expression.We interpret the (NLC-width) k-expression as a k-expression tree T , in which each node v ∈ V (T ) is marked with an operation of the k-expression that is applied to the children of v. Accordingly, T has exactly n leafs, each marked with an operation • i for i ∈ [k], and exactly n − 1 nodes marked with an operation × S for some S ⊆ [k] 2 .For ease of presentation, we assume that there is exactly one node marked with an operation • R for some R : [k] → [k] in between any two nodes marked with × S (using R(i) = i when no actual relabeling is necessary), hence, the length of the k-expression is O(n).Note, that the length of a clique-width k-expression is O(n + m) in general.For a node x ∈ V (T ) we denote by G x the labeled graph that is defined by the subexpression tree of T rooted in x.
The algorithm consists of three phases.In the first phase, we traverse T in a bottom-up manner: For each node x ∈ V (T ) we partition the vertex set into sets of same-labeled vertices and compute the shortest distance for each single vertex to (the closest vertex in) each label set.Additionally, we compute the distance between each pair of label sets, i.e., the shortest distance of two vertices of the respective sets.Note, however, that in the first phase we only consider for each each node x ∈ V (T ) the distances in the graph G x .In the second phase, we perform a top-down traversal of the k-expression tree T and consider the whole graph G. Once we have computed the necessary values in phase one and two, we traverse T one last time and finally compute the shortest path distances between all pairs of vertices.
First Phase.For a node x ∈ V (T ), which corresponds to the k-labeled graph G x , we define ) is an induced subgraph of G for any x ∈ V (T ).We traverse T in a bottom-up manner and compute for each node x ∈ V (T ) and for all pairs (i, j) ∈ [k] 2 of labels the shortest distance between some vertex in L x i and some vertex in L x j .Additionally, we compute for any vertex v ∈ V (G x ) and any label i ∈ [k] the shortest distance from v to some vertex in L x i .To be precise, for a node x ∈ V (T ) we compute the following values: For nodes x ∈ V (T ) that are marked with × S for some S ⊆ [k] 2 we need to compute some auxiliary values.Let y and z be the two children of x in T .This means that G x consists of the disjoint union of G y and G z together with a full join between the vertex sets L y i and L z j for each (i, j) ∈ S. Thus, one can partition the vertex set of G x into the 2k sets For each pair (A, B) ∈ L x × L x of vertex sets, we compute the shortest distance between some vertex in A to some vertex of B. In addition, we compute the shortest distance between A and B with the constraint that either the first edge, the last edge, or the first and the last edge of the shortest path is an edge of a newly inserted full join defined by S. This achieves the effect that we additionally compute the shortest distance from (1) all vertices of A to some vertex of B, (2) from some vertex of A to all vertices of B, and (3) from all vertices of A to all vertices of B. Doing this, one can e.g.combine a path that ends at some vertex of A with a path that can start at any vertex of A.
In the following, we will describe how to compute the required values for each of the three different types of nodes in the k-expression tree T .
For the base case, let x be a leaf of the k-expression tree T .Thus, the node x ∈ V (T ) is marked with • for some ∈ [k].This means that G x consists of a single vertex v with label .In this case the following holds: Let y ∈ V (T ) be the unique child of x in T .Since we traverse T in a bottom-up manner, we have already computed the values a y v,i for all v ∈ V (G y ) and i ∈ [k] and the values c x i,j for all i, j ∈ [k].Note, that unlab(G x ) = unlab(G y ), which, in particular, implies that distances between vertices are identical in both graphs (though distances between label sets may be not, as these sets may be different).

Lemma 7. Let x ∈ V (T ) be an internal node of a k-expression tree T marked with
and let y be the unique child of x in T .Then Proof.The vertex sets of G x and G y can be partitioned into the label sets {L Here, it is crucial that distances between vertices are the same in G x and G y , as noted above.
Note, that the computation of all c x i,j can be realized in time O(k 2 ) by updating for every c y i,j the corresponding value c x R(i),R(j) .The values a x v,i can be similarly computed from the values at the child node: The running time for computing the values a x v,i is O(nk) since we need to consider each value a y v,i for any v ∈ V (G y ) and i ∈ [k] exactly once.Finally, let x ∈ V (T ) be an internal node of the k-expression tree T marked with × S for some S ⊆ [k] 2 .Denote by y ∈ V (T ) and z ∈ V (T ) the two children of x in T , meaning that G x combines the two labeled graphs G y and G z by introducing for each (i, j) ∈ S a full join between the vertices in L y i and those in L z j .Thus, To compute the desired distances between the label sets {L y 1 , . . ., L y k , L z 1 , . . ., L z k }, we construct an edge-weighted directed graph H x that represents all the distances between the label sets in a graph with only 4k vertices.
For each label set L a i of G x with i ∈ [k] and a ∈ {y, z} we create two vertices v a i and } for a ∈ {y, z}.We add a directed full join from V y to U y resp.from V z to U z with weight equal to the length of a shortest path between the two corresponding label sets.Finally, we connect vertices in U y with vertices in V z , resp.vertices in U z with vertices in V y , if and only if the corresponding pair is contained in S, i.e., if there is a full join in G x between the two corresponding label sets.See also Figure 1 for an illustration.Formally, we define the directed, edge-weighted graph H x as follows.
Definition 9. Let x ∈ V (T ) be an internal node of a k-expression tree T marked with × S for some S ⊆ [k] 2 and let y ∈ V (T ) and z ∈ V (T ) be the children of x.We define H x as a directed, edge-weighted graph on 4k vertices created as follows: For each label set with cost zero for all (i, j) ∈ S. Add edges (u z i , v y j ) with cost zero for all (j, i) ∈ S. Note, that some edges may have cost ∞ as there is no path of the requested type exists.Next, we will see that H x exhibits all the desired distances from G x in a compact way.
Figure 1 Construction of the auxiliary graph H x .Each large node consists of k disjoint vertices corresponding to one of the k label sets in G y resp.G z .Between V y and U y there is a full join, each edge between the corresponding vertex of L y i and L y j is weighted by c y i,j , analogously for V z and U z .Vertices in U y are only connected to those vertices in V z for which the corresponding label sets are connected via S, analogously for U z and V y .Theorem 10.Let x ∈ V (T ) be an internal node of a k-expression tree T marked with × S for some S ⊆ [k] 2 with children y and z.Let H x be the graph as defined in Definition 9. Then the following holds: (1) where P is the set of all paths in G x starting in L a i , ending in L b j , and having the second vertex in We prove Theorem 10 in two steps.We first prove that every path in H x corresponds to some path in G x .Later, we prove that also each optimal path between two label sets in G x corresponds to some shortest path in H x .We start with statement (1) of Theorem 10.Lemma 11.Let x ∈ V (T ) be an internal node of the k-expression tree T marked with × S for some S ⊆ [k] 2 with children y and z.Let H x be the auxiliary graph as defined in Definition 9 and let P * be an arbitrary v y i -u z j -path in H x for some i, j ∈ [k].Then there exists an L y i -L z j -path P in G x with ω(P ) = ω H x (P * ).
Proof.Due to the circular structure of H x , each v y i -u z j -path in H x will repeat the sequence (v y p , u y q , v z r , u z s ) for some p, q, r, s ∈ [k] until reaching u z j at the end of a sequence.Thus, each v y i -u z j -path in H x consists of 4 vertices for ∈ N and can be written as One can construct a path P in G x from P * as follows: For each edge (v y pi , u y qi ) in P * of cost c y pi,qi pick a shortest path in G y of total cost c y pi,qi and for each edge (v z ri , u z si ) in P * of cost c y ri,si pick a shortest path in G z of total cost c z ri,si for each i ∈ [ ].Those paths always exist since c y pi,qi resp.c y ri,si are defined as the cost of a shortest L y pi -L y qi -path in G y , resp.as the cost of a shortest L z ri -L z si in G z .Since each edge (u qi , v ri ) only exists if and only if there is a full join between the sets L y qi and L z ri , one can connect the last vertex of the path corresponding to the previous edge in P * (that ends in some vertex in L y qi ) to the first vertex of the path corresponding to the following edge in P * (that starts at some vertex in L z ri ).In the same manner one can argue that due to each edge (u z si , v y pi+1 ) one can connect the last vertex of the path corresponding to the edge (v z ri , u z si ) with the first vertex of the path corresponding to the edge (v y pi+1 , u y qi+1 ).In both cases, the cost of the vertices is already accounted for in the resp.c • •,• value.Thus, each v y i -u z j -path in H x corresponds to an Next, we generalize this argumentation to the following corollary: Corollary 12. Let x ∈ V (T ) be an internal node of the k-expression tree T marked with × S for some S ⊆ [k] 2 with children y and z.Let H x be the auxiliary graph as defined in Definition 9. Then for every i, j ∈ [k] and a, b ∈ {y, z} the following holds: (1) For any v a i -u b j -path P * in H x there exists an Proof.With the same argumentation as in the proof of Lemma 11, one can prove that also every v y i -u y j -path in H x corresponds to an L y i -L y j -path in G x with same cost.The two cases with a = z and b ∈ {y, z} now follow by swapping the roles of y and z.This proves (1).
Similarly, one can argue that any path P * in H x that starts at a vertex u y i (resp.ends at a vertex v z i ) for i ∈ [k], corresponds to a path P = (p 1 , p 2 , . . ., p −1 , p ) in G x that starts in L y i (resp.ends in L z j ) with the additional property (due to the directed edges) that . The cost of P * in H x is exactly the cost of P in G x , minus the cost of the first (resp.last) vertex of P .In this regard, recall that all vertex costs in G x are represented by the edge weights in H x .The remaining cases of Corollary 12 with a = z or b = y again can be shown analogously.
For any path in H x that starts at some vertex u a i (resp.ends at some vertex v b j ) one can find a corresponding path P in G x with the property that the second vertex (resp.the penultimate vertex) is connected to all vertices of L a i (resp.L b j ).Thus, one can extend any path that ends at some vertex in L a i by such a path (resp.one can prepend any path that starts in L b j by such a path).Hence, the cost of the first vertex (resp.last vertex) is neglected if the path starts in some vertex u a i or ends at some vertex v b j for a, b ∈ {x, y}.In general, every path that one can find in H x corresponds to a path in G x of essentially the same cost, possibly without the first or last vertex (which can be chosen as the minimum of the label set).This proves "≤" in the equations of Theorem 10.
For the other direction, we will show that every optimal shortest path between two label sets in G x is represented by a path in H x .Lemma 13.Let x ∈ V (T ) be an internal node of a k-expression tree T marked with × S for some S ⊆ [k] 2 with children y and z.Let H x be the auxiliary graph as defined in Definition 9. Let P be a shortest Then there exist a v y i -u z j -path P * in H x with ω H x (P * ) = ω(P ).
Proof.Let P be a shortest L y i -L z j -path in G x for fixed i, j ∈ [k] of length ω(P ) = dist x (L y i , L z j ).Since V (G x ) can be subdivided into the two vertex sets V (G y ) and V (G z ), we can split the path P into maximal subpaths consisting of vertices completely in V (G y ) resp.completely in V (G z ).Formally, let P = (P 1 , . . ., P d ) with V (P 2q+1 ) ⊆ V (G y ) and as the set of newly created edges in G x .I.e., E x new is the union of all full joins between the vertex sets L y i and L z j for each (i, j) ∈ S. For the path P it holds by construction that {u , v +1 } ∈ E x new for ∈ [d − 1] and, since P is a shortest path, each P is a shortest v -u -path in G y (for odd) resp. in G z (for even) for all ∈ [d].
To complete the proof, we will show that for each for even and ω(P ) = c z lab(v ),lab(u ) for odd, and thus that each path P corresponds to an arc (v y lab(v ) , u y lab(u ) ) for even and to an arc (v z lab(v ) , u z lab(u ) ) for odd in H x : Let w.l.o.g.be odd, hence V (P ) ⊆ V (G y ).Since P is an L y lab(v ) -L y lab(u ) -path, it holds that ω(P ) ≥ c y lab(v ),lab(u ) .Assume for contradiction that ω(P ) > c y lab(v ),lab(u ) and let Q be an L y lab(v ) -L y lab(u ) -path in G y with ω(Q) = c y lab(v ),lab(u ) .Replace P by Q in P to get P = (P 1 , . . ., P −1 , Q, P +1 , . . ., P d ).Since u −1 is connected to all vertices in L y lab(v ) , and v +1 is connected to all vertices in L y lab(u ) , P is an L y i -L z j -path and since ω(P ) > ω(Q) the total cost of P is smaller than the total cost of P , which is a contradiction.Again, one can generalize the argumentation of Lemma 13 to the following corollary: Corollary 14.Let x ∈ V (T ) be an internal node of the k-expression tree T marked with × S for some S ⊆ [k] 2 with children y and z.Let H x be the auxiliary graph as defined in Definition 9. Then for every i, j ∈ [k] and a, b ∈ {y, z} the following holds: (1) For every shortest L a i -L b j -path P in G x there exists a v a i -u b j -path P * in H x of cost ω H x (P * ) = ω(P ).
(2) For every shortest L a i -L b j -path P in G x with the property that the second vertex is in With the same argumentation as done in Lemma 13 one can show that also every shortest L y i -L y j -path in G x corresponds to a shortest v y i -v y j -path in H x .The cases with a = z follows analogously by renaming y and z.This proves (1).For the remaining cases we observe that for i, j ∈ [k] and a, b ∈ {y, z}, every shortest L a i -L b j -path P in G x with the property that the second vertex is in )), the first (resp.last) vertex of P is a minimum cost vertex in L a i (resp. in L b j ) and that it is not covered in the cost of the corresponding path in H x .
Corollary 14 shows that every shortest L a i -L b j -path in G x is represented in H x for i, j ∈ [k] and a, b ∈ {y, z}.Together with Corollary 12, this proves Theorem 10.
After the construction of the auxiliary graph H x as defined in Definition 9, we compute and store the shortest distances for all pairs of vertices in H x .With those values one can now compute the values c x i,j and a x v,i for i, j ∈ [k] and v ∈ V (G x ).Note that some of the values are only required in the second phase.Corollary 15.Let x ∈ V (T ) be a node in the k-expression tree T marked with × S for some S ⊆ [k] 2 .For all i, j ∈ [k] it holds that . Thus, the shortest path between the vertex sets L x i and L x j for i, j ∈ [k] in G x starts in either L y i or L z i and ends in either L y j or L z j and the following holds: The last equation follows from Theorem 10.
Corollary 16.Let x ∈ V (T ) be a node in the k-expression tree T marked with × S for some ). Subdivide P into two parts P = P 1 , P 2 such that P 1 is the maximal subpath of P with V (P 1 ) ⊆ V (G y ).Let u 1 be the last vertex of P 1 .Then, ω(P 1 ) ≥ a y v,lab(u1) and ω(P 2 ) ≥ dist H x (u y lab(u1) , u a i ), thus a x v,i = ω(P 1 ) + ω(P 2 ) ≥ min j∈[k],a∈{y,z} {a y v,j + dist H x (u y j , u a i )}.On the other hand, for each j ∈ [k] and a ∈ {y, z} there is a path in G x of length a y v,j + dist H x (u y j , u a i ): Per definition there is a (v, L y j )-path P 1 in G x with ω(P 1 ) = a y v,j and due to Theorem 10, there is a L y j -L z i -path P 2 of cost ω(P 2 ) = dist H x (u y j , u a i ) − min v∈L y j ω(v) with the property that the second vertex is connected to all vertices of L y j .Thus, one can combine the path P 1 with the path P 2 except of the first vertex of P 2 .The resulting path is a (v, L a i )-path of desired cost.
Second Phase.In this phase, we process the k-expression tree T in a top-down manner and use the local values that we have computed in the first phase to determine distances in the whole graph G. Consider an internal node x ∈ V (T ) of the k-expression tree T marked with × S for some S ⊆ [k] 2 and let y and z be the children of x in T .Let L y i resp.L z i denote the set of vertices with label i in G y resp.G z for i ∈ [k].For an internal node x with children y and z we will compute for any vertex set L y i resp.L z i and every vertex v ∈ V (G x ) the minimum cost of all paths in G that start in v and end in L y i resp.L z i with the property that the penultimate vertex is in V (G) \ V (G y ), resp. in V (G) \ V (G z ).Thus, the penultimate vertex is connected to all vertices of the vertex set L y i resp.L z i .It will therefore be convenient not to include the cost of the final vertex in these costs (cf.definition below).Note, that we consider the whole graph G in this step instead of just G x .
Formally, for a node x ∈ V (T ) marked with × S for some S ⊆ [k] 2 with children y and z we compute for every v ∈ V (G x ) and i ∈ [k] the following values: v,i,y = min P ∈P ω(P ) − min u∈L y i ω(u) where P is the set of all paths in G starting in v, ending in L y i , and having the penultimate vertex in V (G) \ V (G y ).d x v,i,z = min P ∈P ω(P ) − min u∈L z i ω(u) where P is the set of all paths in G starting in v, ending in L z i , and having the penultimate vertex in V (G) \ V (G z ).
For a node x ∈ V (T ) marked with • R for some R : [k] → [k] and the child y, we only compute d x v,i,y .We start by computing those values for the root node.We can assume, w.l.o.g., that the root node has label × S for some S ⊆ [k] 2 .Lemma 17.Let r ∈ V (T ) be the root node of the k-expression tree T marked with × S for some S ⊆ [k] 2 and let y and z be the children of r.Let further H r be the graph defined in Definition 9.Then, for any v ∈ V (G y ) and for every i Analogously, for any v ∈ V (G z ) and for every i Proof.We prove the first equation for the case that v ∈ V (G y ).The second equation as well as both equations for v ∈ V (G z ) can be shown analogously.
We first show that . Let be the maximal index such that p r ∈ V (G y ) for all 1 ≤ r ≤ and let j ∈ [k] such that p ∈ L y j .It holds that 1 ≤ ≤ n − 2 since p 1 ∈ V (G y ) and p n−1 / ∈ V (G y ).Consider the subpaths P 1 = (p 1 , . . ., p ) and P 2 = (p +1 , . . ., p n ).By construction, P 1 is a v-L y j -path in G y and hence ω(P 1 ) ≥ a y v,j .Since p +1 is connected to p and p +1 ∈ G z , the vertex p +1 is connected to every vertex in L y j .We prepend P 2 by p = arg min u∈L y j ω(u) and denote the resulting path by P 2 .Since P is a shortest v-L y i -path and p n−1 ∈ V (G z ), it holds that p n = arg min u∈L y i ω(u).The path P 2 is an L y j -L y i -path in G[r] = G with the property that the second vertex and the penultimate vertex are in V (G[r]) \ V (G y ) and hence, due to Theorem 10, ω(P 2 ) ≥ dist H r (u y j , v y i )+ω(p )+ω(p n ).Note, that ω(P ) = ω(P 1 )+ω(P 2 )−ω(p ).Thus For the other direction, we observe that for each j ∈ [k] there is always a path in G of cost a y v,j + dist H r (u y j , v y i ) + min u∈L y i ω(u) that starts in v, ends in L y i , and that has its penultimate vertex in V (G) \ V (G y ): For fixed j ∈ [k] let P 1 be a shortest v-L y j -path in G y .By definition it holds that ω(P 1 ) = a y v,j .Let P 2 be a shortest L y j -L y i -path in G[r] = G with the property that the second vertex and the penultimate vertex are in V (G[r]) \ V (G y ).By Theorem 10 it holds that ω(P 2 ) = dist H r (u y j , v y i ) + min u∈L y j ω(u) + min u∈L y i ω(u).Since the second vertex of P 2 is connected to all vertices of L y j we can combine P 1 and P 2 by removing the first vertex of P 2 to get a v-L y i -path P in G with the property that the penultimate vertex is in Because the argument works for all j ∈ [k], it follows directly that d r v,i,y ≤ min j∈[k] {a y v,j + dist H r (u y j , v y i )}, which completes the proof.
Next, we show how to propagate those values downwards in the k-expression tree, starting with a node marked with • R for some R : Let y be the unique child of x and w be the unique ancestor of x in T .Then and that L x i = j:R(j)=i L y j .Thus, all vertices in V (G) \ V (G x ) are either connected to all vertices of L x i or to none.Consider a shortest v-L y i -path P in G with the property that the penultimate vertex is in On the other hand, every shortest v-L x R(i) -path in G with the property that the penultimate vertex is in V (G) \ V (G x ) can be changed to a v-L y i -path by possibly replacing the final vertex with arg min u∈L y i ω(u).Since the cost of this vertex is not included We now show the propagation for nodes x of T that are marked with × S .We start with one specific case and then conclude the general case as a corollary.Lemma 19.Let x ∈ V (T ) be an internal node of the k-expression tree T that is marked with × S for some S ⊆ [k] 2 .Let y and z be the two children of x in T , let w be the unique ancestor of x in T , and let v ∈ V (G y ) be arbitrary.Then d x v,i,y is the minimum of the following three values: Proof.After possibly adding nodes marked with • id to the k-expression tree, with id being the identity function, one can assume, that w is marked with • R for some R : [k] → [k] and that x is the only child of w.
v,i,y .We distinguish three cases: Case 1: ) and all vertices of P are in G x .In this case, we can compute the value in the same way as done in Lemma 17 for the root node and get ).We split the path P into the two subpaths P 1 = (p 1 , . . ., p ) and P 2 = (p +1 , . . ., p n ).Let j ∈ [k] such that p +1 ∈ L x j .Since p is connected to p +1 , the vertex p is connected to every vertex in L x j .
We extend P 1 by p = arg min u∈L x j ω(u) and denote the resulting path by P 1 .Now it holds by definition that ω(P 1 ) − ω(p ) ≥ d w v,j,x , as the penultimate vertex p of P 1 is in V \ V (G x ).Let further c ∈ {y, z} such that p +1 ∈ L c j , noting that it does not change its label at x. Then ω(P 2 ) − ω(p n ) ≥ dist H x (v c j , v y i ) by Theorem 10, as P 2 is a path in G x .Note, that ω(P ) = ω(P 1 ) + ω(P 2 ) − ω(p ).Thus, in this case it holds that We have seen in the case analysis above that in each case d x v,i,y is at least the value considered in the case; in particular, it is at least equal to their collective minimum value.On the other hand, for each case there is a path P fulfilling the definition of d x v,i,y such that ω(P ) − min u∈L y i ω(u) equals the value of the considered case.Thus, d x v,i,y is also at most equal to the minimum taken over all three cases.This completes the proof.Corollary 20.Let x ∈ V (T ) be an internal node of the k-expression tree T marked with × S for some S ⊆ [k] 2 .Let y and z be the unique children of x in T , w be the unique ancestor of x in T , and let α, β ∈ {y, z} be arbitrary.Then, for v ∈ V (G[α]) the value d x v,i,β is the minimum of the following three values: Third Phase.In the third phase, we traverse the k-expression tree T one final time; the ordering is immaterial.We go over all nodes x with label × S for some S ⊆ [k] 2 and compute for each pair of vertices (u, v) with u ∈ V (G y ) and v ∈ V (G z ) the shortest u-v-path in G, where y and z are the two children of x in T .Since the leaves of T correspond one-to-one to single-vertex graphs, one for each vertex of G, this procedure will consider every pair of vertices in G at some node x ∈ V (T ).
Lemma 21.Let x ∈ V (T ) be an internal node of the k-expression tree T marked with × S for some S ⊆ [k] 2 .Let y and z be the two children of x and let Proof.Let P = (u = p 1 , . . ., p n = v) be a shortest u-v-path in G. Let be the largest index such that p ∈ V (G) \ V (G z ).Since p 1 ∈ V (G y ) and p n ∈ V (G z ) this index must exist and it holds that ≤ n − 1. Split P into two subpaths P 1 = (p 1 , . . ., p ) and P 2 = (p +1 , . . ., p n ).Let i ∈ [k], such that p +1 ∈ L z i .Since p is connected to p +1 , the vertex p is connected to every vertex in L z i .We extend P 1 by p = arg min u∈L z i ω(u) and denote the resulting path by P 1 .Now, P 1 is a u-L z i -path with penultimate vertex in V (G) \ V (G z ) and, therefore ω(P 1 ) − ω(p ) ≥ d x u,i,z .Similarly, using that G is undirected, the reverse of Conversely, we show that for each i ∈ [k] there is a u-v-path in G of cost d x u,i,z + a z v,i : For fixed i ∈ [k] let P be a shortest u-L z i -path in G with the property that the penultimate vertex is in V (G) \ V (G z ).By definition it holds that ω(P ) = d x u,i,z + min w∈L z i ω(w).Let P 1 be obtained from P by removing the last vertex.Now, ω(P 1 ) = d x u,i,z and P 1 has the property that it starts in u and that its last vertex is adjacent to all vertices of L z i .Let P 2 be a shortest v-L z i -path in G z .By definition it holds that ω(P 2 ) = a z v,i .Now, we can extend P 1 by the reverse of P 2 to get a u-v-path in G of cost d x u,i,z + a z v,i .This implies that dist G (u, v) ≤ d x u,i,z + a z v,i and, using that the argument works for all i ∈ Running time.First, we need to transform the clique-width k-expression into a NLC-width k-expression tree T , which can be done in linear time O(n + m) [18].
In the first traversal, we compute for every node In the last phase, we consider each pair (u, v) of vertices exactly once and compute each pairwise distance in time O(k).Thus, running time for the last phase is O(n 2 k).In total, we obtain the claimed bound of O(k 2 n 2 ).

General Running Time Theorem
First, we will derive a general running time theorem that is applicable to many algorithms that use modular decomposition trees, for example all algorithms in [19].Since we will focus on functions describing running times, we will restrict ourselves to functions T : R ≥1 → R ≥1 .

Definition 22 ([3]
).A function T : R ≥1 → R ≥1 is superhomogeneous if for all λ ≥ 1 the following holds: ≥1 → R ≥1 be a function that is superhomogeneous in the first component and monotonically increasing in the second component.Then Proof.Since T is monotonically increasing in the second component, the maximum is reached for = m.Pick k with 1 ≤ k ≤ n arbitrarily and set λ ≥ 1 so that λ • k = n.It follows directly that This completes the proof.
We can now state the running time framework.
Theorem 24.Let G be a graph of modular-width equal to mw, let M D(G) be the modular decomposition tree of G, and let T : R 2 ≥1 → R ≥1 be a function that is superhomogeneous in the first component and monotone increasing in the second component.If the running time of an algorithm for any prime node , where n i and m i denote the number of vertices and edges of the quotient graph corresponding to v i , then the total running time can be upper bounded by If, additionally, T is also superhomogeneous in the second component then the running time can also be upper bounded by O(T (mw, m) + n + m).
Proof.In a first step we compute the modular decomposition tree of the input graph G in linear time [26].We can assume that the running time is then dominated by the sum of computations of all prime nodes, since one can replace a series resp.parallel node with k children by a sequence of k − 1 pseudo-prime nodes each with a corresponding quotient graph isomorphic to K 2 resp.I 2 .Let t denote the number of prime nodes after this replacement.For any node v i ∈ V (M D(G)) let n i and m i denote the number of vertices resp.edges in the quotient graph associated with v i .Thus, it holds that n i ≤ mw and m i ≤ mw 2 .Therefore, the running time all nodes can be upper bounded by The last inequality holds due to Lemma 23 and since t i=1 n i counts each node in the modular decomposition (except of the root) exactly once.
Since T is monotone increasing in the second component and each quotient graph has at most mw 2 many edges, one can replace m i by mw 2 in (2) and get a running time for processing all nodes in the modular decomposition tree of O(n T (mw,mw 2 )
If, additionally, T is also superhomogeneous in the second component then the running time can be upper bounded by The second to last inequality holds using the argumentation from Lemma 23 and since t i=1 m i counts every edge at most once.
Example.In [19] it was shown how to solve global minimum vertex cut with a running time per prime node of O(n i m i log n i ), where n i denotes the number of vertices and m i denotes the number of edges in the quotient graph of prime node v Mi in the modular decomposition tree.Thus, by using Theorem 24, one can bound the total running time by O(min{n mw 2 log mw, m mw log mw} + n + m).
Proof of Theorem 25.For an input graph G = (V, E), the algorithm first computes the modular decomposition tree M D(G) and then processes M D(G) in a top-down traversal, starting with the root node.For a node v M in M D(G) with children v M1 , . . ., v M , let M be the corresponding module and P = {M 1 , . . ., M } be the corresponding modular partition of G[M ].We solve k-capped apsp in G[M ] as follows: First, we construct the weighted quotient graph G * /P as defined in Definition 27, and solve vertex-weighted APSP on G * /P .Next, we compute for all pairs of vertices in G[M ] that are in different modules M i the shortest distance in G by utilizing Lemma 28.Afterwards, we compute for each module M i ∈ P the minimum weight of all vertices in adjacent modules using G * /P , i.e, k i = min q M j ∈N (q M i ) ω(q Mj ).Finally, we use Corollary 29 and recurse by solving for each module M i ∈ P the Problem k-capped apsp on G[M i ] with k = k i .For the root node, we set k = v∈V ω(v).
For any prime node v Mi in M D(G) vertex-weighted APSP can be solved in time O(n 2.842 i ) with an algorithm due Yuster [31], where n i denotes the number of vertices in the corresponding quotient graph.With a standard combinatorial algorithm one can solve vertexweighted APSP in time O(n 3 i ).Thus, by Theorem 24, the total running time for this step is O(n mw 1.842 +m) if we use fast matrix multiplication or O(n mw 2 +m) for the combinatorial algorithm.After we have solved vertex-weighted APSP on all nodes in the modular decomposition tree, we use Lemma 28 to compute for each pair of vertices in G that are in different modules the length of a shortest path in constant time.Since we do this for each pair of vertices in G exactly once, this sums up to a total running time of O(n 2 ).The computation of the values k i can be done in time O(n 2 i ), which is dominated by the time of solving vertex-weighted APSP.In total, we obtain a combinatorial algorithm of time O(mw 2 n + n 2 ) and a algorithm of time O(mw 1.842 n + n 2 ) using fast matrix multiplication.

Conclusion
We started the study of vertex-weighted all-pairs shortest paths in the FPT in P framework and obtained efficient parameterized algorithms with respect to clique-width and modular-width.The algorithm parameterized by modular-width is adaptive, i.e., even if the parameter reaches its upper bound of n, the algorithm is not worse than the best unparameterized algorithm, and even for k ∈ O(n 1−ε ) for any ε > 0, it outperforms the best unparameterized algorithm.The algorithm parameterized by the stronger parameter cliquewidth is truly subcubic if cw ∈ O(n 0.5−ε ) for any ε > 0. It also permits us to solve diameter in the same time O(cw 2 n 2 ), complementing the lower bound ruling out O(2 o(cw) • n 2−ε ) for any ε > 0, due to Coudert et al. [5].The algorithms only apply to the vertex-weighted case.Note also that the algorithm relative to clique-width assume to be given a suitable expression or decomposition, whereas the modular decomposition of a graph, and hence its modular-width, can be computed in linear time [26].As mentioned in [19], considering edge-weighted graphs with (low) clique-width resp.low modular-width is hopeless, as one could modify an arbitrary input graph by adding all the missing edges with sufficiently large weights.Clearly, the shortest path lengths do not change, but the resulting graph is a clique and has constant clique-width and modular-width.
Apart from considering other parameters, one interesting open question is whether there is an adaptive algorithm for all-pairs shortest paths parameterized by clique-width, e.g., can the running time be reduced to O(cw n 2 )?This seems quite challenging, since even computing some variant of all-pairs shortest paths for each node in the expression tree (on a graph with cw many nodes) results in a non-adaptive running time.

For a modular
partition P of a graph G, the quotient graph G /P is a compact representation of all the edges in G with endpoints in different modules.If one additionally knows all subgraphs G[M i ], with i ∈ [ ], one can reconstruct G.Each subgraph G[M i ] is called a factor.Instead of explicitly storing all factors, one can recursively decompose them as well until one reaches trivial modules {v}.To make the decomposition unique, one considers only modular partitions consisting of strong modules.A module M ⊆ V of a graph G is called a strong module, if it does not overlap with any other module M of G, meaning that either M and M are disjoint or one module is a subset of the other.One can represent all strong modules of a graph G by an inclusion tree M D(G).Each strong module M in G corresponds to a vertex v M in M D(G).A vertex v A is an an ancestor of v B in M D(G) if and only if B A for the corresponding strong modules A and B of G. Hence, the root node of M D(G) corresponds always to the complete vertex set V of G and every leaf of M D(G) corresponds a singleton set {v} with v ∈ V .Consider an internal node v M of M D(G) with the set of children {v M1 , . . ., v M }, i.e., v M corresponds to a strong module M of G and P = {M 1 , . . ., M } is a modular partition of G[M ] into strong modules where M i is the corresponding module of v Mi , with i ∈ [ ].There are three types of internal nodes in M Lemma 19  shows how to compute the value d x v,i,y for any v ∈ V (G y ).By a similar argumentation one can also compute the value d x v,i,β for any v ∈ V (G[α]) for α, β ∈ {y, z}.
Thus, we compute at most n • k values, each in time O(k), which results in a running time of O(nk 2 ) per node of T .In the case of a node x with label × S for some S ⊆ [k] 2 we first compute the auxiliary graph H x in time O(|V (H)| + |E(H)|) = O(k 2 ) and solve (edge-weighted) all-pairs shortest paths on H x in time O(k 3 ).After this, by using Corollary 15 resp.Corollary 16, we compute each c x i,j in constant time resp.each a x v,i in time O(k) resulting in a running time per node x ∈ V (T ) of O(k 3 + k 2 • n).In the second phase we perform a top-down traversal of T to compute the for each node x the values d x v,i,y and d x v,i,z for all v ∈ G x and i ∈ [k].Again, we compute at most n • k values, each in time O(k), which results in a running time of O(nk 2 ) per node of T .Since there are O(n) nodes in the k-expression tree T , the total running time for Phase One and Phase Two is O