Linear Algorithms for the Hosoya Index and Hosoya Matrix of a Tree

The Hosoya index of a graph is defined as the total number of its independent edge sets. This index is an important example of topological indices, a molecular-graph based structure descriptor that is of significant interest in combinatorial chemistry. The Hosoya index inspires the introduction of a matrix associated with a molecular acyclic graph called the Hosoya matrix. We propose a simple linear-time algorithm, which does not require pre-processing, to compute the Hosoya index of an arbitrary tree. A similar approach allows us to show that the Hosoya matrix can be computed in constant time per entry of the matrix.


Introduction
In Haruo Hosoya's seminal paper, a molecular-graph based structure descriptor is proposed that nowadays is known under the name Hosoya index [1]. It is well-known that several physicochemical properties of chemical structures are well correlated with the Hosoya index of the corresponding molecular graphs. In particular, it is used to predict, from the structure of molecules, some of their physico-chemical properties such as boiling points, entropies, heat of vaporization, and π-electron energy [1,2].
The mathematical investigations on the Hosoya index are often related to trees, see for example [3][4][5][6][7][8]. Note also that there is a strong connection between the energy of a tree and its Hosoya index [9]. It is established that the general problem of determining the Hosoya index is #P-complete even for planar graphs [10]. On the other hand, if the graph is known to be a tree, there are efficient solutions.
In [11], Zhang and co-authors proposed an algorithm that allows computation of the Hosoya index of a tree in linear time. The algorithm uses a relatively complex preprocessing which for a given tree computes the Prüfer code of the corresponding labeled tree. As noted already by the authors, another troublesome aspect of the proposed algorithm is its necessity of computation with fractions. Moreover, it was shown in [12] that the values of the characteristic polynomial of a tree can be computed in linear time, which implies that a computation of the Hosoya index of a tree can be done within the same time-bound; however, an explicit algorithm has not been presented.
It has been demonstrated that the Hosoya index can be computed efficiently for some graphs derived from trees. In particular, for an arbitrary tree T, a closed-form expression is presented for the Hosoya index of the fractal graphs R(T) and RT(T) [13].
Since a graph is entirely determined by specifying either its adjacency structure or its incidence structure, it is natural to state the specification of a graph in matrix form. By associating a matrix with a graph, one can also use selected graph invariants as molecular descriptors. This approach was initiated in a work by Randić, where a matrix based on another well-known molecular-graph structure descriptor, the Wiener index, is proposed [14]. Later, Randić used an analogous approach, based on the Hosoya index, for acyclic systems, which yields the concept of the Hosoya matrix [15].
The paper is organized as follows. In the next section, we give some definitions and concepts needed in this paper. In Section 3, we follow the idea presented in [12] to give a recursive expression for the Hosoya index of a tree. Using this result, we present a linear-time algorithm for computing the Hosoya index of a graph of this class. In Section 4, we apply the previous section's approach to provide a recursive formula for an entry of the Hosoya matrix. The results allow the computation of the Hosoya matrix in constant time per entry of the matrix.

