Tanh-sinh quadrature for single and multiple integration using floating-point arithmetic

Abstract: The problem of estimating single- and multi-dimensional integrals, with or without end-point singularities, is prevalent in all fields of scientific research, and in particular in physics. Although tanh-sinh quadrature is known to handle most of these cases excellently, its use is not widely spread among physicists. Moreover, while most calculations are limited by the use of finite-precision floating-point arithmetic, similar considerations for tanh-sinh quadrature are mostly lacking in literature, where infinite-precision floating-point numbers are often assumed. Also little information is available on the application of tanh-sinh quadrature to multiple integration.We have investigated the risks and limitations associated with limited-precision floating-point numbers when using tanh-sinh quadrature for both single and multiple integration, while obtaining excellent convergence rates. In addition, this paper provides recommendations for a straightforward implementation using limited-precision floating-point numbers and for avoiding numerical instabilities.

\section{Introduction}
\label{sec::intro}

While many automatic integration routines yield excellent results in general, they often fail in specific circumstances, such as the occurrence of end-point singularities.
In these situations, it is paramount to account for the limitations of a specific routine in order to exploit its full potential.
Tanh-sinh quadrature for numerical integration already exists about half a century,\cite{Takahasi73} but is not well-known in the physics community, though being adopted in some recent publications.\cite{Okayama13,Gaudreau15,Vanherck18}\@
The corresponding scheme can be invoked as an almost general purpose quadrature, which is especially efficient for divergent integrands.
Due to its exponential convergence rate and its good behaviour in general, tanh-sinh quadrature has become rather popular in the field of experimental mathematics.
It even has been coined to be the best scheme for integrands typically encountered in that field, which focuses on high-precision integral calculations.\cite{Bailey05,Bailey11,Slevinsky15}

We describe the quadrature in an accessible manner in \Sec{sec::tanhsinh_quad} while elaborating on its implementation with limited-precision floating-point numbers in \Sec{sec::practicals}.
Specifically, we explain how numerical instabilities can be avoided, while showing how to extend the quadrature to multiple integration, which is indispensable in computational physics.
Examples of integrals calculated with the tanh-sinh rule are given in \Sec{sec::examples}, whereas the conclusions and final remarks are presented in Section~\ref{sec::conclusion}.

\section{Tanh-sinh quadrature}
\label{sec::tanhsinh_quad}

Tanh-sinh quadrature computes integrals of the form%
\footnote{
	Any integral $\dint{a}{b}{f\opbrack{y}}{y}$ with $a$ and $b$ finite can be cast in this form by the linear substitution $2y=\nbrack{b-a}x + \nbrack{b+a}$.
}
\begin{equation} \label{eq:normalizedintegral}
	\mathcal{I}
	=
		\dint{-1}{1}{ f\opbrack{x} }{x}.
\end{equation}
The quadrature is based on a variable substitution $x=\tstrans\opbrack{t}$, mapping the original, finite domain $x \in \openinterval{-1}{1}$ onto the entire real axis $t \in \openinterval{-\infty}{+\infty}$:
\begin{equation}
	\mathcal{I}
	=
		\dint{-\infty}{\infty}{
			g\opbrack{ t }
		}{t}
	, \quad
	g\opbrack{ t }
	\coloneqq
		f\opbrack[\big]{ \tstrans\opbrack{t} }
		\tstrans'\opbrack{t}.
\end{equation}
For integration over the entire real axis of exponentially decaying integrands as $\abs{t}\to\infty$, the trapezoidal rule (also known as Sinc quadrature~\cite{Okayama13}) is the most efficient~\cite{Goodwin49,Schwartz69,Sugihara97,Mori05,Trefethen14} among quadratures with equidistant abscissae.
Applying this rule with a step size $h$ between the evaluation points yields the approximate integral
\begin{equation} \label{eq:trapezoidalrule}
	\discint{h}
	=
		\lim\limits_{m\to\infty} \sum_{i=-m}^{+m}
		h \tstrans'\opbrack{t_i}
		f\opbrack[\big]{ \tstrans\opbrack{t_i} },
\end{equation}
the transformed evaluation points being
\begin{equation}
	t_i
	\coloneqq
		i h, \quad i = 0, \pm 1, \pm 2, \ldots
\end{equation}
while introducing a discretization error $\discerror{h}$.
Keeping only $N \coloneqq 2n+1$ function evaluations, we are left with
\begin{equation}
	\disctruncint{h}{n}
	=
		\sum_{i=-n}^{+n}
		h \tstrans'\opbrack{t_i}
		f\opbrack[\big]{ \tstrans\opbrack{t_i} },
\end{equation}
thereby introducing a truncation error $\truncerror$, since the transformed integration domain is now limited to the window $\closedinterval{t_{-n}}{t_{n}}$.
The total error of the resulting approximation as compared to the original integral is limited by $\abs[\big]{ \exactint - \disctruncint{h}{n} }\leqslant \disctruncerror{h}{n} \coloneqq \abs{\discerror{h}} + \abs{\truncerror}$.

As to the mapping $\Psi(t)$, Takahasi and Mori~\cite{Takahasi73} proposed a \emph{tanh-sinh} transformation
\begin{subequations}
\begin{align}
	\tstrans\opbrack{t}
	&=
		\tanh\opbrack[\big]{\lambda \sinh\opbrack{t}} \label{eq:abscissaeformula}
	\\
	\tstrans'\opbrack{t}
	&=
		\frac{
			\lambda \cosh\opbrack{t}
		}{
			\cosh^2\opbrack[\big]{\lambda \sinh\opbrack{t}}
		},\label{eq:weightsformula}
\end{align}
\end{subequations}
where $\lambda=\infrac{\pi}{2}$.
With this choice, which is illustrated in \Fig{fig:transform}, the behaviour of $g\opbrack{ t }$ as $\abs{t}\to\infty$ is optimal in the sense that a faster decay leads to a higher discretization error for a given $h$, while a slower decay causes a larger truncation error because $ \tstrans'\opbrack{t} f\opbrack[\big]{ \tstrans\opbrack{t} }$ remains significant for large values of $\abs{t}$.
An extended error analysis in the complex plane indeed shows that, for a large number of evaluation points, the above transformation often yields an optimal error~\cite{Takahasi73,Sugihara97} as compared to other quadratures based on a variable transformation.
This error is of the order~\cite{Takahasi73,Sugihara97,Mori05,Trefethen14}
\begin{equation} \label{eq:errororder}
	\abs{\disctruncerror{h}{n}}
	=
		\mathcal{O}\opbrack[\bigg]{
			\exp\opbrack[\Big]{ - \frac{ \pi d N }{ \ln\opbrack{2dN} } }
		}
	,\quad\text{with } N=2n+1
\end{equation}
when $h$ is chosen optimally.
The parameter $d$ has to be chosen such that the analytical continuation of the integrand $g\opbrack{z} = \tstrans'\opbrack{z}f\opbrack[\big]{ \tstrans\opbrack{z} }$ in the complex plane is regular in the strip around the real axis defined by $\abs{\Imaginarypart z} < d$, meaning that the integrand lies within the Hardy space.\cite{Hardy15,Sugihara97}\@
The choice of $d$ and $h$ will be detailed in \Sec{sec::hspacing}, the latter being of particular importance in avoiding numerical instabilities.
\begin{figure}[!tb]
	\centering
	\includegraphics{figure_1}
	\caption{
		Tanh-sinh transformation applied to the integral $\dint{0}{1}{\frac{1}{\sqrt{x}}}{x}$, brought to the standard domain $\openinterval{-1}{1}$ (top left).
		The tanh-sinh transformation transforms the integration domain from $x\in\openinterval{-1}{1}$ to $t\in\openinterval{-\infty}{\infty}$, while the integrands magnitude is strongly suppressed as $t\to\infty$ (e.g.,\ $\evalat{g\opbrack{t}}{t=-2.5} \approx 10^{-7}$ and $\evalat{g\opbrack{t}}{t=-4.5} \approx 10^{-59}$).
		The trapezoidal rule is then applied to this transformed integrand within the window $t\in\closedinterval{-t_n}{t_n}$, for $n=2$ (5 evaluation points) as an illustration (top right).
		The abscissa $x_i$ of the original integration domain to which the equidistant points $t_i$ in the transformed domain correspond, can be recovered as $x_i = \Psi\opbrack{t_i}$ (bottom right and dotted connection lines).
		Notice that most of the abscissae $x_i$ end up very close to the domain boundaries.
		At those points, the effect of the original integrand is strongly suppressed because it is multiplied by $\Psi'\opbrack{t}$ (bottom right, grey).
		This results in the excellent convergence properties of the tanh-sinh quadrature, especially for integrands with singularities near the end-points.
	}
	\label{fig:transform}
\end{figure}

The tanh-sinh quadrature scheme has several advantages worth pointing out explicitly.
(i) The error estimate~\eqref{eq:errororder} shows that doubling the number of evaluation points also roughly doubles the number of significant digits, which makes the quadrature especially suited for high precision calculations.\cite{Bailey05,Bailey11}\@
(ii) Even when not all conditions for the error estimate~\eqref{eq:errororder} are strictly fulfilled by the integrand, convergence is still rather fast.
(iii) For integrands with end-point singularities tanh-sinh quadrature converges especially fast as compared to other schemes, thanks to its double-exponential suppression of these divergences (\Fig{fig:transform}).
(iv) The abscissae and weights can be directly extracted from the transformation formulas~\eqref{eq:abscissaeformula} and~\eqref{eq:weightsformula}, in contrast to some other quadrature schemes leaning on iterative processes.
(v) Finally, the transformed integral has equidistant abscissae (trapezoidal rule, \Fig{fig:transform}), which can straightforwardly be reused together with the weights and the function evaluations when doubling the quadrature order is required.

In the present paper we focus on the quadrature scheme as presented above and its generalization to multiple integration, although several extensions are possible.
In general, the transformation function $\Psi\opbrack{t}$ should be chosen such that the transformed integrand $g\opbrack{t}$ behaves like
\begin{equation}
	\abs{g\opbrack{t} }
	\propto
		\exp\opbrack[\Big]{-\frac{\pi}{2} \exp\opbrack{\abs{t}}}
	\quad\text{as}\quad
		t\to \pm \infty
