## The swap edges of a multiple-sources routing tree

Bang Ye Wu* ^{∗}* Chih-Yuan Hsiao

*Kun-Mao Chao*

^{†}

^{‡}Abstract

*Let T be a spanning tree of a graph G and S ⊂ V (G) be a set of sources. The routing*
*cost of T is the total distance from all sources to all vertices. For an edge e of T , the*
*swap edge of e is the edge f minimizing the routing cost of the tree formed by replacing*
*e with f . Given an undirected graph G and a spanning tree T of G, we investigate*
the problem of finding the swap edge for every tree edge. In this paper, we propose an
*O(m log n + n*^{2}*)-time algorithm for the case of two sources and an O(mn)-time algorithm*
*for the case of more than two sources, where m and n are the numbers of edges and*
*vertices of G, respectively.*

Key words: algorithm, graph, swap edge, spanning tree, optimization problem.

### 1 Introduction

A routing tree of a graph is a spanning tree, along which the messages can be routed
*from sources to destinations. Let G = (V, E, w) be an undirected graph with nonnegative*
*edge length function w. All vertices are destinations, whereas some of them are sources.*

The routing cost of a spanning tree is defined by the total distance from sources to all vertices. For the sake of efficiency, the routing cost should be made as small as

*∗*bangye@mail.stu.edu.tw, Department of Computer Science and Information Engineering, Shu-Te
University, YenChau, Kaohsiung, Taiwan 824, R.O.C.

*†*skiky@mail2000.com.tw, Department of Computer Science and Information Engineering, National
Taiwan University, Taipei, Taiwan 106, R.O.C.

*‡*corresponding author, kmchao@csie.ntu.edu.tw, Department of Computer Science and Information
Engineering / Graduate Institute of Networking and Multimedia, National Taiwan University, Taipei,
Taiwan 106, R.O.C.

*possible. Given a graph and a set of p sources, the problem of finding the minimum*
*routing cost spanning tree (MRCT) is NP-hard for any constant p > 1 [9]. When p = 1,*
i.e., there is only one source, it is reduced to the well-known single-source shortest-
*paths tree problem and can be solved in polynomial time. For the p-sources MRCT*
problem, several approximation algorithms have been reported, including a polynomial
time approximation scheme (PTAS) for the case that all vertices are sources [12], a PTAS
for the case of two sources [8], and a 2-approximation algorithm for an arbitrary number
of sources [11]. A survey of the MRCT and some other related problems can be found
in [10].

The survivability of a network is the ability to recover the communication if a link of
the network fails [3]. A tree has the benefits of low cost and simple routing algorithms,
but it suffers from weak survivability. Therefore, how to recover a tree from failures is an
important issue. Any edge of a spanning tree corresponds to a bipartition of the vertex
set. To recover a spanning tree from the failure of an edge, we need to find another edge
*crossing the bipartition so as to minimize the routing cost. For an edge e of T , the swap*
*edge of e is the edge f minimizing the routing cost of the tree formed by replacing e with*
*f .*

*Let m and n be the numbers of edges and vertices of the input graph, respectively.*

*In [7], the authors proposed an O(n*^{2})-time algorithm for finding the swap edge for every
*edge of a single-source shortest-paths tree. More recently, an O(m log*^{2}*n)-time algorithm*
for the same problem was reported in [1], which improves the previous one for sufficiently
sparse graphs. In this paper, we investigate the problem of finding the swap edge for every
*tree edge when the number of sources is more than one. We present an O(m log n + n*^{2})-
*time algorithm for the case of two sources and an O(mn)-time algorithm for the case of*
more than two sources.

The rest of the paper is organized as follows. We first formally define the notations
*and the problem in Section 2. In Section 3, we propose an O(m log n+n*^{2})-time algorithm
*for the two-sources case. Then we propose an O(mn)-time algorithm for the multiple-*
sources case in Section 4. Finally, concluding remarks are given in Section 5.

### 2 Preliminaries

*By G = (V, E, w), we denote a graph G with vertex set V , edge set E, and edge weight*
*(length) function w. In this paper, we consider only undirected graphs with nonnegative*
*edge lengths. For any graph G, the vertex set and edge set are denoted by V (G) and*
*E(G) respectively. Let T be a spanning tree of G. For any e ∈ E(T ), the removal of e*
*cuts T into two subtrees, of which the vertex sets form a bipartition (V*_{1}*, V*_{2}*) of V (G).*

*By C(e), we denote the cut set of the bipartition, i.e., the set of edges with one endpoint*
*in V*_{1} *and the other endpoint in V*_{2}*. For convenience, we assume that e /∈ C(e). It is clear*
*that if C(e) 6= ∅, then the two subtrees can be connected to form another spanning tree*
*of G by inserting any edge in the cut set. We use T** _{e/f}* to denote the new spanning tree

*obtained by replacing an edge e with another edge f ∈ C(e).*

*For u, v ∈ V (G), the distance between u and v in G is denoted by d*_{G}*(u, v), which is*
*the total length of the edges in a shortest path between the two vertices. For U ⊂ V (G),*
*let D*_{G}*(v, U ) =*^{P}_{u∈U}*d*_{G}*(v, u) denote the total distance from v to all vertices in U . For*
*a subgraph H, we also use D*_{G}*(v, H) to denote D*_{G}*(v, V (H)). A source (vertex) is a*
*distinguished vertex in V (G). For a given set S of source vertices, the routing cost of*
*a spanning tree T is denoted by c(T, S), which is the total distance from sources to*
*all vertices, i.e., c(T, S) =*^{P}_{s∈S}*D*_{T}*(s, T ). When there is no confusion, we also simply*
*use c(T ) for the routing cost. For an edge e ∈ E(T ), the swap cost is defined by*
*χ(T, e) = min*_{f ∈C(e)}*c(T*_{e/f}*, S), which is the minimum routing cost of any spanning tree*
*obtained by replacing e with another edge. The swap edge problem is formally defined*