Preliminaries
Let G = (V, E) be a graph, possessing n vertices and m edges. The set of edges X ⊆ E(G) is called a matching of G if any two edges of X have no vertex in common. A matching of G with k edges is called a k-matching of G.
A subset of vertices I ⊆ V(G) is independent if no two vertices of I are adjacent. The number of distinct k-element independent vertex sets is denoted by n(G, k). Note that n(G, 0) = 1 and n(G, n) = n.
The Hosoya index of a graph G, denoted by Z(G), is defined as the total number of matchings of G. Let m(G, k) be the number of k-matchings of G and set m(G, 0) := 1. The Hosoya index can be formulated as A tree is a connected graph without cycles. If u, v are vertices of a tree T, then T admits exactly one path between u and v. We shall denote this path by P uv .
Let T be a tree with the vertex set V(T) = {1, 2, . . . , n}. The Hosoya matrix of a tree T is the n × n matrix H(T), where (i, j)-entry of H(T) is the Hosoya index of the graph obtained from T by removing the edges of the path P ij [15]. More formally, if i, j ∈ {1, 2, . . . , n}, then Let v be a vertex of a graph G. Then N G (v) or simply N(v) denote the set of vertices adjacent to v in G.
A rooted tree is a tree in which one vertex is distinguished from the others and called the root. If r is a root of a tree T, then T r denotes the corresponding rooted tree. Let v = r be a vertex of T r . As already stated, P rv denotes the path between r and v in T r . If u is a vertex of P rv adjacent to v, then u is a parent of v and v is a child of u. If u is a parent of v and w parent of u, then w is a grandparent of v. Note that every vertex of a rooted tree admits at most one parent and grandparent. A vertex z is a descendant of v if v is in the path from z to r. A vertex v of a tree T, and all its descendants induce a subgraph of T r denoted by T r v . It is clear that T r v is a tree. Note that T r v can be seen as a rooted subtree of T r with the root v.
If v is a vertex of T r , then C r (v) denotes the set of children of v.

Recursive Formula for Computing the Hosoya Index
The results of this subsection are obtained by using an approach similar to the work of Mohar where the characteristic polynomial of a tree is studied [12].
The following three results are well-known [1,16].

Proposition 2.
Let G be a graph composed of two disjoint components G 1 and G 2 . Then

