Research Article | Open Access
Bo Bi, Muhammad Kamran Jamil, Khawaja Muhammad Fahd, Tian-Le Sun, Imran Ahmad, Lei Ding, "Algorithms for Computing Wiener Indices of Acyclic and Unicyclic Graphs", Complexity, vol. 2021, Article ID 6663306, 6 pages, 2021. https://doi.org/10.1155/2021/6663306
Algorithms for Computing Wiener Indices of Acyclic and Unicyclic Graphs
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 .
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 . 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–8].
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.  introduced an algorithm to find the Wiener index of acyclic graphs. Azari et al. determined the Wiener index of molecular graphs made by hexagons . Deng investigated the Wiener index of spiro and polyphenyl hexagonal chains . In 2007, Ashrafi et al.  gave the algorithm for computing distance matrix and Wiener index of zigzag polyhex nanotubes. Ili et al.  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. . LTA is similar to the algorithm of Mohar et al.  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 index L[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).
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).
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 = 6 + 4 = 10 diff = diff - val + val = 5 diff = diff - val + val = 3 diff = diff - val + val = 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.
The data used to support this study are included within the article.
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.
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.
- J.-B. Liu, X.-F. Pan, J. Cao, and F.-F. Hu, “A note on some physical and chemical indices of clique-inserted lattices,” Journal of Statistical Mechanics: Theory and Experiment, vol. 6, Article ID P06006, 2014.
- J. B. Liu, S. Wang, C. Wang, and S. Hayat, “Further results on computation of topological indices of certain networks,” IET Control Theory & Applications, vol. 11, no. 13, pp. 2065–2071, 2017.
- H. Wiener, “Structural determination of paraffin boiling points,” Journal of the American Chemical Society, vol. 1, no. 69, p. 1720, 1947.
- D. Bonchev, O. Mekenyan, and G. ProtićN. Trinajstić, Journal of Chromatography, vol. 176, p. 149, 1979.
- D. Papazova, M. Dimov, and D. Bonchev, “Calculation of gas chromatographic retention indices of isoalkanes based on a topological approach,” Journal of Chromatography, vol. 188, p. 297, 1980.
- D. H. Rouvray and B. C. Crafford, “The dependence of physico-chemical properties on topological factors,” South African Journal of Science, vol. 72, p. 74, 1976.
- D. H. Rouvray and W. Tatong, “Novel applications of topological indices,” Zeitschrift für Naturforschung, vol. 41, p. 1238, 1986.
- L. I. Stiel and G. Thodos, “The normal boiling points and critical constants of saturated aliphatic hydrocarbons,” American Institute of Chemical Engineers, vol. 8, p. 527, 1962.
- B. Mohar and T. Pisanski, “How to compute the Wiener index of a graph,” Journal of Mathematical Chemistry, vol. 2, pp. 267–277, 1988.
- M. Azari, A. Iranmanesh, and A. Tehranian, “Two topological indices of three chemical structures,” MATCH Communications in Mathematical and in Computer, vol. 69, pp. 69–86, 2013.
- H. Deng, “Wiener indices of spiro and polyphenyl hexagonal 4 chains,” Mathematical and Computer Modelling, vol. 55, no. 3-4, pp. 634–644, 2012.
- A. R. Ashrafi and S. Yousefi, “A new algorithm for computing distance matrix and Wiener index of zig-zag polyhex nanotubes,” Nanoscale Research Letters, vol. 2, no. 4, pp. 202–206, 2007.
- M. Ili‘c and M. Ili‘c, “On some algorithms for computing topological indices of chemical graphs,” MATCH Communications in Mathematical and in Computer, vol. 78, pp. 665–674, 2017.
- E. R. Canfield, R. W. Robinson, and D. H. Rouvray, “Determination of the Wiener molecular branching index for the general tree,” Journal of Computational Chemistry, vol. 6, no. 6, pp. 598–609, 1985.
Copyright © 2021 Bo Bi et al. This is an open access article distributed under the Creative Commons Attribution License, which permits unrestricted use, distribution, and reproduction in any medium, provided the original work is properly cited.