COMPLEXITYComplexity1099-05261076-2787Hindawi10.1155/2021/66633066663306Research ArticleAlgorithms for Computing Wiener Indices of Acyclic and Unicyclic GraphsBiBo1https://orcid.org/0000-0001-5715-9299JamilMuhammad Kamran2Muhammad FahdKhawaja3https://orcid.org/0000-0001-8928-6073SunTian-Le4AhmadImran5DingLei6KhanAtif1Maanshan Teacher’s CollegeDepartment of Software EngineeringMaanshan 243041China2Department of MathematicsRiphah International UniversityLahorePakistanriphah.edu.pk3FAST School of ComputingNational University of Computer and Emerging SciencesLahorePakistannu.edu.pk4College of EconomicsSichuan Agricultural UniversityChengdu 610000Chinasicau.edu.cn5Faculty of Computer ScienceRiphah International UniversityLahorePakistanriphah.edu.pk6School of Computer EngineeringAnhui Wenda University of Information EngineeringHefei 231201China2021352021202127122020144202121420213520212021Copyright © 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.

Let G=VG,EG be a molecular graph, where VG and EG 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 On.

Key Scientific Research Projects of Colleges and Universities in Anhui ProvinceKJ2018A0903Riphah International UniversityORIC/FEAS-11
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 . 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 .

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

The Wiener index of a molecular graph G is the sum of distances between all pairs of vertices of G, i.e.,(1)WG=u,vVGdu,v.

The Wiener polarity index of a molecular graph G=VG,EG is defined as the number of unordered pairs of vertices u,v of G such that the shortest distance du,v between u and v is 3,(2)WPG=u,v|du,v=3,u,vVG.

