Abstract

Let be a molecular graph, where and 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 (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 .

1. Introduction

Mathematical chemistry is a branch of theoretical chemistry. This 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 [48].

Let be a molecular graph, where and represent the sets of vertices (atoms) and edges (bonds) of the graph . The distance between two vertices and is the length of the shortest path between them and is denoted as . For a vertex , the degree of is the number of the adjacent vertices to . It is denoted as . A star graph with vertices is a graph in which one vertex has a degree and all other vertices have degree 1.

The Wiener index of a molecular graph is the sum of distances between all pairs of vertices of , i.e.,

The Wiener polarity index of a molecular graph is defined as the number of unordered pairs of vertices of such that the shortest distance between and is 3,

The terminal Wiener index of a molecular graph is defined as the sum of distances between all pairs of its pendant vertices. If is the set of all pendant vertices of , 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. Ili 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.

2. The Wiener Indices of Acyclic and Unicyclic Graphs

The 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.

2.1. Algorithm for the Wiener Index

Following variables are used in our LTA:WI is the Wiener indexL[i] is the adjacency list of vertex d[i] is the degree of vertex Stack is the stack of leaf nodes

Is-empty() is a function for stack which returns true if stack is empty and false otherwise. Pop() is a function of stack and it returns the element that was pushed last onto the stack. Push(i) is a function of stack that pushes vertex onto the stack.

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 at the point it becomes a leaf vertex and its edge . Removing from the original graphs disconnects the graph will give the number of vertices in the component containing and there will be a total of paths (shortest paths) going through the edge in the original graph.

LTA for acyclic graphs has the following steps (Algorithm 1).

WI = 0
FOR (i = 1 to n)
 traverse L[i] and count the number of neighbors of i and update d[i]
 IF d[i] = 1
  stack.push(i)
WHILE (stack is not empty)
 leaf = stack.pop()
 WI = WI + 
 d[leaf] = 0
 traverse L[leaf] and look at its neighbor nb.
 IF
  
  d[nb] = d[nb]-1
  IF d[nb] = 1
  stack.push(nb)

The algorithm given above calculates the Wiener index of the given tree, and the numerical value of Wiener is stored in the variable WI.

Example 1. Consider the tree in Figure 1(a). The initial value of WI is zero. The following operations are performed on the vertex present at the top of stack :(1)WI is increased by , so WI is now 15(2)The count of neighbors of is increased by the count of , so is now 2(3) is deleted from the graph and removed from top of stack(4)Now the degree of (neighbor of ) is 1, so it is added to top of stackThe updated graph is shown in Figure 1(b).
At this point, is at the top of stack, so the following step will be performed by LTA:(1)WI is increased by , the value of WI is now 15 + 28 = 43(2)The count of neighbors of is increased by count of , so is now 3(3) is deleted from the graph and removed from top of stack(4)Now, degree of , which was neighbor of , is not 1, so it will not be added to the stackThese 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).

while(d(i) = 0)
DO
  
 traverse L[current] until its neighbor having and is found
  
WHILE ()
FOR ()
FOR ()
FOR ()
FOR ()
  
subtract = 0
IF (k is EVEN)
 FOR()
  
  
 Here, WI gives the Wiener index for unicyclic graphs.

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. These steps are repeated until only a cycle is left as shown in Figure 2(b). The value of the count of vertices (of cycle) is saved in variables , where is the length of the cycle.
These values are used for further calculations.diff[1] = 6 + 4 = 10diff[2] = diff[1] - val[1] + val[3] = 5diff[3] = diff[2] - val[2] + val[4] = 3diff[4] = diff[3] - val[3] + val[0] = 5At At At At After completing all these steps, WI will contain the value of the Wiener index for unicyclic graph which is 391 for this graph.
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 number of vertices. The details are illustrated in Table 1.

2.2. Analysis of Our Algorithm

When we delete a leaf vertex, , its corresponding edge is also deleted. At this point, the number of shortest paths going through the edge is added towards our final solution, but all the paths from vertex are not added. To accommodate for the missing values, we are using the concept of count and the count of is added to the count of . Here, the count of represents the number of vertices including whose total distances depending upon the distances of 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 , and , the values of these counts are . We look at the distances clockwise in order to avoid duplication. We need to calculate the following term:

We first calculate in O(k) steps and then we calculate the remaining terms in O(k) by calculating each of these terms in O(1) steps. This is done by using the fact that

In the end, if is even, then we have calculated the valuesmultiple times. Therefore, we need to fix this double counting by subtracting this value.

2.3. 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. Therefore, 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. We calculate and update the terminal Wiener index by considering of the leaf vertex and adding the value , where is the number of vertices in the graph. Since we want to calculate the distances between leaf vertices only and not all the vertices, , we change this line to where is the total number of leaf nodes in the graph.

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.

2.4. Algorithm for the Wiener Polarity Index

Wiener polarity index is defined as the number of pairs of vertices at distance 3. Here we do not add the distances. We again update LTA to calculate the Wiener polarity index.

In LTA, we repeatedly delete leaf vertices. When vertex becomes leaf vertex, it has only edge . Here, gives us the value of the total number of vertices in the component containing if the edge is deleted from the original graph. Accordingly, we add to the value of the Wiener index. LTA does not look at the actual distance. In the Wiener polarity index, we are interested in counting pairs of vertices at a distance of 3. Instead of the variable , we use and . Here, when a vertex becomes a leaf vertex, then after deleting its edge , gives the number of vertices that are at distance i from the vertex and is on the component containing .

When we delete the edge , instead of adding to the Wiener Index, we need to add the number of pairs that are at distance 3 and update the values of and accordingly. We include the following lines instead of .

This 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

The data used to support this study are included within the article.

Disclosure

The paper was presented as arXiv in Cornell University according to the following link: https://arxiv.org/abs/1801.05146.

Conflicts of Interest

The authors declare that there are no conflicts of interest regarding the publication of this paper.

Acknowledgments

This work was supported by Key Scientific Research Projects of Colleges and Universities in Anhui Province in 2018 (Research on scenario intelligent information push system based on neural network) subject no: KJ2018A0903 and Riphah International University ORIC/FEAS-11.