as follows:

Problem: Swap Edges for a Routing Tree (SERT)

*Instance: A graph G = (V, E, w), a spanning tree T of G, and a set S ⊂ V*
of sources.

*Goal: For each e ∈ E(T ), find χ(T, e).*

*In case that C(e) is empty for some e, we say that there is no swap edge for e and the*
*cost χ(T, e) = ∞. Since the swap edges will be implicitly determined when minimizing*
the swap costs, we shall focus on the swap costs only. In the remaining paragraphs, we
*shall assume that G, T , and S are the input data of the SERT problem.*

### 3 Two-sources case

*In this section, we discuss the two-sources case of the SERT problem. Let S = {s*_{1}*, s*_{2}*} ⊂*
*V be the set of two specified source vertices and P = (s*_{1} *= r*_{0}*, r*_{1}*, r*_{2}*, . . . , r*_{k}*= s*_{2}) be the
*path between the two sources in T . The routing cost of T in this case is defined by*

*c(T, S) =* ^{X}

*v∈V*

*D*_{T}*(v, S) =* ^{X}

*v∈V*

*(d*_{T}*(v, s*_{1}*) + d*_{T}*(s*_{2}*, v)) .*

*Removing all the edges of P from T divides T into k + 1 subtrees. Let T** _{i}* denote the

*subtree containing r*

_{i}*and V*

_{i}*= V (T*

_{i}*) for 0 ≤ i ≤ k. Our method for computing the swap*

*costs for all tree edges is divided into two parts: e /∈ E(P ) and e ∈ E(P ).*

3.1 Edges not in the path

*To compute the swap costs of edges in T*_{i}*, we treat all vertices not in V** _{i}* as a super node,
and reduce the problem to the single-source case, as shown in Figure 1. Construct an

*auxiliary graph G*

_{i}*= (V*

_{i}

^{0}*, E*

_{i}

^{0}*, w*

^{0}

_{i}*) by shrinking all vertices not in V*

_{i}*to a created vertex s*as follows.

*• V*_{i}^{0}*= V*_{i}*∪ {s}, in which s /∈ V is a created node.*

*Figure 1: The super node s for reduction to single-source SERT problem.*

*• The edge set E*_{i}* ^{0}* consists of two parts: The first part contains all the edges of which

*both endpoints are in V*

_{i}*, and the other part contains the edges (s, v) if there exists*

*an edge (v, u) crossing the cut (V*

_{i}*, V − V*

_{i}*) for any v ∈ V*

*.*

_{i}*• For each edge (u, v) such that u, v ∈ V*_{i}*, set w*^{0}_{i}*(u, v) = 2w(u, v).*

*• w*_{i}^{0}*(s, r*_{i}*) = D*_{T}*(r*_{i}*, S) and for each edge (v, s), v ∈ V*_{i}*− {r*_{i}*}, set w*_{i}^{0}*(v, s) =*
min_{u /}_{∈V}_{i}*{2w(u, v) + D*_{T}*(u, S)}.*

*Let T*^{0}*be the spanning tree of G*_{i}*, which is obtained by inserting edge (s, r*_{i}*) into T** _{i}*, and

*let s be the source of T*

*.*

^{0}*Lemma 1: For any vertex v ∈ V*_{i}*, D*_{T}*(v, S) = d*_{T}^{0}*(v, s).*

Proof:

*d*_{T}^{0}*(v, s) = d*_{T}^{0}*(v, r*_{i}*) + w*^{0}_{i}*(r*_{i}*, s)*

*= 2d*_{T}*(v, r*_{i}*) + D*_{T}*(r*_{i}*, S) = d*_{T}*(v, s*_{1}*) + d*_{T}*(v, s*_{2})

Similarly, the following result can be easily shown by the definitions, and the proof is omitted.

*Lemma 2: The swap costs of all edges in T** _{i}*can be computed by solving the single-source

*SERT problem with input (G*

_{i}*, T*

^{0}*, s).*

*Lemma 3: The swap costs for all edges not in P can be found in O(n*^{2}) time.

Proof: *To compute the swap costs, for each V** _{i}*, we construct the auxiliary graph and

*then solve the single-source SERT problem. First we compute d*

_{T}*(v, S) for every v ∈ V*

*by traversing T twice: one for computing d*

_{T}*(v, s*

_{1}

*) and the other for d*

_{T}*(v, s*

_{2}). Also the

*set V*

_{i}*for each i can be found by traversing the tree in linear time. To construct G*

*, we*

_{i}*traverse T*

*in a depth first search order. By storing the graph in an adjacent list, all the*

_{i}*auxiliary graphs can be constructed in O(n + m) time since each edge is checked twice.*

*Since the single-source SERT problem for G*_{i}*can be solved in O(|V (G*_{i}*)|*^{2}) time [7], the
*total time complexity is O(n*^{2}).

3.2 Edges in the path

*In the following, we focus on how to find out the swap costs for the edges in P = (s*_{1} =
*r*_{0}*, r*_{1}*, r*_{2}*, . . . , r*_{k}*= s*_{2}*). Let e*_{i}*= (r*_{i}*, r*_{i+1}*) and n*_{i}*= |V*_{i}*|. Let Y** _{i}*and ¯