The terminal Wiener index TWG of a molecular graph is defined as the sum of distances between all pairs of its pendant vertices. If VT=v1,v2,,vk is the set of all pendant vertices of G, then(3)TWG=u,vVTGdvi,vj.

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. Ilic` 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 i

d[i] is the degree of vertex i

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

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

<bold>Algorithm 1:</bold> Linear time algorithm for computing Wiener index for acyclic graphs.

V=1,2,,n

WI = 0

FOR (i = 1 to n)

counti=1

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 + countleafncountleaf

d[leaf] = 0

traverse L[leaf] and look at its neighbor nb.

IF dnb>0

countnb=countnb+countleaf

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 v16:

WI is increased by countv16ncountv16=115=15, so WI is now 15

The count of neighbors of V16 is increased by the count of V16, so countv15 is now 2

v16 is deleted from the graph and removed from top of stack

Now the degree of v15 (neighbor of v16) is 1, so it is added to top of stack

The updated graph is shown in Figure 1(b).

At this point, v15 is at the top of stack, so the following step will be performed by LTA:

WI is increased by countv15ncountv15=214, the value of WI is now 15 + 28 = 43

The count of neighbors of v15 is increased by count of v15, so countv9 is now 3

v15 is deleted from the graph and removed from top of stack

Now, degree of v9, which was neighbor of v15, is not 1, so it will not be added to the stack

These 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).

A tree with the Wiener index 477.

<bold>Algorithm 2:</bold> Linear time algorithm for computing Wiener index for unicyclic graphs.

i=1

while(d(i) = 0)

i=i+1

k=0

start=i

current=start

prev=1

DO

valk=countcurrent

k=k+1

traverse L[current] until its neighbor having dnb=2 and nb=prev is found

prev=current

current=nb

WHILE (currentstart)

diff1=0

FOR (i=1 to k/2)

diff1=diff1+vali

FOR (i=2 to k1)

diffi=diffi1vali1+vali+k/2modk

FOR (i=1 to k/2)

FOR (i=1 to k1)

subtract = 0

IF (k is EVEN)

FOR(i=0 to k/21)

subtract=subtract+valivali+k/2

WI=WIsubtract

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 val0,val1,,valk1, where k 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 = 5

At i=1

WI=297+66=333

At i=2

WI=333+45

At i=3

WI=353+18=361

At i=4

WI=361+215=391

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 n number of vertices. The details are illustrated in Table 1.

A unicyclic graph having the Wiener index 391.

Computing time (milliseconds) of five algorithms for star graph.

nSAPFAPFWBFSLTA
100.1082520.1052540.09427750.0931660.084932
200.114240.1093580.1074740.103620.086898
500.134960.131320.1282420.1162540.088754
1000.713280.18910.165420.125970.09881
1000615876.157.72280.133120.105416
100002.70720.11802
2.2. Analysis of Our Algorithm

When we delete a leaf vertex, vVG, its corresponding edge uvEG 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 countvi=ci,i=1,2,,k1, the values of these counts are c0,c1,,ck1. We look at the distances clockwise in order to avoid duplication. We need to calculate the following term:(4)i=0k1j=i+1modki+k/2modkcicjjimodk.

We first calculate j=1k/2jcj in O(k) steps and then we calculate the remaining j=i+1i+k/2cjji terms in O(k) by calculating each of these k1 terms in O(1) steps. This is done by using the fact that(5)j=i+2i+k/2+1modkcjjimodkj=i+1i+k/2modkcjjimodk=k/2ci+k/2+1modkj=i+1i+k/2modkcj.

In the end, if k is even, then we have calculated the values(6)i=0k/21k2cici+k/2,multiple 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 countv of the leaf vertex v and adding the value countvncountv, where n is the number of vertices in the graph. Since we want to calculate the distances between leaf vertices only and not all the vertices, n, we change this line to countv×nleafcountv where nleaf 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 v becomes leaf vertex, it has only edge uv. Here, countv gives us the value of the total number of vertices in the component containing v if the edge uv is deleted from the original graph. Accordingly, we add countv×ncountv 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 countv, we use count1v and count2v. Here, when a vertex v becomes a leaf vertex, then after deleting its edge uv, countiv,i=1,2 gives the number of vertices that are at distance i from the vertex v and is on the component containing v.

When we delete the edge uv, instead of adding countv×ncountv to the Wiener Index, we need to add the number of pairs that are at distance 3 and update the values of count1u and count2u accordingly. We include the following lines instead of WI=WI+countv×ncountv.(7)WI=WI+count2u+count2v+count1u×count1v,count1u=count1u+1,count2u=count2u+count1v.

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:(8)WI=WI+i=0kcount1i+count1i+2modk+count2i+count2i+1modk+count1i×count1i+1modk.

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.

LiuJ.-B.PanX.-F.CaoJ.HuF.-F.A note on some physical and chemical indices of clique-inserted latticesJournal of Statistical Mechanics: Theory and Experiment20146P0600610.1088/1742-5468/2014/06/p060062-s2.0-84903639603LiuJ. B.WangS.WangC.HayatS.Further results on computation of topological indices of certain networksIET Control Theory & Applications201711132065207110.1049/iet-cta.2016.12372-s2.0-85016957754WienerH.Structural determination of paraffin boiling pointsJournal of the American Chemical Society19471691720BonchevD.MekenyanO.ProtićG.TrinajstićN.Journal of Chromatography1979176149PapazovaD.DimovM.BonchevD.Calculation of gas chromatographic retention indices of isoalkanes based on a topological approachJournal of Chromatography1980188297RouvrayD. H.CraffordB. C.The dependence of physico-chemical properties on topological factorsSouth African Journal of Science19767274RouvrayD. H.TatongW.Novel applications of topological indicesZeitschrift für Naturforschung1986411238StielL. I.ThodosG.The normal boiling points and critical constants of saturated aliphatic hydrocarbonsAmerican Institute of Chemical Engineers19628527MoharB.PisanskiT.How to compute the Wiener index of a graphJournal of Mathematical Chemistry19882267277AzariM.IranmaneshA.TehranianA.Two topological indices of three chemical structuresMATCH Communications in Mathematical and in Computer2013696986DengH.Wiener indices of spiro and polyphenyl hexagonal 4 chainsMathematical and Computer Modelling2012553-4634644AshrafiA. R.YousefiS.A new algorithm for computing distance matrix and Wiener index of zig-zag polyhex nanotubesNanoscale Research Letters20072420220610.1007/s11671-007-9051-y2-s2.0-34247583395Ili‘cM.Ili‘cM.On some algorithms for computing topological indices of chemical graphsMATCH Communications in Mathematical and in Computer201778665674CanfieldE. R.RobinsonR. W.RouvrayD. H.Determination of the Wiener molecular branching index for the general treeJournal of Computational Chemistry19856659860910.1002/jcc.5400606132-s2.0-84988122955