\end{equation}
for the quadrature to be optimal.\cite{Takahasi73}\@
This observation underlies various transformations involving (semi-)infinite domains and integrands with peculiar behavior near the integration boundaries.\cite{Takahasi73}\@
Quadratures based on such transformations are called double exponential (DE) quadratures for obvious reasons.
A second extension is possible when the function $g\opbrack{t}$ has very different decay-rates for $t\to + \infty$ and $t\to -\infty$.
In that case, it can be beneficial to use an unequal number of evaluation points for $t>0$ and $t<0$ when truncating the series of the trapezoid rule~\eqref{eq:trapezoidalrule}.\@
The details, including error estimates with explicit constants, have been worked out by Okayama et al.\ \cite{Okayama13} and more information on asymmetric ranges can be found in Chapter~2 of the book on Sinc methods by Lund and Bowers.\cite{Lund92}\@
Finally, specific modifications for handling oscillatory~\cite{Ooura91,Ooura99} or indefinite integrals are discussed in refs.~\cite{Stenger81,Muhammad03}.

\section{Discussion}
\label{sec::practicals}

The above mentioned favorable properties of tanh-sinh quadrature can only be achieved if numerical instabilities are avoided, especially when dealing with finite-precision floating-point numbers.
Related caveats will be treated and resolved in the following subsection, together with the extension of tanh-sinh quadrature to multi-dimensional integration domains.

\subsection{Avoiding numerical instabilities}
\label{sec::instabilities}

The above presented $N$-point quadrature scheme can be summarized as
\begin{equation}
	\disctruncint{h}{n}
	=
		h\opbrack{n}
		\sum_{i=-n}^{+n}
			w_i
			f\opbrack{ x_i },
\end{equation}
the weights and abscissae respectively being $w_i = \tstrans'\opbrack{t_i}$ and $x_i = \tstrans\opbrack{t_i}$.
It depends crucially on evaluations very close to the end-points of the integration domain, where the density of abscissae $x_i$ is very high (\Fig{fig:transform}).
Correspondingly, one should be careful not to lose significant figures and therefore it is a good practice to store the array~\cite{Takahasi73}
\begin{equation}
	y_i
	=
		1 \pm x_i
	=
		\frac{
			\exp\opbrack[\Big]{\pm \frac{\pi}{2} \sinh{t_i}}
		}{
			\cosh\opbrack[\Big]{\frac{\pi}{2} \sinh{t_i}}
		}
	\quad \text{for}\quad
	i \lessgtr 0,
\end{equation}
containing the distances from the respective abscissae $x_i$ to the closest interval bound.

In the context of tanh-sinh quadrature, the most important cause of numerical instability is numerical underflow, occurring when numerical values become smaller then the underflow level (UFL)\@.
This smallest positive normalized floating-point number is $\minfl=2^{\minexp}$, where $\minexp$ is the smallest exponent representable in a given floating-point model.
Specifically, we need to pay attention to the weights, the abscissae and the function evaluations at these abscissae.
Both the smallest weight $w_{n}=\min\Set{w_i}$ and the smallest stored abscissa value $y_n=\min\Set{y_i}$ are determined by window size $t_{n}$, which bounds the transformed integration window $t_{i}\in\closedinterval{-t_{n}}{t_{n}}$.
We will determine a window limit $t_{\maximum}$ and choose $t_{n} \leqslant t_{\maximum}$ to avoid numerical underflow in a given floating-point model.