*Y*

*be the two subtrees*

_{i}*obtained by removing the edge e*

_{i}*from T , in which s*

_{1}

*∈ V (Y*

_{i}*). Also let N*

_{i}*= |V (Y*

_{i}*)|.*

*Note that V (Y** _{i}*) =

^{S}

_{j≤i}*V*

_{j}*and N*

*=*

_{i}^{P}

_{j≤i}*n*

_{j}*. For any f ∈ C(e*

_{i}*) and H = T*

*, we have*

_{e/f}*c(H) = (D*_{H}*(s*_{1}*, Y*_{i}*) + D*_{H}*(s*_{2}*, ¯Y*_{i}*)) + (D*_{H}*(s*_{2}*, Y*_{i}*) + D*_{H}*(s*_{1}*, ¯Y*_{i}*)).*

*Obviously, D*_{H}*(s*_{1}*, Y*_{i}*) = D*_{T}*(s*_{1}*, Y*_{i}*) and D*_{H}*(s*_{2}*, ¯Y*_{i}*) = D*_{T}*(s*_{2}*, ¯Y** _{i}*) since they are not

*affected by the removal of e*

_{i}*. Let f = (x, y), as illustrated in Figure 2. By defini-*

*Figure 2: A swap edge for an edge (r*_{i}*, r** _{i+1}*) in the path between the two sources.

*tion, D*_{H}*(s*_{2}*, Y*_{i}*) = D*_{T}*(x, Y*_{i}*) + N*_{i}*(w(f ) + d*_{T}*(y, s*_{2}*)) and D*_{H}*(s*_{1}*, ¯Y*_{i}*) = D*_{T}*(y, ¯Y*_{i}*) + (n −*
*N*_{i}*)(w(f ) + d*_{T}*(x, s*_{1})). Let

*δ(f, i) = D*_{H}*(s*_{2}*, Y*_{i}*) + D*_{H}*(s*_{1}*, ¯Y** _{i}*)

*= D*_{T}*(x, Y*_{i}*) + D*_{T}*(y, ¯Y*_{i}*) + nw(f ) + N*_{i}*d*_{T}*(y, s*_{2}*) + (n − N*_{i}*)d*_{T}*(x, s*_{1}*),* (1)

and our goal is to compute

*χ(T, e*_{i}*) = (D*_{T}*(s*_{1}*, Y*_{i}*) + D*_{T}*(s*_{2}*, ¯Y** _{i}*)) + min

*f ∈C(e**i*)*δ(f, i),*

*for every 0 ≤ i < k. By a na¨ıve method, δ(f, i) can be computed in O(n) time, which*
*implies that the swap cost χ(T, e*_{i}*) can be computed in O(mn) time. Since the number*
*of edges in P may be up to Ω(n), it takes totally O(mn*^{2}) to compute all the swap costs.

In the following, we present an incremental method and show that the two-sources SERT
*problem can be solved in O(m log n + n*^{2}) time.

*Suppose that x ∈ V*_{p}*and y ∈ V*_{q}*, p < q. Observe that, for p < i < q,*

*δ(f, i) − δ(f, i − 1)*

= ^{¡}*D*_{T}*(x, Y*_{i}*) + D*_{T}*(y, ¯Y*_{i}*) + nw(f ) + N*_{i}*d*_{T}*(y, s*_{2}*) + (n − N*_{i}*)d*_{T}*(x, s*_{1})^{¢}

*−*^{¡}*D*_{T}*(x, Y*_{i−1}*) + D*_{T}*(y, ¯Y*_{i−1}*) + nw(f ) + N*_{i−1}*d*_{T}*(y, s*_{2}*) + (n − N*_{i−1}*)d*_{T}*(x, s*_{1})^{¢}

*= (D*_{T}*(x, Y*_{i}*) − D*_{T}*(x, Y*_{i−1}*)) + (D*_{T}*(y, ¯Y*_{i}*) − D*_{T}*(y, ¯Y** _{i−1}*))

*+(N*

_{i}*− N*

_{i−1}*)d*

_{T}*(y, s*

_{2}

*) + ((n − N*

_{i}*) − (n − N*

_{i−1}*))d*

_{T}*(x, s*

_{1})

*= D*_{T}*(x, V*_{i}*) − D*_{T}*(y, V*_{i}*) + n*_{i}*d*_{T}*(y, s*_{2}*) − n*_{i}*d*_{T}*(x, s*_{1}*).*

*Since D*_{T}*(x, V*_{i}*) − D*_{T}*(y, V*_{i}*) = n*_{i}*(d*_{T}*(r*_{i}*, x) − d*_{T}*(r*_{i}*, y)), we have*

*δ(f, i) − δ(f, i − 1)*

*= n*_{i}*(d*_{T}*(r*_{i}*, x) − d*_{T}*(r*_{i}*, y) + d*_{T}*(y, s*_{2}*) − d*_{T}*(x, s*_{1}))

*= n*_{i}*(d*_{T}*(r*_{i}*, x) − d*_{T}*(x, s*_{1}*) + d*_{T}*(y, s*_{2}*) − d*_{T}*(r*_{i}*, y))*

*= n*_{i}*(d*_{T}*(r*_{i}*, r*_{p}*) − d*_{T}*(r*_{p}*, s*_{1}*) + d*_{T}*(r*_{q}*, s*_{2}*) − d*_{T}*(r*_{i}*, r*_{q}*)) .* (2)

