Algorithms for Computing Wiener Indices of Acyclic and Unicyclic Graphs

Let $G=(V(G),E(G))$ be a molecular graph, where $V(G)$ and $E(G)$ are the sets of vertices (atoms) and edges (bonds). A topological index of a molecular graph is a numerical quantity which helps to predict the chemical/physical properties of the molecules. The Wiener, Wiener polarity and the terminal Wiener indices are the distance based topological indices. In this paper, we described a linear time algorithm {\bf(LTA)} that computes the Wiener index for acyclic graphs and extended this algorithm for unicyclic graphs. The same algorithms are modified to compute the terminal Wiener index and the Wiener polarity index. All these algorithms compute the indices in time $O(n)$.


Introduction
Mathematical chemistry is a branch of theoretical chemistry.
is branch deals with predicting the chemical or physical properties of molecules using molecular structure with the help of mathematical methods. In mathematical chemistry, particular attention has been paid to so-called topological indices [1,2]. A topological index is a number associated with the graph obtained from the chemical compound.
Wiener index is one of the oldest and widely used topological indices. Wiener introduced this topological index while he was working on the boiling point of alkane molecules [3]. After that, the Wiener index is used to predict many other physical and chemical properties of the molecules such as critical constants, density, viscosity, surface tension, and ultrasonic sound velocity [4][5][6][7][8].
Let G � (V(G), E(G)) be a molecular graph, where V(G) and E(G) represent the sets of vertices (atoms) and edges (bonds) of the graph G. e distance between two vertices u and v is the length of the shortest path between them and is denoted as d (u, v). For a vertex v ∈ V, the degree of v is the number of the adjacent vertices to v. It is denoted as d (v). A star graph with n vertices is a graph in which one vertex has a degree n − 1 and all other vertices have degree 1. e Wiener index of a molecular graph G is the sum of distances between all pairs of vertices of G, i.e., (1) e Wiener polarity index of a molecular graph G � (V(G), E(G)) is defined as the number of unordered pairs of vertices u, v of G such that the shortest distance d (u, v) between u and v is 3, e terminal Wiener index TW(G) of a molecular graph is defined as the sum of distances between all pairs of its pendant vertices. If VT � v 1 , v 2 , . . . , v k is the set of all pendant vertices of G, then Numerous works have been done on these topological indices. In 1988, Mohar et al. [9] introduced an algorithm to find the Wiener index of acyclic graphs. Azari et al. determined the Wiener index of molecular graphs made by hexagons [10]. Deng investigated the Wiener index of spiro and polyphenyl hexagonal chains [11]. In 2007, Ashrafi et al. [12] gave the algorithm for computing distance matrix and Wiener index of zigzag polyhex nanotubes. Ilic ' et al. [13] described some algorithms for computing topological indices of chemical graphs. In this paper, we described linear time algorithms to compute the Wiener index, the Wiener polarity index, and the terminal Wiener index for acyclic graphs and unicyclic graphs.

The Wiener Indices of Acyclic and Unicyclic Graphs
e main results of this section are linear time algorithms for computing Wiener index (WI) for arbitrary acyclic and unicyclic graphs. Linear time algorithm (LTA) for acyclic graphs improves and simplifies a nice recursive algorithm of Canfield et al. [14]. LTA is similar to the algorithm of Mohar et al. [9] in the way that it calculates the number of shortest paths by looking at the contribution of each edge and summing all these values. LTA achieves this without converting the tree into a rooted tree. We extend LTA to compute the Wiener index of unicyclic graphs. A vertex may be a leaf vertex in the original graph or it becomes a leaf vertex after successive deletion of leaf vertices. Degree of a leaf vertex is 1. Also every edge in a tree is a cut edge. Consider a vertex v at the point it becomes a leaf vertex and its edge e. Removing e from the original graphs disconnects the graph count[v] will give the number of vertices in the component containing v and there will be a total of count[v] × (n − count[v]) paths (shortest paths) going through the edge e in the original graph.
LTA for acyclic graphs has the following steps (Algorithm 1). e algorithm given above calculates the Wiener index of the given tree, and the numerical value of Wiener is stored in the variable WI. Figure 1(a). e initial value of WI is zero. e following operations are performed on the vertex present at the top of stack (v 16 ):