For the weights, numerical underflow is avoided if $w_n = \tstrans'\opbrack{t_{n}} \geqslant \minfl$, such that the corresponding window limit is
\begin{equation}\label{eq:condition_tw}
	t_{\maximum}^{w}
	=
		\max\Set{t \given \tstrans'\opbrack{t} \geqslant \minfl}
\end{equation}
Similarly, the smallest stored abscissa should exceed the UFL, $y_{n} = 1-x_{n} = 1-\tstrans\opbrack{t_{n}} \geqslant \minfl$, such that we can define
\begin{equation} \label{eq:condition_tx}
	t_{\maximum}^{x}
	=
		\max\Set[\big]{
			t
			\given
				t
				\leqslant
					\tstrans^{-1}\opbrack{1-\minfl}
		}
\end{equation}
as the window limit, where
\begin{equation}
	\tstrans^{-1}\opbrack{1-\minfl}
	=
		\sinh^{-1}\opbrack[\bigg]{
			\frac{
				\ln \opbrack[\big]{ \frac{2}{\minfl}-1 }
			}{
			\pi
			}
		}.
\end{equation}
Both window limits $t_{\maximum}^{w}$ and $t_{\maximum}^{x}$ are \emph{intrinsic} to the used floating-point model as well as to the dimensionality of the integral, as will be discussed in subsection ~\ref{sec::multi_dim}.
Since both conditions always apply simultaneously, it is convenient to introduce the intrinsic window limit $t_{\maximum}^{xw} = \min\Set{t_{\maximum}^{x},t_{\maximum}^{w} }$.
All above mentioned intrinsic quantities are listed in Table~\ref{tbl:maxoptvalues} for a few common floating-point models.
\begin{table}[!htb]
	\centering
	\caption{
		This table shows the smallest representable exponent $\minexp$, the corresponding underflow level $\minfl$, the window limits $t_{\maximum}^{x}$, $t_{\maximum}^{w}$ and $t_{\maximum}^{xw}$, and the corresponding intrinsic maximal optimal order $n_{\maximum}^{xw}$ for various floating-point (fp) models (IEEE 754-2008).
	}
	\begin{tabular}{ r c r@{}l c c c c c }
		\toprule
		fp model
		& $\minexp$	& \multicolumn{2}{c}{$\minfl$} & $\dimension$%
		& 	$t_{\maximum}^{x}$ & 	$t_{\maximum}^{w}$ &	$t_{\maximum}^{xw}$ &	$n_{\maximum}^{xw}$
		\\
		\midrule
		\multirow{2}{*}{single}	& \multirow{2}{*}{-126}	& \multirow{2}{*}{1.} & \multirow{2}{*}{$175\cdot 10^{-38}$}
		& 1,2	& 4.026	& 4.076	& 4.026	& 37
		\\
		&&&& 3	& 4.026	& 3.425	& 3.425	& 18
		\\
		\midrule
		\multirow{2}{*}{double} & \multirow{2}{*}{-1022}	& \multirow{2}{*}{2.} & \multirow{2}{*}{$225\cdot 10^{-308}$}
		& 1,2	& 6.112	& 6.121	& 6.112	& 442
		\\
		&&&& 3	& 6.112	& 5.437	& 5.437	& 201
		\\
		\midrule
		\multirow{2}{*}{extended} & \multirow{2}{*}{-16382}	& \multirow{2}{*}{3.} & \multirow{2}{*}{$362\cdot 10^{-4932}$}
		& 1,2	& 8.885	& 8.886	& 8.885	& 10228
		\\
		&&&& 3	& 8.885	& 8.194	& 8.194	& 4725
		\\
		\bottomrule
	\end{tabular}

	\label{tbl:maxoptvalues}
\end{table}

Besides the calculation of the abscissae and weights, also the integrand evaluations need to be carefully inspected in view of the numerical stability, which can be accomplished by a proper choice of another window limit $t_{\maximum}^{\evalabsc}$.
Examples will be discussed in section \Sec{sec::examples}.

Finally, we take $t_{\maximum} = \min\Set{t_{\maximum}^{xw},t_{\maximum}^{\evalabsc} }$ as the window limit, imposing the maximum on the transformed abscissae values $t_i$ as to avoid numerical instabilities.

\subsection{Multiple integration}
\label{sec::multi_dim}

Whereas higher dimensional integrals ($\dimension > 1$) are commonplace in physics,~\cite{Bailey11,Slevinsky15} we restrict ourselves to integration domains that are Cartesian products of lower dimensional domains, or that can be transformed to such a region by variable substitutions or coordinate transformations.
In these cases, it is possible to apply the one-dimensional quadrature rules repeatedly in each dimension, but, once again, caution is in order to avoid numerical underflow.
Slightly more complex multiple integrals, having boundaries that are integration variables in an exterior loop, are accessible through the use of indefinite integration.\cite{Muhammad05}

As an example in two dimensions, consider the integration of a function $f\opbrack{x,y}$ over the domain $-1 \leqslant x, y \leqslant 1$, which is typically approximated as
\begin{equation} \label{eq:multi_dimensional}
	\dint{-1}{1}{
		\dint{-1}{1}{
			f\opbrack{x,y}
		}{y}
	}{x}
	\approx
		\disctruncint{h_1,h_2}{n_1,n_2}
	=
		h_1 h_2
		\sum_{i=-n_1}^{+n_1}
		\sum_{j=-n_2}^{+n_2}
			w_i
			w_j
			f\opbrack{ x_i, x_j },
\end{equation}
where $n_1\neq n_2$ in general, although $n_1=n_2$ is often a very convenient choice.
While it is clear from Eq.~\eqref{eq:multi_dimensional} how to extend the quadrature rule to even higher dimensions, it is recommended to calculate the weights $w_i=\Psi'\opbrack{t_{i}}$ in each dimension separately, and multiply them with the function evaluation on the fly, as underflow problems may quickly arise otherwise.
It is reasonable to limit the weight-specific window limit $t_{\maximum}^{w}$, for which we take the same value in each direction, even more in higher dimensions (multiplying many very small weight quickly renders all the weights effectively zero).
A good value for $t_{\maximum}^{w}$ was found to be such that
\begin{equation}
	t_{\maximum}^{w} = \max \Set[\big]{t \given \nbrack{\tstrans'\opbrack{t}}^{\mathcal{D}} \geqslant \minfl }\text{, where }
		\mathcal{D}
		=
		\max\Set[\big]{1,\dimension-1},
\end{equation}
since then $w_n^\mathcal{D}\geqslant \minfl$.
This ensures that each weight separately exceeds the UFL\@.
For higher dimensions, the product of all but one weight should also be higher then the UFL in order that the corresponding term contribute to the sum.
In the latter case, one weight can be disregarded, because it can be compensated by a large value from the function evaluation.
Examples for one, two and three dimensions are given in Table~\ref{tbl:maxoptvalues}.

\subsection{Abscissae spacing}
\label{sec::hspacing}

For most quadrature schemes, the choice of the quadrature order $n$, together with a window size that is fixed by the integration bounds, determines the abscissae spacing $h$.
In the tanh-sinh quadrature schemes, the window size $\closedinterval{-t_{n}}{t_{n}}$ is not fixed a priori, with bounds $t_{n}=n \cdot h$ that depend themselves on the the abscissae spacing.
A smart choice for $h\opbrack{n}$ is indispensable.
We will discuss two alternatives: (i) the optimal $h_{\optimal}\opbrack{n}$ and (ii) the \emph{maximal} $h_{\maximum}\opbrack{n}$.

The optimal spacing
\begin{equation}
	\label{eq::h_opt_d}
	h\opbrack{n}
	=
	h_{\optimal}\opbrack{n}
	\coloneqq
	\frac{2}{N} \LambertW\opbrack{2 d N}
	,\quad\text{with } N=2n+1
\end{equation}
leads to the optimal error bounds~\eqref{eq:errororder} by making the discretization error $\discerror{h}$ and the truncation error $\truncerror$ contribute equally.\cite{Takahasi73,Sugihara97,Mori05,Trefethen14}\@
The Lambert W-function $\LambertW\opbrack{z}$ is implicitly defined as the solution of $z=w\expof{w}$ for $w$.
One should note here that, in most of literature, the optimal width is given by $h = \infrac{2}{N} \ln\opbrack{2 d N}$, which is only correct in the limit of large $N$.\cite{Trefethen14}\@
The optimal abscissae spacing~\eqref{eq::h_opt_d} depends on the strip width $d$ of regularity of the transformed integrand around the real axis.
Strictly speaking, this strip width needs to be specifically determined for each integral to achieve optimal convergence.
However, when the integrand is the result of a complex numerical routine, such a determination of $d$ is not always possible, let alone desirable when a large range of integrals needs to be calculated.
In practice, $d=\infrac{\pi}{2}$ often leads to reasonable convergence rates.
The optimal transformed abscissae spacing then becomes
\begin{equation} \label{eq::h_opt}
	h_{\optimal}\opbrack{n}
	=
		\frac{2}{N} \LambertW\opbrack{\pi N}
	,\quad\text{with } N=2n+1.
\end{equation}

While $h_{\optimal}\opbrack{n}$ is theoretically optimal in an infinite-precision context, its outstanding results can not always be achieved due to the limitations of floating point types.
The main problem is the window size $t_{n}= n h_{\optimal}\opbrack{n}$ that can quickly reach beyond the window limit $t_{\maximum}$, sometimes before full convergence is reached.
In order to avoid numerical instabilities, the optimal spacing should never be used for orders higher than $n_{\maximum}=\max\Set{n \given n h_{\optimal}\opbrack{n} \leqslant t_{\maximum}}$ at which this happens.
This maximal order can depend on numerical difficulties in the integrand evaluation through $t_{\maximum}$ and must therefore be determined on a per-case basis.
The maximal order itself is always limited by the intrinsic maximal order $n_{\maximum}^{xw}$ which is fully determined by the floating-point type and dimension (Table~\ref{tbl:maxoptvalues}).

As an alternative to the optimal spacing, we propose to use the maximal spacing
\begin{equation} \label{eq::h_max}
	h\opbrack{n}
	=
		h_{\maximum}\opbrack{n}
	\coloneqq
		\frac{t_{\maximum}}{n}
\end{equation}
instead.
Here, the window size $t_{n}$ is fixed, independent of the order $n$, to $t_{\maximum}$ so as to avoid numerical underflow.
Besides the obvious simplicity of the $h_{\maximum}\opbrack{n}$, also the reusability of function evaluations obtained from lower order estimates, is an advantage when it comes to doubling the order index $n$.

\section{Worked examples}
\label{sec::examples}

In this section, we demonstrate the use of the tanh-sinh quadrature as described above, specifically comparing results using optimal and maximal spacing for different floating-point types.
For each example integral, possible pitfalls and noteworthy features are discussed, while showing the relative error as a function of the quadrature order.
This relative error is intrinsically limited from below by the machine precision $\eps_{\text{m}}$, which is indicated by dashed lines for each floating-point models.
The results for optimal spacing can only be obtained up to the maximum quadrature order $n_{\maximum{}}$, which is shown as a larger data point if it falls within the shown domain.
Results obtained using double-precision Gauss-Legendre quadrature with the same number of evaluation points are always shown for comparison.
We focus on integrands with a singularity near the origin of the original integration domain, as may be relevant to solve numerous problems in physics.
For benchmarking purposes, it is not the intent to evaluate the specific integral, but rather to demonstrate how the quadrature behaves for different types of limiting behavior.

\subsection{Integrands with a non-integrable singularity}

Sometimes, one needs to integrate a function with a non-integrable singularity located close to one of the integration limits.
A straightforward example of this type is the integral
\begin{equation} \label{eq:physics:overeps}
	\mathcal{I}_{\delta}
	=
		\dint{\delta}{1}{ \Over{x} }{x}
	=
		-\ln \delta,
\end{equation}
where $\delta$ is an arbitrarily small positive number.
In spite of $\ln \delta$ quickly diverging when $\delta$ tends to zero, tanh-sinh quadrature quickly yields remarkably high precision integrals when the integrand is sampled with many points near $x=\delta$ (\Fig{fig:epserror}).
This can, however, also be a source of errors: if the sampling points are closer to $x=\delta$ than machine precision permits, the function evaluations will be inaccurate.
To prevent this, $t_{\maximum}^{\evalabsc}$ should be chosen such that the abscissae $x=x_{-n}$ remains distinguishable from $x=\delta$.
For the errors shown in \Fig{fig:epserror}, we chose $t_{\maximum}^{\evalabsc}$ such that $\infrac{\nbrack{x_{-n} - \delta}}{\delta} > a\cdot\eps_{\text{m}}$, with $a=100$.
Increasing the value of $a$ diminishes the fluctuations of the resulting error, but increases the achieved minimal relative error.
\begin{figure}[!tb]
	\centering
	\includegraphics{figure_2}
	\caption{
		The relative error decreases exponentially with quadrature order $n$.
		Both optimal and maximal spacing yield similar convergence rates.
		However, whereas the maximal spacing reaches full precision for all floating-point types, the optimal spacing can barely converge because $n_{\maximum}$ is very small.
		The fluctuations as a function of $n$ decrease upon increasing $a$.
		No convergence can be distinguished when using Gauss-Legendre quadrature.
	}
	\label{fig:epserror}
\end{figure}

Notice in \Fig{fig:epserror} that $n_{\maximum}$ is very small and the corresponding error using optimal spacing is still far above the achievable error.
The use of the proposed maximal spacing does allow for a steady and fast decrease of the relative error up to full precision.
Gauss-Legendre quadrature fails completely in this test.
It lacks the fine-grained sampling around $x=\delta$.

\subsection{Multiple integration with singular integrands}

Next, we consider $\dimension$-dimensional integrals
\begin{equation}
	\mathcal{I}_{\dimension}
	=
		\int\limits_{\mathclap{\leftopeninterval{0}{1}^\dimension}}^{} \mrm{d}^{\dimension}\vec{r}\,f_{\dimension}\opbrack{\vec{r}},
\end{equation}
where the integrands are chosen to be
\begin{align*}
	f_1\opbrack{x}
	&=
		\frac{1}{\sqrt{x}}
	\\
	f_2\opbrack{x,y}
	&=
		\frac{1}{\sqrt{x^2+y^2}}
	\\
	f_3\opbrack{x,y,z}
	&=
		\frac{1}{x^2+y^2+z^2},
\end{align*}
all containing a integrable singularity at $\vec{r}=0$.
Integrals of the type $\mathcal{I}_{\dimension}$ are typically encountered in solid state physics, where integration over the first Brillouin zone is very common.
For example, in a recent study of anisotropic quantum Heisenberg ferromagnets~\cite{Vanherck18}, one of the integrands that determines the Curie temperature falls of in the same way as $f_3(\vec{r})$.
The results\footnote{%
	Elementary integration of the function class $f_{\dimension}\opbrack{\vec{r}}$ leads to $\mathcal{I}_1 = 2$, $\mathcal{I}_2 = 2 \ln(1 + \sqrt{2})$ and $\mathcal{I}_3 = 3 \nbrack{ \operatorname{Ti_2}\opbrack{3-2\sqrt{2}} - C} + \frac{3\pi}{4} \tanh^{-1}\opbrack{\frac{2 \sqrt{2}}{3}}$, where $\operatorname{Ti_2}\opbrack{x}$ and $C$ are respectively the inverse tangent integral and the Catalan constant.
} for each of these integrals are shown in \Fig{fig:divergence_fdim}.
The convergence rate is especially large for the one-dimensional integral, but also for higher dimensions the convergence is fast and steady up to maximum precision.
While no special numerical concerns apply to $f_1$, numerical underflow in the calculation of the denominator of $f_2$ and $f_3$ needs to be avoided by choosing $t_{\evalabsc}$ such that $x_{-n} \geqslant \sqrt{\minfl}$.
\begin{figure}[!tb]
	\begin{adjustwidth}{-3cm}{3cm}
	\centering
	\includegraphics{figure_3}
	\end{adjustwidth}
	\caption{
		The relative error as compared to the exact result for one-dimensional quadrature order $n$ for functions with an integrable singularity at the origin in one, two and three dimensions.
		The total number of sampling points in $\dimension$ dimensions is $(2n+1)^{\dimension}$.
		The quadrature with optimal spacing always converges slightly faster than the maximal spacing variant.
		However, it does not reach full precision for the single-precision floating-point type in the two- and three-dimensional examples, whereas the quadrature with $h_{\maximum}\opbrack{n}$ does.
		While still being fast, the convergence is more slow in higher dimensions.
		Gauss-Legendre quadrature converges, but only very slowly.
	}
	\label{fig:divergence_fdim}
\end{figure}

In the latter two cases, the maximal order $n_{\maximum{}}$ was reached in the single precision optimal scheme before convergence up to full precision was achieved.
In the double-precision equivalent, full precision is reached at an order just below $n_{\maximum{}}$.
All convergence rates seem to become lower when the dimensionality of the integral is increased.
This might be due to the fact that the choice of $d=\infrac{\pi}{2}{}$ in \Eq{eq::h_opt_d} is not optimal for the outer integrals.
The scheme using maximal spacing always reaches full precision.
Even though its convergence rate is somewhat slower than the optimal (especially for the extended precision calculation), it still has a decent performance.
In every case, Gauss-Legendre quadrature converges, be it at a much lower rate than the tanh-sinh quadratures.

\section{Conclusion}
\label{sec::conclusion}

We have reviewed tanh-sinh quadrature, which is very efficient for integrands with end-point singularities, that are often encountered in physics.
We demonstrated that even limited-precision floating-point arithmetic facilitates fast convergence at machine precision levels.
Extending the scheme to higher dimensions turns out to be straightforward, although the convergence rate is typically lower as a function of the quadrature order.

We showed that care has to be taken to avoid numerical underflow in the abscissae and the weights, as well as to avoid other numerical instabilities related to integrand evaluations.
These problems can typically be resolved straightaway by choosing a suitable window limit $t_{\maximum}$.
For the optimal spacing $h_{\optimal{}}\opbrack{n}$, this implies a maximum usable quadrature order $n_{\maximum}$, sometimes hampering convergence.
The proposed maximal spacing rule $h_{\maximum}\opbrack{n}$ alleviates this limitation.
Despite having slightly slower convergence rates, this rule achieves full machine precision accuracy more consistently.
Moreover, its implementation is even more straightforward and it allows for the reuse of abscissae, weights and function evaluations when it comes to recompute a particular integral for increasing quadrature orders, as is often convenient to monitor convergence.

Numerical Analysis (math.NA); Computational Physics (physics.comp-ph)


Maximum Eccentric Connectivity Index for Graphs with Given Diameter

Abstract: The eccentricity of a vertex $v$ in a graph $G$ is the maximum distance between $v$ and any other vertex of $G$. The diameter of a graph $G$ is the maximum eccentricity of a vertex in $G$. The eccentric connectivity index of a connected graph is the sum over all vertices of the product between eccentricity and degree. Given two integers $n$ and $D$ with $D\leq n-1$, we characterize those graphs which have the largest eccentric connectivity index among all connected graphs of order $n$ and diameter $D$. As a corollary, we also characterize those graphs which have the largest eccentric connectivity index among all connected graphs of a given order $n$.

\section{Introduction}
Let $G=(V,E)$ be a simple connected undirected graph. The \emph{distance} $\dist(u,v)$
between two vertices $u$ and $v$ in $G$ is the number of edges of a shortest path in $G$ connecting $u$ and $v$.
The \emph{eccentricity}
$\ecc(v)$ of a vertex $v$ is the maximum distance between $v$ and
any other vertex, that is $\max \{ \dist(v, w) ~|~ w \in V \}$. The
\emph{diameter}
of $G$ is the maximum eccentricity among all
vertices of $G$.
The \emph{eccentric connectivity index} $\eci(G)$ of $G$ is defined by
\[
\eci(G) = \sum_{v \in V} \dg(v) \ecc(v).
\]
This index was introduced by Sharma {\emph{et al.}}~in~\cite{Sharma97}. Alternatively,
$\eci$ can be computed by summing the eccentricities of the extremities
of each edge:
\[
\eci(G) = \sum_{vw \in E} \ecc(v) + \ecc(w).
\]
We define the weight of a vertex by $\W(v) = \dg(v) \ecc(v)$, and we thus have $\eci(G) = \sum_{v \in V} \W(v)$.
Morgan {\emph{et al.}}~\cite{Morgan2011} give the following asymptotic
upper bound on $\eci(G)$ for a graph $G$ of order $n$ and with a given diameter $D$.

\begin{thm}[Morgan, Mukwembi and Swart, 2011~\cite{Morgan2011}]
  Let $G$ be a connected graph of order $n$ and diameter $D$. Then,
  \[
    \eci(G) \le D(n-D)^2 + {\mathcal{O}}(n^2).
  \]
\end{thm}
In what follows, we write $G \simeq H$ if $G$ and $H$ are two isomorphic
graphs, and we let $\K{n}$ and $\Path{n}$ be the \emph{complete graph} and the
\emph{path} of order $n$, respectively.  We refer to Diestel~\cite{Diestel00}
for basic notions of graph theory that are not defined here.
A \emph{lollipop} $L_{n,D}$ is a graph obtained from a path $\Path{D}$ by
joining an end vertex of this path to $\K{n-D}$. Morgan {\emph{et al.}}~\cite{Morgan2011}
state that the above asymptotic bound is best possible by showing that
$\eci(L_{n,D}) = D(n-D)^2 + {\mathcal{O}}(n^2)$. The aim of this paper is to give a
precise upper bound on $\eci(G)$ in terms of $n$ and $D$, and to completely
characterize those graphs that attain the bound. As a result, we will observe
that there are graphs $G$ of order $n$ and diameter $D$ such that $\eci(G)$
is strictly larger than $\eci(L_{n,D})$.

Morgan {\emph{et al.}}~\cite{Morgan2011} also give an asymptotic upper bound on $\eci(G)$ for graphs $G$ of order $n$ (but without a fixed diameter), and show that this bound is sharp by observing that it is attained by $L_{n,\frac{n}{3}}$.

\begin{thm}[Morgan, Mukwembi and Swart, 2011~\cite{Morgan2011}]
  Let $G$ be a connected graph of order $n$. Then,
  \[
  \eci(G) \le \frac{4}{27}n^3 + {\mathcal{O}}(n^2).
  \]
\end{thm}
We give a precise upper bound on $\eci(G)$ for graphs $G$ of order $n$,
and characterize those graphs that reach the bound. As a corollary, we show
that for every lollipop, there is another graph $G$ of same order, but with
a strictly larger eccentric connectivity index.

\section{Results for a fixed order and a fixed diameter}

The only graph with diameter $1$ is the clique, and clearly,
$\eci(\K{n})=n(n-1)$. Also, the only connected graph with $3$ vertices and
diameter 2 is $\Path{3}$, and $\eci(\Path{3})=\eci(\K{3})=6$.  The next
theorem characterizes the graphs with maximum eccentric connectivity index
among those with $n\geq 4$ vertices and diameter $2$. Let  $\M{n}$ be the graph obtained from $\K{n}$ by removing
a maximum matching ({\emph{i.e., }} $\lfloor\frac{n}{2}\rfloor$ disjoint edges) and,
if $n$ is odd, an additional edge adjacent to the unique vertex that still
has degree $n-1$. In other words, all vertices in $\M{n}$ have degree $n-2$,
except possibly one that has degree $n-3$. For illustration, $\M{6}$ and $\M{7}$ are drawn in Figure \ref{fig:specialcases}.
\begin{figure}[!h]
  \begin{center}
  \begin{tikzpicture}
    \begin{scope}
    \graph[empty nodes] {
      subgraph C_n [n=4, clockwise] -- mid;
    };
    \node at (0, -1.6) {\(H_1\)};
    \end{scope}
    \begin{scope}[xshift={3cm}]
    \graph[empty nodes, no placement] {
      {subgraph K_n [n=4, clockwise, phase=45,
              nodes={xshift={1.5cm}}]};
      a -- {3, 4};
      {1, 2} -- b [x=3];
    };
    \node at (1.5, -1.6) {\(H_2\)};
    \end{scope}
    \begin{scope}[xshift={7.5cm}]
    \graph[empty nodes] {
      a -- {[nodes={yshift=1cm}] 1, 2, 3} --[complete bipartite]
      {[nodes={yshift=0.5cm}] 4, 5} -- b;
      4 -- 5;
      1 -- 2 -- 3;
      1 --[bend right] 3
    };
    \node at (1.5, -1.6) {\(H_3\)};
    \end{scope}
    \begin{scope}[yshift={-3.7cm}]
    \begin{scope}
    \graph[simple, empty nodes] {
      subgraph K_n [n=6, clockwise, phase=120],
        1 -!- 2, 3 -!- 4, 5 -!- 6;
     };
     \node at (0, -2) {\M{6}};
    \end{scope}
    \begin{scope}[xshift={4.5cm}]
    \graph[simple, empty nodes] {
      subgraph K_n [n=7, clockwise],
      1 -!- 2, 3 -!- 4, 5 -!- 6, 7 -!- 1;
    };
    \node at (0, -2) {\M{7}};
    \end{scope}
    \begin{scope}[xshift={7cm},yshift={-1cm},new set=K]
    \graph[empty nodes]{
      subgraph P_n[n=5,branch right,name=P],
      {[nodes={set=K}, no placement, clique]
        1[y=1], 2[x=1,y=1], 3[x=0.5, y=1.87cm]},
      {P 1, P 2} --[complete bipartite] (K),
      P 3 --[dashed] (K);
    };
    \foreach \v [count=\i from 0] in {1,...,5}{
      \node[below=.01cm of P \v] {\(u_\i\)};
    }
    \node at (2, -1) {\extG{8}{4}{k}};
    \end{scope}
    \end{scope}
  \end{tikzpicture}
  \end{center}
\vspace{-0.5cm}\caption{Graphs $H_1, H_2, H_3$, $\M{6}$, $\M{7}$ and $\extG{8}{4}{k}$, (dashed edges depend
  on $k$)}
  \label{fig:specialcases}
\end{figure}

\begin{thm}
  Let $G$ be a connected graph of order $n\geq 4$ and diameter $2$. Then,
  \[
  \eci(G) \le 2n^2-4n-2(n\bmod 2)
  \]
  with equality if and only if $G\simeq\M{n}$ or $n=5$ and
  $G\simeq H_1$ (see Figure~\ref{fig:specialcases}).
\end{thm}
\begin{proof}
  Let $G$ be a graph of order $n$ and diameter $2$, and let $x$ be the number of vertices of degree $n-1$ in $G$. Clearly, $W(v)=n-1$ for all  vertices $v$ of degree $n-1$, while $W(v)\leq 2(n-2)$ for all other vertices $v$. Note that if $n-x$ is odd, then at least one vertex in $G$ has degree at most $n-3$. Hence,
\vspace{-0.2cm}  \begin{align*}
  \eci(G)&\leq x(n-1)+2(n-x)(n-2)-2((n-x)\bmod 2)\\
  &= 2n^2-4n+x(3-n)-2((n-x)\bmod 2).
  \end{align*}
  For $n=4$ or $n\geq 6$, this value is maximized with $x=0$. For $n=5$, both $x=1$ ({\emph{i.e., }} $G\simeq H_1$) and $x=0$ ({\emph{i.e., }} $G\simeq \M{5}$) give the maximum value $28=2n^2-4n+(3-n)-2((n-1) \bmod 2)=2n^2-4n-2(n \bmod 2)$.
\end{proof}

Before giving a similar result for graphs with diameter $D\geq 3$, we prove the following useful property.

\begin{lem} \label{lem:outsiders_connections_to_P}
  Let \(G \) be a connected graph of order \(n \geq 4\) and diameter
  $D\geq 3$. Let \(P\) be a shortest path in \(G\)  between two vertices at distance $D$, and assume there is a vertex \(u\) on \(P\) such that $\ecc(u)$ is strictly larger than the longest distance $L$ from \(u\) to an extremity of \(P\). Finally, let $v$ be a vertex in $G$ such that $d(v,u)=\ecc(u)$ and let $v=w_1 - w_2 -\ldots - w_{\ecc(u)+1}=u$ be a path of length $\ecc(u)$ linking $v$ to $u$ in $G$. Then
  \begin{shortlist}
    \item vertices $w_1, \ldots, w_{\ecc(u)-L}$ do not belong to \(P\);
    \item vertex $w_{\ecc(u)-L}$ has either no neighbor on \(P\), or its
    unique neighbor on \(P\) is an extremity at distance $L$ from $u$;
    \item if $\ecc(u)-L>1$ then vertices $w_1, \ldots, w_{\ecc(u)-L-1}$ have no neighbor on \(P\).
  \end{shortlist}
\end{lem}
\begin{proof}
  No vertex $w_i$ with $1\leq i \leq \ecc(u)-L$ is on $P$, since this would imply $d(u,w_i)\leq L$, and hence $d(u,v)=d(u,w_1)\leq L+i-1\leq \ecc(u)-1$. Similarly, no vertex $w_i$ with $1\leq i \leq \ecc(u)-L-1$ has a neighbor on $P$, since this would imply $d(u,w_i)\leq L+1$, and hence $d(u,v)=d(u,w_1)\leq L+1+i-1\leq \ecc(u)-1$. If vertex $w_{\ecc(u)-L}$ has at least one neighbor on $P$, then this neighbor is necessarily an extremity of $P$ at distance $L$ from $u$, else we would have $d(u,w_{\ecc(u)-L})\leq L$, which would imply $d(u,v)=d(u,w_1)\leq L+(\ecc(u)-L-1)=\ecc(u)-1$. We conclude the proof by observing that if both extremities of $P$ are at distance $L$ from $u$, then  $w_{\ecc(u)-L}$ is adjacent to at most one of them since $D\geq 3$.
	\end{proof}

Let $n, D$ and $k$ be integers such that $n \ge 4$, $3 \le D
\le n-1$ and $0 \le k \le n-D-1$, and let $\extG{n}{D}{k}$ be
the graph (of order $n$ and diameter $D$) constructed from a path
$u_0-u_1-\ldots-u_D$ by joining each vertex of a clique $\K{n-D-1}$
to $u_0$ and $u_1$, and $k$ vertices of the clique to $u_2$ (see
Figure~\ref{fig:specialcases}). Observe that $\extG{n}{D}{0}$ is the
lollipop $L_{n, D}$ and that $\extG{n}{D}{n-D-1}$ can be viewed as a
lollipop with a missing edge between $u_0$ and $u_2$. Also, if $D = n-1$,
then $k = 0$ and $\extG{n}{n-1}{0} \simeq \Path{n}$.

\begin{figure}[!h]
  \label{fig:extr_graphs}
\end{figure}

\begin{lem}
  \label{lem:eci_for_extremal_graph}
  Let $n, D$ and $k$ be integers such that $n \ge 4$, $3 \le D \le n-1$
  and $0 \le k \le n-D-1$, then
  \begin{align*}
    \eci(\extG{n}{D}{k}) ={}&
      2\sum_{i=0}^{D-1}\max\{i,D-i\}+\Big(n-D-1\Big)\Big( 2D-1+D(n-D)\Big) \\
    & +k \Big(2D-n-1+\max\{2,D-2\}\Big).
  \end{align*}
\end{lem}

\begin{proof}
The sum of the weights of the vertices outside $P$ is
\begin{align*}
  \sum_{v \in V \setminus V(P)} \W(v) &= k \left(
    n-D+1\right)\left(D-1\right) + (n-D-1-k) \left(n-D\right) D,\\
  &= k \left( 2D-n-1 \right) + (n-D-1)(n-D)D.\\
\end{align*}
We now consider the weights of the vertices in $P$. The weight of $u_0$ is $D(n-D)$, the weight of $u_1$ is $(D-1)(n-D+1)$, and the weight of $u_2$ is
$(k+2)\max\{2,D-2\}$. The weight of $u_i$ for $i=3,\ldots,D-1$ is $2\max\{i,D-i\}$, and the weight of $u_D$ is $D$.
Hence, the total weight of the vertices on $P$ is
\begin{align*}
   & (n-D)D+(n-D+1)(D-1)+(k+2)\max\{2,D-2\}+2\sum_{i=3}^{D-1}\max\{i,D-i\}+D\\
={}& \Big((n-D-1)D+D\Big)+\Big((n-D-1)(D-1)+2(D-1)\Big)\\
   & +\Big(k\max\{2,D-2\}+2\max\{2,D-2\}\Big)+2\sum_{i=3}^{D-1}\max\{i,D-i\}+D\\
={}& 2\sum_{i=0}^{D-1}\max\{i,D-i\}+(n-D-1)(2D-1)+k\max\{2,D-2\}
\end{align*}
By summing up all weight in $G$, we obtain the desired result.
\end{proof}

In what follows, we denote $\displaystyle f(n,D)=\max_{k=0}^{n-D-1}\eci(\extG{n}{D}{k})$. It follows from the above lemma that
\[
f(n,D)=
\begin{cases}
14+\Big(n-4\Big)\Big(3n-4+\max\{0,2D-n+1\}\Big)&\text{if }D=3;\\[3.5ex]
\begin{split}
&2\sum_{i=0}^{D-1}\max\{i,D-i\}\\
&\quad+\Big(n-D-1\Big)\Big( 2D-1+D(n-D)+\max\{0,3D-n-3\}\Big)
\end{split} &
\text{if }D\geq 4.
\end{cases}
\]
Lemma~\ref{lem:eci_for_extremal_graph} allows to know for which values of $k$
we have $ \eci(\extG{n}{D}{k})=f(n,D)$.

\begin{cor}
  Let $n$ and $k$ be integers such that $n \ge 4$,
  and $0 \le k \le n-4$.
  \begin{shortlist}
    \item If $n < 7$, then $\eci(\extG{n}{3}{k}) \le
      f(n,3)=2n^2\!-\!5n+2$, with equality if and only if  $k = n-4$.
    \item If $n > 7$, then $\eci(\extG{n}{3}{k}) \le
      f(n,3)=3n^2-16n+30$ with equality if and only if $k=0$.
    \item If $n = 7$, then all $\eci(\extG{n}{3}{k})$
      are equal to $65$ for $k = 0, \ldots, n-4$.
  \end{shortlist}
\end{cor}

\begin{cor} \label{cor:max_k}
  Let $n, D$ and $k$ be integers such that $n \ge 5$, $4 \le D \le n-1$
  and $0 \le k \le n-D-1$.
  \begin{shortlist}
    \item If $n < 3(D-1)$, then $\eci(\extG{n}{D}{k})=f(n,D)$ if and only
      if  $k = n-D-1$.
    \item If $n > 3(D-1)$, then $\eci(\extG{n}{D}{k})=f(n,D)$ if and only
      if $k=0$.
    \item If $n = 3(D-1)$, then $\eci(\extG{n}{D}{k})=f(n,D)$ if and only
      if $k\in\{ 0, \ldots, n-D-1\}$.
  \end{shortlist}
\end{cor}

The graph $H_2$ of Figure \ref{fig:specialcases} has $6$ vertices, diameter $3$, and is not isomorphic to $\extG{6}{3}{k}$, while  $\eci(H_2)=f(6,3)=44$. Similarly, the graph $H_3$ of Figure \ref{fig:specialcases} has $7$ vertices, diameter $3$, and is not isomorphic to $\extG{7}{3}{k}$, while  $\eci(H_3)=f(7,3)=65$. In what follows, we prove that all graphs $G$ of order $n$ and diameter $D\geq 3$ have $\eci(G)\leq f(n,D)$. Moreover, we show that if $G$ is not isomorphic to a $\extG{n}{D}{k}$, then equality can only occur if $G\simeq H_2$ or $G \simeq H_3$. So, for every $n\geq 4$ and $3\leq D\leq n-1$, let us consider the following graph class $\mathcal{C}_n^D$:
\[
\mathcal{C}_n^D=
\begin{cases}
  \{\extG{n}{3}{n-4}\} & \text{if } n=4,5  \text{ and } D=3 ;\\
  \{\extG{n}{3}{2},H_2\} & \text{if } n=6  \text{ and } D=3 ;\\
  \{\extG{n}{3}{0},\ldots,\extG{n}{3}{3},H_3\} & \text{if } n=7  \text{ and }  D=3 ;\\
  \{\extG{n}{3}{0}\} & \text{if } n>7  \text{ and }  D=3 ;\\
  \{\extG{n}{D}{n-D-1}\} & \text{if } n<3(D-1) \text{ and }  D\geq 4 ;\\
  \{\extG{n}{D}{0},\ldots,\extG{n}{D}{n-D-1}\} & \text{if } n=3(D-1)  \text{ and }  D\geq 4 ;\\
  \{\extG{n}{D}{0}\}  & \text{if } n > 3(D-1)  \text{ and }  D\geq 4 .
\end{cases}
\]
Note that while Morgan {\emph{et al.}}~\cite{Morgan2011} state that the lollipops
reach the asymptotic upper bound of the eccentric connectivity index, we
will prove that they reach the more precise upper bound only if $D=n-1$,
$D=3$ and $n\geq 7$, or $D\geq 4$ and $n\geq 3(D-1)$.
\begin{thm} \label{thm:ub_diam}
  Let \(G\) be a connected graph of order \(n \ge 4\) and diameter
  \(3 \le D \le n-1\). Then $\eci(G) \le f(n,D)$, with equality if and only if $G$ belongs to $\mathcal{C}_n^D$.
\end{thm}

\begin{proof}
We have already observed that all graphs $G$ in $\mathcal{C}_n^D$ have $\eci(G)= f(n,D)$. So let $G$ be a graph of order $n$, diameter $D$ such that $\eci(G)\geq f(n,D)$. It remains to prove that $G$ belongs to $\mathcal{C}_n^D$.

Let \(P=u_0 - u_1 - \dots - u_D\) be a shortest path in \(G\) that connects two vertices $u_0$ and $u_D$ at distance $D$ from each other. In what follows, we use the following notations for all $i=0,\ldots,D$:
\begin{shortlist}
	\item \(o_i\) is the number of vertices outside $P$ and adjacent to $u_i$;
	\item \(\delta_i = \max\{i, D-i\}\);
	\item $r_i=\ecc(u_i)-\delta_i$.
\end{shortlist}
Also, let $r^*=\max_{i=1}^{D-1}r_i$. Note that $\delta_i\geq 2$ and $r_i\leq \lfloor\frac{D}{2}\rfloor$ for all $i$, and $r_0=r_D=0$ since $\ecc(u_0)=\ecc(u_D)=\delta_0=\delta_D=D$. Since $P$ is a shortest path linking $u_0$ to $u_D$, no vertex  outside $P$ can have more than three neighbors in $P$. We consider the following partition of the vertices outside $P$ in 4 disjoint sets $V_0,V_{1,2},V_3^{D-1},V_3^D$, and denote by $n_0,n_{1,2},n_3^{D-1},n_3^D$ their respective size:
\begin{shortlist}
	\item $V_0$ is the set of vertices outside $P$ with no neighbor on $P$;
	\item $V_{1,2}$ is the set of vertices outside $P$ with one or two neighbors in $P$;
	\item $V_3^{D-1}$ is the set of vertices $v$ outside $P$ with three neighbors in $P$ and $\ecc(v)\leq D-1$;
	\item $V_3^{D}$ is the set of vertices $v$ outside $P$ with three neighbors in $P$ and $\ecc(v)=D$.
\end{shortlist}

\vspace{0.2cm}Clearly, all vertices $v$ outside $P$ can have $\ecc(v)=D$ except those in $V_3^{D-1}$. The maximum degree of a vertex in $V_0$ is $n-D-2$, while it is $n-D$ for those in $V_{1,2}$ and $n-D+1$ for those in $V_3^{D-1}\cup V_3^{D}$.
For a vertex $v\in V_{1,2} \cup V_3^{D-1} \cup V_3^{D}$, let
\begin{gather*}
  \rho(v)=\max\{r_i \;|\; u_i \text{ is adjacent to }v\}\\
  \rho^*=\max_{v\in V_{1,2} \cup V_3^{D-1} \cup V_3^{D}}\rho(v)
\end{gather*}
Hence, $r^*\geq \rho^*$.
We first show that the total weight of the vertices in $V_0\cup V_{1,2}$ is at most
\begin{align*}
     &D(n-D)(n-D-1-n_3^{D-1}-n_3^{D})-2Dr^*+D\min\{1, \rho^*\}.

\end{align*}
\begin{itemize}
	\item If $r^*=0$, then the largest possible weight of the vertices in $V_0\cup V_{1,2}$ occurs when all of them have two neighbors in $P$ (i.e., $n_0=0$ and no vertex in $V_{1,2}$ has one neighbor on $P$). In such a case, $n_0+n_{1,2}=n-D-1-n_3^{D-1}-n_3^{D}$, and all these vertices have degree $n-D$. Hence, their total weight is at most $D(n-D)(n-D-1-n_3^{D-1}-n_3^{D})$.
	\item If $r^*>0$ and $\rho^*>0$, then let $i$ be such that $r_i=r^*$. It follows from Lemma \ref{lem:outsiders_connections_to_P} that there is a path $w_1 -\ldots - w_{\ecc(u_i)+1}$ such that $w_1,\ldots,w_{r^*-1}$ have no neighbor on $P$ and $w_{r^*}$ has at most one neighbor on $P$. Hence, the largest possible weight of the vertices in $V_0\cup V_{1,2}$ occurs when $r^*-1$ vertices have 0 neighbor on $P$, one vertex has one neighbor on $P$, and $n-D-1-n_3^{D-1}-n_3^{D}-r^*$ vertices have 2 neighbors in $P$. Hence, the largest possible weight for the vertices in $V_0\cup V_{1,2}$ is
	\begin{align*}
	   & D(n-D-2)(r^*-1)+D(n-D-1)+D(n-D)(n-D-1-n_3^{D-1}-n_3^{D}-r^*)\\
	={}& D(n-D)(n-D-1-n_3^{D-1}-n_3^{D})-2Dr^*+D.
	\end{align*}
	\item If $r^*>0$ and $\rho^*=0$, then consider the same path $w_1 -\ldots - w_{\ecc(u_i)+1}$ as in the above case. If $w_{r^*}$ has no neighbor on $P$, then there are at least $r^*$ vertices with no neighbor on $P$ and the largest possible weight for the vertices in $V_0\cup V_{1,2}$ is
	\[
	\begin{array}{l}
	D(n-D-2)(r^*)+D(n-D)(n-D-1-n_3^{D-1}-n_3^{D}-r^*)\\
	=D(n-D)(n-D-1-n_3^{D-1}-n_3^{D})-2Dr^*.
	\end{array}
	\]
	Also, if there are at least two vertices in $V_{1,2}$ with only one neighbor on $P$, then the largest possible weight for the vertices in $V_0\cup V_{1,2}$ is
	\[
	\begin{array}{l}
	D(n-D-2)(r^*-1)+2D(n-D-1)+D(n-D)(n-D-1-n_3^{D-1}-n_3^{D}-r^*-1)\\
	=D(n-D)(n-D-1-n_3^{D-1}-n_3^{D})-2Dr^*.
	\end{array}
	\]
	So assume $w_{r^*}$ is the only vertex in $V_{1,2}$ with only one neighbor on $P$. We thus have $\dist(u_i,w_{r^*})=\delta_i+1$. We now show that this case is impossible. We know from Lemma \ref{lem:outsiders_connections_to_P} that $w_{r^*}$ is adjacent to $u_0$ or (exclusive) to $u_D$. Since $\rho(v)=0$ for all vertices $v$ outside $P$, we know that $u_i$ has no neighbor outside $P$. Hence, $w_{\ecc(u_i)}$ is $u_{i-1}$ or $u_{i+1}$, say $u_{i+1}$ (the other case is similar). Then $w_{r^*}$ is not adjacent to $u_0$ else there is $j$ with $r^*+1\leq j \leq \ecc(u_i)-1$ such that $w_j$ is outside $P$ and has $w_{j+1}$ as neighbor on $P$, and since $w_j$ must have a second neighbor $u_{\ell}$ on $P$ with $\ell\geq i+2$, we would have
	\[
	i+2\leq \ell=\dist(u_0,u_{\ell})\leq \dist(w_{r^*},w_j)+2 \leq (\dist(w_{r^*},u_i)-2)+2=i+1.
	\]

	Hence, $w_{r^*}$ is adjacent to $u_D$. Then there is also a path linking $u_i$ to $w_1$ going through $u_{i-1}$ else $d(u_0,w_1) = d(u_0,u_i)+d(u_i,w_1)>i+\delta_i\geq D$. Let $Q$ be such a path of minimum length. Clearly, $Q$ has length at least equal to $\ecc(u_i)$. So let $w'_1 - \ldots - w'_{\ecc(u_i)+1}$ be the subpath of $Q$ of length $\ecc(u_i)$ and having $u_i$ as extremity (i.e., $w'_{\ecc(u_i)}=u_{i-1}$ and $w'_{\ecc(u_i)+1}=u_{i}$). Applying the same argument to $w'_{r^*}$ as was done for $w_{r^*}$, we conclude that $w'_{r^*}$ has $u_0$ as unique neighbor on $P$. We thus have two vertices in  $V_{1,2}$ with a unique neighbor on $P$, a contradiction.
\end{itemize}

The total weight of the vertices in $V_3^{D-1}\cup V_3^{D}$ is at most
$(n-D+1)\Big((D-1)n_3^{D-1}+Dn_3^{D}\Big)$, which gives the following upper bound $B$ on the total weight of the vertices outside $P$:
\begin{align*}
  B={}& D(n-D)(n-D-1-n_3^{D-1}-n_3^{D})+(n-D+1)\Big((D-1)n_3^{D-1}+Dn_3^{D}\Big)\\
      &-2Dr^*+D\min\{1,\rho^*\}\\
   ={}&(n-D-1)D(n-D)+n_3^{D-1}(2D-n-1)+Dn_3^{D}-2Dr^*+D\min\{1,\rho^*\}.
\end{align*}
This bound can only be reached if all vertices outside $P$ are pairwise adjacent. But Lemma~\ref{lem:outsiders_connections_to_P} shows that this cannot happen if $\rho^*>0$. Indeed, consider a vertex $v$ in $V_{1,2}\cup V_{3}^{D}\cup V_{3}^{D-1}$ with $\rho(v)>0$. There is  a vertex $u_i$ in $P$ adjacent to $v$ such  that $\rho(v)=r_i=\ecc(u_i)-\delta_i>0$. We know from Lemma \ref{lem:outsiders_connections_to_P} that there is a shortest path $w_1 - w_2 -\ldots - w_{\ecc(u_i)+1}=u_i$ linking $u_i$ to a vertex $w_1$ with $d(u_i,w_1)=\ecc(u_i)$ and such that $w_1, \ldots, w_{\rho(v)}$ do not belong to \(P\). In what follows, we denote $Q^v$ such a path. If $v$ is adjacent to a $w_j$ with $1\leq j\leq \rho(v)$, then the path $u_i - v - w_j - \ldots - w_1$ links $u_i$ to $w_1$ and has length at most $\rho(v)+1<r_i+\delta_i=\ecc(u_i)$, a contradiction. Hence $v$ has at least $\rho(v)$ non-neighbors outside $P$. Also, as shown in Lemma \ref{lem:outsiders_connections_to_P}, $w_1,\ldots, w_{\rho(v)-1}$ belong to $V_0$, while $w_{\rho(v)}$ belongs to $V_0\cup V_{1,2}$. In the upper bound $B$, we have assumed that $\ecc(w_1)=\ldots=\ecc(w_{\rho(v)})=D$. Hence, if $v\in V_{1,2}\cup V_{3}^{D}$, we can gain $2D$ units on $B$ for every $w_j$, $j=1,\ldots,\rho(v)$ ($D$ for $v$ and $D$ for $w_j$), while the gain is $2D-1$ ($D-1$ for $v$ and $D$ for $w_j$) if $v\in V_{3}^{D-1}$.

We can gain an additional $2D$ for every $v\in V_{3}^{D}$. Indeed, consider such a vertex $v$ and let $w^*$ be a vertex at distance $D$ from $v$. Note that $w^*$ is not on $P$ and has at most one neighbor on $P$ else $d(v,w^*)\leq D-1$. Hence, if $\rho(v)=0$, we can gain $2D$ (one $D$ for $v$ and one $D$ for $w$) in the above upper bound. So assume $\rho(v)>0$, and consider again the shortest path $Q^v=w_1 - w_2 -\ldots - w_{\ecc(u_i)+1}=u_i$, with $\rho(v)=r_i$. Also, let $W=\{w_1,\ldots,w_{\rho(v)}\}$. To gain an additional $2D$, it is sufficient to determine a vertex in $(V_0\cup V_{1,2})\setminus W$ which is not adjacent to $v$. So assume no such vertex exists, and let us prove that such a situation cannot occur. Note that $w^*\notin V_{3}^{D}\cup V_{3}^{D-1}$ (since it has at most one neighbor on $P$), which implies
$w^*\in W$.

	\begin{itemize}
		\item If a vertex $w_j\in W$ has a neighbor $x\in V_0\cup V_{1,2}$ outside $W$, then $v$ is adjacent to $x$, and the path $v - x - w_j - \ldots - w^*$ has length at most $1+\rho(v)\leq 1+\lfloor\frac{D}{2}\rfloor<D$, a contradiction.
		\item If a vertex $w_j\in W$ has a neighbor $x\in V_3^D\cup V_3^{D-1}$, then $d(u_i,w_1)\leq d(u_i,x)+d(x,w_1)\leq \delta_i-1+r_i<\ecc(u_i)$, a contradiction.
	\end{itemize}
	Since $G$ is connected and $w_1,\ldots,w_{\rho(v)-1}$ have no neighbors outside $Q^v$, we know that $w_{\rho(v)}$ is adjacent to the extremity of $P$ at distance $\delta_i$ from $u_i$ (and to no other vertex on $P$). Hence, the vertices on $P$ and those in $W$ induce a path of length $D+\rho(v)>D$ in $G$, a contradiction.

In summary, the following value is a more precise upper bound on the total weight of the vertices outside $P$:
\begin{align*}
&B-\sum_{v\in V_{1,2}\cup V_{3}^{D}}2D\rho(v)-\sum_{v\in V_{3}^{D-1}}(2D-1)\rho(v)-2Dn_{3}^{D}\\
\leq{}& (n-D-1)D(n-D)+n_3^{D-1}(2D-n-1)-Dn_3^{D}-2Dr^*+D\min\{1,\rho^*\}\\
&\quad-\sum_{v\in V_{1,2}\cup V_{3}^{D}\cup V_{3}^{D-1}}(2D-1)\rho(v).
\end{align*}
Let us now consider the vertices on $P$. We have $W(u_0)=D(1+o_0)$, $W(u_D)=D(1+o_D)$, and $W(u_i)=\ecc(u_i)(2+o_i)$ for $i=1,\ldots,D-1$. Since $\ecc(u_i)=\delta_i+r_i$, the total weight of the vertices on $P$ is
\begin{align*}
& 2D+D(o_0+o_D)+\sum_{i=1}^{D-1}(\delta_i+r_i)(2+o_i)\\
={}& 2\sum_{i=0}^{D-1}\delta_i+2\sum_{i=1}^{D-1}r_i+\sum_{i=1}^{D-1}r_io_i+\sum_{i=0}^{D}\delta_io_i.
\end{align*}
Each edge that links a vertex $v$ outside $P$ to a vertex $u_i$ in $P$ contributes for $r_i\leq \rho(v)$ in the sum $\sum_{i=1}^{D-1}r_io_i$. Hence,
\[
\sum_{i=1}^{D-1}r_io_i \leq \sum_{v\in V_{1,2}}2\rho(v) +
\sum_{v\in  V_{3}^{D}\cup V_{3}^{D-1}}3\rho(v) \leq
\sum_{v\in V_{1,2}\cup V_{3}^{D}\cup V_{3}^{D}}3\rho(v).
\]
Since $2\sum_{i=1}^{D-1}r_i\leq 2r^*(D-1)$, we get the following valid upper bound on the total weight of the vertices on $P$:
\[
2\sum_{i=0}^{D-1}\delta_i+ \sum_{i=0}^{D}\delta_io_i +2r^*(D-1) +
\sum_{v\in V_{1,2}\cup V_{3}^{D}\cup V_{3}^{D}}3\rho(v).  \]
Summing up the bounds for the vertices outside $P$ with those on $P$, we get the following upper bound for the total weight of the vertices in $G$:
\begin{multline*}
(n-D-1)D(n-D)+n_3^{D-1}(2D-n-1)-Dn_3^{D} +
2\sum_{i=0}^{D-1}\delta_i + \sum_{i=0}^{D}\delta_io_i \\
-\sum_{v\in V_{1,2}\cup V_{3}^{D}\cup V_{3}^{D-1}}(2D-4)\rho(v) -
2r^*+D\min\{1,\rho^*\}.
\end{multline*}
Let us decompose this bound into two parts $A_1+A_2$ with $A_1$ being equal to the sum of the first terms of the above upper bound, and $A_2$ being equal to the sum of the last ones:
\begin{align*}
A_1 &= (n-D-1)D(n-D)+n_3^{D-1}(2D-n-1)-Dn_3^{D} +
  2\sum_{i=0}^{D-1}\delta_i+ \sum_{i=0}^{D}\delta_io_i\\
A_2 &= -\sum_{v\in V_{1,2}\cup V_{3}^{D}\cup V_{3}^{D-1}}(2D-4)\rho(v)
  -2r^*+D\min\{1,\rho^*\}.
\end{align*}
\begin{shortlist}
  \item If $r^*=0$, then $A_2=0$, which implies $A_1+A_2=A_1$.
  \item If $\rho^*>0$, then $A_2\leq 4-2D-2r^*+D=4-D-2r^*<0$, which implies
    $A_1+A_2<A_1$.
  \item If $r^*>0$ and $\rho^*=0$, then $A_2=-2r^*<0$, , which implies
    $A_1+A_2<A_1$.
\end{shortlist}

In summary, the best possible upper bound is $A_1$ and is attained only if $r^*=0$, $n_0=0$, $\ecc(v)=D$ for all vertices in $V_{1,2}$, and the vertices outside $P$ are pairwise adjacent. We now have to compare $A_1$ with $f(n,D)$.

Let us start with $D=3$. In that case, we have
$f(n,3)=14+(n-4)(3n-4+\max\{0,7-n\})$, while $A_1=(n-4)3(n-3) + n_3^{2}(5-n)
- 3n_3^{3} +14 + \sum_{i=0}^{3}\delta_io_i$. Hence, the difference is :
\[
f(n,3)-A_1=(n-4)(5+\max\{0,7-n\})-n_3^{2}(5-n)+3n_3^{3}-\sum_{i=0}^{3}\delta_io_i.
\]
We have
\[
\sum_{i=0}^{3}o_i\leq 3(n_3^{2}+n_3^{3})+2(n-4-n_3^{2}-n_3^{3})=2(n-4)+n_3^{2}+n_3^{3}.
\]
Since $o_0+o_3\leq n-4$ to avoid a path of length 2 joining $u_0$ to $u_3$, we have
\[
\sum_{i=0}^{3}\delta_io_i\leq 3(n-4)+2(n-4+n_3^{2}+n_3^{3}).
\]
Hence,
\[
f(n,3)-A_1 \geq (n-4)\max\{0,7-n\}-n_3^{2}(7-n)+n_3^{3}.
\]
This difference is minimized if and only if $n_3^{3}=0$, while $n_3^{2}=0$ if $n>7$, $n_3^{2}=0,1,2$ or $3$ if $n=7$, and $n_3^{2}=n-4$ if $n<7$. In all such cases, we get $f(n,3)-A_1=0$.
\begin{itemize}
  \item If $n=4$, there is no vertex outside $P$, and $G\simeq \extG{4}{3}{0}$
    which is the unique graph in $\mathcal{C}_4^{3}$.
  \item If $n=5$, $n_3^{2}=1$, which means that the unique vertex outside
    $P$ is adjacent to 3 consecutive vertices on $P$. Hence, $G\simeq
    \extG{5}{3}{1}$ which is the unique graph in $\mathcal{C}_5^{3}$.
  \item If $n=6$, $n_3^{2}=2$,  which means that both vertices outside $P$
    are adjacent to 3 consecutive vertices on $P$. If one of them is adjacent
    to $u_0,u_1,u_2$, while the other is adjacent to $u_1,u_2,u_3$, we have
    $G\simeq H_2$. Otherwise, we have $G\simeq \extG{6}{3}{2}$.
  \item If $n=7$, $n_3^{2}\in\{0,1,2,3\}$ and $n_{1,2}=3-n_3^{2}$. If
    $n_{1,2}>0$ then the vertices in $V_{1,2}$ are all adjacent to $u_0$ and
    $u_1$ or all to $u_2$ and $u_3$, since they are pairwise adjacent, and
    they all have eccentricity $3$. So assume without loss of generality, they
    are all adjacent to $u_0$ and $u_1$. Then the vertices in $V_{3}^{2}$ are
    all adjacent to $u_0,u_1,u_2$, else the vertices in $V_{1,2}$ would have
    eccentricity 2. But $G$ is then equal to $\extG{7}{3}{0},\extG{7}{3}{1}$
    or $\extG{7}{3}{2}$. If $n_{1,2}=0$, then the three vertices outside
    $P$ are all adjacent to three consecutive vertices on $P$. If they are
    all adjacent to $u_0,u_1,u_2$, or all to $u_1,u_2,u_3$, then $G\simeq
    \extG{7}{3}{3}$, else $G\simeq H_3$.
  \item If $n>7$, all vertices outside $P$ are adjacent to $u_0,u_1$, or
    to $u_2,u_3$ (so that they all have eccentricity 3). Hence, $G\simeq
    \extG{n}{3}{0}$.
\end{itemize}

Assume now $D\geq 4$. We have
\[
f(n,D)=2\sum_{i=0}^{D-1}\delta_i+\Big(n-D-1\Big)\Big( 2D-1+D(n-D)+\max\{0,3D-n-3\}\Big)
\] and
\[
A_1 = 2\sum_{i=0}^{D-1}\delta_i + (n-D-1)D(n-D) + n_3^{D-1}(2D-n-1) - Dn_3^{D}
+ \sum_{i=0}^{D}\delta_i o_i.
\]
Hence, the difference is:
\[
f(n,D)-A_1 = (n-D-1)(2D-1+\max\{0,3D-n-3\}) - n_3^{D-1}(2D-n-1) + Dn_3^{D}
- \sum_{i=0}^{D}\delta_io_i.
\]
We have
\[
\sum_{i=0}^{D}o_i\leq 3(n_3^{D-1} + n_3^{D}) + 2(n-D-1-n_3^{D-1}-n_3^{D})
= 2(n-D-1) + n_3^{D-1} + n_3^{D}.
\]
Let $p$ be the number of vertices linked to both $u_1$ and $u_{D-1}$.
\begin{itemize}
  \item If $D\geq 5$, then $p=0$, else $d(u_0,u_D)\leq 4 <D$.
  \item If $D=4$, then no vertex outside $P$ linked to $u_1$ and $u_{D-1}$ can also be linked to $u_0$ or to $u_D$ since $d(u_0,u_D)$ would be strictly smaller than $4$. Since no vertex outside $P$ can be linked to both $u_0$ and $u_D$ (else $d(u_0,u_D)<3$) we have $o_0+o_D\leq n-D-1-p$ and $o_1+o_{D-1}\leq n-D-1+p$. Hence, $o_2\leq n_3^{D-1}+n_3^{D}$. So,
  \begin{align*}
    \sum_{i=0}^{D}\delta_io_i &\leq
      D(n-D-1-p)+(D-1)(n-D-1+p)+(D-2)(n_3^{D-1}+n_3^{D})\\
    &= (n-D-1)(2D-1)+(D-2)(n_3^{D-1}+n_3^{D})-p.
  \end{align*}
  This value is maximized for $p=0$.
\end{itemize}
Hence, in all cases, we have
\[
\sum_{i=0}^{D}\delta_io_i\leq (n-D-1)(2D-1)+(D-2)(n_3^{D-1}+n_3^{D}).
\]
Hence,
\[
f(n,D)-A_1\geq (n-D-1)\max\{0,3D-n-3\}-n_3^{D-1}(3D-n-3)+2n_3^{D}.
\]
This difference is minimized if and only if $n_3^{D}=0$, while $n_3^{D-1}=0$ if $n>3(D-1)$, $n_3^{D-1}\in\{0,\ldots,n-D-1\}$ if $n=3(D-1)$, and $n_3^{D-1}=n-D-1$ if $n<3(D-1)$. In all such cases, we get $f(n,D)-A_1=0$.
\begin{itemize}
  \item If $n<3(D-1)$, then all vertices outside $P$ are adjacent to 3 consecutive vertices on $P$. They are all adjacent to $u_0,u_1,u_2$, or all adjacent to $u_{D-2},u_{D-1},u_D$, else $d(u_0,u_D)\leq 3<D$. Hence, we have $G\simeq \extG{n}{D}{n-D-1}$.
  \item If  $n=3(D-1)$, $n_3^{D-1}\in\{0,\ldots,n-D-1\}$ and $n_{1,2}=2D-2-n_3^{D-1}$. If $n_{1,2}>0$ then the vertices in $V_{1,2}$ are all adjacent to $u_0$ and $u_1$ or all to $u_{D-1}$ and $u_{D}$, since they are pairwise adjacent, and they all have eccentricity $D$. So assume without loss of generality, they are all adjacent to $u_0$ and $u_1$. Then the vertices in $V_{3}^{D-1}$ are all adjacent to $u_0,u_1,u_2$, else $d(u_0,u_D)\leq 3<D$. But $G$ is then equal to $\extG{n}{D}{n_3^D}$. If $n_{1,2}=0$, then all vertices outside $P$ are adjacent to $u_0,u_1,u_2$, or all of them are adjacent to $u_{D-2},u_{D-1},u_D$, else $d(u_0,u_D)\leq 3<D$. Hence, $G\simeq \extG{n}{D}{n-D-1}$.
  \item If $n>3(D-1)$, all vertices outside $P$ are adjacent to $u_0,u_1$, or to $u_2,u_3$ (so that they all have eccentricity D). Hence, $G\simeq \extG{n}{D}{0}$.
\end{itemize}
\end{proof}

\section{Results for a fixed order and no fixed diameter}

We now determine the connected graphs that maximize the eccentric connectivity index when the order $n$ of the graph is given, while there is no fixed diameter. Clearly, $\K{3}$ and $\Path{3}$ are the only connected graphs of order $n=3$ and $\eci(\K{3})=\eci(\Path{3})=6$. For $n>3$, $\eci(\M{n})=2n^2-4n-2(n\bmod 2) >n^2-n =\eci(\K{n})$, which means that the optimal diameter is not $D=1$.
\begin{itemize}
  \item If $n=4$, $f(4,3)=14<\eci(\M{4})=16$, which means that $\M{4}$ has maximum eccentric connectivity among all connected graphs with 4 vertices.
  \item If $n=5$, $f(5,3)=27$, $f(5,4)=24$ and $\eci(\M{5})=30$, which means that $\M{5}$ and $H_1$ have maximum eccentric connectivity index among all connected graphs with 5 vertices.
  \item If $n=6$, $f(6,3)=44$, $f(6,4)=42$, $f(6,5)=38$ and $\eci(\M{6})=48$, which means that $\M{6}$ has maximum eccentric connectivity index among all connected graphs with 6 vertices.
\end{itemize}
Assume now $n\geq 7$. We first show that lollipops are not optimal. Indeed, consider a lollipop $\extG{n}{D}{0}$ of order $n$ and diameter $D$.
\begin{itemize}
  \item If $D=n-1$, then $G\simeq \Path{n}$ which implies
    \begin{align*}
      \eci(\extG{n}{n-1}{0}) &= \sum_{i=1}^{D-1}2\max\{i,D-i\}+2D =
        \frac{3D^2+D\bmod 2}{2}\\
    &\leq \frac{3D^2+1}{2} = \frac{3n^2}{2}-3n+2 < 2n^2-4n-2 \leq \eci(\M{n}).
    \end{align*}
  \item If $D<n-1$ then either $n<3(D-1)$, and we know from Corollary \ref{cor:max_k} that $\eci(\extG{n}{D}{n-D-1})>\eci(\extG{n}{D}{0})$, or $n\geq 3(D-1)$, in which case we show that  $\eci(\extG{n}{D+1}{n-D-2})>\eci(\extG{n}{D}{0})$. Since $2\sum_{i=0}^{D-1}\max\{i,D-i\}=\frac{3D^2 + D \bmod 2}{2}$, we know from Lemma \ref{lem:eci_for_extremal_graph} that
\begin{align*}
\eci(\extG{n}{D+1}{n-D-2})={}
   &2\sum_{i=0}^{D}\max\{i,D+1-i\}\\
   &+\Big(n-D-2\Big)\Big( 2(D+1)-1+(D+1)(n-D-1)\Big)\\
   &+\Big(n-D-2\Big)\Big(2(D+1)-n-1+(D+1)-2\Big)\\
={}&\frac{3(D+1)^2 + (D+1) \bmod 2}{2}+\Big(n-D-2\Big)\Big(3D+D(n-D)\Big)
\end{align*}
and
\begin{align*}
\eci(\extG{n}{D}{0}) &=
  2\sum_{i=0}^{D-1}\max\{i,D-i\}+\Big(n-D-1\Big)\Big( 2D-1+D(n-D)\Big) \\
&=\frac{3D^2 + D\bmod 2}{2}+\Big(n-D-1\Big)\Big( 2D-1+D(n-D)\Big).
\end{align*}
Simple calculations lead to
\[
\eci(\extG{n}{D+1}{n-D-2})-\eci(\extG{n}{D}{0})=n-2D+(D-1) \bmod 2\geq n-2\left(  \frac{n}{3}+1 \right)=\frac{n}{3}-2>0.
\]
\end{itemize}
Hence, the remaining candidates to maximize the eccentric connectivity index when $n\geq 7$ are $\M{n}$ and $\extG{n}{D}{n-D-1}$. Let
\[
g(n)=\max_{D=\left\lceil\frac{n}{3}+2\right\rceil}^{n-D-1}
\eci(\extG{n}{D}{n-D-1}).\]
We can rewrite $\eci(\extG{n}{D}{n-D-1})$ as follows:
\[
\eci(\extG{n}{D}{n-D-1})=D^3-D^2(n+\frac{5}{2})+D(n^2+5n-1)-n^2-3n+4+ D\bmod 2.
\]
It is then not difficult to show that $g(n)=\eci(\extG{n}{D^*}{n-D^*-1})$ with $D^*=\lceil\frac{n+1}{3}\rceil+1$, and simple calculations lead to
\[
g(n)=\frac{1}{54}(8n^3+21n^2-36n+
\begin{cases}
0&\text{if }n \bmod 6=0\\
6n+1&\text{if }n \bmod 6=1\\
32&\text{if }n \bmod 6=2\\
27&\text{if }n \bmod 6=3\\
6n+28&\text{if }n \bmod 6=4\\
59&\text{if }n \bmod 6=5\\
\end{cases}\quad).
\]
We then have $g(7)=66<68=\eci(\M{7})$, which means that $\M{7}$ has the largest eccentric connectivity among all graphs with $7$ vertices. Also, $g(8)=96=\eci(\M{8})$, which means that both $\extG{8}{4}{3}$ and $\M{8}$ have the largest eccentric connectivity index among all graphs with $8$ vertices. For graphs of order $n\geq 9$, we have $\frac{8n^3+21n^2-36n}{54} > 2n^2-4n$, which means that $\extG{n}{D^*}{n-D^*-1}$ is the unique graph with largest eccentric connectivity index among all graphs with $n$ vertices.
These results are summarized in Table \ref{table1}, where ${\eci_n}^*$ stands for the largest eccentric connectivity index among all graphs with $n$ vertices.

\begin{table}[h!]
	\begin{center}
		\caption{Largest eccentric connectivity index for a fixed order $n$}
		\label{table1}

		\begin{tabular}{ccc}
			$n$ & ${\eci_n}^*$ & optimal graphs \\
			\hline
			3&6&$\K{3}$ and $\Path{3}$\\
			4&16&$\M{4}$\\
			5&30&$\M{5}$ and $H_1$\\
			6&48&$\M{6}$\\
			7&68&$\M{7}$\\
			8&96&$\M{8}$ and $\extG{8}{4}{3}$\\
			$\geq 9$&$g(n)$&$\extG{n}{\left\lceil\frac{n+1}{3}\right\rceil+1}{n-\left\lceil\frac{n+1}{3}\right\rceil-2}$
		\end{tabular}
	\end{center}
\end{table}
Note finally that Tavakoli~{\emph{et al.}}~\cite{Tavakoli2014} state that
$g(n)=\eci(\extG{n}{D}{n-D-1})$ with $D=\lceil\frac{n}{3}\rceil+1$ while we have shown that the best diameter for a given $n$ is $D=\lceil\frac{n+1}{3}\rceil+1$. Hence for all $n\geq 9$ with $n\bmod 3=0$, we get a better result. For example, for $n=9$, they consider $\extG{9}{4}{4}$ which has an eccentric connectivity index equal to 132 while $g(9)$=134.

\section{Conclusion}
We have characterized the graphs with largest eccentric connectivity index among those of fixed order $n$ and fixed or non-fixed diameter $D$. It would also be interesting to get such a characterization for graphs with a given order $n$ and a given size $m$. We propose the following conjecture which is more precise than the one proposed in~\cite{Zhang2014}
\begin{conj*}

  Let $n$ and $m$ be two integers such that $n\geq 4$ and $m\leq{ {n-1}\choose{2}}.$ Also, let
	\[D=\left\lfloor\frac{2n+1-\sqrt{17+8(m-n)}}{2}\right\rfloor \text{ and } k=m-{{n-D+1}\choose{2}}-D+1
	\]
	Then, the largest eccentric connectivity index among all graphs of order $n$ and size $m$ is attained with $\extG{n}{D}{k}$. Moreover,
	\begin{itemize}
		\item if $D>3$ then $\eci(G)<\eci(\extG{n}{D}{k})$ for all other graphs $G$ of order $n$ and size $m$.
		\item if $D=3$ and $k=n-4$, then the only other graphs $G$ with $\eci(G)=\eci(\extG{n}{D}{k})$ are those obtained by considering a path $u_0-u_1-u_2-u_3$, and by joining $1\leq i\leq n-3$ vertices of a clique $\K{n-4}$ to $u_0,u_1,u_2$ and the $n-4-i$ other vertices of $\K{n-4}$ to $u_1,u_2,u_3$.
	\end{itemize}
\end{conj*}

\bibliographystyle{acm}

Discrete Mathematics (cs.DM); Combinatorics (math.CO)