*It implies that δ(f, i) can be computed from δ(f, i−1) in constant time if all the required*
distances are available after a preprocessing stage. Consequently all the swap edges can
*be found in O(mn) time by computing δ(f, i) for each f and each i. In the following,*
*we propose an efficient method to find the edge minimizing δ(f, i) without calculating*
*δ(f, i) for all the edges crossing the cut.*

*For any edge (x, y) in which x ∈ V*_{p}*and y ∈ V*_{q}*, we say that x is the left endpoint*
*and y is the right endpoint if p < q. In the following, we shall assume that x is in V** _{p}*,

*p < k. Let E*

_{x}*denote the set of all edges with left endpoint x. Letf (i) = δ(f, i). Our*

^{b}

*approach is to compute a function F*

*defined by*

_{x}*F*_{x}*(i) = min*

*f ∈E**x*

*f (i).*b

*Once the function F*_{x}*for each vertex x is determined, the minimized δ(f, i) can be found*
*by taking the minimum of F*_{x}*(i) for all x. Since F** _{x}*is defined by the minimum of a set of

*functions, it can be regarded as a piecewise function. To determine F*

*, it is required to find the consecutive intervals and to determine which of the functions*

_{x}*f is the minimum*

^{b}for each interval.

For two discrete functions*f*^{b}_{1} and*f*^{b}_{2}, we say the two functions intersect in the interval
*[l*_{1}*, l*_{2}] if *f*^{b}_{1}*(l*_{1}*) <* *f*^{b}_{2}*(l*_{1}) and *f*^{b}_{1}*(l*_{2}*) >* *f*^{b}_{2}*(l*_{2}), or vice versa. If *f*^{b}_{1}*(l*_{1}*) <* *f*^{b}_{2}*(l*_{1}*) and [l*_{1}*, l*_{2}]
*is an intersection interval, the insertion point is the minimum i such thatf*^{b}_{1}*(i) >f*^{b}_{2}*(i).*

*Define right(f ) = q if the right endpoint of f is in V** _{q}*. We shall show some properties
which are crucial for our algorithm.

*Lemma 4: For two edges f*_{1} *and f*_{2}*in E*_{x}*, if right(f*_{1}*) = right(f*_{2}), then*f*^{b}_{1} and*f*^{b}_{2}never
*intersect. Furthermore, by an O(n*^{2})-time preprocessing, we can determine which of the
two functions is the smaller in constant time.

Proof: *Let f*_{1} *= (x, y*_{1}*), f*_{2} *= (x, y*_{2}*), and right(f*_{1}*) = q. By (2),* *f*^{b}_{1}*(i) −f*^{b}_{1}*(i − 1) =*
*f*b_{2}*(i) −f*^{b}_{2}*(i − 1) for any p < i < q. That is, the difference betweenf*^{b}_{1} and *f*^{b}_{2} is fixed in
the domain, thus they never intersect.

By (1),

*f*b_{1}*(i) −f*^{b}_{2}*(i) = N*_{i}*(d*_{T}*(y*_{1}*, s*_{2}*) − d*_{T}*(y*_{2}*, s*_{2}*)) + n(w(x, y*_{1}*) − w(x, y*_{2}))

*+D*_{T}*(y*_{1}*, ¯Y*_{i}*) − D*_{T}*(y*_{2}*, ¯Y*_{i}*),*

and

*D*_{T}*(y*_{1}*, ¯Y*_{i}*) − D*_{T}*(y*_{2}*, ¯Y** _{i}*)

*= (n − N*_{i}*− n*_{q}*)(d*_{T}*(r*_{q}*, y*_{1}*) − d*_{T}*(r*_{q}*, y*_{2}*)) + D*_{T}*(y*_{1}*, V*_{q}*) − D*_{T}*(y*_{2}*, V*_{q}*).*

In a preprocessing stage, we compute the following:

*• d*_{T}*(u, v) for any u, v ∈ V .*

*• For each vertex v, find i such that v ∈ V** _{i}*.

*• n*_{i}*= |V*_{i}*| and N*_{i}*= |V (Y*_{i}*)| for each i.*

*• D*_{T}*(v, V*_{i}*) for each i and each v ∈ V** _{i}*.

*The preprocessing can be done in O(n*^{2}) time. After the preprocessing stage, we can
compute*f*^{b}_{1}*(i) −f*^{b}_{2}*(i) in constant time and determine the smaller one.*

*Lemma 5: If f*_{1}*, f*_{2} *∈ E*_{x}*and right(f*_{2}*) < right(f*_{1}), the difference (*f*^{b}_{2}*(i) −f*^{b}_{1}*(i)) is*
*monotonically increasing as i increases for any p ≤ i < right(f*_{2}).

Proof: *Let right(f*_{2}*) = q and right(f*_{1}*) = j. For any p < i < q,*

(*f*^{b}_{2}*(i) −f*^{b}_{1}*(i)) − (f*^{b}_{2}*(i − 1) −f*^{b}_{1}*(i − 1))*

= (*f*^{b}_{2}*(i) −f*^{b}_{2}*(i − 1)) − (f*^{b}_{1}*(i) −f*^{b}_{1}*(i − 1))*

*= n*_{i}*(d*_{T}*(r*_{i}*, r*_{p}*) − d*_{T}*(r*_{p}*, s*_{1}*) + d*_{T}*(r*_{q}*, s*_{2}*) − d*_{T}*(r*_{i}*, r** _{q}*))

