^{1}

^{2}

^{3}

^{4}

^{5}

^{6}

^{1}

^{2}

^{3}

^{4}

^{5}

^{6}

Let

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 [

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 [

Let

The Wiener index of a molecular graph

The Wiener polarity index of a molecular graph

The terminal Wiener index

Numerous works have been done on these topological indices. In 1988, Mohar et al. [

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

Following variables are used in our LTA:

WI is the Wiener index

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(

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

LTA for acyclic graphs has the following steps (Algorithm

WI = 0

FOR (

traverse

IF

stack.push(

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] =

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.

Consider the tree in Figure

WI is increased by

The count of neighbors of

Now the degree of

The updated graph is shown in Figure

At this point,

WI is increased by

The count of neighbors of

Now, degree of

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

A tree with the Wiener index 477.

while(d(

DO

traverse L[current] until its neighbor having

WHILE (

FOR (

FOR (

FOR (

FOR (

subtract = 0

IF (

FOR(

Here, WI gives the Wiener index for unicyclic graphs.

Consider the unicyclic graph as shown in Figure

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

These values are used for further calculations.

diff[1] = 6 + 4 = 10

diff[2] = diff[1] - val[1] + val[3] = 5

diff[3] = diff[2] - val[2] + val[4] = 3

diff[4] = diff[3] - val[3] + val[0] = 5

At

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

A unicyclic graph having the Wiener index 391.

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

10 | 0.108252 | 0.105254 | 0.0942775 | 0.093166 | 0.084932 |

20 | 0.11424 | 0.109358 | 0.107474 | 0.10362 | 0.086898 |

50 | 0.13496 | 0.13132 | 0.128242 | 0.116254 | 0.088754 |

100 | 0.71328 | 0.1891 | 0.16542 | 0.12597 | 0.09881 |

1000 | 6158 | 76.15 | 7.7228 | 0.13312 | 0.105416 |

10000 | 2.7072 | 0.11802 |

When we delete a leaf vertex,

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

We first calculate

In the end, if

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

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.

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

When we delete the edge

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:

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.