Several authors studied the problem of finding a median path [4, 20, 22] in a tree network where each vertex has only one weight. Becker et al. [5] provided an algorithm for a median path with bounded length in weighted tree with time complexity O(n log2n). Tamir et al. [30] gave an O(n logn) algorithm for locating a median path in weighted tree with length constraint. Here, an O(n logn) algorithm for finding a median path of a weighted tree with bounded length is mentioned. The proposed algorithm starts with a decomposition of the tree T into sequence of subtrees. This decomposition depends on the definition of a middle vertex of a tree.
Definition 2
[5,17] Given a weighted tree T, a middle vertex m of T is a vertex which minimizes the maximum of the number of vertices of the subtrees obtained by removing m.
Remark 1
A middle vertex has maximum subtree cardinality less than or equal to \(\frac {n}{2}\). Computing the middle vertex requires O(n) time as presented in [13].
In Fig. 2, there is a tree T consisting of seven vertices. The vertex v4 is considered as the middle vertex of T since removing v4 (see Fig. 3) leads to three subtrees with maximum cardinality \(3 \leq \frac {7}{2}\).
The median path algorithm
Let T=(V,E) be an n-tree. Each vertex v∈V has only one non-negative weight wv and each edge e∈E is assigned to a positive length l(e). In this case, there is only one sum of weighted distances function \(\mathbb {D}\) and it is defined as
$$ \mathbb{D}(P) = \sum_{v \in V} w_{v}d(v,P). $$
(4)
Running a middle decomposition of the tree T leads to sequence of subtrees. For each subtree T′ in this decomposition, the distances from its middle, say m′ to all other vertices in T′ are computed and sorted. This occurs in O(n logn) total time. Let T be rooted at its middle vertex m, and denote by Tm the rooted tree. Let f(v) be the father of v in Tm, Son(v) be the set of sons of v. Each vertex v∈V splits the tree Tm into two subtrees, \(T^{B}_{v}=\left (V^{B}_{v},E^{B}_{v}\right)\) and \(T^{U}_{v}=\left (V^{U}_{v},E^{U}_{v}\right)\), where \(T^{B}_{v}\) denotes the below subtree of Tm rooted at vertex v and \(T^{U}_{v}\) denotes the upper subtree of Tm which was induced by \(\left (V-V^{B}_{v}\right) \cup \{v\}\), that is \(V^{B}_{v} \cup V^{U}_{v} = V\) and \(V^{B}_{v} \cap V^{U}_{v} = \{v\}\) (see Fig. 4). The tree Tm can be decomposed into two subtrees T1=(V1,E1) and T2=(V2,E2) such that V1∪V2=V, V1∩V2={m}, E1∪E2=E, and |V1|=n1, |V2|=n2. Let Son(1,m) and Son(2,m) be the sets of the sons of the middle vertex m in T1 and T2, respectively. Denote by SumB(v) and \(\mathbb {D}_{B}(v)\) the sum of weights of vertices in \(T^{B}_{v}\) and the sum of weighted distances of the vertices in \(T^{B}_{v}\) to the vertex v, respectively, see [5]. By scanning from the leaves to the root, SumB(v) and \(\mathbb {D}_{B}(v)\) are computed in O(n) time as follows:
$$\begin{array}{@{}rcl@{}} \text{Sum}_{B}(v) &=&\left\{ \begin{array}{c} w_{v}~\text{if}~v~\mathrm{is~a~leaf~in}~T_{m,} \\ w_{v}+\sum\limits_{u\in \text{Son}(v)}~\text{Sum}_{B}(u)~\mathrm{otherwise.} \end{array} \right. \\ \end{array} $$
(5)
$$\begin{array}{@{}rcl@{}} \mathbb{D}_{B}(v) &=&\left\{ \begin{array}{c} 0~\text{if}~v~\mathrm{is~a~leaf~in}~T_{m,}\\ \sum\limits_{u\in \text{Son}(v)} \mathbb{D}_{B}(u)~+~\text{Sum}_{B}(u)l(v,u)~\mathrm{otherwise.} \end{array} \right. \end{array} $$
(6)
The following quantities will be needed:
$$ \mathbb{D}^{r}(m)=\sum_{u\in \text{Son}(r,m)} \mathbb{D}_{B}(u)+\text{Sum}_{B}(u)l(m,u),\mathrm{ \ \ }r=1,2. $$
(7)
Definition 3
[5] Given two paths \(P_{v_{i},v_{j}}\) and \(P_{v_{j},v_{k}}\) with edge disjoint, the distance saving of \(P_{v_{j},v_{k}}\) with respect to \(P_{v_{i},v_{j}}\), is the reduction of the sum of weighted distances obtained by adding \(P_{v_{j},v_{k}}\) to \(P_{v_{i},v_{j}}\), that is,
$$ \text{sav}(P_{v_{i},v_{j}},P_{v_{j},v_{k}}) = \mathbb{D}(P_{v_{i},v_{j}}) - \mathbb{D}(P_{v_{i},v_{k}}). $$
(8)
Remark 2
If the first path consists of only one vertex vi, write \(\phantom {\dot {i}\!}sav(v_{i},P_{v_{i},v_{k}})\) instead of \(sav(P_{v_{i},v_{i}},P_{v_{i},v_{k}})\). In linear time, \(\phantom {\dot {i}\!}sav(v_{i},P_{v_{i},v_{k}})\) is computed as follows:
$$ \text{sav}(v_{i},P_{v_{i},v_{k}})=\left\{ \begin{array}{l} 0\qquad\text{if}\ v_{k}=v_{i}\mathrm{,} \\ \text{sav}(v_{i},P_{v_{i},f(v_{k})})+\text{Sum}_{B}(v_{k})l(v_{k},f(v_{k}))\mathrm{otherwise.} \end{array} \right. $$
(9)
Also, calculate ∀vk∈Vr, r=1,2, the following quantities in linear time.
$$ \mathbb{D}(r,v_{k}) = \mathbb{D}^{r}(m)-\text{sav}(m,P_{m,v_{k}}),\mathrm{ \ \ }v_{k}\in T_{r}. $$
(10)
Note that \(\mathbb {D}(r,v_{k})\) is the sum of weighted distances of all vertices in Tr from the path \(P_{m,v_{k}}\). Let \(\mathcal {V}^{1}=(v_{i(1)},v_{i(2)},...,v_{i(n_{1})})\) and \(\mathcal {V}^{2}=(v_{q(1)}, v_{q(2)},..., v_{q(n_{2})})\) be the non-decreasing ordering of the vertices in V1 and V2 by their distances from the middle vertex m respectively. The best paths of length at most l which are included in T1∖{m} and T2∖{m} may be found by computing for each vertex \(v_{k}\in \mathcal {V}^{1}\), the best path of length bounded above by l which has vk as an endpoint. Starting with \(v_{i(n_{1})}\), if \(d(m,v_{i(n_{1})})>l \), there is no such path. Otherwise, find the largest index t=t(n1) such that \(d(v_{i(n_{1})},v_{q(t)})\leq l \) and \(d(v_{i(n_{1})},v_{q(t+1)})>l \). Let vQ, \(Q\in \mathcal {Q}=\{q(1),q(2),...,q(t(n_{1}))\}\) be a vertex which gives \(\min \limits _{s=1,...,t(n_{1})}\mathbb {D}(2,v_{q(s)})\) and put
$$ \alpha_{i(n_{1})} = \mathbb{D}(1,v_{i(n_{1})}) + \mathbb{D}(2,v_{Q}), $$
(11)
where \(\alpha _{i(n_{1})}\) is the value of the sum of the weighted distances of the best path which has \(v_{i(n_{1})}\) as an endpoint. Continuing on with \(v_{i(n_{1}-1)},...,v_{i(1)}\), in linear time all terms αi(r),r=n1,...,1 are evaluated. The value of the median function of the best path of length at most l which passes through the middle vertex m and contains vertices in both V1−{m} and V2−{m} is given by \(\min \limits _{r=n_{1},...,1}\alpha _{i(r)}\).
Suppose that \(v_{I}, I\in \mathcal {I}=\{i(1),i(2),...,i(n_{1})\}\) is a vertex where \(\min \limits _{r = n_{1},..., 1}\alpha _{i(r)}\) takes place therefore the path \(P_{v_{I},v_{Q}}\) is passing through the middle vertex m and minimizing the median function with \(\mathbb {D}(P_{v_{I},v_{Q}}) = \min \limits _{r = n_{1},..., 1}\alpha _{i(r)}\).
To find the best path in T1, replace the weight of m by \(\sum \limits _{v\in V_{2}}w_{v}\), delete all vertices in V2−{m} from T, and solve the problem in the resulting subtree T1 and add the constant \(\mathbb {D}^{2}(m)=\sum \limits _{v_{t}\in V^{2}}w_{t}d(v_{t},m)\) to the value of the median function. By similar technique, the best path in T2 can be located. For simplicity, define i′ as (to be used later):
$$ i^{\prime }=\left\{ \begin{array}{c} 1\mathrm{ \ \ if }\ i=2, \\ 2\mathrm{ \ \ if }\ i=1. \end{array} \right. $$
(12)
Theorem 1
(See [30].) The time complexity of median path algorithm is O(n logn).
The k-best median path in a tree
It is known that \(\left \vert \mathcal {P}\right \vert \leq \frac {n}{2}(n+1)\). The k-best median path \(P\in \mathcal {P}\) means the k-path which minimizes the median function with \(k=1,2,...,\frac {n}{2}(n+1)\). The algorithm presented in the previous subsection finds the first best median path, but it is sometimes needed to locate the second, the third,..., etc., best median paths of the tree T. Therefore, this algorithm is modified. For the sake of simplicity, it is assumed that the best median path \(P^{\ast }=P_{v_{I},v_{Q}}\) with endpoints vI∈T1 and vQ∈T2. To compute the second best path in T, the first median path \(P^{\ast }=P_{v_{I},v_{Q}}\) is excluded and the second median path is located by updating the quantities αi(j) in linear time as follows:
$$ \alpha_{i(j)} = \left\{ \begin{array}{c} \mathbb{D}(1,v_{i(j)})+\min\limits_{q(s)\in \mathcal{Q}-\{Q\}} \mathbb{D}(2,v_{q(s)})\textrm{ \ \ if }v_{i(j)}=v_{I}, \\ \alpha_{i(j)}\textrm{ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ otherwise.} \end{array} \right. $$
(13)