*−n*_{i}*(d*_{T}*(r*_{i}*, r*_{p}*) − d*_{T}*(r*_{p}*, s*_{1}*) + d*_{T}*(r*_{j}*, s*_{2}*) − d*_{T}*(r*_{i}*, r** _{j}*))

*= n*_{i}*((d*_{T}*(r*_{q}*, s*_{2}*) − d*_{T}*(r*_{j}*, s*_{2}*)) + (d*_{T}*(r*_{i}*, r*_{j}*) − d*_{T}*(r*_{i}*, r** _{q}*)))

*= n*_{i}*(d*_{T}*(r*_{q}*, r*_{j}*) + d*_{T}*(r*_{q}*, r** _{j}*))

*= 2n*_{i}*d*_{T}*(r*_{q}*, r*_{j}*) ≥ 0.*

It follows that (*f*^{b}_{2}*(i) −f*^{b}_{1}*(i)) ≥ (f*^{b}_{2}*(i − 1) −f*^{b}_{1}*(i − 1)). That is, the difference is mono-*
*tonically increasing as i increases.*

*By Lemma 4, for each q > p, at most one edge with right endpoint in V** _{q}* needs to be

*considered, and all the other edges in E*

_{x}*can be eliminated. Let E*

_{x}

^{0}*= {f*

_{i}*|1 ≤ i ≤ h} be*

*the set of edges that should be taken into consideration, in which right(f*

_{i}*) > right(f*

*)*

_{i+1}*for each i. Starting at*

*F*

^{b}

_{1}=

*f*

^{b}

_{1}, our algorithm iteratively computes

*F*

^{b}

_{i}*= min{F*

^{b}

_{i−1}*,f*

^{b}

_{i}*}*

*for i from 2 to h, and by definition F*

*=*

_{x}*F*

^{b}

*.*

_{h}*Let p = α*_{0} *< α*_{1} *< . . . < α*_{t}*≤ k. We denote the piecewise function* *F*^{b}* _{i}* by
S

_{t}*j=1**(α*_{j}*,f*^{b}_{g(j)}*), in which g(j) ≤ i andF*^{b}_{i}*(λ) =f*^{b}_{g(j)}*(λ) for each j and any λ ∈ [α** _{j−1}*+

*1..α*

_{j}*]. We say that [α*

_{j−1}*+ 1..α*

_{j}*] is the j-th interval ofF*

^{b}

*.*

_{i}*Lemma 6: For any i, the function* *f*^{b}* _{i}* intersects

*F*

^{b}

*at most once. If they intersect,*

_{i−1}*f*

^{b}

*must appear at the first interval of*

_{i}*F*

^{b}

*.*

_{i}Proof: By definition, *F*^{b}* _{i−1}* = min

_{j<i}*f*

^{b}

_{j}*, and right(f*

_{i}*) < right(f*

_{j}*) for any j < i. By*Lemma 5, the difference (

*f*

^{b}

_{i}*(λ) −f*

^{b}

_{j}*(λ)) is monotonically increasing as λ increases. That*is, if

*f*

^{b}

_{i}*(λ) >F*

^{b}

_{i−1}*(λ) for some λ, we can conclude that*

*f*

^{b}

_{i}*(λ*

^{0}*) >F*

^{b}

_{i−1}*(λ*

^{0}*) for any λ*

^{0}*> λ,*and the result follows.

Based on Lemma 6, the merging algorithm for computing *F*^{b}* _{i}* is designed as follows.

For convenience, let*f (λ) = ∞ for any λ ≥ right(f ).*^{b}

Algorithm Merge(*F*^{b}_{i−1}*,f*^{b}* _{i}*)

*Input: An edge f*_{i}*= (x, y) and a piecewise function*
*F*b* _{i−1}*=

^{S}

^{t}

_{j=1}*(α*

_{j}*,f*

^{b}

_{g(j)}*) stored in a link list L.*

Output: *F*^{b}_{i}*stored in L.*

1: if *F*^{b}_{i−1}*(p) ≤ f*_{i}*(p)*

2: return; /* no intersection */

*3: Scan L from the head so as to find the smallest α** _{j}* such that

*F*b

_{i−1}*(α*

_{j}*) < f*

_{i}*(α*

*);*

_{j}*4: Use binary search to find the intersection point λ in [α*_{j−1}*, α** _{j}*];

*5: Remove all intervals before the j-th interval from L;*

*6: Break the j-th interval by inserting (λ − 1,f*^{b}_{i}*) at the head of L;*

*7: return L.*

The algorithm for computing the swap costs for all edges in the path is as follows.

Algorithm Two SERT P

*Input: A graph G = (V, E, w), a spanning tree T and two source vertices s*_{1} *and s*_{2}.
*Assume that G is stored in an adjacency list;*

*Output: χ(T, e) for each e in the path between the two sources;*

1: Compute the following:

1.1: *the path P = (r*_{0}*, r*_{1}*, . . . , r*_{k}*) between s*_{1} *and s*_{2};
1.2: *d*_{T}*(u, v) for any u, v ∈ V ;*

1.3: *for each vertex v do find i such that v ∈ V** _{i}*;
1.4:

*n*

_{i}*= |V*

_{i}*| and N*

_{i}*= |V (Y*

_{i}*)| for each i;*

1.5: *D*_{T}*(v, V*_{i}*) for each i and each v ∈ V** _{i}*;

1.6: *D*_{T}*(v, Y*_{i}*) and D*_{T}*(v, ¯Y*_{i}*) for each vertex v and each i;*

*2: for each vertex x do*

3: *Find E** _{x}* by scanning the adjacency list;