Example 1. Consider the tree in
] is now 2 (3) v 16 is deleted from the graph and removed from top of stack (4) Now the degree of v 15 (neighbor of v 16 ) is 1, so it is added to top of stack e updated graph is shown in Figure 1(b). At this point, v 15 is at the top of stack, so the following step will be performed by LTA: (14), the value of WI is now 15 + 28 � 43 (2) e count of neighbors of v 15 is increased by count of v 15 , so count[v 9 ] is now 3 (3) v 15 is deleted from the graph and removed from top of stack (4) Now, degree of v 9 , which was neighbor of v 15 , is not 1, so it will not be added to the stack ese steps will be repeated until there are no vertices with degree 1 left in the graph. In case of trees, in the end, a singleton vertex will remain in the graph and the value of WI will give the Winer index of the given graph.
For unicyclic graphs, the following lines of code should be added after the algorithm described above (Algorithm 2). Example 2. Consider the unicyclic graph as shown in Figure 2(a).
For unicyclic graph, the initial steps are the same as for tree as shown in the previous example. ese steps are repeated until only a cycle is left as shown in Figure 2

Complexity 3
We compared the computing time of LTA with Slow-All-Pairs (SAP), Faster-All-Pairs (FAP), Floyd-Warshall (FW), and Breadth-First Search (BFS) algorithms by considering star graph with n number of vertices. e details are illustrated in Table 1.

Analysis of Our Algorithm.
When we delete a leaf vertex, v ∈ V(G), its corresponding edge uv ∈ E(G) is also deleted. At this point, the number of shortest paths going through the edge uv is added towards our final solution, but all the paths from vertex v are not added. To accommodate for the missing values, we are using the concept of count and the count of v is added to the count of u. Here, the count of u represents the number of vertices including u whose total distances depending upon the distances of u are with the remaining vertices. We need to do number of steps proportional to the number of edges; hence, LTA runs in linear time.
We extend this idea to unicyclic graphs. We continue removing leaf vertices until we are left with a cycle. Suppose that the cycle is of order k, and count[v i ] � c i , i � 1, 2, . . . , k − 1, the values of these counts are c 0 , c 1 , . . . , c k−1 . We look Here, WI gives the Wiener index for unicyclic graphs. ALGORITHM 2: Linear time algorithm for computing Wiener index for unicyclic graphs. 4 Complexity at the distances clockwise in order to avoid duplication. We need to calculate the following term: We first calculate ⌊k/2⌋ j�1 jc j in O(k) steps and then we calculate the remaining i+⌊k/2⌋ j�i+1 c j (j − i) terms in O(k) by calculating each of these k − 1 terms in O(1) steps. is is done by using the fact that In the end, if k is even, then we have calculated the values multiple times. erefore, we need to fix this double counting by subtracting this value.

Algorithm for the Terminal Wiener
Index. Now we extend these algorithms to calculate the terminal Wiener index.
Terminal Wiener index is defined as the sum of distances between leaf vertices as opposed to the Wiener index that is defined as the sum of distances between all the vertices. erefore, the distances of nonleaf vertices should not be included in this algorithm. We modify LTA by initializing the count of nonleaf vertices to 0. Hence, the contribution of all such vertices is nullified.
We need another modification to complete the algorithm for calculating the terminal Wiener index correctly. With only these changes, our algorithm will find the terminal Wiener index of acyclic graphs as well as terminal Wiener index of unicyclic graphs. Since the number of steps is still the same as LTA, this algorithm also takes linear time.

Algorithm for the
is calculates the Wiener polarity index of acyclic graphs in linear time as the number of steps is similar to the LTA.
For calculating the Wiener polarity index of unicyclic graphs, we add the following values to WI after only a cycle is left:   Clearly, this can be calculated in linear time, and therefore, our algorithm calculates the Wiener polarity index for acyclic graphs and unicyclic graphs in deterministic linear time.

Data Availability
e data used to support this study are included within the article.
Disclosure e paper was presented as arXiv in Cornell University according to the following link: https://arxiv.org/abs/1801. 05146.

Conflicts of Interest
e authors declare that there are no conflicts of interest regarding the publication of this paper.