4.1. Definition of Parameters
The parameters “”, d, d, “msdsn”, “msdfa”, “”, “d”, “k(d)”, “k(d)”, “(fa)”, “”, “” and , will be defined in the following.
Definition 5. Let be the number of the vertices in the subtree , rooted at v, the is computed as follows for each , then.where Sd is the set of leaves, i.e, Sd is the set of the vertices in the tree of degree 1 and m is the number of the vertices in the subtree that are descendants of the vertex v. Definition 6. Let be a set of the vertices in the subtree and be a set of the edges in the subtree , and be the sum of the distance from the vertex sn to all vertices in the subtree which it is called “distancesum” [25]. Definition 7. Let be a set of vertices in the subtree , be the set of the edges in the subtree and be the sum of the distances form the vertex fa to all vertices in the subtree , and it is called “distancesum” [25]. The “distancesum” is defined by Dijkstra’s algorithm solves the single-source shortest-paths problem on a weighted, directed graph
for the case in which all edge weights are non-negative [
26].
The strategy of Dijkstra’s algorithm depends on visiting all reachable vertices from the source. Then we adapt Dijkstra’s algorithm for calculating the values of the
, d
and d
, which are defined by Formulas (
6)–(
8).
Consequently, Algorithms 2 and 3 are designed to calculate the values of the
, d
and d
in the two subtrees
and
, respectively.
Algorithm 2 Adaptation and modification of Dijkstra’s algorithm to compute and d |
Input: , , and fath. Output:
- 1:
for each ; , set sn=fs(2). do - 2:
delete the edge fs from to create the subtree . - 3:
end for - 4:
for each do - 5:
. - 6:
. - 7:
set is defined in ( 9). - 8:
end for - 9:
we find for the vertex u in that represents the sn and we set and . - 10:
, the sum of the distances from the vertex “sn” to all reachable vertices in . - 11:
, counter of the vertices that are accessible in . - 12:
while
do - 13:
, select the element of with the min distance (d). - 14:
. - 15:
- 16:
for each do - 17:
. - 18:
if then - 19:
, relaxation step. - 20:
. - 21:
end if - 22:
end for - 23:
, the vertex u has been visited. - 24:
deleted the vertex u with its distance (d) from . - 25:
replace the old value of the d by the new value into the for each . - 26:
end while - 27:
set d. - 28:
set and . - 29:
store the values of d and into - 30:
return
|
Algorithm 3 Adaptation and modification of Dijkstra’s algorithm to compute and d |
Input:
, , and fath Output:
- 1:
for each ; , set sn=fs(2). do - 2:
delete the edge fs from to create the subtree . - 3:
end for - 4:
for each do - 5:
. - 6:
. - 7:
set is defined in ( 9). - 8:
end for - 9:
we find for the vertex u in that represents the “fa” and we set and . - 10:
, the sum of the distances from the vertex “fa” to all reachable vertices in . - 11:
, counter of the vertices that are accessible in . - 12:
while
do - 13:
, select the element of with the min. distance (d). - 14:
. - 15:
- 16:
for each do - 17:
. - 18:
if then - 19:
, relaxation step. - 20:
. - 21:
end if - 22:
end for - 23:
, the vertex u has been visited. - 24:
deleted the vertex u with its distance (d) from . - 25:
replace the old value of the d by the new value into the for each . - 26:
end while - 27:
set d. - 28:
set and . - 29:
store the values of d and into - 30:
return
|
In both Algorithms 2 and 3, the process mechanism of selecting a vertex u depends on the distance d of a current vertex to the source. In our case, the source is the vertex “sn” or the vertex “fa”.
We organize this process into three rows at the priority queue
in Matrix (
9) and
in Matrix (
10). The two matrices
and
are used in Algorithms 2 and 3, respectively. We store all vertices of the two subtrees
and
in the first row of the
in (
9) and the
in (
10), respectively. We list the sum of the distances from each vertex to source in the second row of both Matrices (
9) and (
10).
Similarly, the third row of Matrices (
9) and (
10) contains the color of the current vertex, i.e, a current vertex is the vertex that we want to process; it is the vertex
u that we will extract from the first row of the
and
according to the value of the distance d
.
At the beginning, we set d and for all vertices in the subtrees and . These procedures are implemented by Lines 4–8 in both algorithms.
In Line 9 of Algorithm 2, we find the vertex
u in Q represents the source “sn” and we set
sn and d
. Similarly, in Line 9 of Algorithm 3, we find the vertex
u in Q represents the source “fa” and we set
fa and d
.
Definition 8. Let msdsn be the maximum distance saving of a path in the subtree starting from the vertex “sn”.
To compute the values of the msdsn, we use two different types of recursive principles in their operation. See [14,21,24,25,27,28]. In the following scenario, we explain how the values of the “msdsn” are calculated.
For each edge
, where “sn” is the root of the subtree
as we have illustrated in
Section 3 and
Figure 2.
Let be the set of the sons of the vertex sn, where the is the number of the elements in the set .
We set
and
, where the
is the
son for the vertex sn. We compute the values of the “msdsn” using the following recursive formulas.
where the
is the path connecting sn and w
with length
and maximum distance saving.
If deg(sn)
, calculated a path with length
and the second maximum distance saving using as follows.
and
, where
,
and the vertices
and
belong to the subtree
.
The value of the
represents the largest distance saved by the path
calculated by (
11).
Let be the first edge in the path , where .
The value of the represents the second largest distance saved by the path .
Let be the first edge in the path , where .
Hence, the two paths and of the length , represent the paths with the largest and the second largest distance saving in the subtree rooted at the vertex sn.
Note 1: The edge
and the value of the
will be used in the first branch of Formula (
16). The computations in Formula (
11) are repeated (n-1) times at
.
Note 2: When , i.e, , then , i.e, , also, if , then .
In the following example, we show how the above scenario, Formula (
11), and Algorithm 4, are used.
Algorithm 4 For finding the maximum distance saving “msdsn” in the subtree |
Input: fath, n and . Output: MsdSn. - 1:
for each fs∈ fath, do - 2:
set sn=fs(2), is the set of sons of sn. - 3:
calculate which is the number of elements in the set . - 4:
set . - 5:
if then - 6:
set . - 7:
else - 8:
for each and do - 9:
go to Formulas ( 11) and ( 12) to calculate the and respectively. - 10:
end for - 11:
if then - 12:
set - 13:
end if - 14:
end if - 15:
end for - 16:
return .
|
Given the tree
T in
Figure 4, which is rooted at a vertex b, by deleting the edge
the tree
is divided into two subtrees, in this case, the first one is the subtree
,
, and then
, i.e,
,
,
,
, are the sons of the vertex H and
.
∵, , , , , , , therefore, , hence , , and , where and .
The rest of the results of the
for the other edges in the tree
are listed in
Table 1 below. Column 1 in
Table 1,
Table 2,
Table 3,
Table 4,
Table 5,
Table 6,
Table 7 and
Table 8 gives the edge “fs” under processing. Column 2 of
Table 1 denoted by
presents the values of the
that obtained from Formula (
6) using Algorithm 2. Columns 3 and 4 denoted by
and
include the first and the second largest distance saving obtained by Formulas (
11) and (
12), respectively. The edge
is listed in Column 5 of
Table 1. Columns 6–9 in
Table 1 are similar to Columns 3–4. The values of the
and
of the edge fs
are stored into the matrix MsdSn that is defined by the following.
Definition 9. Let msdfa be the maximum distance saving of a path of length ; in the subtree .
To compute the maximum distance saving “msdfa” in the subtree , we designate two cases as follows.
Case 1: For any edge
of a family matrix “fath”, if
, then the values of the
can be calculated by Formula (
15) below. Since the root
r has no father “fa”, i.e, it has no predecessors, but it has one or more sons. So, we cannot use our approach that we have presented in Algorithm 1 which used to create the subtree
of
for computing the “msdfa”, so, instead of that, we present the following approach to compute the values of the “msdfa” regorging the root
r with its sons and descendants to the leaves of a tree network. This case represents a special case for calculating the values of the
.
Let
be the set of the sons of the root
r,
is the number of the elements in the set
, we set
and
is the set of edges linked directly to the root.
where
, the values of the
are calculated by Formula (
11) and
.
If , and fs , then set . Otherwise, , then find for each edge with the root r.
Let be the maximum distance saving obtained by attaching a path of the length through the edge in the tree rooted at r. Also, let be the second maximum distance saving obtained by attaching a path of the length through one of the edges of in the tree rooted at r.
Therefore, if
, the values of the
can be computed as follows.
For more clarification on how Formulas (
14) and (
15) can be applied, we use the tree
in
Figure 4 as follows.
Suppose we are at the vertex
on the tree network of
Figure 4, and we want to compute the value of the
as we have shown above.
Since
,
, this means the
,
,
,
,
and
. Accordingly, we have
Because at , , , and , therefore, .
Hence, , , , .
Therefore, for and and for at .
Similarly, the rest of the results are obtained from the root “b” to the end of the vertices in the tree
listed in
Table 2. The above approach finds two paths of the length
of maximum and second maximum distance saving, which are attached at the root node
r.
Case 2: For all
,
and if
, the “msdfa” is computed by the following.
First of all, we go to Algorithm 1 for creating the subtree
. Let gf be a father of the vertex fa we set g=(gf, fa) and
, and we compute the values of the “msdfa” by Formula (
16).
where the value of the
is calculated by the following equation:
where
is the number of vertices of the tree
,
is the number of vertices in the subtree
, it is calculated by Formula (
6) and the value of the
is computed by Formula (
12).
For example, assume we choose the edge
of the tree network in
Figure 4, as we have done above where
and
. So, the subtree
is created by Algorithm 1.
We set as the father of the vertex W, therefore, .
Now, we compare
with the edges
that are listed in Column 5 of
Table 1, i.e, the edges
that we have obtained from Formula (
11), when we computed the value of the
, it is obtained by the edge
.
Since,
is listed in Line 6 of Column 5 in
Table 1 at
, where
is the edge that corresponds the value of the
and
.
Therefore, we apply the first branch of Formula (
16) as follows:
,
,
,
,
.
Therefore, at .
The rest of the results are in
Table 2 below. Columns
contain the values of the
, with
, that we have obtained from Formulas (
15) and (
16) and Algorithms 5 and 6.
The results of the “msdfa” that we have obtained from Cases 1 and 2 of Definition 9 are stored in the matrix MsdFa in the following.
Algorithm 5 For finding the maximum distance saving in the subtree |
Input: “fath” and n. Output:
, if fs and if fs . - 1:
Let be the set of sons of the root r. - 2:
compute is the number of elements in the set . - 3:
set where . - 4:
ifthen - 5:
set . - 6:
else - 7:
for each and do - 8:
go to Formula ( 15) to compute the values of the and . - 9:
end for - 10:
end if - 11:
return , if fs and if fs .
|
Algorithm 6 For finding the maximum distance saving “msdfa” in subtree |
Input: fath, n, and . Output: MsdFa. - 1:
for each fs ∈ fath, . do - 2:
set and go to Algorithm 1 to create the subtree . - 3:
if then - 4:
return to Formula ( 16) to compute the value of the . - 5:
else - 6:
go to Algorithm 5 to compute the value of the msdfa, where fa. - 7:
end if - 8:
end for - 9:
return
|
Definition 10. Suppose, the best path in the subtree is defined by the following.
where
,
, the edge
is incident to sn contained in the path that gives the value of
by Formula (
11), and then the sum of the distance form the path
to all vertices in the subtree
is defined by the following equation.
where
, for example, when
,
, then the
, where the term
is the sum of the distances from the root “sn” of the subtree
to all vertices in the subtree
, it is defined by (
7).
This means the beginning of the targeted path is attached to the head of the edge .
Hence, Formula (
11) is used to find the maximum distance saving obtained by attaching a path of length
to the vertex sn. For example, when the edge
is deleted from the tree network
of
Figure 1, we obtain two subtrees; the first one is the subtree
. Hence, the number of adjacent edges of the vertex
are three edges, the optimal edge of them is the edge
. See
Figure 2. The edge
is obtained by Formula (
11), in this case, when
and
, so the targeted path is
, hence
. Therefore, the sum of the distance from the path
to all vertices in the subtree
equals to
.
Similarly, at and , , then the “distancesum” of the path equals to the .
For further illustration, we use the tree network depicted in
Figure 4 as follows.
Assume we pick the edge
and
, then
and
, hence d
. Now, when
and
, we have to return to Formula (
11) for computing the value of the
, and this result is obtained by the edge
, and then
. Hence,
, similarly, when
and
, therefore,
, this result has been obtained through the path
, finally when
and
, therefore,
, there is no edge that gives this result. It means the calculation on the subtree
has finished.
The rest of the results are presented in
Table 3. Column 2 of
Table 3 denoted by d
contains a sequence of the sum of the distances from the vertex “sn” to all vertices in the subtree
, which are computed by Formula (
7) using Algorithm 2, Columns
of
Table 3 show the values of the d
that we have obtained from Formula (
20).
Definition 11. The best path in the subtree can be defined as follows.
In Definition 9, we have used two cases as follows.
According to Formula (14) in Case 1, when , the best edge of the adjacent edges, which are connected to the root r, is the edge . In this case, the best path in the subtree. is defined by the following.
Now, when and , the targeted path is , hence .
For example, in
Figure 1, when we want to compute the value of the
for the edge
, we delete the edge
of the tree
, and then we obtain the subtree
. At
and
, the value of the
is calculated by Formula (
14), then
, is obtained by the edge
, hence
.
In Case 2 of Definition 9, when
, the values of the
are computed by Formula (
16), therefore, the best edge is an edge of the predecessor or descendant that is associated with the vertex “fa” without the edge
that we have deleted from the tree
.
Note 1: If the condition in the first branch of Formula (
16) satisfies and
, the value of the
equals to
.
Note 2: If the condition in the first branch of Formula (
16) satisfies and
, this means that the value of the
equals to
. Accordingly, in Case 2, the best path in the subtree
is defined by the following.
where the edge
is connected directly to the deleted edge fs = (fa, sn) and the
and
are the first and second best edges connected to the deleted edge fs = (fa, sn) according to (
11) and (
12), respectively.
For example, in
Figure 1, when we compute the value of the
for the edge
, we delete the edge
of the tree
, and then we obtain the two subtrees
and
.
We return to Formula (
11) to compute
, at
, the
is obtained by the edge
and
is obtained by the edge
.
Now, when we want to calculate the value of the
for the edge
. Firstly, we delete edge
of the tree
, and then we obtain two subtrees, which are
and
. According to Formula (
16),
, we note that the
, then we use the first branch of Formula (
16). At
,
, we compute the value of the
, by Formula (
17),
, i.e,
, therefore, we apply the second branch of Formula (
22) to specify the best path in the subtree
, and the best path is
.
Therefore, the sum of the distance form the path
to all vertices in the subtree
is defined in the following equation.
where
, for example, when
,
, then the
. The term
is the sum of the distances from the root “fa” of the subtree
to all vertices in the subtree
, according to (
8).
This means that the beginning of the targeted path is sitting at the vertex “fa” of the edge , i.e., the best edge is either the predecessor of the vertex “fa” or the descendant of the vertex “fa”.
Hence, Formula (
16) will drive the vertex “fa” to the best edge of the subtree
.
Therefore, the sum of the distance form the path
to all vertices in the subtree
equals to
, where the value of the
is constant for the edge “fs” at
, i.e., it represents the initial value of Formula (
23) and the value of the
is calculated by Formula (
16). For more illustration, we use the example that we present in
Figure 4 by the following.
Assume we pick the edge
and
, then
, hence d
, and this value has been computed by Algorithm 3. Now, when
and
, d
, where the value of the
is calculated by Formula (
16), hence this result has been obtained through the edge g
, and
. Similarly, at
and
, d
, this result has been obtained through the edge g
, and
, at
and
, d
, and this result has been obtained through the edge g
, and
, at
and
, d
, and this result has been obtained through the edge g
, and
, where
. Finally, when
and
, therefore,
, there is no edge that gives this result. It means the calculation on the subtree
has finished.
The rest of the results are presented in
Table 4. Column 1 of
Table 4 is labeled by d
, which contains a sequence of the sum of the distances from the vertex “fa” to all vertices in the subtree
, which are computed by Formula (
8) using Algorithm 3, Columns
in
Table 4 show the values of the d
that we have obtained from Formula (
23).
Definition 12. Let be used to compare the “distancesum” of all paths with in the subtrees for the the edge “fs” of length , i.e., the value of the represents the “distancesum” of a best path of length in the subtrees , that starts from the vertex “sn” to the farthest leaf on the subtree . See [21,25]. The is defined by the following. Definition 13. Let be used to keep the “distancesum” at a minimum value of the in the subtree for the the edge “fs” at length .
In other words, the value of the is the “distancesum” of a best path of length , that begins from the vertex “fa” to the farthest leaf on the subtree .
The is defined by Note 1: In the following, we show how Formulas (
24) and (
25) are used.
Given,
on the tree network of
Figure 4, where sn
, fa
, because the value of the
equals to 28 at
, therefore,
, by using the first branch of Formula (
24).
Now, when , at the same edge , , hence , similarly, , then .
Finally, at , , therefore, .
Similarly, for the same edge
regorging to the subtree
, we use Formula (
25), then
,
,
,
, finally, at
,
.
The rest of the results of the
and
are presented in
Table 5 and
Table 6, respectively. Column 1 in both
Table 5 and
Table 6 is labeled by
and
contains the sum of the distance form both vertices “sn” and “fa” to the all vertices in the subtrees
and
that have been obtained by Formulas (
24) and (
25). The other columns in both tables give the values of the
and
, which represent the “distancesum” from a path
to all vertices in the two subtrees
and
, with the length
, respectively.
In the following we present a mechanism to specify the optimal path on the whole tree.
Definition 14. Let ψ and μ be two functions by which the best path in the two subtrees and is combined by picking the best edge of both to obtain a composite path that represents the median path (ℓ-core) for a tree network with the length ℓ.
These two functions are defined byandwhere , the denotes the sum of the distance from a candidate path to all vertices in the subtree and denotes the sum of the distance from a candidate path to all vertices in the subtree . For example, at the tree network depicted in
Figure 4, if
and the edged is fs
, then
, for fs
,
, for fs
,
, for fs
,
and for fs
,
.
The five values of the , denote the sum of the distances from each edge of to all vertices in the tree , e.g., the , denotes the sum of the distances from the edge fs to all vertices in the tree .
Similarly, there are the five values of the .
According to Problem (
1) that describes the optimal path, we find the minimum distance among the set values of the
and
.
We note that the minimum value is 97, that corresponds to either the edge or , so the best path with length is either or . Now, we need to create a path from the both functions and that will represent the optimal path in the tree .
In general, the optimal path
is selected according to the following formulas:
where
,
, i.e ,
.
Formula (
28) means that the fs edge, which gives the minimum value of the function
, will be added to the the optimal path defined by Formula (
29).
For example, at , for each fs on the tree , we obtain .
Hence, the value of the , is the minimum sum of the distance from the edge “fs” to all vertices on the tree .
Therefore, the optimal path is , where the is the initial (empty) path.
Similarly, when , for each fs on the tree we obtain .
Hence, the value of the represents the minimum sum of the distance form the path to all vertices on the tree . Therefore, the optimal path is , and so on up to the required length, i.e., .
In general,
, the value of the
represents the minimum sum of the distances from the path
to all vertices on a tree
network. For further illustration, see
Table 7,
Table 8 and
Table 9, regorging the tree
in
Figure 4.