4: *Sort the edges f in E*_{x}*such that right(f ) are in a non-increasing order;*

5: *Construct E*^{0}_{x}*= {f*_{i}*|1 ≤ i ≤ h} by eliminating the useless edges*
as described in Lemma 4;

6: *F*^{b}_{1} *←f*^{b}_{1}*; and create a link list L with one element (right(f*_{1}*),f*^{b}_{1});

7: *for i ← 2 to h do*

*F*b_{i}*← Merge(F*^{b}_{i−1}*,f*^{b}* _{i}*);

8: *Compute F*_{x}*(i) for each e*_{i}*in P ;*
*9: for each e*_{i}*in P*

*Find χ(T, e** _{i}*) = min

_{x}*F*

_{x}*(i);*

*Lemma 7: In algorithm Two SERT P, Step 1 can be done in O(n*^{2}) time.

Proof: It is obvious that Step 1.1 can be done in linear time. As shown in Lemma 4,

*Steps 1.2–1.5 takes O(n*^{2}) time. Now let’s calculate the time complexity of Step 1.6
*as follows. Observe that D*_{T}*(v, Y*_{0}*) = n*_{0}*d*_{T}*(v, r*_{0}*) + D*_{T}*(r*_{0}*, V*_{0}*) and that D*_{T}*(v, Y** _{i}*) =

*D*

_{T}*(v, Y*

_{i−1}*) + n*

_{i}*d*

_{T}*(v, r*

_{i}*) + D*

_{T}*(r*

_{i}*, V*

_{i}*) for i > 0. Since all the required values are available*

*before this step, D*

_{T}*(v, Y*

_{i}*) for all i can be done in O(n) time. Similarly, the distances*

*D*

_{T}*(v, ¯Y*

_{i}*) for all i can also be found by an incremental method. Therefore, the total time*

*complexity for Step 1 is O(n*

^{2}).

The next result is straightforward by (1).

*Lemma 8: After Step 1, the value δ(f, i) for any edge f and any i can be computed in*
constant time.

*Lemma 9: The time complexity of algorithm Two SERT P is O(m log n + n*^{2}) time.

Proof: *First, by Lemma 7, Step 1 can be completed in O(n*^{2}) time. Let’s analyze the
*time for the loop in Steps 2–8. Step 3 takes the linear time O(|E*_{x}*|), and the sorting at*
*Step 4 can be completed in O(|E*_{x}*| log |E*_{x}*|) time. By Lemma 4, Step 5 can be also done*
*in O(|E*_{x}*|) time. Step 6 takes only constant time. By Lemma 8, Step 8 takes O(n) time.*

To analyze the time complexity of Step 7, we consider the Merge algorithm first.

In Merge, we check the intervals one by one until we find the intersecting interval.

Once the interval is found, a binary search is employed to find the intersecting point.

*Obviously, the binary search needs at most O(log n) comparisons between the values of*
two functions. Since, by Lemma 8, the function values can be found in constant time,
*the binary search takes O(log n) time. To show the time complexity of Steps 3 and 5 in*
*Merge, we use an amortized analysis. Let L** _{i}* denote the number of intervals of

*F*

^{b}

*and*

_{i}*β*

*denote the number of intervals checked while merging*

_{i}*f*

^{b}

*. We have that*

_{i}*L*_{i}*= L*_{i−1}*− β*_{i}*+ 2.*

*Summing up for i from 2 to h, we obtain*

*L*_{h}*= L*_{1}*−*^{X}

*i*

*β*_{i}*+ 2(h − 1).*

*Since L*_{h}*≥ 1 and L*_{1} = 1,
X

*i*

*β*_{i}*= L*_{1}*+ 2(h − 1) − L*_{h}*≤ 2h − 2.*

*That is, the total number of checked intervals is O(|E*_{x}^{0}*|). Since checking for an interval*
takes only constant time by Lemma 8, the total time complexity of Step 7 in Algorithm
*Two SERT P is O(|E*_{x}^{0}*| log n).*

*In summary, the time complexity of the whole loop for each x is O(|E*_{x}*| log |E*_{x}*|).*

Since^{P}_{x∈V}*|E*_{x}*| = m, summing up for all vertices, the total time complexity of the loop*
*is O(m log n). Finally, since Step 9 can be easily done in O(n*^{2}) time, we conclude that
*the total time complexity of the algorithm is O(m log n + n*^{2}).

The next theorem summarizes the result in this section, which directly follows Lem- mas 3 and 9.

*Theorem 10: The two-sources SERT problem can be solved in O(m log n + n*^{2}) time.

### 4 Multiple-sources case

In this section, we consider the case where there are more than two sources. By def-
*inition, the routing cost of a tree T with source set S can be calculated by c(T, S) =*
P

*s∈S**D*_{T}*(s, V (T )). Since there is no limit on the number of sources, a direct method*
*for computing the routing cost of a tree will take O(n*^{2}) time even when the all-to-all
*distances are already known. For each edge e in E(T ), if we compute c(T*_{e/f}*, S) for every*
*f in the cut set, the time complexity for finding the swap cost χ(T, e) will be up to*

*O(mn*^{2}*), and consequently it takes O(mn*^{3}*) to find the swap costs for all edges in T . We*
*shall present an O(mn) time algorithm for the SERT problem with multiple sources.*

First we introduce a new formula for computing the routing cost of a tree. Let
*e = (u*_{1}*, u*_{2}*) ∈ E(T ). As mentioned previously, removing e from T will result in two*
*subtrees. Let T*_{1} *and T*_{2} *be the two subtrees, in which u*_{1} *∈ V (T*_{1}*). Let V*_{i}*= V (T** _{i}*)