Proposition 3.
Let v be a vertex of a graph G. Then We also need the following Proposition 4. Let v and u be vertices of a tree T. Then (i) Proof. (i) If T is a single vertex, the claim immediately follows. Otherwise, note that . Proposition 2 now yields the assertion.
(ii) We can see that T − {v, u} is composed of two sets of connected components: one corresponds to components of T − v with the exception of T v u , while the other corresponds to T v u − u. Let us denote the first set of connected components by ( .

From Proposition 2 now it follows that
. From Proposition 2 and (i) then it follows . This assertion concludes the proof.

Theorem 1.
Let v and r be (not necessarily distinct) vertices of a tree T. Then Proof.
If v is a leaf, then T r v is a single vertex and the case is settled.
. Proposition 4 now completes the proof.

Algorithm for Computing the Hosoya Index
Let v be a vertex of a nonempty tree T. The data structure of the rooted tree T v is given by Algorithm 1, where C v and p v represent the list of children of v and the parent of v in T v , respectively.
Let v be a vertex of T and w ∈ N(v). If N(w) is represented by a (doubly connected) adjacency list, then we may remove v from N(w) in constant time. It is easy to see now that the time complexity of Rooted is linear.
Algorithm 2 computes the Hosoya index Z v of a tree T with respect to a root v. Before the algorithm is applied, the algorithm Rooted is called for the vertex v. Thus, for every vertex u in T v , the rooted tree is represented by the list of children C u .
Proof. The validity of the algorithm follows from Theorem 1. The algorithm computes vectors Z andZ, such that the entry Z v (resp.Z v ) represents Z(T) (resp. Z(T − v)). The algorithm also maintains the variable zu, which for u ∈ C v (u) represents the value of ∏ If v is a leaf in T, then |C v | = 0 and Z v is set to one. Otherwise, the algorithm is recursively called for each child of v in Step 3. Note that the call of the algorithm for a vertex w computes the Hosoya index of the corresponding subtree T v w . Thus, after Step 3 is executed, the Hosoya index is already computed for all subtrees of v. Next, ∏ is computed in Step 4. This gives the initial value of Z v assigned in Step 5, while the sum is computed in Step 6. Steps 6.1 and 6.2 compute the value of . If u is a leaf, then |C u | = 0 and the value of zu remains equal to one. Otherwise, the value of zu is equal to ∏ x∈C v (u) Z(T v x ) after Step 6.2 is executed. In Step 6.3, the value of zu is multiplied byZ v and divided by Z u ; thus, the value added to Z v in this step is equal to . This assertion completes the proof of the correctness of the algorithm. For the time complexity first note that the time complexity of a single recursive call neglecting recursive calls (in Step 3) and neglecting the Steps 4 and 6 is clearly constant. Since the number of recursive calls of the algorithm equals the number of vertices of the tree, we have to show that the total number of operations executed in Steps 4 and 6 is linear in the number of vertices. This number can bounded above by the number of entries of the vector Z used by the algorithm. In Step 4, the values of Z w , w ∈ C v are used, while in Step 6.2.1, for u ∈ C v , the entries Z x , x ∈ C u are used. Since every vertex v of a rooted tree admits at most one parent and grandparent, the corresponding entry Z v is used at most three times: twice when Hosoya is called for the parent of v (Steps 4 and 6.3) and once when Hosoya is called for the grandparent of v (Step 6.2.1).
It follows that the total number of operations executed in Steps 4 and 6 is bounded above with some constant multiplied by the number of vertices of T.
Since the space complexity of the algorithm is clearly linear, the proof is complete.
Note that the parameter p of the algorithm Rooted as well as the parameterZ of the algorithm Hosoya are not used for the computation of the Hosoya index. These parameters will be needed in the next section.

Example
As an example of the execution of the algorithm Hosoya observe the three T with the vertex set {1, 2, . . . , 11} depicted in Figure 1. We demonstrate the algorithm for the root 4. Note that before the algorithm is applied, the algorithm Rooted is called for the vertex 4.
Since 4 is not a leaf, the algorithm is recursively called for the vertices 3, 8, and 11 (the neighbors of 4) in Step 3. The recursion stops when a leaf is reached and the corresponding entry of the vector Z is set to one. We therefore obtain Z 1 = Z 5 = Z 6 = Z 7 = Z 9 = Z 10 = 1. For the vertex 2 we then obtainZ 2 = Z 1 · Z 5 = 1 · 1 = 1 in Step 4. Since C 1 and C 5 are both empty, Step 6.2.1 is never executed and zu remains equal to one. The final value Z 2 = 3 is therefore obtained in Step 6.3, where for the vertices 1 and 5 the value one is added to the current value of Z 2 . Analogously, for the vertex 3 we obtainZ 3 = Z 2 · Z 6 = 3 · 1 = 3 in Step 4. Since C 2 = {1, 5}, we have zu = Z 1 · Z 5 = 1 · 1 = 1 for u = 2 and zu = 1 for u = 6 in Step 6.2. Finally, for u = 2 and u = 6 we add zu ·Z 3 /Z 2 = 1 · 3/3 = 1 and zu ·Z 3 /Z 6 = 1 · 3/3 = 3 to Z 3 in Step 6.3. These operations give Z 3 = 7. The other values are obtained in an analogous manner. Thus, we obtain Z 8 = 2, Z 11 = 3, and Z 4 = 95. The Hosoya index of a (sub)tree T 4 v (resp. the value of Z(T 4 v − v)) is shown at the upper left-hand side (resp. the right hand side) of the vertex v in Figure 1.
An intuitive explanation of the algorithm Hosoya for a tree T rooted at r is as follows.

1.
All the entries of the vector Z that correspond to leaves of T r are set to one.

2.
If v is a vertex of T r such that the Hosoya indices are already computed for all descendants of v, the computation of Z v goes as follows: Consider again the rooted tree T 4 in Figure 1 and suppose that the Hosoya indices are already computed for all descendants of 4. For Step a., we obtain the initial value of Z 4 , which equals Z 3 · Z 8 · Z 11 = 7 · 2 · 3 = 42. For Step b., we have

Hosoya Matrix
We will show that the Hosoya matrix of a tree of order n can be computed in O(n 2 ) time.

Recursive Formula for Computing the Hosoya Matrix
Proposition 5. If uv is an edge of a tree T, then Proof. Note first that T − uv is composed of two connected components: T − V(T u ) and T v u . Proposition 2 yields By Proposition 1, we have The assertion now follows immediately from Proposition 4.

Theorem 3.
If P = v 1 , v 2 , . . . , v k is a path in a tree T, then Proof. If k = 2, then this is Proposition 5.
If k > 2, we first show that T − E(P) is composed of connected components: Let us assume that the proposition holds for all paths of length less than k, and let P = v 1 , v 2 , . . . , v k−1 and P = v 1 , v 2 , . . . , v k . By the induction hypothesis, , and since the edge v k−1 v k belongs to the tree T v 1 v k−1 , the assertion easily follows.
By Proposition 5, we have Proof. By Theorem 3, we have to show that P k = . We use induction on k.
If k = 2, then P k = 1 and the assertion is trivial. Let k ≥ 3 and assume that the claim holds for k − 1. By Proposition 5,

It follows
Since by the induction hypothesis we have , the assertion follows.

Algorithm for Computing the Hosoya Matrix
Algorithm 3 computes the entries of the r-th row of the Hosoya matrix for the tree T. In other words, the algorithm for the rooted tree T r computes the value H r,v for every v ∈ V(T). If v is a vertex of T r , the list C v represents the children of v, while p v represent the parent of v. Vectors Z andZ for entries Z v andZ represent Z(T v ) and Z(T v − v) in T r , respectively.  3. for all w ∈ C v do Row(w, r, C, p, Z,Z, H); end. Proposition 6. Let r ∈ {1, 2, . . . , n} be a vertex of a tree T. Then algorithm Row computes the r-th row of the Hosoya matrix of T in linear time.
Proof. The correctness of the algorithm is based on Corollary 1. Note that p v and C v represent the parent of v and the children of v, respectively. The algorithm maintains variables Zk and Pk representing the values Z k and P k of Corollary 1, respectively. We first consider the following two cases.
A. If v = r, the correctness easily follows. The algorithm sets the values of Pk and Zk to one and Z v , respectively.
B. If r is the parent of v, the value of Pk remains equal to one after Step 2.1 is executed, since Zk is set to Z(T r ) in case A. The new value of Z k is established in Step 2.2., while Step 2.3 provides the correct value of H r,v .
Since in the above cases the correct value of Pk is computed, the correctness for other cases follows from Corollary 1.
For the time complexity, note that the number of recursive calls of the algorithm equals the number of vertices of T r . Since the time complexity of a single call of the algorithm (neglecting the recursive calls) is constant, the assertion follows.

Conclusions
Quantitative structure-property and structure-activity relationships of molecular compounds are frequently modeled using the molecular topological features of these compounds. In that regard, topological indices are crucial for investigating chemical compounds to grasp chemical structures' fundamental topology.
In this paper, we have obtained efficient algorithms for computing the Hosoya index and the Hosoya matrix on an arbitrary acyclic graph. Both algorithms are optimal in the sense that the running time of an algorithm is constant per a fundamental essential item of the input: a vertex (for the computation of the Hosoya index) and an entry of the matrix (for the computation of the Hosoya matrix). The complexity of the presented algorithm for computing the Hosoya index of a tree is within the same time bound as some previously presented procedures. That said, the algorithm presented in this paper is much simpler than its predecessor given in [11] and does not require pre-processing.
It is worth noticing that both presented algorithms exploit the recursive nature of a tree. Thus, a similar approach could be applied for computing the Hosoya index and the Hosoya matrix of tree-like graphs, e.g., cactus graphs, and of graphs derived from trees. Moreover, the presented concepts could initiate studies of efficient methods of computation for other topological indices, especially the ones that are closely connected to the Hosoya index, particularly the Merrifield-Simons index [17,18], the energy of a graph [3], and the matching energy of a graph [19].

Conflicts of Interest:
The author declares no conflict of interest.