*and E*

_{i}*= E(T*

_{i}*) for i = 1, 2. Let S*

_{1}

*and S*

_{2}

*denote the sets of sources in V*

_{1}

*and V*

_{2}, respectively.

*Apparently, there are |S*_{1}*| × |V*_{2}*| paths from a source in S*_{1} *to a vertex in V*_{2}. Similarly
*the number of paths from a source in S*_{2} *to a vertex in V*_{1} *is |S*_{2}*| × |V*_{1}*|. By defining the*
*routing load l(T, e) = |S*_{1}*| × |V*_{2}*| + |S*_{2}*| × |V*_{1}*|, we have the following formula.*

*c(T, S) =* ^{X}

*e∈E(T )*

*l(T, e)w(e).* (3)

*The routing cost of an edge e is defined by l(T, e)w(e). The above property shows*
*that the routing cost of T can be calculated by summing up the routing costs of all the*
*tree edges. Based on (3), we can compute the routing cost of a tree in O(n) time when*
the routing costs of all edges are available. All we need to know is the routing load of
*each edge. By rooting T at an arbitrary vertex and traversing the tree in post-order, we*
can easily compute the numbers of sources and the numbers of vertices in both sides of
*every edge. Thus, the routing loads of all edges can be obtained in O(n) time.*

To solve the multiple-sources SERT problem, our algorithm finds the swap cost of
*each edge individually. For each edge e, we calculate the routing cost of T** _{e/f}* for each

*edge f crossing the cut. We shall show that, after a preprocessing stage, the routing cost*

*of T*

*can be computed in constant time.*

_{e/f}*For any edge f ∈ C(e), by (3), we have*

*c(T*_{e/f}*, S) =* ^{X}

*e*^{0}*∈E*1

*l(T*_{e/f}*, e*^{0}*)w(e*^{0}*) + l(T*_{e/f}*, f )w(f ) +* ^{X}

*e*^{0}*∈E*2

*l(T*_{e/f}*, e*^{0}*)w(e*^{0}*),* (4)

*Figure 3: Y*_{v}*is the subtree of Y rooted at a vertex v.*

*in which l(T*_{e/f}*, f ) = l(T, e) since e and f are crossing the same cut.*

*To reduce the computation time, one difficulty is that the routing load of an edge p*
*in a tree T*_{e/f}*is not fixed but depends on f . The following simple observation is crucial*
*for our algorithm. For each edge e*^{0}*∈ E(T ) − {e}, there are only two possible values of*
*l(T*_{e/f}*, e*^{0}*) for all f ∈ C(e), depending on which side of e*^{0}*the swap edge f is connected*
to.

*In the following, we shall give a procedure for computing function γ*_{1}*(e, v) for each*
*v ∈ V*_{1}*, which is defined to be the total routing cost of all edges in E*_{1} subject to that
*the swap edge f is connected to v. By a similar procedure, we can compute function*
*γ*_{2}*(e, v), which is defined to be the total routing cost of all edges in E*_{2} subject to that
*the swap edge f is connected to v. Once the two functions are completed, the routing*
*cost of T*_{e/f}*for any f can be calculated in constant time.*

*Let Y be the tree obtained by rooting T*_{1} *at u*_{1}*. By Y*_{v}*, we denote the subtree of Y*
*rooted at a vertex v. For an edge (u, v) ∈ E(Y ), we assume that u is the parent of v in*
*Y , as shown in Figure 3.*

*Let n*_{v}*and s** _{v}* denote the numbers of vertices and sources, respectively, in the subtree

*Y*_{v}*. For any e*^{0}*= (u, v) ∈ E(Y ), define*

*¯l*_{1}*(e*^{0}*) = (s*_{v}*(n − n*_{v}*) + (|S| − s*_{v}*)n*_{v}*)w(e*^{0}*),* (5)

*which is the routing cost of e*^{0}*if the swap edge is connected to any vertex not in V (Y** _{v}*).

Similarly, define

*¯l*2*(e*^{0}*) = ((s*_{v}*+ |S*_{2}*|)(|V*_{1}*| − n*_{v}*) + (|S*_{1}*| − s*_{v}*)(n*_{v}*+ |V*_{2}*|))w(e*^{0}*),* (6)

*which is the routing cost of e*^{0}*if the swap edge is connected to any vertex in V (Y** _{v}*).

*Then, by definition, the value γ*_{1}*(e, v) can be calculated by*

*γ*_{1}*(e, v) =* ^{X}

*e*^{0}*∈P**/*

*¯l*_{1}*(e** ^{0}*) +

^{X}

*e*^{0}*∈P*

*¯l*_{2}*(e*^{0}*),*

*in which P is the path between v and the root u*_{1}*. Clearly, for the root u*_{1},

*γ*_{1}*(e, u*_{1}) = ^{X}

*e*^{0}*∈E(Y )*

*¯l*1*(e*^{0}*),* (7)

*and, for any child v of a vertex u,*

*γ*_{1}*(e, v) = γ*_{1}*(e, u) − ¯l*_{1}*(u, v) + ¯l*_{2}*(u, v).* (8)

*By (7) and (8), we can compute γ*_{1}*(e, v) for all vertices v in Y by traversing Y twice: a*
*post-order traversal for computing γ*_{1}*(e, u*_{1}), and then a preorder traversal for computing
*γ*_{1}*(e, v) from the value of its parent for every other vertex v. The whole algorithm is as*
follows.

Algorithm Multiple SERT

*Input: A graph G = (V, E, w) and a spanning tree T and the source vertex set S.*

*Output: χ(T, e) for each e ∈ E(T ).*

*1: for each tree edge e = (u*_{1}*, u*_{2}) do

2: *Find the two subtrees T*_{1} *and T*_{2};
3: *Construct Y by rooting T*_{1} *at u*_{1};

4: *Traverse Y in a post-order to find s*_{v}*and n*_{v}*for each v;*

5: *Compute ¯l*_{1}*(e*^{0}*) for each e*^{0}*∈ E(Y ) by (5);*

6: *Compute ¯l*_{2}*(e*^{0}*) for each e*^{0}*∈ E(Y ) by (6);*

7: *Compute γ*_{1}*(e, u*_{1}) =^{P}_{e}*0**∈E(Y )**¯l*_{1}*(e*^{0}*) by traversing Y in a post-order;*

8: *Traverse Y in a preorder to find γ*_{1}*(e, v) by (8) for each v ∈ V*_{1}*− {u*_{1}*};*

9: *Use the steps similar to Steps 3–8 to find γ*_{2}*(e, v) for each v ∈ V*_{2};
10: *for each f = (x, y) ∈ C(e) do*

*c(T*_{e/f}*) ← γ*_{1}*(e, x) + l(T, e)w(f ) + γ*_{2}*(e, y)*
11: *Find χ(T, e) = min*_{f}*c(T** _{e/f}*).

Theorem 11: The multiple-sources SERT problem can be solved by Multiple SERT
*in O(mn) time.*

Proof: The correctness follows directly from the above explanation. It remains to
*show the time complexity of Multiple SERT is O(mn). Clearly, Steps 2–9 can be done*
*in O(n) time. The time complexity of Steps 10 and 11 is O(m). Since there are n − 1*
*edges in the tree, the total time complexity is O(mn).*

### 5 Concluding Remarks

In this paper, we propose algorithms for finding the swap cost of every edge of a multiple-
*sources routing tree. Our algorithms take O(m log n + n*^{2}*) and O(mn) time for the two-*
sources case and the multiple-sources case, respectively. It is easy to see that the swap
edges have been implicitly found by the algorithms.

The algorithms developed in this paper can be easily extended to the case of weighted
*routing cost, in which we are given a nonnegative weight r(v) for each vertex v, and the*
*routing cost of v is defined by r(v)D*_{T}*(v, S). Note that it includes the case that not all*
vertices are destinations by setting zero weight to vertices which are not destinations.

The swap edge problem we defined in this paper is to minimize the total, or equiv- alently the average, length of every path from a source to a destination. We are also interested in the swap edge problem with respect to other criteria [4, 7]. Another inter- esting problem about survivability is to find the most vital edge of a shortest path or a minimum spanning tree [2, 5, 6].

### Acknowledgements

We thank the reviewers for their helpful comments. Bang Ye Wu was supported in part by NSC grants 93-2213-E-366-001 and 93-2213-E-366-014, Taiwan. Chih-Yuan Hsiao and Kun-Mao Chao were supported in part by NSC grants 92-2213-E-002-059 and 93-2213- E-002-029, Taiwan.

### References

[1] A. Di Salvo and G. Proietti, Swapping a failing edge of a shortest paths tree by minimizing the average stretch factor, (SIROCCO’04), LNCS 3104, 99–110, 2004.

[2] B. Dixon, M. Rauch, and R.E. Tarjan, Verification and sensitivity analysis of mini-
*mum spanning trees in linear time, SIAM Journal on Computing, 21(6), 1184–1192,*
1992.

*[3] M. Gr¨otschel, C.L. Monma, and M. Stoer, Design of survivable networks, Handbook*
*in OR and MS, Vol.7, Elsevier, Amsterdam, 617–672, 1995.*

[4] G. F. Italiano and R. Ramaswami, Maintaining spanning trees of small diameter,
*Algorithmica, 22(3), 275–304, 1998.*

[5] K. Iwano and N. Katoh, Efficient algorithms for finding the most vital edge of a
*minimum spanning tree, Information Processing Letters, 48(5) , 211–213, 1993.*

[6] E. Nardelli, G.Proietti, and P. Widmayer, A faster computation of the most vital
*edge of a shortest path, Information Processing Letters, 79(2), 81–85, 2001.*

[7] E. Nardelli, G.Proietti, and P. Widmayer, Swapping a failing edge of a single source
*shortest paths tree is good and fast, Algorithmica, 35(1), 56–74, 2003.*

[8] B.Y. Wu, A polynomial time approximation scheme for the two-source minimum
*routing cost spanning trees, Journal of Algorithms, 44, 359–378, 2002.*

*[9] B.Y. Wu, Approximation algorithms for the optimal p-source communication span-*
*ning tree, Discrete Applied Mathematics, 143, 31–42, 2004.*

*[10] B.Y. Wu and K.-M. Chao, Spanning Trees and Optimization Problems, Chapman*

& Hall / CRC Press, 2004.

[11] B.Y. Wu, K.-M. Chao, and C.Y. Tang, Approximation algorithms for some optimum
*communication spanning tree problems, Discrete Applied Mathematics, 102, 245–*

266, 2000.

[12] B.Y. Wu, G. Lancia, V. Bafna, K.-M. Chao, R. Ravi and C.Y. Tang, A polynomial
*time approximation scheme for minimum routing cost spanning trees, SIAM Journal*
*on Computing, 29, 761–778, 1999.*