\documentclass{elsarticle}
\usepackage{amsmath,amssymb,amscd,amsfonts,graphicx, verbatim, amsthm}
%\usepackage{algpseudocode,algorithm}
%\setlength{\textwidth}{15truecm}
%\setlength{\oddsidemargin}{6mm}
%\setlength{\evensidemargin}{6mm}
%\setlength{\marginparwidth}{25mm}

\newcommand{\R}{{\mathbb R}}
\newcommand{\C}{{\mathbb C}}
\newcommand{\Q}{{\mathbb Q}}
\newcommand{\N}{{\mathbb N}}
\newcommand{\Z}{{\mathbb Z}}
\newcommand{\K}{{\mathbb K}}
\newcommand{\eps}{\epsilon}
\newcommand{\ol}{\overline}
\newcommand{\pro}{{\mathbb{RP}^3}}
\newcommand{\calP}{{\mathcal P}}
\newcommand{\nota}{\marginpar{\qquad \huge !!}}

\newtheorem{teo}{Theorem }[section]
\newtheorem{propo}[teo]{Proposition }
\newtheorem{Rem}[teo]{Remark }
\newtheorem{lemma}[teo]{Lemma }
\newtheorem{coro}[teo]{Corollary }
\newtheorem{defi}[teo]{Definition }
\newtheorem{ex}[teo]{Example }
\newtheorem{constr}[teo]{Construction }
\newtheorem{notation}[teo]{Notation }
%\newproof{proof}{Proof}

\bibliographystyle{elsarticle-num}

\begin{document}

\begin{frontmatter}


\title
{Computation of topological invariants for real 
projective surfaces with isolated singularities}



\fntext[fn1]{This research was partially supported by M.I.U.R.}  
 
\author[dmp]{E. Fortuna\fnref{fn1}}
\ead{fortuna@dm.unipi.it}

\author[dmp]{P. Gianni\fnref{fn1}}
\ead{gianni@dm.unipi.it}

\author[wrc]{B.M. Trager\corref{cor1}}
\ead{bmt@us.ibm.com}

\cortext[cor1]{Corresponding author}

\address[dmp]{Dipartimento di Matematica, Universit\`a
  di Pisa,  Largo B. Pontecorvo 5, \\  I-56127 Pisa,
  Italy}
\address[wrc]{IBM T.J.Watson Research Center, 1101 Kitchawan Road,
  \\ Yorktown Heights, NY 10598, USA}




\begin{abstract}
 Given a real algebraic surface $S$ in $\pro$, we propose a
procedure to determine the topology of $S$ and to compute
non-trivial topological invariants for the pair $(\pro, S)$ under the
hypothesis that the real singularities of $S$ are isolated. In
particular, starting from an implicit equation of the surface, we
compute the number of connected components of $S$, their Euler
characteristics and the labelled 2-adjacency graph of the surface.
   
%{\em 2000 Mathematics Subject Classification:} 
\end{abstract}

\begin{keyword}
Real algebraic surfaces \sep topology computation \sep adjacency graph
%\texttt{}\sep \LaTeX\sep Elsevier \sep template
\end{keyword}
\end{frontmatter}

\centerline {\it Dedicated to the memory of Domenico Luminati}

\section{Introduction}


Given a real algebraic surface $S$ in $\pro$ defined by a square-free 
polynomial equation, the problem of recognizing the topology of the surface can
be addressed at two different levels: either considering $S$ only as
an abstract topological space, or taking into account also its
embedding in $\pro$ and looking at the topology of the pair $(\pro,S)$.

We say that two surfaces $S, S'$ are ambient-homeomorphic if there exists
a homeomorphism $\varphi : \pro \to \pro$ such that $\varphi(S)=S'$;
in this case we also say that the pairs $(\pro, S)$ and $(\pro, S')$
are homeomorphic.  At present there is no classification of the pairs
$(\pro, S)$ up to homeomorphism even in the non-singular case and
deciding whether two pairs are
homeomorphic is a very hard problem, even for simple classes of
surfaces such as tori.  Hence a useful contribution in
this direction is to be able to compute topological invariants of the
pair $(\pro, S)$.

A classical approach to an algorithmic determination of the topology
of a surface is via Cylindrical Algebraic Decomposition
(\cite{Collins}, see also \cite{Roy-book}), which provides a cellular
decomposition of the pair $(\pro, S)$. 

Other algorithms for studying the topology of affine surfaces of degree $d$ in $\R^3$ with
arbitrary singularities have been presented in \cite{Berberich},
\cite{Mourrain}, \cite{Diatta} and  \cite{Cheng}. 
The first three compute triangulations of the surface using $\mathcal O(d^7)$ cells, 
while the latter one
computes a curvilinear wireframe model. \cite{Berberich} handles surfaces 
not in general position, while
the other three put conditions on the coordinate frame. All of these methods 
perform much better than CAD, e.g.
\cite{Mourrain} and \cite{Diatta} show that their method requires $\mathcal O(d^7)$ points 
to compute the triangulation while
CAD would require $\mathcal O(d^{13})$.

It is not easy to decide whether two pairs $(\pro, S)$ and $(\pro, S')$
are homeomorphic or not, even after computing a combinatorial
description of each pair. This is why our aim is a different one: we want to find 
a small discrete set of data that are algorithmically
computable and sufficient to determine the topology of the surface,
that are non-trivial topological invariants for the pair $(\pro, S)$
and, most important, easy to compare and hence easy to be used as a
test to exclude topological equivalence. 

In \cite{FGLP}, developing the ideas in \cite{GT}, \cite{FGPT} and
\cite{FGL}, this goal is achieved in the case of a non-singular
surface; the authors describe a method to compute not
only the number of its connected components and the Euler
characteristic of each of them but also the ``labelled adjacency
graph'' of the surface. This graph, though not sufficient to determine
the pair $(\pro,S)$, gives information both on the mutual disposition
of the connected components and on their contractibility.

In this paper we address the same
questions when the surface contains at most  isolated
singularities.

In our approach the basic topological information is that in a small
3-dimensional ball $D$, centered at an isolated singular point, $S\cap
D$ is homeomorphic to the cone over the curve $C$ obtained as the
intersection of $S$ with the boundary of D (see
\cite{Milnor}). Then, up to homeomorphism, the portion of $S$ inside
the ball can be seen as the space obtained taking the union of as many
2-dimensional disks as the connected components of $C$, choosing a
point in each of these disks and collapsing these points to a single
point. In this way we see the isolated singularity as the result of
two successive operations: first the glueing of a 2-cell (i.e. a
subset homeomorphic to a closed 2-dimensional disk) along each
connected component of $C$ and then the collapsing of a set containing a
point in each of the attached 2-cells.

Applying this procedure to all of the singularities, we obtain a compact
topological surface $T$ without boundary such that $S$, except its 
isolated points, is homeomorphic
to the topological quotient $T/{\mathcal R (T)}$ where $\mathcal R (T)$ is the
equivalence relation that collapses suitable finite families of points
of $T$. 

In order to determine the topology of $T$, it is sufficient to compute 
the number of its connected components and to find a topological model 
for each of them. By the topological
classification theorem any compact connected orientable topological surface 
is homeomorphic to the
connected sum of a sphere and $g$ tori, i.e.  it is homeomorphic to a
sphere with $g$ handles. The number $g$, called the {\em genus}, is a
topological invariant that determines a compact connected orientable
surface up to homeomorphism.  We can equivalently determine any
orientable connected surface by computing its {\em Euler
characteristic} $\chi$, since it turns out that $\chi= 2-2g$ (which in
particular is always an even integer). Moreover (see
\cite{Viro-mutual}, 1.3.A) any compact connected
non-orientable surface contained in $\pro$ is homeomorphic 
to the connected sum of a projective plane and a compact connected
orientable surface of genus $g$; the Euler characteristic is then
$\chi= 1-2g$ (i.e. an odd integer). 

Thus we topologically  determine $S$ by computing
the Euler characteristics of the connected components of $T$, the equivalence relation that, through a collapsing process, produces the
singularities of $S$ and the number of isolated points on the surface.

Then, considering how $S$ is embedded in $\pro$, 
we define the \emph{labelled 2-adjacency graph} of
$S$ containing information about the mutual disposition of the connected 
components of the surface and we show that it is a topological invariant 
of the pair $(\pro,S)$. We also describe an algorithm to compute the previous 
labelled graph and to recognize the position of the isolated points of $S$ in 
the connected components of $\pro \setminus S$.

This paper is a natural evolution of the articles \cite{FGPT},
\cite{FGL} and \cite{FGLP}, which dealt with non-singular surfaces.
Here we use some of those techniques  for reducing the problem to the compact affine 
case; for this latter case we develop a new procedure able to investigate the 
topological nature of the isolated singularities.

The main definitions, the necessary theoretical background and the
list $D(S)$ of data invariant up to homeomorphism of the pair
$(\pro,S)$ are presented in Section
\ref{definitions}. In order to deal with isolated singularities we use
a generalization of classical Morse theory to singular spaces, first
introduced by Lazzeri (\cite{L}), that we briefly recall in Section
\ref{Morse-theory}, where we prove also some related results necessary
for the algorithm. In Section \ref{affinecase} we describe an
algorithm to compute $D(S)$ when the surface is contained
in an affine chart of $\pro$.  
When $S$ is not
affine, it is possible to construct a suitable compact algebraic
surface $\widehat S$ in $\R^3$ and to recover $D(S)$ from $D(\widehat
S)$, which can be computed by means of the affine-case algorithm. This
reduction procedure and the general-case algorithm are presented in
Section \ref{generalcase} which also contains some examples.
Finally in Section \ref{tests} we present algorithms to perform 
some auxiliary computations concerning the
critical and singular points and tests for connectedness. We also give a rough 
complexity analysis showing that, after the auxiliary computations, 
the total number of points required by 
our algorithm to compute all our topological invariants is $\mathcal O (d^7)$. 

A preliminary version of this paper was written in collaboration 
with our dear friend ``Mimmo" with whom we 
collaborated for a long time and who passed away too early. With him we had 
many stimulating and 
enriching discussions  concerning  many of the topics presented 
in this paper. This is why 
we wish to dedicate this paper to him. 


\section{Remarks on the topology of surfaces with isolated
  singularities}\label{definitions} 

Let $S$ be the real projective algebraic surface in $\pro$
defined by the equation $F(x,y,z,t)=0$, where $F$ is a square-free homogeneous
polynomial of degree $d$ with rational coefficients (or more generally with 
coefficients in a computable subfield of $\R$). A point $P\in S$ is
a singular point of the surface if it annihilates 
all the first partial derivatives of $F$; we will denote by $Sing\, S$ the 
algebraic set consisting of the singular points of $S$. 

In this paper we will consider only the case when $Sing\, S$ contains 
only finitely many points.  Note that we make no assumption on
the singular locus $Sing\, S_\C$ of the complex projective surface
$S_\C$ in ${\mathbb{CP}^3}$ defined by the equation $F=0$; since $F$
is square-free, $Sing\, S_\C$ cannot have dimension $2$, but it can be
a complex curve.

A point $P\in S$ is called an \emph{isolated point} of $S$ if
there exists an open neighborhood $U$ of $P$ in $\pro$ such that $S
\cap U =\{P\}$; the set $Isol(S)$ of all isolated points of $S$ is 
contained in $Sing\, S$. 


Let us present an explicit way to see $S \setminus Isol(S)$, up to 
homeomorphism, as the quotient space  $T/{\mathcal R (T)}$ where $T$ 
is a compact topological surface (i.e. a 2-dimensional
topological manifold) and $\mathcal R (T)$ is an equivalence relation on $T$ 
which collapses finitely many subsets, each consisting of finitely many 
points. 

For simplicity, consider first the case when $S$ is contained in an 
affine chart of $\pro$ and defined in affine coordinates 
by the square-free polynomial equation $f(x,y,z)=0$. 
We will use the following notations:

\begin{notation} 
\begin{enumerate}
\item If $Q\in \R^3$, $\eps$ is a real positive number  and $d(\cdot,\cdot)$ is the Euclidean distance in $\R^3$,  denote by:\\ 
--- $B(Q,\eps)= \{X\in \R^3\ |\ d(X,Q)<\eps\}$ \\
--- $D(Q,\eps)= \{X\in \R^3\ |\ d(X,Q)\leq \eps\}$\\
--- $S(Q,\eps)= \{X\in \R^3\ |\ d(X,Q)=\eps\}$,\\
respectively the open ball,  the closed ball and
the sphere of radius $\eps$ centered at $Q$. Moreover 
denote by  $C(Q,\eps)$ the curve $S \cap S(Q,\eps)$. 
\item If $A\subseteq \R^3$ and $P$ is a point in
$\R^3$, by \emph{cone over $A$ with vertex $P$} we will mean the
union of all segments joining $P$ with any point of $A$. By convention the 
cone over the empty set with vertex $P$ is the set $\{P\}$.
\item If $U$ is a topological space and $W$ is a
  subspace, let $G(U,W)$ denote the \emph{adjacency graph of the
  pair} $(U,W)$, that is the graph whose vertices
are the connected components of $U \setminus W$ and where two distinct
vertices $\Omega_1, \Omega_2$ are joined by an edge if and only if the
topological closures of $\Omega_1$ and $\Omega_2$ are not disjoint.
\end{enumerate}
\end{notation}


The following result, proved by Milnor also in the complex case, gives
the important information that locally at an isolated singularity $S$
is topologically a cone: 
\begin{teo} (\cite{Milnor}, Proposition
  2.10)\label{Milnorradius} Let $Q$ be an isolated singular point of
  $S\subseteq \R^3$. Then there exists $r>0$ such that for all
  positive $\eps \leq r$ 
\begin{enumerate}
\item[i)] $C(Q,\eps)$ is a non-singular curve
  (possibly empty) 
\item[ii)] $S \cap D(Q,\eps)$ is homeomorphic to the cone over $C(Q,\eps)$ 
with vertex $Q$.
\end{enumerate}
\end{teo}
 
Recall that the \emph{critical points} of a polynomial function 
$g\colon S \to \R$ 
are the points of $S=V(f)$ where the rank of the matrix 
$\begin{pmatrix} f_x & f_y & f_z \\ g_x & g_y & g_z\end{pmatrix}$
is less than or equal to $1$; in particular all the points of $Sing\, S$ 
are critical. If $P$ is critical for $g$, the real number $g(P)$ is 
called a \emph{critical
value}.  Any non critical value is called a \emph{regular value}. 
Recall also (see \cite{Milnor}, Corollary 2.8) that the restriction 
of $g$ to $S\setminus Sing\, S$ can have at most finitely many critical values; 
since in our hypotheses $S$ has finitely many singular points,  
any polynomial function $g$ defined on $S$ 
can have at most finitely many critical values. 

If $Q\in S$, any $r >0$ such that $D(Q,r) \setminus \{Q\}$ contains no singular
points of $S$ and no critical points of the restriction to $S$ of the
function $X \to d(X,Q)^2$ satisfies the thesis of Theorem \ref{Milnorradius}. 
 For any $\eps \leq r$, we will call $\eps$ a \emph{Milnor
radius} at $Q$ and $S(Q,\eps)$ a \emph{Milnor sphere} at $Q$. 
In this case, though the non-singular curve $C(Q,\eps)$ is not plane, 
we will call its connected components \emph{ovals}. In particular, 
if $Q\not\in Sing\, S$ then $C(Q,\eps)$ consists of a single oval; if 
$Q$ is an isolated point then $C(Q,\eps)$ is empty.

\smallskip

\begin{Rem}\label{Durfee} According to the literature (see \cite{Durfee}), 
an \emph{algebraic neighborhood} of a point $Q$ in a compact 
algebraic set $M\subseteq \R^n$ is the set $\alpha ^{-1} ([0, \eps])$ 
where $\alpha \colon M \to \R$ is a non-negative proper polynomial 
(or rational or even semialgebraic) function such that $\alpha ^{-1} (0)=
\{Q\}$ and $\eps >0$ is a real number such that $(0, \eps]$ does not 
contain any critical value of $\alpha$ (any such function is called a 
\emph{rug function}). The Uniqueness Theorem 
proved by Durfee in \cite{Durfee} shows that any two algebraic 
neighborhoods $V_1, V_2$ of an isolated singular point 
$Q$ in $M$ are homeomorphic by means 
of a homeomorphism $h\colon M \to M$ such that $h(Q)=Q$ and 
$h|_{V_1 \setminus \{Q\}}$ is a diffeomorphism between 
$V_1 \setminus \{Q\}$ and $V_2 \setminus \{Q\}$. In particular the 
notion of algebraic neighborhood of $Q$ in $M$ is independent of 
the embedding of $M$ in $\R^n$; moreover the \emph{link} (i.e. 
the boundary of an algebraic neighborhood of $Q$ in $M$) is 
a topological invariant which is independent of 
the embedding of $M$ in its ambient space.

Thus, coming back to our situation and using as $\alpha$ the polynomial function 
 $X \to d(X,Q)^2$ , the set $S \cap D(Q,\eps)$ is an 
algebraic neighborhood of $Q$ in the surface $S$ and $C(Q,\eps)$ is 
the link of $Q$ in $S$.
\end{Rem}


It is possible to associate to $S$ a topological surface $T\subset \pro$  
from which we can obtain again $S$, except its
isolated points if any, by means of a suitable quotient. 
This can be done by removing from $S$ the portion contained in Milnor spheres 
centered at the singular points, and then suitably 
attaching, inside the previous Milnor spheres, non-intersecting 
$2$-cells along the ovals of the links. 
The next proposition describes the main properties of the surface obtained 
by means of this procedure:

\begin{propo}\label{constrT} For any $Q\in Sing\, S$, let $\eps_Q>0$ be a 
Milnor radius at $Q$ and assume that 
$D(Q,\eps_Q) \cap D(Q',\eps_{Q'}) =\emptyset$ whenever $Q, Q'$ 
are distinct singular points. 
Then, for each  $Q\in Sing\, S$, we can attach to 
$S \setminus \left( \bigcup_{Q\in Sing\, S} 
D(Q,\eps_Q)\right)$ a $2$-cell along each oval of the 
non-singular curve $C(Q,\eps_Q)$ in such a way that the 
 embedded topological surface without boundary $T$ so obtained 
 satisfies the following properties:
\begin{enumerate}
\item $T \cap S(Q,\eps_Q)=C(Q,\eps_Q)$  for each $Q\in Sing\, S$,
\item $S \setminus \left( \bigcup_{Q\in Sing\, S} 
D(Q,\eps_Q)\right)
= T \setminus \left( \bigcup_{Q\in Sing\, S} 
D(Q,\eps_Q)\right)$
\item for each $Q\in Sing\, S$ the adjacency graph \, 
$G(D(Q,\eps_Q),T\cap D(Q,\eps_Q))$ \,
is isomorphic to the  adjacency graph $G(S(Q,\eps_Q),C(Q,\eps_Q))$
\item if for any $Q\in Sing\, S$ we denote by $Z(Q)$ the  set obtained 
by choosing a point in each
$2$-cell attached to the ovals of $C(Q,\eps_Q)$ and by 
$\mathcal R (T)$ the equivalence
relation on $T$ that collapses to a point each of the sets  $Z(Q)$, then 
$S$ is homeomorphic to the disjoint union of
$T/{\mathcal R (T)}$ and $Isol (S)$.
\end{enumerate} 
\end{propo}


\begin{ex}\label{ese-step1} {\rm The surface represented in
    Figure \ref{fig:ex_surf} has three isolated singularities
    $N_1,N_2, R_1$ and $R_1$ is an isolated point. 
    The topological surface $T$ constructed as explained above
    has three connected components $T^1, T^2, T^3$ and all of them are
    spheres. The singularity $N_1$ can be obtained collapsing one
    point of $T^1$ and two points of $T^2$ to a single point;
    similarly $N_2$ can be obtained collapsing three points chosen
    respectively in $T^1$, $T^2$ and $T^3$.  
 \qed }
\end{ex}
\begin{figure}[htbp]
  \centering
  \includegraphics[height=.2\hsize]{newS.eps}\qquad\qquad
  \includegraphics[height=.2\hsize]{T.eps}
  \caption{\footnotesize \sl A surface with three singular points
  (left-hand side) and the topological surface $T$ associated to it
  (right-hand side).}
  \label{fig:ex_surf}
\end{figure}

\begin{Rem}\label{number-points} The choice of the sets $Z(Q)$ in 
Proposition \ref{constrT} is not unique. However, since the 
topological type of the space obtained by collapsing
finitely many points in a compact connected topological surface does not depend on
the choice of these points but only on their number, 
in order to determine $\mathcal R (T)$ it will be sufficient to compute the  number 
of points it collapses in each connected component of $T$ 
at each singular point. 

Observe also that, if $Q$ is a singular non-isolated point which 
does not disconnect $S$ locally, then $S \cap D(Q,\eps_Q)$ is homeomorphic 
to a $2$-cell, $Z(Q)$ contains a unique point and the action
of $\mathcal R (T)$ is trivial at $Q$. In other words the effect of $\mathcal R (T)$ 
is interesting  only at the disconnecting singular points.
\end{Rem}



\begin{Rem}\label{enlargeConstr} If $Q$ is a non-singular point 
of the surface and $\eps_Q$ is a Milnor radius at $Q$, then $C(Q,\eps_Q)$ 
consists of a single oval and 
$S \cap D(Q,\eps_Q)$ is homeomorphic to a $2$-dimensional disk. 
Thus, if we modify $S$ also inside the Milnor ball 
$D(Q, \eps_Q)$ as described in Proposition \ref{constrT},  
the $2$-cell that we attach 
along  $C(Q,\eps_Q)$ is  homeomorphic to 
$S \cap D(Q, \eps_Q)$. In this case  the set $Z(Q)$ of points to be glued 
(defined as in the statement of 
Proposition \ref{constrT}) contains a unique point, so that 
collapsing $Z(Q)$ to a point has a trivial effect. This shows that, when we modify locally $S$ 
according to Proposition \ref{constrT} to get $T$, it is possible to enlarge the set of 
the singularities of $S$, including in that 
set also finitely many non-singular points: all the topological surfaces we 
obtain are the same up to homeomorphism.
\end{Rem}


In the general case when $S$ is a projective surface contained in $\pro$, 
it is possible to see $S \setminus Isol(S)$ as a quotient of a topological 
surface using affine  charts. Namely, let $N$ be a singular  
point of $S$, $H$ a hyperplane not passing through $N$  and let 
$g \colon U_H= \pro \setminus H \to \R^3$ be a chart. Then,  according to   
Proposition \ref{constrT}  we can remove from  
$g(S \cap U_H)\subseteq \R^3$ the portion  $g(S \cap U_h)\cap D(g(N),\eps)$ and  
attach along the link 
finitely many $2$-cells inside $ D(g(N),\eps)$. If $Y_{g(N)}$ is the union 
of these $2$-cells, we locally modify $S\subseteq \pro$ at $N$ attaching 
to $S\setminus g^{-1}(D(g(N),\eps))$ the $2$-cells $g^{-1}(Y_{g(N)})$ 
along the link of $S$ at $N$. By Durfee's results (\cite{Durfee}), the surface $T$ so 
obtained after performing these local modifications at all the singular 
points does not depend on the choice of the charts.
\smallskip


We can show that any topological surface $T$ and equivalence relation  $
\mathcal R (T)$ associated to $S$ 
according to Proposition \ref{constrT} (see also Remark \ref{number-points} 
and Remark \ref{enlargeConstr}), together with the number  of isolated points 
of $S$, uniquely characterize 
the surface $S$ up to homeomorphism in the sense we are going to make 
precise.


\begin{defi}\label{finite-rel} 
\begin{enumerate}
\item We say that an equivalence relation $ \mathcal R_U$ on a compact 
topological surface $U$ is \emph{generically $1-1$} if the projection to the quotient 
$\pi_U\colon U \to U/{\mathcal R_U}$ has finite fibers and $\pi_U$ is  bijective 
except on a finite subset of $U$.
\item Let $\varphi \colon U\to W$ be a homeomorphism between 
compact topological surfaces. Two generically $1-1$ equivalence relations 
$ \mathcal R_U, \mathcal R_W$ respectively on $U$ and $W$ are called 
$\varphi$-\emph{compatible} if for each $x\in U$ the cardinality of 
the equivalence class $[x]_{\mathcal R_U} $ coincides with the cardinality of 
the equivalence class $[\varphi(x)]_{\mathcal R_W} $.
\end{enumerate}
\end{defi}

\begin{propo}\label{quo-inv} Let $U,W$ be compact topological surfaces 
and let $ \mathcal R_U, \mathcal R_W$ be generically $1-1$ equivalence relations 
respectively on $U$ and $W$. Then $U/{\mathcal R_U}$ and 
$W/{\mathcal R_W}$ are homeomorphic if and only if there exists 
a homeomorphism $\varphi \colon U\to W$ such that $ \mathcal R_U$ and 
$ \mathcal R_W$  are $\varphi$-\emph{compatible}.
\end{propo}
\begin{proof} 
Denote by $\pi_U\colon U \to U/{\mathcal R_U}$ and 
$\pi_W\colon W \to W/{\mathcal R_W}$ the projections to the quotient. 
Let $U_1=\{x\in U \ |\ [x]_{\mathcal R_U} =\{x\}\}$ and 
$W_1=\{x\in W \ |\ [x]_{\mathcal R_W} =\{x\}\}$. By hypothesis 
both $U\setminus U_1$ and $W\setminus W_1$ are finite sets and 
both $\pi_U|_{U_1}$ and $\pi_W|_{W_1}$ are injective. 

Assuming that $\psi \colon U/{\mathcal R_U} \to W/{\mathcal R_W}$ is a 
homeomorphism, we want to 
define a homeomorphism $\varphi \colon U\to W$.

Let $x\in U_1$. Since $U_1$ is open in $U$, there exists an open 
neighborhood $V_x$ of $x$ contained in $U_1$ and homeomorphic to 
a disk. The restriction $\pi_U|_{V_x}$ is a homeomorphism, hence also 
$U/{\mathcal R_U}$ is locally homeomorphic to 
a disk near $\pi_U (x)$. By hypothesis $W/{\mathcal R_W}$ is then 
locally homeomorphic to a disk near $\psi(\pi_U (x))$ and the fiber 
of $\pi_W$ above $\psi(\pi_U (x))$ consists of a single point. Thus 
the map $\varphi_1 \colon U_1\to W_1$ given by $\varphi_1=
(\pi_W|{W_1})^{-1}\circ \psi \circ \pi_U|_{U_1}$  is  well 
defined and a homeomorphism between $U_1$ and $W_1$.

We want to extend the map $\varphi _1$ to the whole $U$. 
Let $x\in U\setminus U_1$ and 
assume that $[x]_{\mathcal R_U}=\{x_1, \ldots x_m\}$ so that $\pi_U(x_1)=
\ldots = \pi_U(x_m)=P_x$. Locally at each $x_i$ the surface $T$ 
is homeomorphic to a disk $D_i$ and $\varphi_1$ is defined  on $D_i 
\setminus \{x_i\}$. Up to shrinking the disks $D_i$ if necessary, 
locally at $P_x$ the space  $U/{\mathcal R_U}$ is the 
union of $m$ disks modulo the glueing of a point from each disk, hence 
it is locally at $P_x$ homeomorphic to the cone over $m$ disjoint ovals. 
Since modifying a space by means of a homeomorphism preserves 
the number of local connected components, also $W/{\mathcal R_W}$ is 
homeomorphic to the cone over $m$ disjoint ovals at 
$\psi(P_x)$.  Hence there exist $y_1, \ldots, y_m\in W$ such that 
$\{y_1, \ldots, y_m\}$ is an equivalence class w.r.t. $\mathcal R_W$ and 
$\pi_W(y_1)=\ldots = \pi_W(y_m)=\psi(P_x)$. Since $\varphi_1$ is already 
defined on $U_1$, by continuity for each $i$ there exists a unique $j_i
\in \{1, \ldots, m\}$ such that, setting $\varphi(x_i)=y_{j_i}$, $\varphi$ is a 
bijective continuous extension of $\varphi _1$ and hence  a homeomorphism.

Conversely if $\varphi \colon U\to W$ is a homeomorphism and 
$ \mathcal R_U$ and 
$ \mathcal R_W$  are $\varphi$-\emph{compatible},  then  a bijection 
$\psi \colon U/{\mathcal R_U} \to W/{\mathcal R_W}$ 
such that $\psi \circ \pi_U= \pi_W \circ \varphi$ is well 
defined. By the universal property 
of quotient spaces, $\psi$ is continuous and hence a homeomorphism.
\end{proof}

Henceforth we will always denote by $T$ and $\mathcal R (T)$ 
a topological surface and an equivalence relation associated to $S$ 
according to Proposition \ref{constrT}; observe that $T$ can be considered 
as a sort of topological 
desingularization of $S\setminus Isol(S)$. 

As well known the surface $T$ is topologically determined by the Euler characteristics
of its connected components. Thus, as a consequence of the previous results,  
we can determine $S$ topologically by computing the list $\chi(T)=[\chi (T^1), \ldots, \chi (T^r)]$ of the Euler
  characteristics of the connected components $T^1, \ldots, T^r$ of
  $T$, recognizing 
$\mathcal R (T)$ and computing the number of the isolated points of $S$.



So far our topological investigation has not taken into account the
way in which the surface is embedded in $\pro$, which can occur in different 
ways with possible phenomena of self-knotting of a connected component or 
linking of distinct components. In particular it
gives no information about the mutual disposition of the connected
components of $S$ and the connected components (or \emph{regions}) of
$\pro \setminus S$.  
We introduce now the so-called \emph{labelled 2-adjacency graph} of
the surface $S$ and  we show that it is a topological invariant of the pair 
$(\pro,S)$ (see \cite{FGLP} where similar data were introduced and computed  in the case of a non-singular surface). 


\subsection{The 2-adjacency graph}\label{2-adjacency-graph}

If $S$ is singular, it may occur that the closures of two regions of
the complement of $S$ share only finitely many points; think for
instance of the surface consisting of two spheres tangent at a common
point. This justifies the following definition: 

\begin{defi}\label{2-graph} The \emph{2-adjacency graph} $G(S)$ is 
the graph whose
vertices are the regions of $\pro \setminus S$ and where two
distinct vertices are joined by an edge if and only if the closures of
the two regions of $\pro \setminus S$ meet in a 2-dimensional subset
of $S$.
\end{defi}

\begin{Rem}\label{two-sided} 
Recall (see for instance \cite{Viro-mutual}) that a connected topological surface 
can be situated in $\pro$ in two 
different ways: either it disconnects $\pro$ into two connected components 
and it is the common boundary of those regions (and then it is 
called \emph{two-sided}), or it does not
disconnect $\pro$ (and then it is called \emph{one-sided}). When $S\subseteq \pro$ 
is a non-singular algebraic surface, the nature of its 
connected components with respect
to the previous property is determined by the degree of the
surface: if it is even, all the components are two-sided; if it is
odd, $S$ contains exactly one  component which is one-sided  and all the 
others are two-sided. 
For odd-degree surfaces the one-sided component is the only one having an odd 
Euler characteristic. 
Since two adjacent regions of 
$\pro \setminus S$ share in
their boundaries a connected component of $S$, the edges of the
standard adjacency graph $G(\pro, S)$ are in 1-1 correspondence 
with the connected
components of the surface, with the only exception that, when $S$ has
odd degree, the unique one-sided component of $S$ is not
represented in the graph. 
Thus the 2-adjacency graph $G(S)$ just defined coincides with the ordinary 
adjacency graph in the non-singular case.

When $S$ is singular, in general  there is not a bijective correspondence
between the set of the edges of $G(S)$ and the set of the connected
components of the surface, even if $S$ has even degree.
However if $S$ contains only isolated singularities and  $S_1, \ldots, S_n$ 
are  the  connected components of 
$S  \setminus Sing\, S$, if  the degree of $S$ is even then 
for all $j$ \ $\ol{S_j}$ disconnects
 $\pro$ into two connected regions; if instead the degree of $S$ is odd, then 
  there exists a unique $i_0$ such   that $\pro \setminus \ol{S_{i_0}}$
  is connected, while for any $j\ne i_0$ the set $\ol{S_j}$ disconnects
  $\pro$ into two connected regions. Thus   the situation of the non-singular 
  case still holds, up to replacing the connected components of $S$ by 
the closures of the connected components of $S  \setminus Sing\, S$. 
By an abuse of terminology we will call 
$S_{i_0}$ the one-sided component of $S  \setminus Sing\, S$, 
we will denote $\ol{S_{i_0}}$ by $\Gamma $ and call it the one-sided component of $S$.
As a consequence, there is a 1-1 correspondence
between the closures of the connected components of $S \setminus
Sing\, S$ and the edges of $G(S)$, except for odd-degree surfaces when
the one-sided component  is not represented in $G(S)$.
\end{Rem}

\begin{Rem}\label{prop-2-graph}
\begin{enumerate}
\item The graph $G(S)$ is a topological invariant of the pair $(\pro,
  S)$.
\item The isolated points of $S$, if any, are not represented in $G(S)$.
\item The 2-adjacency graph $G(S)$
is isomorphic to the adjacency graph $G(T)$.
\end{enumerate}
\end{Rem}



\subsection{Labels and roots} \label{weights-roots}

In order to give information about the mutual disposition of connected
components of the surface, we have to precise the intuitive idea by
which, if $A$ and $B$ are connected surfaces,  we say that $A$ 
envelopes $B$ if $B$ is contained in the interior part of $A$.

Even in the case of a topological surface it may occur that a connected 
component disconnects $\pro$ in two regions (i.e. it is two-sided) which 
are homologically equivalent, so that it is not possible to distinguish 
one of them from the other (think for instance of a one-sheeted hyperboloid). 
This phenomenon is strictly related to the so-called property of 
``contractibility''; we refer to Viro (\cite{Viro-mutual}) for a detailed 
survey of this notion and for a proof of the results we are going to recall.

Adopting the terminology used by Viro, we say that
a subset $A\subset \pro$ is
 \emph{contractible in $\pro$} (or briefly \emph{contractible})
 if any loop in $A$ is homotopically trivial as a loop in $\pro$,
\emph{non-contractible} otherwise.  Equivalently $A$ is contractible if the
homomorphism $\pi_1(A) \to \pi_1(\pro)$ induced by the inclusion is trivial.


Classical general results  ensure that the
one-sided component of a topological surface $V$, if present, 
is always non-contractible, while the other components  can be either
contractible or non-contractible. For instance any component contained
in an affine chart of $\pro$ is contractible, while a one-sheeted hyperboloid
is not. Moreover  a contractible connected component $W$ of $V$
disconnects $\pro$ into two connected regions, only one of which is
contractible and called the \emph{interior part} of $W$. Hence 
for such components it is possible to define a natural partial order relation.

Coming back to our situation and taking into account the previous 
remarks, we can define a
partial order relation in the set of the closures of the connected
components of $S \setminus Sing\, S$ as follows:

\begin{defi}\label{inside}  Let $S_i$ and $S_j$ be distinct connected
  components of $S \setminus Sing\, S$. We say that $\ol{S_i}$ is
  inside $\ol{S_j}$ if the following two conditions
  hold:
\begin{enumerate}
\item  $\ol{S_j}$ disconnects $\pro$ into two regions and one of these is
  contractible,
\item $S_i$ is contained in the contractible component of $\pro
  \setminus \ol{S_j}$.
\end{enumerate}
\end{defi}

The partial order relation among the closures $\ol{S_i}$   of the  connected
components of $S \setminus Sing\, S$ introduced in Definition 
\ref{inside} coincides with the one among the 
corresponding components of $T$.

We can now endow $G(S)$ with labels by means of a function 

\centerline{$Contr(S)\colon  \{
\mbox{vertices of } G(S)\} \to \{c,nc\}$}
\noindent that marks each vertex of
$G(S)$ (i.e. each region of $\pro \setminus S$) as \emph{contractible}
or \emph{non-contractible}.  
In a similar way we can define the function 

\centerline{$Contr(T)\colon  \{
	\mbox{vertices of } G(T)\} \to \{c,nc\}$}
	\noindent that marks each vertex of
$G(T)$ (i.e. each region of $\pro \setminus T$) as contractible
or non-contractible. 


\begin{Rem} For a topological surface, such as $T$, it turns out 
that a two-sided component  is non-contractible if and only if the two regions 
of its complement are both non-contractible. Thus  the knowledge of the function $Contr(T)$ allows us also  to
know which components of $T$ are contractible and which components are
not. Note that in the singular case it is no longer true that the labels on the two
vertices of an edge are sufficient to determine the contractibility of
the edge. For instance, if $S$ is a cone, $G(S)$ has two vertices, one
marked $c$, the other $nc$ and still the only edge of $G(S)$ (i.e. the
cone itself) is non-contractible. 
\end{Rem}

It is possible to reconstruct the partial order relation introduced in 
Definition \ref{inside} through $G(S)$ fixing on it a set of roots as follows. 

Denote by $G_{nc}(S)$ the subgraph
of $G(S)$ formed by the non-contractible vertices and by the edges
having both vertices marked $nc$; instead denote by
$\ol{G_c(S)}$ the subgraph formed by all the contractible vertices,
all the edges where at least one vertex is contractible and all the
vertices of these edges. One can see that, if $S$
has odd degree, all the regions of $\pro \setminus S$ are
contractible and hence $G_{nc}(S) =\emptyset$. Instead, when the
degree of $S$ is even, the closure $\ol{S_j}$ of each connected
component of $S \setminus Sing\, S$ disconnects $\pro$ into two
connected regions and at least one of them is non-contractible
(possibly both, as in the case of a one-sheeted hyperboloid); in
particular $G_{nc}(S)$ is not empty. 

The way we fix roots in $G(S)$ depends on the degree of $S$.

If $S$ has even degree, $G_{nc}(S)$ is connected and each connected
component of $\ol{G_c(S)}$ is a tree that contains exactly one vertex
marked $nc$: we choose these vertices as a \emph{set of roots} of
$G(S)$. In this way the order induced on each connected component of
$\ol{G_c(S)}$ by the only root contained in it coincides with the
partial order described in Definition \ref{inside}.

If $S$ has odd degree, $G_{nc}(S)$ is empty; however we are
able to choose a root in $G(S)$ also in this case: we will call
\emph{root} of $G(S)$ the unique region of $\pro \setminus S$ which is
adherent to the one-sided component $\Gamma$.

Note that, while for even-degree surfaces the information about which
vertices are the roots of $G(S)$ is obtained from the labels, for
odd-degree surfaces this notion is independent of the labels on
$G(S)$ because in this case each vertex is marked $c$.  However this
piece of information is quite important since sometimes it is the only
one that allows us to realize that two pairs $(\pro,S)$ and $(\pro,S')$
are not homeomorphic: if, for instance, $S$ consists of a projective
plane and two topological spheres, only the knowledge of the root
allows us to recognize whether the two spheres are mutually external or
one of them encircles the other one.

If $Roots(S)$ denotes the set of roots of $S$ defined as above, we will call 
the triple $(G(S), Contr(S), Roots(S))$ the \emph{labelled
  2-adjacency graph} of $S$.

The previous definition of $G_{nc}(S)$ and $\ol{G_c(S)}$ can be used 
also for the graph $G(T)$; this allows us to define a set of roots in 
$G(T)$, denoted $Roots(T)$,  independently of the notion of degree
used above: if $G_{nc}(T) \ne \emptyset$, we choose as roots in $G(T)$ 
the common vertices to $G_{nc}(T)$ and $\ol{G_c(T)}$; if 
$G_{nc}(T)= \emptyset $, we choose as unique root the region of $\pro 
\setminus T$ external to all the connected components of $T$.

The triple $(G(T), Contr(T), Roots(T))$ will be called the \emph{labelled
adjacency graph} of $T$.

With the previous definitions we have that

\begin{propo} (i) The labelled 2-adjacency graph of $S$ is an
  invariant of the pair $(\pro,S)$ up to homeomorphism.

(ii) The labelled 2-adjacency graph of $S$ is isomorphic to the
  labelled adjacency graph of $T$.
\end{propo}

The isolated points of $S$ do not appear at all in $G(S)$. While their
number is sufficient for the topological characterization of $S$, in
order to take into account the embedding of $S$ in $\pro$ we need to
know in which regions of the complement they lie. For that it will be 
sufficient to compute a list
 $IsReg (T)=[isReg(\Sigma_1), \ldots, isReg(\Sigma_k)]$ where
$\Sigma_1, \ldots , \Sigma_k$ are the regions of $\R^3 \setminus T$ and
$isReg(\Sigma_i)$ is the number of isolated points contained in $\Sigma_i$.

We will collect all the mentioned data concerning the surface in a
single list of data
$$D(S)= [\chi(T), G(T), Contr(T),Roots(T), \mathcal R(T), IsReg (T)].$$
By the previous considerations we have that
\begin{enumerate}
\item $D(S)$ is an invariant up to homeomorphism of the pair
  $(\pro,S)$,
\item $D(S)$ completely determines the topological type of $S$,
\item though not sufficient to determine the pair $(\pro,S)$ 
\begin{figure}[htbp]
  \centering
  \begin{tabular}{ccccc}
    \resizebox{0.175\textwidth}{!}{%
      \includegraphics{tori1.eps}
    }
    &&
    \resizebox{0.315\textwidth}{!}{%
      \includegraphics{tori2.eps}
    }
    &&
    \resizebox{0.325\textwidth}{!}{%
      \includegraphics{tori3.eps}
    }
  \end{tabular}
  \caption{\footnotesize \sl  Surfaces pairwise not ambient-homeomorphic but sharing the same $D(S)$}
  \label{fig:tori}
\end{figure}
(see for instance the example shown in Figure \ref{fig:tori}), the set
  $D(S)$ gives useful information on the surface up to
  ambient-homeomorphism. For instance the surfaces $S$ and $S'$
  represented in Figure \ref{fig:coni} are homeomorphic but the pairs
  $(\pro,S)$ and
  $(\pro,S')$ are not homeomorphic since the labelled adjacency graphs
  of $T$ and $T'$ are not isomorphic (because of the position of the labels 
   $c$ and $nc$).
\end{enumerate}

\begin{figure}[htbp]
  \centering
  \includegraphics[width=.35\hsize]{coni.eps}
  \caption{\footnotesize \sl Two surfaces that are homeomorphic, but
  not ambient-homeomorphic.}
  \label{fig:coni}
\end{figure}


The next sections will be devoted to show that all the data in $D(S)$
can be computed starting from an equation of $S$, even though $T$ is not
algebraic.


\section{Passing through a critical point - Theoretical results}\label{Morse-theory}

In this section we describe our general strategy to compute
the topology of a real algebraic surface $S$ having only isolated
singularities and we present the mathematical theoretical results on
which the algorithm bases its correctness. We will present the algorithm and 
discuss all the computational aspects in the next sections. 

In the present section we
assume that $S$ is a compact affine surface in $\R^3$ defined by a square-free 
polynomial equation $f(x,y,z)=0$ (as we will
see, the general case can be reduced to this one). 
\smallskip


Denote by $p: S \to \R$ the projection defined by $p(x,y,z)=z$. Thus a
point $P\in S$ is critical for $p$ if it annihilates the first partial derivatives $f_x$ and 
$f_y$. As recalled in Section \ref{definitions}, 
since in our hypotheses $S$ has finitely many singular points,  
the projection $p$ can have at most finitely many critical values. 

We assume that  $(x,y,z)$ is a \emph{good frame}, i.e. that  
the fiber $p^{-1}(\gamma)$ over any critical value 
$\gamma$ contains only one critical point in $S$, which turns out to be the only singular 
point of the section curve $S \cap \{z=\gamma\}$.
In Section \ref{tests} we will show that, up to a generic 
linear change of coordinates, we can assume that $(x,y,z)$ is a good frame.

Since $S$ is compact, let $\tau$ be a positive real number such that 
$S \subset \R^2 \times (-\tau,\tau)$.

For any $W\subseteq \R^3$ and for any $U\subseteq  \R$ let $W_U=W \cap p^{-1}(U)$. 
Thus, if $a< b$, 
we have that $S_{[a,b]}=S\cap \{a \leq z\leq b\}$. For simplicity we will denote by $S_a$
the level curve $S_{\{a\}}=S \cap \{z=a\}$ and by $S_{\leq a}$  the level surface 
$S_{[-\infty,a]}= S\cap\{z\leq a\}$  of $S$  having $S_a$ as its boundary.  
  
Denote by $Q_1, \ldots, Q_m$ the points in $S$ which are critical for $p$. 

We will need to work at each point $Q_i$ in a sufficiently  small Milnor ball 
where both the surface and its plane horizontal section at $Q_i$ have 
a conical structure. To this purpose, using the Theorem of 
semialgebraic triviality for the 
  function $X\to d(X,Q)$ and adapting suitably the proof of Theorem
  9.3.6 in \cite{BCR}, we have:

\begin{propo}\label{conical-structure} Let $Q=(\alpha, \beta, \gamma)$
  be an isolated singular point of  $S$.  Let $r$ be a Milnor radius
  at $Q$ both for the surface $S$ and 
for the plane curve $S\cap \{z=\gamma\}$.  Then for all $0 < \eps \leq
r$ there exists a homeomorphism $\phi:D(Q,\eps) \to D(Q,\eps)$
such that:
  \begin{enumerate}
  \item $\phi(S \cap D(Q,\eps))$ coincides with the cone over $C(Q,\eps)$ with vertex 
  $Q$;
  \item $\phi(D(Q,\eps)\cap \{z=\gamma\})=D(Q,\eps)\cap \{z=\gamma\}$;
  \item $\phi(x) =x$ for all $x\in  S(Q,\eps)$;
  \item $\phi(S(Q,\eps '))=S(Q,\eps ') $ for all $\eps ' \leq \eps$.
  \end{enumerate}
\end{propo}


As an immediate consequence, one also has

\begin{coro}\label{plane-conical-structure} In the hypotheses of
  Proposition \ref{conical-structure}, $\phi(S \cap D(Q,\eps)\cap
 \{z=\gamma\})$ 
 coincides with the cone over $C(Q,\eps)\cap \{z=\gamma\} $ with vertex 
  $Q$.
\end{coro}
\smallskip



Henceforth denote by $T$ a topological surface that we obtain by modifying $S$ 
as described in Proposition \ref{constrT} (see also Remark \ref{number-points} and 
Remark \ref{enlargeConstr}) inside 
all the Milnor balls $D(Q_i,\eps_i)$ centered 
at the critical points $Q_i$,
 for sufficiently small $\eps_i$. As explained in Section 
 \ref{definitions}, if $S$ contains $s$ isolated points, the surface $S$
is homeomorphic to the disjoint union of $s$ points and the 
quotient $T / {\mathcal R (T)}$ where $\mathcal R (T)$ is a generically 1-1  equivalence relation.  

We choose to represent the equivalent relation $\mathcal R (T)$ by means of 
an $r \times m$  matrix  $Rel (T)$, where $r$ denotes the number 
of connected components of $T$, including the data 
relative to all the critical points:
 
 \begin{notation}\label{notRel}
 \begin{enumerate}
 \item For any connected component $Y$ of $T$, we denote by 
 $rel (Y)$ the vector of length $m$ whose $i$-th element 
 $rel (Y)(i)$ is the number of ovals in which $Y$ meets the Milnor 
 sphere $S(Q_i, \eps_i)$ centered at $Q_i$ (which coincides 
with the number of points in $Y \cap Z(Q_i)$.
 \item If $T^1, \ldots, T^r$ are the connected components of $T$, 
 we denote by $Rel (T)$ the $r \times m$ matrix whose $h$-th 
 row is the vector $rel (T^h)$.
\end{enumerate}
 \end{notation}
 
 
\begin{Rem}\label{propRel} 
\begin{enumerate}
\item It is possible to extract from $Rel (T)$ the data sufficient to 
recover the equivalence relation $\mathcal R (T)$ such that $S \setminus Isol (S)$ is 
homeomorphic to $T/ {\mathcal R (T)}$ using only  the columns 
corresponding to the singular non-isolated points.
\item The notation introduced in Notation \ref{notRel} can be used also 
for any level surface $T_{\leq b}$ with respect to its connected components: 
if $Y^1, \ldots, Y^g$ are the connected components of $T_{\leq b}$, then 
$rel (Y^j)$  is a vector of length $m$ whose $i$-th element 
$rel(Y^j)(i)$ is the number of ovals in which 
$Y^j$ meets the Milnor sphere $S(Q_i ,\eps)$ centered at $Q_i$, while 
$Rel (T_{\leq b})$ is the $g \times m$ matrix having as 
 rows  $rel (Y^1), \ldots, rel ( Y^g)$.
\end{enumerate}
\end{Rem}

Thus our aim is  to compute the list of data
$$D(S)= [\chi(T), G(T), Contr(T), Roots(T), Rel (T), IsReg (T)].$$ 

In the affine case the computation
of the function $Contr(T)$ and of the set $Roots(T)$  is straightforward: 
since $T$ is contained in $\R^3$, all its
components are contractible and all the regions of $\pro \setminus T$
are contractible except the only one, say $U_0$, external to all the components of
$T$. It is therefore sufficient to recognize $U_0$ as the only unbounded 
region of $\R^3 \setminus T$, choose it as the only root of $G(T)$, mark
it as non-contractible and mark as contractible all other vertices in
$G(T)$.


In order to compute  $D(S)$ it is 
sufficient to be able to study the level surface  $T_{\leq b}$ assuming 
the knowledge  of $T_{\leq a}$ when the  strip $\R^2 \times [a,b]$ contains only 
one critical point $Q$ for $p$ in its interior part. 
Since  $T_{\leq b} = T_{\leq a} \cup T_{[a,b]}$, our strategy  
 to compute $D(S_{\leq b})$ from $D(S_{\leq a})$ will consist in combining the data 
 concerning the level surface $T_{\leq a}$ contained in $D(S_{\leq a})$ with information 
 concerning the surface $T_{[a,b]}$ 
 in the strip $\R^2 \times [a, b]$. 
 

Therefore for the rest of this section we assume that:

$\bullet$ the  strip $\R^2 \times [a,b]$ contains only one critical point 
$Q=(\alpha,\beta,\gamma)$ for $p$ which is not isolated in $S$, 
with $a< \gamma <b$,
(in particular $a$ and $b$ are regular values),
 
$\bullet$ $\eps$ is a positive number which is a Milnor radius both for $S$ 
and for the curve $S_{\gamma}$  (which 
in particular guarantees that each oval of the curve $C(Q,\eps)= S \cap S(Q,\eps)$ 
is transversal to $\{z= \gamma\}$); 
we assume also that $D(Q,\eps)$ is contained in the open
 strip $\R^2 \times (a,b)$.
 
\smallskip


The tool we intend to use in order to compute the topology 
of the connected components of $T_{\leq b}$ starting from the topology 
of $T_{\leq a}$ is the following theorem, which is an easy consequence 
of results due to Lazzeri (\cite{L}) concerning more general 
``spaces with isolated singularities''. Recall that, if $Z$ is a topological space, 
the space $Z\times [0,1]/ Z \times \{1\}$, obtained by collapsing to a point the
subspace $Z \times \{1\}$, is called \emph{the cone over $Z$}; we will
denote it by $Cone(Z)$. Conventionally the cone over the empty set
consists of a point. If $W \subseteq Z$, we will denote by $C(Z,W)$ the 
space obtained from the disjoint union of $Z$ and $Cone(W)$ by 
identifying each $w\in W$ with $w \times \{0\}\in Cone(W)$.

\begin{teo}\label{teoLazzeri} (\cite{L})  Let $X$ be a compact semialgebraic 
subset of $\R^3$ of dimension $2$. 
Let $Q=(\alpha,\beta,\gamma)\in X$ be a 
critical point  for the projection $p: X \to \R$ defined by $p(x,y,z)=z$.
Let $a<\gamma <b$ and assume that  
\begin{enumerate}
\item[(i)] the boundary of $X_{[a,b]}$ is contained in $\{z=a\} \cup \{z=b\}$,
\item[(ii)]  the strip $\R^2 \times [a,b]$  
does not contain any critical point for $p$ 
except $Q$ \ (in particular $X_{(a,b)} \setminus \{Q\}$ is a 
$2$-dimensional differentiable manifold and $Q$ is the
only singular point for the level curve $X_{\gamma}$).
\end{enumerate} 

Then there exists $r>0$  such that
\begin{enumerate}
\item for any $\eps\in (0,r)$ there exists $\eta(\eps)>0$ such that
  for any $\eta \in (0,\eta(\eps)]$ the plane set $V^{\eps}_{\gamma -
  \eta}=X_{\gamma - \eta}\cap D(Q,\eps)$ is a smooth compact curve
  (with boundary $X_{\gamma - \eta}\cap S(Q,\eps)$) whose
  diffeomorphism type depends neither on $\eps$ nor on $\eta$ (the
  diffeomorphism class will be called the \emph{vanishing manifold} of
  $X$ at $Q$ and denoted  simply by $VM(Q)$),
\item $X_{\leq \gamma}$ is homeomorphic to $X_{\leq \gamma - \eta}/{V^{\eps}_{\gamma -
  \eta}}$ for any $\eta \in (0,\eta(\eps))$,
 \item if $K\subseteq X_{\gamma - \eta}$ is any deformation retract of $V^{\eps}_{\gamma -
  \eta}$, then $X_{\leq \gamma}$ is homotopically
  equivalent to $C(X_{\leq \gamma - \eta},K)$,
\item $X_{\leq \gamma}$ is a deformation retract of $X_{\leq b}$,
\item $(X_{\gamma} \setminus B(Q,\eps)) \cup (X \cap S(Q,\eps))$ is a 
deformation retract of $X_{[a,b]}\setminus B(Q,\eps)$.
\end{enumerate} 
\end{teo} 


As outlined in \cite{L}, the previous theorem  
can be proved by making use of the integral curves of a vector field 
suitably constructed starting from the gradient field of the projection 
$p$ on $X_{[a,b]} \setminus \{Q\}$ and adapted with respect to the 
Milnor ball $D(Q,\eps)$.



\begin{figure}[htbp]
  \centering
  \includegraphics[width=.55\hsize]{pasta-sections.eps} 
  \caption{\footnotesize \sl Sections of a surface with a Milnor
  sphere centered at a singular point $Q$ and with a horizontal plane
   $\{z = \gamma - \eta\}$ passing below the singularity.}
  \label{fig:ex-pasta-sections}
\end{figure}




\begin{Rem}\label{comments-on-teoLazzeri} 
When the strip $\R^2 \times [a,b]$  
does not contain any critical point for $p$  on the algebraic surface $S$
except $Q$, 
the vanishing manifold $VM(Q)$ is the union of $h$ ovals and $k$ arcs, 
with $h \geq 0$ and $k \geq 0$. Since an arc of a curve is
homotopically equivalent to a point, a deformation retract $K$ of $VM(Q)$ 
consists of $h$ ovals and  $k$ points, so that by Theorem \ref{teoLazzeri} (3) and 
(4) $S_{\leq b}$ is homotopically equivalent to the space obtained by 
suitably   attaching to $S_{\leq \gamma - \eta}$ the cone over $h$
ovals and  $k$ points. Since $S_{\leq a}$ is a deformation retract of 
 $S_{\leq \gamma - \eta}$ (by means of a deformation which is the identity 
 on $S_{\leq a}$),  $S_{\leq b}$ is also homotopically equivalent to the space 
 obtained by  suitably attaching to $S_{\leq a}$ the cone over $h$
ovals and  $k$ points. In particular, if the vanishing manifold consists 
of $k$ arcs, then $\chi(S_{\leq b})= \chi(S_{\leq a}) + (1-k)$.
\end{Rem}

Theorem \ref{teoLazzeri}  cannot be directly applied to study the topology 
of $T$ because $T$ is only  a topological manifold. It might be applied to $S$, 
but $S$ is not homotopically equivalent to $T$. Proposition \ref{cc-strip} 
will show  that it is possible to compute 
the Euler characteristics of the connected components $W$ of $T_{[a,b]}$ 
applying Theorem \ref{teoLazzeri}  to a suitable semialgebraic set 
homeomorphic to $W$. In this spirit observe that, even though 
$T$ is not algebraic, 
however  outside the union of the Milnor balls centered at the critical 
points of $S$, 
the surfaces $S$ and $T$ coincide (see Proposition \ref{constrT} (2)), 
hence $T$ is here described 
by an algebraic equation.  Instead, inside each Milnor ball  $D(Q,\eps)$ 
the local behavior of $S$ and $T$ 
is different, for instance because $S \cap D(Q,\eps)$ is connected, 
while $T \cap D(Q,\eps)$ has as many connected 
components  as the curve $C(Q,\eps)$. 


\subsection{Computation of $\chi(T_{\leq b})$.}
 
Observe that (see Figure \ref{fig:up-down}):

- each connected component of $T_{\leq a}$ is contained in a single 
connected component of $T_{\leq b}$, but several connected components 
of $T_{\leq a}$ may be contained in the same
connected component of $T_{\leq b}$,

- a connected component of $T_{\leq b}$ can intersect the strip 
$\{a \leq z \leq b\}$ in several distinct connected components of $T_{[a,b]}$.


\begin{figure}[htbp]
  \centering
  \includegraphics[width=.45\hsize]{up-down.eps} 
  \caption{\footnotesize \sl A connected  component $Y$ of $T_{\leq b}$ 
  for which both $Y \cap T_{\leq a}$ and $Y \cap T_{[a,b]}$ are not connected.}
  \label{fig:up-down}
\end{figure}

Recall that:
\begin{lemma}\label{chi-unione} If $U_1, U_2$ are compact 
topological  surfaces with boundary such that 
$U_1$ intersects $U_2$ in a union of ovals, then 
$\chi(U_1 \cup U_2)= \chi(U_1)+ \chi( U_2)$. 
\end{lemma}

As a consequence, the next result, assuming  
we know  $T_{\leq a}$, reduces the computation of the topology of $T_{\leq b}$
to the investigation of the connected components of $T_{[a,b]}$:

\begin{propo}\label{chi-glue} Let $Y$ be a connected component of $T_{\leq b}$. 
Then  the Euler characteristic
$\chi(Y)$ is the sum of the Euler characteristics of all the 
connected components of $Y\cap T_{\leq a}$ 
and of the Euler characteristics of all the connected components of 
$T_{[a,b]}$ contained in  $Y$. 
\end{propo}

Therefore we now focus to the first task of recognizing the connected 
components  
of $T_{[a,b]}$ and computing the Euler characteristic of each of them.

Let $W$ be a connected component of $T_{[a,b]}$. If $W\cap D(Q,\eps)=\emptyset$, then $W$ is topologically a cylinder. 
Otherwise  $W$ intersects the Milnor sphere $S(Q,\eps)$
in finitely many ovals of the curve $C(Q,\eps)$, say 
$\omega_1, \ldots , \omega_q$ 
with $q\geq 1$. As already pointed out, 
we cannot directly apply Theorem  \ref{teoLazzeri} to the topological surface 
$W$ obtained by attaching a $2$-cell along each oval $\omega_i$ to 
the semialgebraic set $E= W \setminus B(Q,\eps)$. 
However the $2$-cell attached along $\omega_i$ is homeomorphic to the cone over 
 $\omega_i$ with vertex $Q$ and, by the conical structure, it is also 
homeomorphic to the closure of the connected component of 
$(S \cap D(Q,\eps)) \setminus \{Q\}$ containing $\omega_i$.


In order to be able to give precise statements, 
it will be useful to introduce a suitable notation to indicate, 
for each oval $\omega$ of $C(Q,\eps)$, the ``external" component of 
$S_{[a,b]}$  and the ``internal" component of 
$S \setminus \{Q\}$ inside the Milnor ball $D(Q,\eps)$ containing $\omega$ in 
its boundary:

\begin{notation}\label{in-ext-tubes}
For any oval $\omega$ of $C(Q,\eps)$ 
\begin{enumerate}
\item $E(\omega)$ denotes the connected component of $S_{[a,b]} \setminus 
B(Q,\eps)$ (= $T_{[a,b]} \setminus B(Q,\eps)$) containing $\omega$,
\item  $I(\omega)$ denotes the connected component of 
$(S \cap D(Q,\eps)) \setminus \{Q\}$ containing $\omega$.
\end{enumerate}
\end{notation}

\begin{Rem}\label{sameE} It may occur that $E(\omega_1)=E(\omega_2)$ 
for distinct ovals $\omega_1, \omega_2$ of $C(Q,\eps)$: for 
instance in the case  of the ``pinched torus", represented in Figure 
\ref{fig:ex-pinched-torus} and obtained by 
collapsing a circle in a torus, we have $C(Q,\eps)=\omega_1\cup \omega_2$ and   
$E(\omega_1)=E(\omega_2)$. 
In particular this example shows that  $S_{[a,b]} \setminus B(Q,\eps)$ may not have as many connected components as the curve $C(Q,\eps)$.
\end{Rem}
\begin{figure}[htbp]
  \centering
  \includegraphics[width=.30\hsize]{pinched_torus.eps} 
  \caption{\footnotesize \sl Sections of a pinched torus with a Milnor
  sphere centered at a singular point  and with two planes 
   $\{z =a\}$ and  $\{z =b\}$.}
  \label{fig:ex-pinched-torus}
\end{figure}



In order to compute the Euler characteristic of any connected component $W$ 
of $T_{[a,b]}$, if  $W \cap S(Q,\eps)= \omega_1 \cup \ldots \cup \omega_q$, 
we will make use of the vanishing manifolds of 
$\ol{I(\omega_1)}, \ldots , \ol{I(\omega_q)}$. 
We will see that, for each  oval $\omega$ of $C(Q,\eps)$, 
the nature of the vanishing manifold of $\ol{I(\omega)}$ at $Q$ and also 
of $E(\omega) \cap (\{z=a\} \cup \{z=b\})$ 
is strictly related to the position of the oval $\omega$  with respect
to the circle $S(Q,\eps) \cap \{z=\gamma \}$ that we will call the ``equator" and denote by $Eq(Q,\eps)$. This is the reason why we 
 introduce the following terminology:


\begin{defi}\label{+-} If $Q=(\alpha, \beta, \gamma)$, a connected subset $X$
 of $S(Q,\eps)$ is called  
\begin{enumerate}
\item of type $(+)$ if $X \subseteq \{z>\gamma\}$
\item of type $(-)$ if $X \subseteq \{z<\gamma\}$
\item of type $(+-)$ if $X \cap \{z=\gamma\}\ne \emptyset$.
\end{enumerate}
\end{defi}


Observe that, by Proposition \ref{conical-structure},  the type of an oval $\omega$ 
of $C(Q,\eps)$ does not change if we reduce the sphere radius, i.e.

\begin{propo}\label{propo:sametype} Let $\omega$ be an oval of
  $C(Q,\eps)$. For any positive $\eps' \leq \eps$, let $\omega (\eps')= I(\omega) \cap
S(Q,\eps')$. Then, for all $\eps' \le \eps$ we have:
  \begin{enumerate}
  \item If $\omega$ is an oval of type $(+)$ (resp. $(-)$), then
    $\omega (\eps')$ is an oval of type $(+)$ (resp. $(-)$).
  \item If $\omega$ is an oval of type $(+-)$, then $\omega (\eps')$ is
    an oval of type $(+-)$ and $\omega (\eps') \cap \{z=\gamma\}$
    contains as many points as $\omega \cap \{z=\gamma\}$.
  \end{enumerate}
\end{propo}

The following result describes the vanishing manifold of $\ol{I(\omega)}$ and the 
topological type of $E(\omega)$ for an oval $\omega$ of type $(-)$:
  
  
\begin{propo}\label{ovals-} Let $\omega$ be an oval of
  $C(Q,\eps)$ of type $(-)$. Then 
  \begin{enumerate}
  \item  there exists $\eta_0 > 0$ such that for all positive $\eta \leq \eta_0$
 the set $\ol{I(\omega)} \cap \{z = \gamma
    - \eta\}$ consists of exactly one oval;
\item    $E(\omega)\cap \{z=a\}$  consists of
  a single oval $\omega '$, the boundary of $E(\omega)$ is equal to $\omega
  \cup \omega '$ (in particular $E(\omega)\cap \{z=b\}=\emptyset$)  and $E(\omega)$ is homeomorphic to a connected cylinder.
  \end{enumerate}
\end{propo}

\begin{proof} 
(1) 
Since $\eps$ is a Milnor radius both for $S$ 
and for the curve $S_{\gamma}$, $I(\omega)\cap \{z=\gamma \}=\emptyset$ 
and  $\ol{I(\omega)}\cap \{z=\gamma \}=\{Q\}$.

Let $\eta_0 >0$ be such that $\omega \subset \{ z <
\gamma - \eta_0\}$. By connectivity, for any $c\in (\gamma - \eta_0,\gamma)$ the set
$I(\omega)_c$ is not empty. Since $\omega \cap
\{z=c\} = \emptyset$ and the plane $\{z = c\}$ is
transversal to $I(\omega)$, then $I(\omega)_c$ is a non-singular
curve without boundary, i.e. consisting only of ovals.
 
Let $Y$ be the connected component of $S_{[a,\gamma)}$ containing $\omega$. 
By Thom's first isotopy Lemma there exist an oval $\omega ' \subseteq S_a$ 
such that $Y$ is homeomorphic  to $\omega ' \times [a,\gamma)$. In 
particular for any $c\in [a,\gamma)$ $Y_c$ is connected and $Y_a= \omega'$.

Since $I(\omega)$ is a connected set containing $\omega$, then $I(\omega) 
\subseteq Y$. In particular for any $c\in (\gamma - \eta_0,\gamma)$ we have 
that $I(\omega)_c \subseteq Y_c$ and hence $I(\omega)_c$ consists of just one oval.

More precisely $Y \cap S(Q, \eps)=\omega$, because if there exists 
a point $R\in (Y \cap S(Q, \eps))\setminus \omega$ and $\sigma$ is the 
oval of $C(Q,\eps)$ containing $R$, then $Y$ contains $I(\sigma)\cap \{z< 
\gamma\}$. Then for any $c\in (\gamma - \eta_0,\gamma)$ the set 
$Y_c \setminus I(\omega)_c$
contains points in $I(\sigma)_c$, while $Y_c$ is connected.

Consider the connected component $W$ of $E(\omega)_{[a,\gamma)}$ 
containing $\omega$. Since  $I(\omega)\cup W$ is a connected set containing 
$\omega$, then $I(\omega)\cup W\subseteq Y$. It follows that  
$W \cap S(Q, \eps)=\omega$ and hence $Y= I(\omega)\cup W$.

Since $\ol Y \cap \{z=\gamma \}=\{Q\}$, then $\ol W \cap \{z=\gamma \}=\emptyset$ 
so that $W$ coincides with the connected component of $S_{[a,b]} \setminus 
B(Q,\eps)$ containing $\omega$, i.e. $W=E(\omega)$. 

As a consequence $E(\omega)\cap \{z=a \}=Y\cap \{z=a \}=\omega '$ and  
the boundary of $E(\omega)$ is equal to $\omega   \cup \omega '$. Moreover  
$\ol Y = Y \cup \{Q\}$ is topologically a disk; also $\ol{I(\omega)}= I(\omega) 
\cup \{Q\}$ is topologically a disk; hence $E(\omega)= \ol Y \setminus 
\ol{I(\omega)}$ is a connected cylinder.
\end{proof}


In a similar way one can prove that:


\begin{propo}\label{ovals+} Let $\omega$ be an oval of
  $C(Q,\eps)$ of type $(+)$. Then
 \begin{enumerate}
  \item  there exists $\eta_0 > 0$ such that for all positive $\eta \leq \eta_0$
we have that $\ol{I(\omega)} \cap \{z = \gamma     - \eta\}=\emptyset$;
\item  $E(\omega)\cap \{z=b\}$  consists of
  a single oval $\omega '$, the boundary of $E(\omega)$ is equal to $\omega
  \cup \omega '$ (in particular $E(\omega)\cap \{z=a\}=\emptyset$)  and $E(\omega)$ is homeomorphic to a connected cylinder.
 \end{enumerate}
 \end{propo}  

 
 For the ovals of type $(+-)$ the situation is more complicated (see for instance 
 Figure \ref{fig:merge_cups} and Figure \ref{fig:ex-pinched-torus}). 
 Nevertheless the next proposition guarantees that also for any 
oval $\omega$ of $C(Q,\eps)$ of type $(+-)$  the nature of 
the vanishing manifold of $\ol{I(\omega)}$ 
can be derived from information concerning the algebraic curve $C(Q,\eps)$.  


\begin{propo}\label{ovals+-} Let $\omega$ be an oval of
  $C(Q,\eps)$ of type $(+-)$ and assume that 
  $\omega \cap \{z \le \gamma\}$ 
  is the union of $k$ arcs. Then there exists $\eta_0 > 0$ such that, 
  for all positive $\eta \leq \eta_0$, $\ol{I(\omega)}
    \cap \{z = \gamma - \eta\}$ is also the union of $k$ arcs 
(in particular, in order to count the number of arcs in the vanishing manifold of $\ol{I(\omega)}$, it suffices to count the number of points in $\omega$ on the equator 
$Eq(Q,\eps)$).
 \end{propo}
 
 
\begin{proof} Since $\omega$ is transversal to 
$\{z=\gamma\}$, there exists $\eta_0 >0$ such that $\omega \cap \{z
\le \gamma-\eta\}$ is the union of $k$ arcs for all positive $\eta \leq \eta_0$.
Furthermore, for $\eta \leq \eta_0$ by a transversality argument $I(\omega) \cap
\{z =\gamma-\eta\}$ is a smooth curve with a boundary equal to $\omega
\cap \{z=\gamma-\eta\}$ and hence it contains exactly $k$ arcs .
  
We have to prove that this curve cannot contain any oval. Suppose that
there exists $\eta'\leq \eta_0$ such that $I(\omega) \cap \{z=\gamma-\eta'\}$ contains an
oval $\sigma$. Since $\overline{I(\omega)}$ is homeomorphic to a disk, then
$\sigma$ disconnects $\overline{I(\omega)}$. Let $I(\omega)'$ be 
the connected component
of $\overline{I(\omega)} \setminus \sigma$ not containing $\omega$. It is clear that $I(\omega)' \cap S(Q,\eps)=\emptyset$.

Let $m = \min_{\overline{I(\omega) '}}z$ and
$M=\max_{\overline{I(\omega) '}}z$. Since the function $p(x,y,z)=z$ cannot be constant
on $\overline{I(\omega)'}$, then $M\ne m$, and since $z$ is constant on the
boundary of $\overline{I(\omega)'}$ then at least one of them must be achieved
in an interior point $R$ of $\overline{I(\omega)'}$.

If $Q \not\in I(\omega)'$,  such a point $R$ should be a critical point for 
the function $p$ on $S$ different from $Q$, while no such point 
exists in the strip we are working in.

Suppose instead that $Q \in I(\omega)'$. If the maximum $M$ were achieved 
in a point different from $Q$, we would have a contradiction as before. So
we can suppose that $I(\omega)'\setminus\{Q\} \subseteq \{z < \gamma\}$. The
set $I(\omega)'$ is open in $\overline{I(\omega)}$, hence for $\eps'$ small enough
$D(Q,\eps') \cap \overline{I(\omega)} \subseteq I(\omega)'$. Then $D(Q,\eps') \cap
\overline{I(\omega)} = D(Q,\eps') \cap I(\omega)'$ and thus $S(Q,\eps') \cap \overline
{I(\omega)} = S(Q,\eps') \cap I(\omega)'$. Since $I(\omega)'\setminus\{Q\} \subseteq \{z <
\gamma\}$, we have $S(Q,\eps') \cap \overline{I(\omega)}\subseteq S(Q,\eps')
\cap \{z < \gamma\}$ which means that $\omega(\eps')=I(\omega) \cap
S(Q,\eps')$ should be an oval of type $(-)$, contradicting Proposition
\ref{propo:sametype}.
\end{proof}
 

As a consequence of all the previous results we obtain a complete 
description of the possible types of connected components of 
$T_{[a,b]}$ and of the Euler characteristic 
of each of them:

\begin{teo}\label{cc-strip} Let $W$ be a connected component of $T_{[a,b]}$. 
Then
\begin{enumerate}
\item if $W \cap C(Q,\eps)= \emptyset$, then $W$ is a cylinder intersecting 
both $\{z=a\}$ and $\{z=b\}$ in a single oval, and $\chi(W)=0$, \\  (in this case we say that 
$W$ is a component of $T_{[a,b]}$ of type Cylinder)
\item if $W \cap C(Q,\eps)$ contains an oval $\omega$ of type $(-)$, then 
 $W \cap C(Q,\eps)=\omega$; moreover  $W$ intersects $\{z=a\}$  in a single oval, 
 does not intersect $\{z=b\}$ and $\chi(W)=1$,\\  (in this case we say that 
$W$ is a component of $T_{[a,b]}$ of type $(-)$)
\item if $W \cap C(Q,\eps)$ contains an oval $\omega$ of type $(+)$, then 
 $W \cap C(Q,\eps)=\omega$; moreover  $W$ intersects $\{z=b\}$  in a single oval, 
 does not intersect $\{z=a\}$ and $\chi(W)=1$,\\  (in this case we say that 
$W$ is a component of $T_{[a,b]}$ of type $(+)$)
 \item  if $W \cap C(Q,\eps)$ contains an oval $\omega$ of type $(+-)$, then 
 all the other ovals of $W \cap C(Q,\eps)$, 
 say $\omega_1=\omega, \omega_2, \ldots, \omega_q$, are of type $(+-)$; 
 if $k_i$ denotes the number of arcs of $\omega_i \cap \{z\leq \gamma\}$ 
(also called the negative arcs of $\omega_i$), 
then $\chi(W)= \sum_{i=1}^q 
(1-k_i)$; moreover $W$ intersects both $\{z=a\}$  and $\{z=b\}$ in at least one oval,
\\  (in this case we say that 
$W$ is a component of $T_{[a,b]}$ of  type $(+-)$)
\end{enumerate}
\end{teo}
\begin{proof} (1) follows from the fact that the restriction of $p$ to $W$ 
has no critical points.

(2) (resp. (3)) $W$ is homeomorphic to $E(\omega) \cup \ol{I(\omega)}$, 
where $E(\omega)$ is a topological cylinder by Proposition \ref{ovals-} (resp. 
Proposition \ref{ovals+}) and $\ol{I(\omega)}$ is a $2$-cell. Since 
$E(\omega) \cap \ol{I(\omega)} =\omega$, by Lemma \ref{chi-unione} we have 
$\chi (W)= \chi (E(\omega)) + \chi (\ol{I(\omega)})=1$.

(4) The exterior part $E(\omega)$ cannot intersect $C(Q,\eps)$ in ovals
 different from $\omega$ of type $(+)$ or $(-)$ by Propositions 
\ref{ovals-} and \ref{ovals+}. 
Let $\widehat W= (W\setminus B(Q, \eps))\cup 
\ol{I(\omega_1)} \cup \ldots \cup \ol{I(\omega_q)}$. By Theorem \ref{teoLazzeri} 
we get that $\chi(\widehat W)= 1- \sum_{i=1}^q k_i$. 
Since $\widehat W\cap D(Q, \eps)$ is contractible, then 
$\chi( \widehat W\cap D(Q, \eps))=1$. 
Thus $\chi( W\setminus B(Q, \eps)) = \chi (\widehat W\setminus B(Q, \eps)) = 
(1- \sum_{i=1}^q k_i) -1= - \sum_{i=1}^q k_i$.
Since $W$ is obtained attaching $q$ $2$-cells to $ W\setminus B(Q, \eps)$, 
we have that  $\chi(W)= \chi( W\setminus B(Q, \eps)) +q=  - \sum_{i=1}^q k_i +q=
 \sum_{i=1}^q (1-k_i)$.
\end{proof} 

By Proposition \ref{ovals-} and Proposition \ref{ovals+} there is a $1-1$ correspondence between the ovals $\omega$ of $C(Q,\eps)$ of type $(-)$ or $(+)$ 
 and the connected components of $T_{[a,b]}$ containing them, which is not true 
 for ovals of type $(+-)$. 
 Moreover:
\begin{enumerate}
\item if $\omega$ is of type $(+)$, attaching a 2-cell along $\omega$
  to $S \setminus B(Q,\eps)$, there appears in $T_{[a,b]}$ (and in $T_{\leq b}$) 
a new connected  component homeomorphic to a disk and with boundary 
a single oval $\omega'\subset C_b$;
 \item if $\omega$ is of type $(-)$, the attachment of a 2-cell along
 $\omega$ is topologically equivalent to the attachment of a 2-cell
 along an oval $\omega '$ of $S_a$; thus it contributes increasing
 by 1 the Euler characteristic of the connected component $Y'$ of $T_{\leq a}$
 containing $\omega'$. Note that the connected component of $T_{\leq b}$ 
 containing $Y'$ may
 intersect $S(Q,\eps)$ also in other ovals of type $(-)$ or $(+-)$,
 thus its topology passing from level $a$ to level $b$ will be
 simultaneously influenced by the attachment of 2-cells along all
 these ovals.
 \end{enumerate}


Therefore, in order to reconstruct correctly $\chi(T_{\leq b})$, it is necessary 

$\bullet$  to recognize the connected components of $T_{[a,b]}$ on the basis 
of the characterization given by Theorem \ref{cc-strip},

$\bullet$  to determine all the attachment relations among the connected 
components of $T_{\leq a}$ and the connected 
components of $T_{[a,b]}$,

$\bullet$  to compute 
the Euler characteristic of any connected component $Y$ of $T_{\leq b}$ using 
Proposition \ref{chi-glue} and the Euler characteristic of the components of 
$T_{\leq a}$ contained in $Y$.

We will see in Section \ref{affinecase} how all these tasks can be performed 
effectively. 


\subsection{Computation of $G(T_{\leq b})$.}

The ability to determine the attachment relations among the connected 
components of $T_{\leq a}$ and the connected 
components of $T_{[a,b]}$ is also important for computing $G(T_{\leq b})=
G(\{z\leq b\}, T_{\leq b})$ starting from $G(T_{\leq a})$. 


Recall that we can choose as a root in the tree $G(T_{\leq a})$ 
the vertex corresponding to the unique unbounded region of 
$\{z\leq a\} \setminus T_{\leq a}$. 
This choice makes $G(T_{\leq a})$ an ordered tree; in particular if $v_1, v_2$ are 
the two vertices of an edge $e$ and $v_1$ is the vertex which is nearer to the root, we 
can call $v_1$ the \emph{first vertex} of $e$ and $v_2$ its 
\emph{second vertex}.
We can apply to $G(T_{\leq a})$ the following terminology:

\begin{defi}\label{parent} Let $G$ be a rooted  tree, $r$ its root and let 
$e$ be an edge of $G$.  
\begin{enumerate}
\item[(i)] Unless $r$ is a  vertex of $e$, we call \emph{parent} 
of $e$ (denoted ${\bf parent}(e)$) the unique edge of $G$ having as second 
vertex the first vertex of $e$.
\item[(ii)] We call \emph{children} of $e$ (denoted ${\bf children}(e)$) the set 
of all the edges of $G$ 
having as first vertex the second vertex of $e$.
\end{enumerate}
\end{defi}

Let $Y$ be a  connected component of $T_{\leq b}$ 
such that $Y\cap \{z \leq a\}=\emptyset$; then $Y$ coincides with a connected 
component of $T_{[a,b]}$ of type $(+)$ and therefore we will call it a component 
of $T_{\leq b}$ of type $(+)$. Let $\beta=Y\cap \{z=b\}$.
 Each other connected component of $T_{\leq b}$ which meets $\{z=b\}$ in an  oval 
 contained in $\beta$ is necessarily of type $(+)$. Therefore removing 
 from $G(T_{\leq b})$ all the edges corresponding to the connected components of 
$T_{\leq b}$ of type $(+)$ yields a connected tree 
$G'(T_{\leq b})$. We will compute $G(T_{\leq b})$ by first computing $G'(T_{\leq b})$
and then glueing to it  the edges corresponding to the connected components of type $(+)$.


If $Y$ is a  connected component of $T_{\leq b}$ that 
meets $\{z=a\}$, and we regard it as $Y=(Y\cap T_{\leq a}) \cup (Y\cap T_{[a,b]})$, then 
all the distinct edges of $G(T_{\leq a})$ corresponding to the connected components 
of $Y\cap T_{\leq a}$ glue into a single edge in $G(T_{\leq b})$. 
If we have recognized which edges of $G(T_{\leq a})$ glue, we can compute 
the subgraph $G'(T_{\leq b})$ as a quotient of $G(T_{\leq a})$ by means 
of the equivalence relation given by the edge glueing. 

However observe that two connected 
components of $T_{\leq a}$ that glue in $T_{\leq b}$ do not necessarily lie 
in the boundary of one region of $\{z\leq a\} \setminus T_{\leq a}$ 
(see Figure \ref{fig:merge_cups}). So the corresponding two edges in $G(T_{\leq a})$ 
glue into a single edge of $G(T_{\leq b})$ but they do not necessarily 
have a vertex in common.


\begin{figure}[htbp]
  \centering
  \includegraphics[width=.55\hsize]{merge_cups.eps} 
  \caption{\footnotesize \sl Example of two connected components $Y_1, Y_2$ of 
  $T_{\leq a}$ which lie in the boundaries of different regions of 
  $\{z\leq a\} \setminus T_{\leq a}$ but which glue in $T_{\leq b}$.}
  \label{fig:merge_cups}
\end{figure} 

In general identifying two edges without a common vertex is ambiguous, 
since it is not clear how the two pairs of vertices have to be identified. 
Though, using the fact that $G'(T_{\leq b})$ is a tree, we can see that 
$G'(T_{\leq b})$ can be recursively obtained from $G(T_{\leq a})$ 
by applying finitely many identifications between pairs of edges 
sharing a common vertex:


\begin{defi}\label{fold}
A fold is a graph transformation which identifies 2 vertices of distance 2 
and the path between them with a single edge, i.e. the 2 edges along the 
path are folded at the common vertex.
\end{defi}


\begin{lemma}\label{fold-tree}
Applying a fold to a tree produces a tree.
\end{lemma}
\begin{proof} Recall that a connected graph is a tree if and only if the 
number of vertices is one more than the number of edges.
The graph obtained applying a fold to a tree is obviously connected. Moreover 
a fold reduces by one both
the number of vertices and edges, so the number of vertices remains one 
more than the number
of edges; hence  the resulting graph is a tree.
\end{proof}

\begin{propo}\label{reduction-to-folds}
Any reduction of a tree modulo an equivalence relation on edges 
and vertices which produces a tree can 
be achieved by applying an arbitrary sequence of folds compatible 
with the equivalence relation
until there are no more folds possible.
\end{propo}
\begin{proof} Since the result of applying a maximum compatible 
sequence of folds is a tree, we can assume
we begin with the folded tree and a reduced equivalence relation 
such that there are no remaining 
compatible folds. We want to show that the equivalence relation is trivial. 
If not let $v_1 \neq v_2$
be two equivalent vertices of minimum distance. Since there are no 
possible folds, their distance is greater than 2.
Let $w_1$ and $w_2$ be the first and last vertices encountered 
on the unique path from $v_1$ to $v_2$.
Since the length of the path is greater than 2, $w_1$ and $w_2$ are 
distinct. By minimality of the path between $v_1$ and $v_2$
there are no pairs of nodes along the path from $v_1$ to $v_2$ which 
are equivalent, in particular
$w_1$ and $w_2$ are inequivalent and there are no nodes along 
the path from $w_1$ to $w_2$ which
are equivalent to $v_1$. If we now identify $v_1$ and $v_2$, 
then we obtain two distinct paths
from $w_1$ to $w_2$ which contradicts the hypothesis that the result 
is a tree, thus the resulting equivalence relation is trivial.
\end{proof}

The previous proposition will be used in a situation where we have determined the
equivalence relation only on the edges (components). However the fact 
that the associated equivalence
relation on the vertices exists (although we do not compute it) and the fact that 
the merging can be
performed by a sequence of folds, is enough to uniquely determine the merged graph.

\smallskip


After computing $G'(T_{\leq b})$ we need to attach to it the edges corresponding to 
the connected components of $T_{\leq b}$ of type $(+)$ (i.e. the 
``new'' components of $T_{\leq b}$). 

Let $\beta_1$ be an oval of $S_b$ which bounds a connected component $Y_1$ of 
$T_{\leq b}$ of type $(+)$ and maximal with respect to 
this property. We want to update 
$G'(T_{\leq b})$ by attaching to it the edge $Y_1$ in a vertex $v$ 
that we need to detect.

There are two possible cases:

\begin{enumerate}
\item[(i)] $\beta_1$ is contained in the interior part of an oval $\beta$ of 
$S_b$ : \\ in this case $\beta$ bounds a connected component 
of $T_{\leq b}$ (necessarily not of type $(+)$) that we have already 
reconstructed as an edge $e_{\beta}$ of $G'(T_{\leq b})$; 
then we attach an edge (corresponding to $Y_1$) to the second vertex $v$ of $e_{\beta}$.
\item[(ii)] $\beta_1$ is not contained in the interior part of any oval of 
$S_b$:\\ in this case the exterior part of $\beta_1$ is unbounded and lies in the boundary 
of the unbounded region of $\{z \leq b\} \setminus T_{\leq b}$; then we 
attach an edge (corresponding to $Y_1$) to the root of $G'(T_{\leq b})$ 
(which is the required vertex $v$).
\end{enumerate}

If $G_{\beta_1}( S_b)$ is the subtree of $G(S_b)$ consisting 
of the ovals contained in $\beta_1$, then the connected components of 
$T_{[a,b]}$ bounded by these ovals form a subtree isomorphic to 
$G_{\beta_1}( S_b)$. Hence $G'(T_{\leq b})$ can be updated by attaching to it 
 a tree isomorphic to  
$G_{\beta_1}( S_b)$ in the vertex $v$ detected before.


\subsection{Computation of $Rel (T_{\leq b})$.}

Assume that $Q=Q_i$, i.e. it is the $i$-th point in the list of the critical points 
$Q_1, \ldots, Q_m$.

Recall that, if $Y^1, \ldots, Y^g$ are the connected components of $T_{\leq b}$, then 
(see Remark \ref{propRel} (2)) 
$Rel (T_{\leq b})= [rel(Y^1), \ldots, rel (Y^g)]$, where, for any $j=1, \ldots, g$, 
$rel(Y^j)$ is a vector 
of length $m$ whose $i$-th element $rel(Y^j)(i)$ is the number of ovals in which 
$Y^j$ meets the Milnor sphere $S(Q_i ,\eps)$ centered at $Q_i$.

If $Y$ is a connected component of $T_{\leq b}$ such that $Y\cap \{z\leq a\} =\emptyset$, 
then  $Y$ is a connected component of $T_{[a,b]}$  of type $(+)$, it meets 
$S(Q_i ,\eps)$ in one oval (see Theorem \ref{cc-strip}) and so $rel(Y)$ is the vector 
whose elements are zero except $rel(Y)(i)=1$.

If $Y$ is a connected component of $T_{\leq b}$ such that 
$Y\cap \{z\leq a\} \ne \emptyset$ and 
$Y \cap S(Q_i ,\eps) =\emptyset$, then $rel(Y)$ coincides with the vector $rel(Y
\cap T_{\leq a})$ as appearing in $Rel(T_{\leq a})$.

It remains to consider the case when $Y\cap \{z\leq a\} \ne \emptyset$ and 
$Y \cap S(Q_i ,\eps) \ne \emptyset$. In this case $Y$ intersects $S(Q_i ,\eps)$ 
in some ovals, say $n_Y$, of type either $(+-)$ or  $(-)$, that we detected 
during the reconstruction process described above. 
If $T^1_a, \ldots, T^c_a$ are the  connected components of $T_{\leq a}$ 
contained in $Y$, then $rel(Y)= rel(T^1_a) + \ldots + rel( T^c_a) + n_Y e_i$, where
$e_i$ is the vector of length $m$ whose $i$-th element is $1$ and all the other 
elements are zero.

If $Q$ is an isolated point, since $T\cap S(Q, \eps)=\emptyset$, then 
$Rel(T_{\leq b})=Rel(T_{\leq a})$.


\subsection{Computation of $IsReg (T_{\leq b})$.} 

We compute the list $IsReg (T)$ by recursively computing 
the list  $$IsReg(T_{\leq b})=[isReg(\Omega_1), \ldots, isReg(\Omega_h)]$$
 where $\Omega_1, \ldots , \Omega_h$ are the regions of $\{z\leq b\} \setminus T_{\leq b}$ and
$isReg(\Omega_i)$ is the number of isolated points contained in $\Omega_i$.
There are two possible situations:

Case 1: the strip $\R^2 \times (a,b)$ does not contain any isolated point. 

In this case, for any region $\Omega$ of $\{z\leq b\} \setminus T_{\leq b}$, if  
$\Omega \cap \{z\leq a\}= \Sigma_1 \cup \ldots \cup 
\Sigma_j$, we recover from the list  $IsReg(T_{\leq a})$  the elements 
$isReg(\Sigma_1), \ldots , isReg(\Sigma_j)$ and 
we insert in  $IsReg(T_{\leq b})$ the element 
$isReg(\Omega)=isReg(\Sigma_1)+ \ldots + isReg(\Sigma_j)$.
\smallskip

Case 2: the strip $\R^2 \times (a,b)$ contains an isolated point 
 $Q=(\alpha, \beta, \gamma)$ (which is revealed by the 
fact that $S(Q, \eps) \cap S =\emptyset$). 

In this case, denote by 
$\Omega$  the region of $\{z\leq b\} \setminus T_{\leq b}$ containing 
$Q$. Since $\Omega \cap \{a \leq z\leq b\}$ is a cylinder, we can detect 
it by means of the plane horizontal section $S_{\gamma}$ passing through 
$Q$ having equation 
$f(x,y,\gamma)=0$. If $Q$ is contained in the unbounded region of 
$\{z=\gamma\} \setminus S_{\gamma}$, then it is contained in the 
unbounded region of $\{z\leq b\} \setminus T_{\leq b}$. Otherwise, after choosing a 
point $M$ on the innermost oval of  $S_{\gamma}$ containing $Q$ in its interior part, 
it suffices to lift $M$ up to the level $z=b$,  to recognize 
$\Omega \cap \{z=b\}$ and hence 
$\Omega$. The techniques to do that will be described in detail in the next 
section. After detecting $\Omega$, if $\Omega \cap \{z\leq a\}= \Sigma$, 
we insert in  $IsReg(T_{\leq b})$ the element 
$isReg(\Omega)=isReg(\Sigma) +1$.


\section{The algorithm in the compact affine case}\label{affinecase}


In this section we describe a constructive procedure to compute the
list of data $D(S)= [\chi(T), G(T), Contr(T), Roots (T), Rel(T), IsReg(T)]$ when the real 
algebraic surface $S \subset \R^3$ is compact, defined by a square-free polynomial 
equation $f(x,y,z)=0$. 
We assume that:
\begin{enumerate}
\item  $S$ has finitely many critical points $Q_1, \ldots, Q_m$ w.r.t. the projection 
$p\colon S \to \R, \ \  p(x,y,z)=z$  
(and in particular finitely many singular points); let $ Crit(S)$ 
denote the critical locus, 
\item if  $Q_i$ and $Q_j$ are distinct critical points and $i <j$  then $p(Q_i) <  p(Q_j)$,
\item  $m+1$ rational numbers $a_1, \ldots, a_{m+1}$ are chosen such that 
 each $a_i$ is not a critical value and $Q_i$ is the only critical point such that 
 $p(Q_i) \in (a_{i},a_{i+1})$
\item  for each critical point $Q_i$, a real $\eps_i >0$ is known,  which  is a Milnor 
radius at $Q_i$ both for the surface and for the plane curve obtained 
intersecting $S$ with the horizontal plane through $Q_i$; if $Q_i\in \R^2 \times (a_i, a_{i+1})$, we require also that 
$D(Q_i, \eps_i)\subset \R^2 \times (a_{i}, a_{i+1})$.
\end{enumerate}

Since  $S$ has finitely many singular points,  up to a generic linear change of coordinates, 
conditions (1) and (2) are satisfied as shown in Section \ref{tests}, where a method to 
compute  a Milnor radius $\eps$  for the critical point $Q$ will also be described.  


In order to compute the data $D(S)$ we construct  a rooted tree,  that, by 
abuse of notation,  we call  again $G(T)$, with edges representing connected 
components  and vertices representing regions,  labelled with all the required 
information.  To describe  the edges of $G(T)$ we introduce the following data type:

\noindent
{\bf compRegion}=$[ {\bf ccComp}, {\bf position}]$, where:

\begin{enumerate}
\item{} ${\bf ccComp} = [points, \chi,rel]$ is a record representing  
a connected component $cc$ with:
\begin{enumerate}
\item[-] $points(cc)$  a set of  points on $cc$  
\item[-] $\chi(cc)$  the Euler characteristic of $cc$
\item[-] $rel(cc)$   a vector of length $m = \# Crit(S)$,  such that 
$rel(cc)(i)$= number of ovals in  $cc \cap S(Q_i,\eps_i)$
\end{enumerate}
\item{} ${\bf position} = [isolPts, parent, children]$ is a record where $parent$ 
and $child\-ren$ are respectively the parent of $cc$ and the children of $cc$, 
and  $isolPts$ are the isolated  points lying in the region of $\R^3 \setminus T$ corresponding to the common vertex of  $cc$ and $children(cc)$, (i.e. the second vertex of $cc$).
\end{enumerate}
\smallskip

In this representation the first field {\bf ccComp} 
contains all the information  relative to the connected components of $T$, while 
the second field {\bf position} contains the data necessary both to recover 
the structure of the tree  (i.e. the relation of an edge with the other edges of the tree) 
and to recognize the isolated points in the associated region.  We omit $Contr(T)$
since in this case it is completely determined by the knowledge of the root (see Section 
\ref{Morse-theory}).  Notice that the vertices (i.e. the regions of 
$\R^3 \setminus T$) are  only implicitly represented.
Finally we insert a conventional element  $rootG:=[{\bf null}, [isolPts, {\bf null}, children]]$, 
with  {\bf ccComp} = {\bf null} and $parent = {\bf null}$ so  that its  second vertex 
represents the unbounded region of $\R^3 \setminus T$.


For simplicity of notation, let $a=a_i$ and $b=a_{i+1}$.
As described in the previous section, the algorithm iteratively  constructs $G(T)$
 merging  the information (already) computed  for $T _{\le a}$  and the
new information for  $T_{[a,b]}$ in order to determine  $T_{\le b}$, where
$\R^2 \times [a,b]$ contains exactly  one critical point $Q$ of $S$. 
This construction is done with the functions {\bf ccStrip}, {\bf lift}  and {\bf mergeComponents}.

As described in Theorem \ref{cc-strip} the topological structure of the connected 
components of $T_{[a,b]}$  are determined by their intersections with 
the planes $\{z=a\}$, $\{z =b\}$ and with the equator $Eq(Q,\eps)$  of the Milnor 
sphere of radius $\eps$ centered at $Q$. 
If  we know ${\mathcal A} \subset  S  \cap \{z =  a\}$,   ${\mathcal B}\subset  
S \cap \{z=b\}$ and $\mathcal E \subset   S\cap Eq(Q,\eps)$  finite sets of points  
(containing at least one point in each oval of the respective curves), we distinguish 
the connected components of $T_{[a,b]}$  partitioning  the set ${\mathcal A} 
\cup {\mathcal B} \cup {\mathcal E}$  into maximal subsets of points belonging 
to the same connected component.
This action is performed by applying a function  {\bf connComp} which, given  a 
semialgebraic
set $SAL$ and a finite set of points $\mathcal Pts \subset SAL$, computes
a partition of $\mathcal Pts$, say {\bf connComp}$(SAL, \mathcal Pts)=  [Z_1 , \ldots ,
 Z_h]$, having the property  that,  for any points $P_i, P_j \in Pts$ there exists 
$k\in \{1, \ldots, h\}$ such 
that $P_i, P_j \in Z_k$ if and only if $P_i$ and $P_j$ belong to the same connected 
component of $SAL$. A possible description of  {\bf connComp} is given is Section 
\ref{tests}.

In order to compute $\mathcal A$ and $\mathcal B$ we consider the 
affine compact non-singular algebraic curves $S_a$ and $S_b$. 
All their  connected components 
are ovals whose containment relations are described by  rooted
adjacency graphs $G(S_a)$ and $G(S_b)$, where the root 
corresponds to the unbounded region 
of $\{z=a\} \setminus S_a$, (resp.$\{z=b\} \setminus S_b$) and each 
edge represents an oval. 
It is possible to study these curves  using an algorithm that returns their adjacency graphs  
 (see Section \ref{tests} and, for additional references, \cite{Roy-book}). More precisely 
 we assume to have a function
{\bf  planeGraph}$(g)$, which, given a polynomial    $g(x,y) \in \Q[x,y]$ such that  
$V(g)$ is compact and  non-singular,
returns  a description of  the topology of $(\R^2, \{ g=0\})$ as a list of  
{\bf plane-oval} =$[points,parent,children ]$, a record where:
\noindent
 
\begin{enumerate}
\item{}$points(\omega)$   is a non-empty list of points on the oval represented by  
$\omega$
\item{}  $parent(\omega)$  is the oval $\omega'$
such that $\omega' \supsetneq \omega$ and $\omega'$ is contained in all the ovals properly containing $\omega$  or ${\bf null}$ if no oval contains properly $\omega$
\item{}  $children(\omega)$ is the list of  the set of ovals having $\omega$ as parent.
\end{enumerate}

\noindent
and a function {\bf findOval}$(g,Q) $ which finds a  point on the innermost oval of $V(g)$ containing  $Q$ in its interior or {\bf null}.


Regarding the set $\mathcal E$, it is a subset of  $S \cap Eq(Q,\eps)$ containing 
exactly  one point for each oval of $C(Q,\eps)$ intersecting the equator. It can be 
computed applying the function 
{\bf connComp} to the semialgebraic set $\{f=0, \|X-Q\|^2 = \epsilon^2\}$ and to the set 
$\mathcal Pts = S \cap Eq(Q,\eps)$ and then 
choosing a single element in each set of the partition.  . 


We describe now the function {\bf ccStrip}, which identifies the connected components 
of $T$  in a strip $\R^2 \times [a,b]$.  In it and in the following algorithms,  
${\bf e}_i \in \N^m$ denotes the unit vector of length $m=\#Crit(S)$ with 
$1$ in the $i$-th position.

\medskip 
\noindent
{\bf Algorithm} {\bf ccStrip}$(f,{\mathcal A}, {\mathcal B}, i, Q, a, b)$

where
\begin{enumerate}
\item[$\bullet$]  $f \in \Q[x,y,z]$ 
\item[$\bullet$]  ${\mathcal A}$ is a set of  points on $S_{a}$ and  ${\mathcal B}$ 
is a set of points on $S_{b}$ (at least one for each oval)
\item[$\bullet$]  $i \in \{1, ...,m\}$
\item[$\bullet$]  $Q=(\alpha,\beta,\gamma) = Crit(S)(i)$  is the $i$-th  element of $Crit(S)$ 
\item[$\bullet$]   $a,b \in \Q$ such that $a<\gamma<b$
\end{enumerate}

{\bf Output}: a list of {\bf ccComp}=$[points, \chi, rel]$,  each element 
representing a connected component  of $T_{[a,b]}$. 


\medskip
\noindent {\bf Begin}
\begin{enumerate}
\item[]--  compute   the Milnor radius at $Q$
\item[]$\eps$ := {\bf milnorRadius}($f, Q, a, b$)
\item[] -- analysis of the Milnor sphere; find the points on $Eq(Q, \eps)$ 
\item[] $EqPts := {\bf V}(f, (x-\alpha)^2 + (y-\beta)^2 -\eps ^2, z- \gamma)$

\item [] if $\# EqPts > 0$ then
\begin{enumerate}
\item [] --  partition points on the equator into subsets of points 
\item [] --  belonging to the same oval
\item[] $\{Eq_1,..,Eq_h\} := {\bf connComp} (\{f=0, \|X-Q\|^2 = \epsilon^2\}, EqPts)$
\item[] $\mathcal {E} :=\{{\bf first}(Eq_1),...,{\bf first}(Eq_h)\}$
\item[] $numArcs := [\#Eq_i /2$ for $i$ in $1 .. h$]
\end{enumerate}
\item[] else $\mathcal {E} :=\emptyset$
\item[] -- partition the points of $\mathcal A \cup \mathcal B \cup \mathcal E$ 
into subsets of points
\item[] -- belonging to the same connected component of $T_{[a,b]}$
 \item[] $\mathcal Z$:= {\bf connComp}$(\{f=0, \|X-Q\|^2 \ge \epsilon^2, a \le z\le b\},\mathcal A \cup \mathcal B \cup \mathcal E)$ 
\item[]  -- attach to any element of $\mathcal Z$ its Euler characteristic and its $rel$ value 
\item [] $ccSt :=$ emptyList 
\item[] for  $\zeta$ in $\mathcal Z$ repeat
\begin{enumerate}
\item [] $[E_1,..,E_s] := \zeta \cap \mathcal E$

\item[] if $s > 0$ then
\begin {enumerate} 
\item[]--  the component is of type $(+-)$ 
\item [] $\chi := \sum_{i=1}^s (1-k_i)$ where $k_i =  numArcs(position(E_i, \mathcal E))$
\item [] $new\_cc := [\zeta  \cap (\mathcal A \cup \mathcal B) , \chi ,s {\bf e}_i]$
\end{enumerate}

\item[] if $s = 0$ then\begin {enumerate}
\item[] -- components without  intersection with the equator
\item[] $(na,nb):= (\#(\zeta \cap \mathcal A), \#(\zeta \cap \mathcal B))$
\item [] --  component of type Cylinder  
\item[] if $(na,nb) = (1,1)$   then $new\_cc := [\zeta , 0,0{\bf e}_i ]$ 
\item[] -- component of type $(-)$
\item[] if $(na,nb)= (1,0)$  then $new\_cc := [\zeta , 1,{\bf e}_i]$ 
\item[] -- component of type $(+)$
\item[] if $(na,nb) = (0,1)$ then $new\_cc := [\zeta , 1 ,{\bf e}_i ]$ 
\end {enumerate}
\item[] $ccSt := {\bf insert}(new\_cc, ccSt)$
\end{enumerate}
\item[] return $ccSt$
\end{enumerate}
{\bf End}

\medskip

After computing the connected components  of $T_{[a,b]}$ we need to determine their attachment relations with the connected components of $T_{\le a}$ in order to ``lift" all the information to level $z=b$.  As we already remarked, different components of $T_{[a,b]}$ 
intersecting  the plane $\{z=a\}$ may belong to the same connected component 
of $T_{\le b}$. The  identification of  these components and the computation of  
the corresponding values of $\chi$ and $rel$ is done in the function {\bf lift}, applying 
{\bf mergeComponents}. 

At first we limit our attention to the components $cc_{st}$ of $T_{[a,b]}$ that 
intersect the plane $\{z=a\}$, postponing the analysis of the components of 
type $(+)$. 

If $cc_{st}$ intersects $\{z=a\}$ in precisely one oval 
belonging to a component  $cc_a$ of $T_{\le a}$, 
then  $cc_{st}$ is connected to  $cc_a$ and  we add  the 
points of $cc_{st}$ at level $z=b$ (maybe none, if $cc_{st}$ is of type $(-)$) 
to the list of points identifying $cc_a$  and we accordingly update 
the fields $\chi$ and $rel$.

If  $cc_{st}$ intersects $\{z=a\}$ in several ovals, possibly contained in  different 
components of 
$T_{\le a}$, (i.e.  $cc_{st}$ is of type $(+-)$),  we add the points of $cc_{st}$ to all the 
components of $T_{\le a}$ connected to $cc_{st}$ but we only modify  the fields 
$\chi$ and $rel$ of one of 
them. This corresponds to attaching the component in the strip to only one of the 
components of $T_{\le a}$  it meets at level $z=a$.

In this way  all the edges  of the original tree $G(T_{\le a})$  
intersecting components of $T_{[a,b]}$ of type Cylinder or of type  $(+-)$  
contain  points both on $\{z=a\}$ and on $\{z=b\}$ and 
the edges of $G(T_{\le a})$ that 
are contained in a single connected component of 
$G(T_{\le b})$ can be detected being precisely the edges sharing a common point  
on $\{z=b\}$. The merging of these components into a unique edge  of 
$G(T_{\le b})$  is done by  {\bf mergeComponents}.

Before returning the final tree $G(T_{\le b })$, {\bf lift} analyzes the 
components of $T_{[a,b]}$ that do not intersect  the plane $\{z=a\}$ 
(i.e. the components of type $(+)$). In this case  new 
elements need to be inserted in the tree; the information on the ovals of 
$S_{b}$ and their mutual positions are used to determine the $parent$ 
and the $children$ of these new edges. This identification is done 
processing the inclusion relations of the ovals of $S_{b}$ starting from the outermost  
oval of each nest.

\medskip
\noindent
{\bf Algorithm  lift}$(f, a, b, \mathcal G, ccS, curveG_b)$

where
\begin{enumerate} 
\item[$\bullet$]   $f \in \Q[x,y,z]$ 
\item[$\bullet$]   $a,b \in \Q$, with $a<b$ 
\item[$\bullet$]  $\mathcal G$  is the list of {\bf compRegion} of $G(T_{\leq a})$
\item[$\bullet$]  $ccS$ is the list of ${\bf ccComp}$ of $T_{[a,b]}$
\item[$\bullet$]  $curveG_b$ is the (rooted) tree of the plane curve $\{f(x,y,b)=0\}$
\end {enumerate}

{\bf Output}:  the list of {\bf compRegion} of  $G(T_{\leq b})$


\medskip
\noindent {\bf Begin}

\begin{enumerate}

\item[]  $ccPlus :=\emptyset$

\item[] -- iterative step

\item[] for $cc_{st}$ in $ccS$ repeat

\begin {enumerate}

\item[] -- select the components of $T_{\leq a}$ which intersect $cc_{st}$

\item[] $cc_a := [g  \;\; in \;\; {\mathcal G} \;|\;  points({\bf ccComp}(g)) \cap points(cc_{st})  \neq \emptyset]$



\item[] if $(\# cc_a) > 0$ then 

\begin{enumerate}
\item[] -- $cc_{st}$ is not of type $(+)$, temporarily we collect all the points, 
\item[] -- update  $\chi$ and the number of intersections 
\item [] --  with the Milnor sphere
\item[] for $cc$ in $cc_a$ repeat

\begin {enumerate}
\item [] $points({\bf ccComp}(cc)) := points({\bf ccComp}(cc)) \cup points(cc_{st})$
\end{enumerate}

\item[] $cc_1:= {\bf ccComp}( {\bf first}(cc_a))$

\item[] $\chi(cc_1) :=\chi(cc_1)+  \chi(cc_{st})$

\item[] $rel(cc_1) :=rel(cc_1) + rel(cc_{st})$

\end{enumerate}
\item[] else 
\begin{enumerate}
\item[] -- collect the components of type $(+)$ (to be processed later)
\item[] $ccPlus$ :={\bf insert}$(cc_{st},ccPlus)$
\end{enumerate}

\end{enumerate}

\item[] -- identify the components of $T_{[a,b]}$ which are contained in the same  
\item[] -- connected component of $T_{\leq b}$
\item[] $\mathcal G := {\bf mergeComponents}(\mathcal G)$
\item[] -- consider the  components of $T_{[a,b]}$ of type $(+)$
\item[] -- the entries of  $curveG_b$ are sorted with respect to containment 
\item[] -- partial order
\item[] for $ov \; in  \; curveG_b$  repeat

\begin {enumerate}
\item[] for $cc_ 1$ in $ccPlus$ repeat
\begin{enumerate}
\item[] if  $points(ov)  \cap  points(cc_1) \neq \emptyset$ then  
\item[] -- insert the new component in the tree

\item[] if $parent(ov) = {\bf null}$ then $new\_parent := {\bf root}(\mathcal G)$

\item[]  else $new\_parent:= $
\item[] \hskip .5 cm $ {\bf find}(cc \in \mathcal G \;|\; points({\bf ccComp}(cc)) \ni points(parent(ov)))$

\item[] $new\_cc := cc_1$
\item[] $children({\bf position}(new\_parent)) := \\
{}\hspace{.5 cm}{\bf insert}(new\_cc, children({\bf position}(new\_parent)))$

\item[] $\mathcal G := {\bf insert}(new\_cc,\mathcal G$) 

\end{enumerate}
\end{enumerate}

\item[] return $\mathcal G$
\end{enumerate}

\noindent{\bf End}

\medskip

In the previous function we called  {\bf mergeComponents} to
complete the construction of the connected  components of $T_{\le b}$ in the case when
components of $T_{[a,b]}$ of type $(+-)$ attach to multiple components of $T_{\le a}$. As already remarked, the components
to be identified correspond to edges sharing a common point 
on $\{z=b\}$. We perform a sequence of foldings in the adjacency graph
looking for pairs of edges having a common point 
and a common vertex. We identify them and update accordingly all the data
on the new merged edge. This procedure is then repeated on the 
updated graph. According to Proposition \ref{reduction-to-folds}, eventually any pair
of edges that must be identified will have a common point and a common 
vertex, so that the algorithm will be able to identify them.

\medskip
\noindent
{\bf Algorithm  mergeComponents}($\mathcal G$)

where  $\mathcal G$ is a list of {\bf compRegion}. 
\smallskip

{\bf Output}: updated $\mathcal G$

\medskip 
\noindent
{\bf  Begin}

\begin {enumerate}
\item[] -- search for edges with a common vertex and with points in common

\item[] for $(cr_1,cr_2)$ in  ${\mathcal G} \times {\mathcal G}$ $|$ $cr_1 \neq cr_2$  repeat
\begin {enumerate}

\item[] $c_1 := {\bf ccComp}(cr_1)$
\item[] $c_2 := {\bf ccComp}(cr_2)$

\item[] if $points(c_1) \cap points(c_2) \neq \emptyset$ then
\begin {enumerate}

\item[] $r_1 := {\bf position}(cr_1)$
\item[] $r_2 := {\bf position}(cr_2)$


\item[] if $cr_1 = parent(r_2)$  then 

\begin {enumerate}
\item[]--  the first edge is the parent of the other one
\item[] $points(c_1):= points(c_1) \cup points(c_2)$

\item[] $\chi(c_1):=\chi(c_1) + \chi(c_2)$

\item[] $rel(c_1):=rel(c_1) + rel(c_2)$

\item[] $children(r_1):=   {\bf remove}(cr_2,children(r_1))$

\item[] $reg\_par := {\bf position}(parent(r_1))$

\item[] $children(reg\_par) := children(r_2) \cup children(reg\_par)$
\item[] $isolPts(reg\_par) := isolPts(r_2) \cup isolPts(reg\_par)$

\item[] return  {\bf mergeComponents} ($ {\bf remove}(cr_2,{\mathcal G})$)

\end{enumerate}
\item[]

\item[]  if $parent(r_1) = parent(r_2)$  then
\begin{enumerate}
\item[]--  the edges have a common parent
\item[] $points(c_1):= points(c_1) \cup points(c_2)$

\item[] $\chi(c_1):=\chi(c_1) + \chi(c_2)$

\item[] $Rel(c_1):=Rel(c_1) + Rel(c_2)$

\item[] $children(r_1) := children(r_1) \cup children(r_2)$

\item[] $isolPts(r_1):=isolPts(r_1) \cup isolPts(r_2)$

\item[] $reg\_par := {\bf position}(parent(r_1))$

\item[]$children(reg\_par) := {\bf remove}(cr_2,children(reg\_par))$
\item[] return {\bf mergeComponents} ($ {\bf remove}(cr_2,{\mathcal G})$)
\end{enumerate}
\end{enumerate}
\end{enumerate}
\item[] return $\mathcal G$
\end{enumerate} 
 

\noindent{\bf End}

\medskip

The last operation to describe is how to deal with the isolated singular points. 
This is done with the function {\bf isolatedPt} which is used when the critical point 
$Q =(\alpha,\beta,\gamma)$ is an
isolated point. In this case, as seen in Section \ref{Morse-theory}, essentially 
we only need to find the region to which $Q$ belongs, 
which is determined by the position of the oval 
containing $Q$ in the graph of the plane section $S_{\gamma}$, if there exists one.

\medskip
\noindent
{\bf Algorithm isolatedPt}($f ,Q, b,\mathcal B, \mathcal G$) 
where
\begin{enumerate}
\item[$\bullet$]   $f \in \Q[x,y,z]$ 
\item[$\bullet$]   $Q=(\alpha,\beta,\gamma)$ is an isolated singular point 
\item[$\bullet$]  ${\mathcal B}$ is a list of  points on $S \cap \{z=b\}$ (one for each oval)
\item[$\bullet$]    $\mathcal G$ is a list of {\bf compRegion}. 
\end{enumerate}

{\bf Output:} the element of $\mathcal G$ identifying the region containing $Q$ 

\medskip 
\noindent
{\bf  Begin}
\begin {enumerate}

\item[] --find  the innermost oval of $S_{\gamma}$ containing the
 isolated point $Q$ or {\bf null}
\item[] $ov_{iso}:=$ {\bf findOval}$( f(x,y,\gamma), Q)$

\item[] --  $Q$ belongs to the unlimited region

\item[] if $ov_{iso} = {\bf null}$ then return  ${\bf root}(\mathcal G)$ 

\item[] else

\begin{enumerate}
\item[] $iso\_pts:=\mathcal B \cup  ov_{iso}$ 
\item[] $Z$ := {\bf connComp}$(\{f=0, \gamma  \le z \le b\}, iso\_pts$)

\item[] $\zeta_1$ := [ $\zeta \in Z \; | \; ov_{iso} \in \zeta]$
\item[] return  $[ cc  \;\; in \;\; {\mathcal G} \;|\;  points({\bf ccComp}(cc)) \cap \zeta_1 \neq \emptyset]$
\end{enumerate}
\end{enumerate}

\noindent{\bf End}
\medskip


We can now describe the complete algorithm:

\medskip
\noindent {\bf Algorithm affineSurface}$(f)$
\begin{enumerate}
\item[] where $f \in \Q[x,y,z]$  is such that $S=V(f)$ satisfies the hypotheses required in 
this section
\item[] {\bf Output}:  A list of {\bf compRegion} describing the tree $G(T)$ 
\end{enumerate}


\medskip
\noindent {\bf Begin}

\begin{enumerate}
\item []--  compute the critical points in $S$ and sort them 
by ascending $z$-coordinates 
\item [] $Crit$:= {\bf criticalPoints}($f$)
\item []--  compute a list $la =[a_1,..,a_{m+1}]$ of rationals separating the 
$z$-coordinates 
\item []--  of the critical points
\item[]  $la$:= ${\bf separate_z}(Crit)$
\item [] --  initialize the adjacency graph of $T$
\item[] $rootG :=  [{\bf null},[\emptyset, {\bf null},\emptyset]]$ 

\item[] -- $\mathcal G$ is a list of compRegion  representing at each step $T_{\le a_i}$ 

\item[] ${\mathcal G} := [rootG]$
\item [] --  iterative step 

\item[] for $i$ in $1..\#Crit$ repeat

\begin{enumerate} 
\item[] -- consider the $i$-th critical point $Q$
\item[] $Q := Crit(i)$

\item[] --  study the curve   $S_ {a_{i+1}}$  
\item[] $curveG$ := {\bf planeGraph}$(f(x,y,a_{i+1}))$
\item[] -- extract the points on $T_{\le {a_i}} \cap \{z=a_i\}$ from  $\mathcal G$ , 
\item[] -- there is at least one point for each connected component of   $\mathcal G$
\item[] ${\mathcal  A} := \cup_{cc  \in {\mathcal G}}\{ points({\bf ccComp}(cc)) \cap \{z=a_{i}\} \}$
\item[] -- extract the points from  $curveG$,  there is one point for each oval
\item[] ${\mathcal  B} := \{points(ov)$ for ov in $ curveG\}$
\item[] -- analyze the connected components of $T_{[a_{i}, a_{i+1}]}$ 
\item[] $ccSt := {\text {\bf ccStrip}}(f, \mathcal A, \mathcal B, i, Q, a_{i}, a_{i+1})$ 

\item[] --  lift the information to level $a_{i+1}$ 

\item[] $\mathcal G :=  {\bf lift}(f, a_{i}, a_{i+1} , Q,  {\mathcal  G}, ccSt, curveG)$

\item[] if $rel({\bf ccComp}(cc)).i=0$ forall $cc \in \mathcal G$ then 
\begin{enumerate}
\item[] -- none of the components intersects the Milnor ball, 
\item[] -- $Q$ is an isolated singular point
\item[] $isolCC$ := {\bf isolatedPt}$(f, Q, a_{i+1},\mathcal B,\mathcal G)$
\item[] $isolPts ({\bf position}(isolCC)) := \\
{}\hspace{3cm}{\bf insert}(Q, isolPts({\bf position}(isolCC)))$
\end{enumerate}

\end{enumerate}

\item[] return $\mathcal G$ 
\end{enumerate}

\noindent
{\bf End}

\medskip

\begin{ex}\label{tang_pants}{\rm Let $f=f_1\, f_2$ where
\begin{align*} f_1(x,y,z)&=z^4+ (2x^2+2y^2 -26)z^2 + x^4 + 
(2y^2+10)x^2 +y^4-26y^2+25  \\
f_2(x,y,z)&=16z^4 +8z^2(y^2+4x^2-10) +16x^4 + 8(y^2+8)x^2 +y^4 -20y^2 +64. 
\end{align*}

The equation $f(x,y,z)=0$ defines a surface $S$ which is the union of two tori, 
one inside the other and tangent 
at the points $(0,0,-1)$ and $(0,0,1)$. In particular we have that 
$Sing \, S =\{ (0,0,-1),(0,0,1)\}$, while the critical values w.r.t. the projection $p(x,y,z)=z$ 
are $-5, -2, -1, 1, 2, 5$.
 

\begin{figure}[htbp]
  \centering
  \includegraphics[width=.45\hsize]{tangent_pants.eps} 
  \caption{\footnotesize \sl Components of the surface of Example \ref{tang_pants}
  in the strip containing the singular point $(0,0,1)$.}
  \label{fig:tangent_pants}
\end{figure}

The overall algorithm chooses as separating rationals the values 
$a_1=-6, a_2= -3.5, a_3= -1.5, a_4=0, a_5= 1.5, a_6=3.5, a_7=6$ which correspond to the boundaries of the six strips to be studied.

Figure \ref{fig:tangent_pants} corresponds to the components in the fourth strip 
(i.e. $i=4$ in {\bf ccStrip}) between $z=0$ and $z=1.5$. 

The output of the third step of the algorithm marks the four ovals in 
the curve $S_0$ with $8$ points $A_1,A_2,A_3,A_4,A_5,A_6,A_7,A_8$ 
as represented in Figure \ref{fig:tangent_pants}, while the function {\bf planeGraph} 
marks the two ovals of $S_{1,5}$ with the points $B_1,B_2,B_3,B_4$.

The function {\bf ccStrip}  determines that there are 8 points on the equator of
the Milnor sphere which are contained in two nested ovals, 4 points per oval.
Thus $\mathcal E = [E_1, E_2]$ and $numArcs = [2,2]$.


The result of the second call to {\bf connComp} is
$$\mathcal Z =[\zeta_1, \zeta_2]= [[A_1,A_4,A_5,A_8,E_1,B_1,B_4],[A_2,A_3,A_6,A_7,E_2,B_2,B_3]]$$
The two components $\zeta_1, \zeta_2 $ in the strip are both of type $(+-)$ and 
for each we have
that $\chi = 1-2=-1$ and $s=\#(\zeta_1 \cap \mathcal E)
=\#(\zeta_2 \cap \mathcal E)=1$. Since  $i=4$, we have $s {\bf e}_i = [0,0,0,1,0,0]$. 
Thus the two components returned by {\bf ccStrip} are:
$$[[A_1,A_4,A_5,A_8,B_1,B_4], -1, [0,0,0,1,0,0]]$$
$$[[A_2,A_3,A_6,A_7,B_2,B_3], -1, [0,0,0,1,0,0]]$$


Here we show the information returned by the function {\bf lift} for the adjacency graph associated to the various strips
\begin{enumerate}
\item []$T_{\le -6}$: $\chi = []$, $rel = []$, $G = \bullet$
\item []$T_{\le -3.5}$: $\chi = [1]$, $rel = [[1,0,0,0,0,0]]$, $G$ = $\bullet\!\!$  --- $\!\!\bullet$ 
\item []$T_{\le -1.5}$: $\chi = [1,1]$, $rel = [[1,0,0,0,0,0],[0,1,0,0,0,0]]$, $G$ = $\bullet\!\!$ --- $\!\!\bullet\!\!$ --- $\!\!\bullet$
\item []$T_{\le 0}$: $\chi = [0,0]$, $rel = [[1,0,1,0,0,0],[0,1,1,0,0,0]]$, $G$ = $\bullet\!\!$ --- $\!\!\bullet\!\!$ --- $\!\!\bullet$ 
\item []$T_{\le 1.5}$: $\chi = [-1,-1]$, $rel = [[1,0,1,1,0,0],[0,1,1,1,0,0]]$, $G$ = $\bullet\!\!$ --- $\!\!\bullet\!\!$ --- $\!\!\bullet$
\item []$T_{\le 3.5}$: $\chi = [-1,0]$, $rel = [[1,0,1,1,0,0],[0,1,1,1,1,0]]$, $G$ = $\bullet\!\!$ --- $\!\!\bullet\!\!$ --- $\!\!\bullet$
\item []$T_{\le 6}$: $\chi = [0,0]$, $rel = [[1,0,1,1,0,1],[0,1,1,1,1,0]]$, $G$ = $\bullet\!\!$ --- $\!\!\bullet\!\!$ --- $\!\!\bullet$
\end{enumerate}
}\end{ex}


\section{The general case and examples}\label{generalcase}


If the real algebraic surface $S=\{F(x,y,z,t)=0\}$  does not intersect in 
real points the plane $\{t=0\}$ of $\pro$, then it is contained in the affine 
chart $\{t\ne 0\}$ where it is described by the affine equation 
$f(x,y,z)= F(x,y,z,1)=0$; thus we can compute $D(S)$ by using the 
Algorithm {\bf affineSurface}  described in Section \ref{affinecase}. 

If $S$ intersects the plane $\{t=0\}$, in this section we show that we can 
achieve the same goal by constructing an affine real
algebraic surface $\widehat S \subset \R^3$, computing $D(\widehat S)$
by means of the Algorithm {\bf affineSurface} and then recovering $D(S)$ from
$D(\widehat S)$. 


The strategy of using $\widehat S$ was already used in \cite{FGL} and \cite{FGLP}
respectively to compute the topological type and the labelled
adjacency graph in the case of a non-singular algebraic surface. Here
we will show that the previous construction can be helpful even when $S$ has
isolated singularities and can be used to compute also the matrix $Rel (T)$ and 
the list $IsReg (T)$ containing the needed information about the
singularities of $S$. At first we
briefly recall the essential features of the construction of
$\widehat S$ and some of its properties that can be found in detail in
the two papers mentioned above; then we focus our attention on how 
to use $D(\widehat S)$ to recover  $D(S)$.

Denote by ${\bf S^3}$ the unit sphere in $\R^4$ centered 
at the origin and by $\pi : {\bf S^3} \to \pro$ the map that associates to any point
$(x,y,z,t)$ of ${\bf S^3}$ the point having  homogeneous
coordinates $[x,y,z,t]$ in $\pro$; then each fiber contains two
antipodal points on the sphere and ${\bf S^3}$ turns out to be a $2$-sheeted
covering space of $\pro$. If $S$ is defined by the homogeneous
equation $F(x,y,z,t)=0$ and we lift $S$ through $\pi$,
the surface
$$\widetilde S = \pi ^{-1} (S)= \{(x,y,z,t)\in \R^4 \ |\
F(x,y,z,t)=0\} \cap {\bf S^3}$$ is invariant with respect to the antipodal
map $ap : {\bf S^3} \to {\bf S^3}$ defined by $ap(v)=-v$. If $(0,0,0,1) \not\in
\widetilde S$ (which we can assume up to an affine translation of $S$)
and if $\varphi: {\bf S^3} \setminus \{(0,0,0,1)\} \to \R^3$ denotes the
stereographic projection given by $\varphi (x,y,z,t)= ( \frac{x}{1-t},
\frac{y}{1-t},\frac{z}{1-t})$, then the image $\widehat S=\varphi
(\widetilde S)$ is a compact algebraic surface in $\R^3$, homeomorphic
to $\widetilde S$ and defined implicitly by the polynomial equation
$F(2X, \|X\|^2 -1)=0$ where $X=(x,y,z)$ and $\|X\|^2=x^2+y^2+z^2$. 
 Furthermore, if $inv= \varphi \circ ap \circ
\varphi^{-1} : \R^3 \setminus \{0\} \to \R^3 \setminus \{0\}$ denotes the
involution  $inv(X)= -\frac{X}{\|X\|^2}$ corresponding to
$ap$ via the stereographic projection, then $\widehat S$ is invariant
with respect to $inv$.

In \cite{FGL} and \cite{FGLP} it was shown that, when $S$ is
non-singular, the ability to recognize the action of $inv$ on the set
of the connected components of $\widehat S$ and on the set of the
regions of $\R^3 \setminus \widehat S$,  together with the
topology of $\widehat S$ and the adjacency graph $G(\widehat S)$, is
sufficient to compute $\chi(S)$ and the labelled adjacency graph of
$S$. 

In the case we are examining, when $S$ has at most isolated
singularities, in order to compute $D(S)$ we make use of the
topological surface $T$ obtained from $S$ applying the construction 
described in Proposition \ref{constrT} inside the Milnor balls at the
singularities of $S$ that are not isolated points. Also the
topological surface $\varphi(\pi^{-1}(T))$ is invariant with respect
to $inv$; more precisely $inv$ induces an involution on the set
${\mathcal F}$ of the connected components of $\varphi(\pi^{-1}(T))$
and on the set ${\mathcal R}$ of the regions of $\R^3 \setminus
\varphi(\pi^{-1}(T))$.

Hence we can split
${\mathcal F}$ as the union  ${\mathcal F}_1 \cup {\mathcal F}_2$, where  
$${\mathcal F}_1 = \{\widehat Y \in {\mathcal F}\ |\ inv(\widehat Y)
=\widehat Y \} \quad \mbox{ and } \quad {\mathcal F}_2 = {\mathcal F}
\setminus {\mathcal F}_1
$$
and split 
${\mathcal R}$ as the union  ${\mathcal R}_1 \cup {\mathcal R}_2$, where  
$${\mathcal R}_1 = \{\widehat \Sigma \in {\mathcal R} \ |\
inv(\widehat \Sigma) =\widehat \Sigma \} \quad \mbox{ and } \quad
{\mathcal R}_2 = {\mathcal R} \setminus {\mathcal R}_1.$$ 

Our procedure to derive the needed data relative to
$T$ (and hence to $S$) from the data on
$\varphi(\pi^{-1}(T))$ is based on the following
characterization:
\begin{propo}\label{up-down} Let $Y$ be a connected component 
of $T$ and $\Sigma$ a region of $\pro \setminus T$. Then 
\begin{enumerate}
\item $\varphi(\pi^{-1}(Y))$ is either a connected component of
  $\varphi(\pi^{-1}(T))$ (so that it belongs to ${\mathcal F}_1$) or it
  is the union of two distinct  connected components $\widehat Y_1$ and 
  $\widehat Y_2$ of
  $\varphi(\pi^{-1}(T))$ transformed each into the other by $inv$. Moreover 
  in the first case $\chi (\varphi(\pi^{-1}(Y)))= 2 \, \chi (Y)$, 
  in the second case  $\chi (\widehat Y_1)=\chi (\widehat Y_2) =\chi (Y)$. 
\item $Y$ is non-contractible if and only if $\varphi(\pi^{-1}(Y))\in
  {\mathcal F}_1$, 
\item $\Sigma$ is non-contractible if and only if
  $\varphi(\pi^{-1}(\Sigma))\in {\mathcal R}_1$.
\end{enumerate}
\end{propo}

The previous results were proved in the mentioned papers \cite{FGL} and
\cite{FGLP} for the components and regions of a non-singular algebraic
surface; since the proof uses only the fact that $({\bf S^3}, \pi,
\pro)$ is a double covering, it holds also for singular surfaces and
even for topological 2-manifolds contained in $\pro$.

Since $S$ has only isolated singularities, also the  singularities of
$\widehat S$ are isolated, so that we can compute $D(\widehat S)$ by
means of the  Algorithm {\bf affineSurface}; we obtain these data from the
study of the topological 2-manifold $\widehat T$ associated to 
$\widehat S$ by modifying this latter surface 
inside Milnor balls centered at its  singularities which are not isolated points, 
according to Proposition \ref{constrT} . 

Note that, by Durfee's result recalled in Section \ref{definitions},  
$\widehat T$ is homeomorphic to 
$\varphi(\pi^{-1}(T))$ and also the pairs $(\R^3,\widehat T)$  and 
 $(\R^3,\varphi(\pi^{-1}(T)))$ are homeomorphic, so that $G(\varphi(\pi^{-1}(T)))
 =G(\widehat T)$. 
The fact that the pairs $(\R^3,\widehat T)$ and
 $(\R^3,\varphi(\pi^{-1}(T)))$ are homeomorphic is very important
 because it allows us to recover the data in $D(S)$ by
 means of a procedure based on the properties of
 $\varphi(\pi^{-1}(T))$ with respect to $inv$, but using the data
 relative to the surface $\widehat T$ computed by the 
 Algorithm {\bf affineSurface}.   
 
The previous considerations allow us to attribute to $\widehat T$ the properties 
of pairing among components of  $\varphi(\pi^{-1}(T))$. For instance, by an 
abuse of terminology we will say that two components of $\widehat T$ are 
paired through $inv$ if the corresponding components of $\varphi(\pi^{-1}(T))$ 
are exchanged by $inv$; accordingly we will denote such a pair of 
components by $\widehat Y$  and $inv (\widehat Y)$. 
 \smallskip
 
Let us now see how we can identify the components in $\mathcal F_1$ 
and recognize the pairing among the components of 
$\mathcal F_2$. We obtain a procedure to do that by combining 
the previous observations with the method to recognize the connected 
components used in Section \ref{affinecase}, where any 
component was identified by means of a list of points lying on 
the planes $\{z=a_i\}$ that it meets. We can slightly modify that 
procedure with the aim of using it also to recognize the pairing in 
$\mathcal F$.

Let $Q_1, \ldots, Q_m$ be the critical points of the projection $p$ on 
$\widehat S$.  Up to a translation of the surface $S$, we can assume that $0$ 
is not a critical value for $p$, so that we can choose $0$ as one of the  
regular levels $a_1, \ldots, a_{m+1}$ used by the Algorithm {\bf affineSurface}. 
We can also assume that the $a_i$s are chosen 
 in such a way that $a_i$ is different from the 
$z$-coordinate of $inv (Q_j)$ for each $i=1, \ldots, m+1$ and for each 
$j=1, \ldots, m$. 

Since 
$inv(X)= -\frac{X}{\|X\|^2}$, the plane $\{z=0\}$ is invariant through $inv$ and 
each point with a negative $z$-coordinate is transformed by $inv$ 
into  a point with a positive $z$-coordinate and viceversa. 


Consider first the strips $\{a_{i-1} \leq z \leq a_i\}$ with $a_{i}\leq 0$. Whenever 
a new component $\widehat Y$ of $\widehat T$ appears in the strip, 
a point in $\widehat Y \cap 
\{z=a_i\}$ is chosen to identify the component; assume to mark such a point 
and denote by $\{V_1, \ldots, V_g\}$ the set of points so found. 
By the assumption made above,
for any $V_i$ the point $inv (V_i)$ cannot be a critical point for $p$. 

Consider now the strips $\{a_{i-1} \leq z \leq a_i\}$ with $a_{i}> 0$. For each such strip 
consider the set $\mathcal M _i$ of all the points $V_j$ such that  
$inv (V_j)\in \{a_{i-1} \leq z \leq a_i\}$ and choose 
a Milnor radius $\eps$ at the unique critical point $Q$ contained in the strip such that 
$D(Q, \eps)$ does not contain any of the  $inv (V_j)$s. 
Enlarge the set of points used by the algorithm in the strip to identify the components 
of $\widehat T \cap \{a_{i-1} \leq z\leq a_i\}$ 
by adding to it the points $inv (V_j)$ for $V_j \in \mathcal M _i$.  When the algorithm 
stops, any component of $\widehat T$ is identified by a list of points containing at 
least one point in $\{V_1, \ldots, V_g, inv (V_1), \ldots, inv (V_g)\}$. Comparing the lists 
of points identifying the different connected components we can detect the pairing: if 
two points $V_j, inv (V_j)$ lie on the same connected component, this component  
belongs to $\mathcal F_1$; if they belong to different 
 components, these two components are exchanged by $inv$ and so they 
   belong to $\mathcal F_2$. 
\smallskip

The following result shows how the knowledge of the action of $inv$ on the set 
$\mathcal F$ and on the set of the singular points of $\widehat S$ 
is sufficient to derive  from $D(\widehat S)$ the data $\chi(T), Rel(T)$ and 
$IsReg (T)$. 

Each row of the matrix $Rel (\widehat T)$ 
computed by the Algorithm {\bf affineSurface} contains the data concerning 
the behavior 
of a connected component $\widehat Y$ of $\widehat T$ near all the critical 
points for $p$ on $\widehat S$.



If $N_1, \ldots, N_t$ are the singular points of $S$ 
which are not isolated 
points, then   $\widehat S$ has $2t$ singular 
non-isolated points that we can order  as 
$\widehat N_1, \ldots, \widehat N_t$, $inv (\widehat N_1), \ldots, inv (\widehat N_t)$. 
We can extract from each row of $Rel (\widehat T)$ the information 
concerning only the singular non-isolated 
points of $\widehat S$, so that each row is a vector of length $2t$. 
In the following proposition for any connected component $\widehat Y$ 
of $\widehat T$ we denote by $rel (\widehat Y)$ the vector of 
length $2t$ so extracted regarding the behavior of $\widehat Y$ 
only at the singular  non-isolated points.


\begin{propo}\label{labels} Let $Y$ be  a connected component of $T$ and let 
$\Sigma$ be a region of $\pro \setminus T$. For any singular non-isolated point 
$N$ of $S$, let  $\varphi(\pi^{-1}(N))= \{\widehat N, inv(\widehat N)\}$. 
\begin{enumerate}
\item If $\varphi(\pi^{-1}(Y))= \widehat Y$, with $\widehat Y$ connected, then 
$$\chi(Y)= \frac {\chi(\widehat Y)}2 \quad \mbox{  and } \quad 
rel(Y)(N)= rel(\widehat Y)(\widehat N)= rel(\widehat Y)(inv (\widehat N)).$$
\item If $\varphi(\pi^{-1}(Y)$ is the union of the two distinct connected components 
$\widehat Y$ and $inv ( \widehat Y) $ of $\varphi(\pi^{-1}(T))$, then 
$\chi(Y)= \chi(\widehat Y)= \chi(inv (\widehat Y))$; moreover 
\begin{enumerate}
\item $rel(Y)(N) = rel (\widehat Y)(\widehat N) + rel (inv (\widehat Y))(\widehat N)$
\item $rel(Y)(N) = rel (\widehat Y)(\widehat N) + rel (\widehat Y)(inv (\widehat N))$.
\end{enumerate}
\item If  $\Sigma$ contains $n$ isolated points of $\widehat S$, then 
$\varphi(\pi^{-1}(\Sigma))$ contains $2n$ points if  it is connected.
\item If  $\Sigma$ contains $n$ isolated points of $\widehat S$ and 
$\varphi(\pi^{-1}(\Sigma))$ is the union of the two distinct regions $\Omega, \Omega '$ of 
$\R^3 \setminus \varphi(\pi^{-1}(T))$, then both  $\Omega$ and $\Omega '$ contain 
$n$ isolated points.
\end{enumerate}
\end{propo}
\begin{proof} The results concerning $\chi(Y)$ follow from Proposition \ref{up-down} (1).

(3) and (4) are easy consequences of the pairing induced by $inv$.

As for $rel$, it suffices to observe that 
by construction $Y$ intersects a Milnor sphere at $N$ in 
as many ovals as  $\widehat Y \cup inv(\widehat Y)$ 
intersects a Milnor sphere at $\widehat N$. Moreover, 
because of the pairing induced by $inv$, 
$\widehat Y$  intersects a Milnor sphere at $\widehat N$ in 
as many ovals as $inv ( \widehat Y)$ 
intersects a Milnor sphere at $inv (\widehat N)$, i.e. 
$rel(\widehat Y)(\widehat N)= rel(inv(\widehat Y))(inv (\widehat N)).$
\end{proof}

We can now see that the knowledge of the pairing among the connected 
components and also among the 
singular points of $\widehat S$ induced by $inv$ is sufficient 
to compute all the data in 
$D(S)$. It is convenient 
to distinguish two cases according to the 
parity of the degree $d$ of $S$.

\underline{Case 1: $d$ is even}.

By Section \ref{definitions} $G_{nc}(T)$ is non-empty and connected;
moreover each connected component of $\ol{G_c(T)}$ is a tree containing exactly 
one vertex $v$ lying in $G_{nc}(T)$.

The pairing among components in $\mathcal F$ can be seen as 
an equivalence relation in the set of the
connected components of $\varphi(\pi^{-1}(T))$  and hence on the 
edges of $G(\varphi(\pi^{-1}(T)))=G(\widehat T)$, which determines the edges 
of $G(T)$ as equivalent classes either of the form
$ [\widehat Y]$ or of the form $ [\widehat Y, inv (\widehat Y)]$.
In other words the edges of $G(T)$ can be obtained by means of the 
corresponding identification of pairs of edges in $G(\widehat T)$. 
Since $G(T)$ is a tree, as seen in Section \ref{Morse-theory} $G(T)$ 
can be obtained by means of a finite sequence of folds between pairs 
of edges sharing a common vertex.


We can also think of $\chi(Y)$ and $rel(Y)$ as labels attached to each edge 
$Y$ of $G(T)$ and of $isReg(\Sigma)$ as a label attached to each vertex 
$\Sigma$ of $G(T)$. 


The result of Proposition \ref{labels} shows how it is possible to 
compute the labelled graph $G(T)$, and hence $D(S)$, 
from the labelled graph $G(\widehat T)$  by means of the edge identification process  recalled above, proceeding as follows:
 
\begin{enumerate}
\item[(a)] We perform on $G(\widehat T)$ the folds that realize the identification 
process induced by $inv$; in particular, 
whenever we identify two paired edges   $e, e'$
\begin{enumerate}
\item[-] we attach to the new edge $[e,e']$ as $\chi$-label the value  
$\chi(e) + \chi(e')$ and as $rel$-label the vector 
$rel(e) + rel (e')$; 
\item[-] if $v, v'$ are the two distinct vertices identified by the fold, 
we attach to the new vertex $[v,v']$ as $isReg$-label the value  
$isReg(v) + isReg(v')$ and mark it $c$.
\end{enumerate}
\item[(b)] At the end of step (a), we divide by $2$ the $\chi$-label of each edge, 
we divide by $2$ the $isReg$-label of each vertex and we 
remove from all the $rel$-vectors  the duplicates 
associated to paired singular points.
 \item[(c)] We mark $nc$ all the vertices not marked $c$ so far.
 \item[(d)] We choose the roots according to the process described in Subsection 
\ref{weights-roots}.
 \end{enumerate}
 
 The labelled graph so obtained and endowed with the chosen roots is isomorphic 
 to the rooted labelled graph $G(T)$.

\underline{Case 2: $d$ is odd}.

 
 In this case  (see Section \ref{definitions}) $T$
contains a non-contractible component $\Gamma$, not
represented in $G(T)$ since it does not disconnect $\pro$, 
while the component $\varphi(\pi^{-1}(\Gamma)$), lying in
${\mathcal F}_1$, is represented in $G(\widehat T)$ as an edge
$e_0$.  Since in this case all the other components of $T$ are
contractible, ${\mathcal F}_1$ contains only one element, and each
component of $T$ different from $\Gamma$ has a preimage in $\R^3$ formed
by two components lying in ${\mathcal F}_2$ and exchanged by
$inv$. Moreover the preimage of each region in $\pro \setminus T$
consists of two connected regions of $\R^3 \setminus \widehat T$
exchanged by $inv$ (in other words ${\mathcal R}_1 = \emptyset$); in
particular, if $\Sigma _0$ is the region in $\pro \setminus \Gamma$
external to all the two-sided components of $T$, then the two regions
in $\varphi(\pi^{-1}(\Sigma _0))$ correspond to the vertices $v_1, v_2$ of
$e_0$.

Thus the graph $G(\widehat T)$, apart from the ``special''
edge $e_0$, detectable as the unique element in ${\mathcal F}_1$,
having no counterpart in $G(T)$, is such that $G(\widehat T)
\setminus \{e_0\}$ consists of two connected and isomorphic
subgraphs $\widehat {G_1}$ and $\widehat {G_2}$. 

As a consequence, we might compute $D(S)$ proceeding as in the 
even-degree case after collapsing to a vertex  the edge $e_0$. However, 
taking advantage of the theoretical result that the two 
subgraphs $\widehat {G_1}$ and $\widehat {G_2}$ are isomorphic 
(and each of them is isomorphic to $G(T)$), we can compute  the labelled graph 
$G(T)$, and hence $D(S)$, as follows:

\begin{enumerate}
\item[(a)] We  detect in $G(\widehat T)$ the edge $e_0$ as the unique
  element in ${\mathcal F}_1$.
\item[(b)] We choose one of the two connected components of $G(\widehat T)
\setminus \{e_0\}$, say for instance $\widehat {G_1}$.
 \item[(c)] We leave unchanged the $\chi$-labels on all edges and the label 
 $isReg(v)$ on all vertices. 
\item[(d)] We modify  the $rel$-label of each  edge of $\widehat {G_1}$, 
according to Proposition \ref{labels} (2)(b), producing a vector of lenght $t$ 
obtained by summing the entries of position $i$ and $i+t$ for all $i=1, \ldots, t$.
 \item[(e)] We mark $c$ all the vertices. 
\item[(f)] If $v_1$ denotes  the vertex of $e_0$ contained in $\widehat {G_1}$, 
we choose $v_1$ as a root.
\item[(g)] Since the edge $e_0$ does not appear in $\widehat {G_1}$, 
apart we insert among the data as $\chi(e_0)$ half of the $\chi$-value relative to $e_0$ 
that appeared in $D(\widehat S)$; moreover we compute the datum $rel(e_0)$ 
using  Proposition \ref{labels} (1).
 \end{enumerate}

 The labelled graph so obtained and endowed with the chosen roots is isomorphic 
 to the rooted labelled graph $G(T)$.

\smallskip
 

\begin{ex}\label{cono} {\rm Consider the surface represented in
    the left-hand side of Figure \ref{fig:ex-cono} consisting of a cone and
    two isolated points.

\begin{figure}[htbp]
  \centering
  \includegraphics[width=.5\hsize]{ex-cono.eps} 
  \caption{\footnotesize \sl An even degree non-affine surface.}
  \label{fig:ex-cono}
\end{figure}

The right-hand side represents the doubled surface $\widehat S$ and
the adjacency graph $G(\widehat T)$, where $\widehat T$ is the union
of two spheres. Note that the symbols used in the figure, according to
the notation used in this section, indicate the components and regions
of $T$ and $\widehat T$, even if these surfaces are not
represented in the figure.

Using the method described above we get the following
data concerning $\widehat T$:\\
Regions: $inv(\widehat \Sigma_1)= \widehat \Sigma_1$ and  
$inv(\widehat \Sigma_2)= \widehat \Sigma_3$; hence, labelling by means
of the index $i$ each region $\widehat\Sigma_i$, we have  ${\mathcal R}_1 =
\{1\}$ and ${\mathcal R}_2 =\{2,3 \}$.\\
Components: $inv(\widehat Y_1)=\widehat
Y_2$; hence, again labeling by means
of the index $i$ each connected component $\widehat Y_i$, we get
  ${\mathcal F}_1 = \emptyset$ and ${\mathcal F}_2 = \{1,2\}$. 
Moreover $\chi( \widehat T)=[2,2]$\\
Singularities: $rel( \widehat Y_1)=[1,1], \ rel(\widehat Y_2)=[1,1], \ 
isReg(\widehat \Sigma_1)=2, 
isReg(\widehat \Sigma_2)=1, isReg(\widehat \Sigma_3)=1$.
 

The procedure described in this section yields $\chi(T)=[2]$, \ $rel(Y_1)=[2]$, \
$isReg (\Sigma_1)=isReg (\Sigma_2)=1$, so we recognize that $T$ is a sphere 
and that $S$ is the
union of two isolated points and the space obtained collapsings two
points in the sphere $T$. The labelled 2-adjacency graph of $S$ is
represented in Figure \ref{fig:ex-cono} below $S$.
}
\end{ex}

\begin{ex}\label{ex-cono_piano} {\rm The surface $S$ represented in
    the left-hand side of Figure \ref{fig:ex-cono_piano} contains a cone and
    a plane passing through the vertex of the cone, thus there is only
    one singular point in $S$ which is not an isolated point.

\begin{figure}[htbp]
  \centering
  \includegraphics[width=.6\hsize]{ex-cono_piano.eps} 
  \caption{\footnotesize \sl An odd degree non-affine surface.}
  \label{fig:ex-cono_piano}
\end{figure}

Proceeding as in the previous example, using the notations appearing
in the figure and the same way of labeling, we compute:\\
Regions: $inv(\widehat \Sigma_3)=\widehat \Sigma_4$ and 
$inv(\widehat \Sigma_1)=\widehat \Sigma_2$; hence ${\mathcal R}_1 =\emptyset$
and ${\mathcal R}_2 =\{1,2,3,4 \}$.\\
Components: $inv(\widehat Y_1)=\widehat Y_1, inv(\widehat Y_2)=\widehat Y_3$; 
hence ${\mathcal F}_1 = \{1\}$ and 
${\mathcal F}_2 = \{2,3\}$. Moreover  $\chi( \widehat T)=[2,2,2]$.\\
Singularities: $rel( \widehat Y_1)=rel(\widehat Y_2)=rel(\widehat Y_3)=[1,1],$\ 
$isReg(\widehat \Sigma_i)=0 \mbox{ for } i=1,2,3,4$. 

Using the procedure described in this section we get $\chi(T)=[1,2]$, 
 \ $rel(Y_1)=[1]$, \ $rel(Y_2)=[2]$, \ $isReg (\Sigma_1)=isReg (\Sigma_2)=0$, i.e. 
$T$ is the disjoint union of a projective plane and a
sphere and $S$ is obtained from it collapsing a
point in the plane with two points in the sphere. 
}
\end{ex}


\begin{ex}\label{ex-torostriz_piano} {\rm Also the surface in
    Figure \ref{fig:ex-torostriz_piano} contains a projective plane and
    only one singular point. 

\begin{figure}[htbp]
  \centering
  \includegraphics[width=.6\hsize]{ex-torostriz_piano.eps} 
  \caption{\footnotesize \sl Another odd degree non-affine
  surface.}
  \label{fig:ex-torostriz_piano}
\end{figure}

The usual procedure yields:\\
Regions: 
$inv(\widehat \Sigma_3)=\widehat \Sigma_4$ and 
$inv(\widehat \Sigma_1)=\widehat \Sigma_2$; hence ${\mathcal R}_1 =\emptyset$
and ${\mathcal R}_2 =\{1,2,3,4 \}$.\\
Components: $inv(\widehat Y_1)=\widehat Y_1, inv(\widehat Y_2)=\widehat Y_3$; 
hence ${\mathcal F}_1 = \{1\}$ and 
${\mathcal F}_2 = \{2,3\}$. Moreover  $\chi( \widehat T)=[2,2,2]$.\\
Singularities: $rel( \widehat Y_1)=[1,1], \ rel(\widehat Y_2)=[2,0], \ rel(\widehat Y_3)=[0,2],$\  $isReg(\widehat \Sigma_i)=0 \mbox{ for } i=1,2,3,4$. 

Using the procedure described in this section  we get $\chi(T)=[1,2]$, 
\ $rel(Y_1)=[1]$, \ $rel(Y_2)=[2]$, \ $isReg (\Sigma_1)=isReg (\Sigma_2)=0$,  
i.e. $T$ is the disjoint union of a projective plane and a
sphere and $S$ is obtained from it collapsing a
point in the plane with two points in the sphere. 
}
\end{ex}



\begin{Rem} Observe that the output $D(S)$ obtained in Example 
\ref{ex-torostriz_piano} coincides with the one of the
surface $S$ of Example \ref{ex-cono_piano}, in spite of the fact that
the two surfaces cannot be mapped each into the other by means of a
homeomorphism of $\pro$. However also in this case there is an
invariant that distinguishes the two surfaces: the lists $\widehat
l_1, \widehat l_2$. This shows  that the method of ``doubling'' $S$
into $\widehat S$ is not only  a useful technical device to compute
$D(S)$ but also provides new additional invariants by homeomorphism. 
\end{Rem}



\section{Computational remarks and complexity of the algorithm}\label{tests}

\subsection{Good frame test and genericity}  

In Section \ref{Morse-theory} we assumed that $(x,y,z)$ is a 
\emph{good frame} for the surface $S$, i.e. that  
the fiber over any critical value of the projection 
$p$ contains only one critical point in $S$. When $S$ is non-singular, 
it is well-known that we can assume to be in this situation,  
up to a generic linear change of coordinates. A proof of this result can be 
found for instance in \cite{Roy-book}. We show here how the same result 
can be obtained also in our singular situation by modifying the proof of 
\cite{Roy-book}  taking into account the presence of the isolated 
singularities.

\smallskip

Denote by ${\bf S^2}$ the unit sphere in $\R^3$ centered 
at the origin. 
For any $v\in {\bf S^2}$ let $\phi_v\colon S \to \R$ be the map defined by 
$\phi_v (X)= \langle v,X\rangle$ for any $X\in S$, where 
$\langle \cdot , \cdot \rangle$ denotes the 
standard scalar product in $\R^3$. If $v=(0,0,1)$, then 
$\phi_v (x,y,z)=z= p(x,y,z)$.  A point $X\in S$ is critical for  $\phi_v$ if and only if 
the gradient $\nabla f (X)$ is parallel to $v$.

Let $G \colon S \setminus Sing\; S \to {\bf S^2}$ denote the Gauss map defined 
by $G(X)= \frac{\nabla f(X)}{\|\nabla f(X)\|}$ and recall the following 
classical result (for a proof see for instance \cite{Roy-book} ):

\begin{lemma}\label{Gauss} Let $X_0\in S \setminus Sing\; S$ be a critical point for 
$\phi_v$. Then $X_0$ is non-degenerate for $\phi_v$ if and only if 
$X_0$ is not a critical point for $G$.
\end{lemma}

\begin{propo} Up to a linear change of coordinates in $\R^3$ we can assume that 
the projection $p(x,y,z)=z$ has finitely many critical points on $S$ and  
that the critical values are distinct. 
\end{propo}
\begin{proof} By Lemma \ref{Gauss} the points $v\in {\bf S^2}$ such that $\phi_v$ 
is not a Morse function on $S \setminus Sing\; S$  are exactly the critical 
values of the Gauss map $G$, which by the semialgebraic version of 
Sard's Lemma (see \cite{Roy-book}, Theorem 5.57) 
form a semialgebraic set of dimension at most $1$  
in ${\bf S^2}$. Hence there exists a dense open subset $A$ in ${\bf S^2}$, invariant 
w.r.t. the antipodal map $ap\colon {\bf S^2} \to {\bf S^2}$, $ap(X)=-X$, such that 
$\phi_v| _{S \setminus Sing\; S}$ is a Morse function for all $v\in A$.

Let $Z=\{(X,v)\in \R^3 \times {\bf S^2}\ |\ X\in S, \nabla f(X) 
\mbox{ is parallel to } v\}$.
The set $Z$ is a closed algebraic set of dimension $2$ such that, for 
each $X\in S \setminus Sing\; S$, \ $Z\cap (\{X\} \times {\bf S^2})$ consists exactly 
of the two points $(X,\frac{\nabla f(X)}{\|\nabla f(X)\|})$ and 
$(X, - \frac{\nabla f(X)}{\|\nabla f(X)\|})$. 

Denote by $W$ the topological closure of $\{(X,v)\in Z\  |\ X\notin Sing\; S\}$, 
which is a semialgebraic set. If $Sing\; S=\{P_1, \ldots, P_t\}$, for each 
$i=1, \ldots, t$ denote by $\Gamma_i$ the subset of ${\bf S^2}$ such that 
$W\cap (\{P_i\} \times {\bf S^2})= \{P_i\} \times\Gamma_i$. Since 
$\{P_i\} \times\Gamma_i$ is contained in the singular locus of $Z$, then 
$\dim \Gamma_i \leq 1$ for each $i$. Hence $\Gamma =\bigcup
_{i=1}^t \Gamma_i$ is a semialgebraic curve in ${\bf S^2}$.

Observe that, if $L$ is a compact subset of ${\bf S^2}$ such that $L\cap 
\Gamma_i =\emptyset$, then there exists an open neighborhood 
$U_i$ of $P_i$ in $S$ such that $v$ is not parallel to $\nabla f(X)$ 
for all $v\in L$ and all $X\in U_i \setminus \{P_i\}$.  

\begin{comment}

Namely, assume by contradiction that for all $n\in \N$ there exist 
$v_n\in L$ and  $X_n\in B(P_i,\frac 1n)$ such that $\nabla f(X_n)$ is 
parallel to $v_n$. Up to considering subsequences, we can assume that 
$X_n$ converges to $P_i$ and that $v_n$ converges to some point 
$v_0\in L$. Then $v_0$ corresponds to a limiting tangent plane at $P_i$, i.e. 
$v_0 \in L\cap \Gamma_i$. Contradiction, since $L\cap 
\Gamma_i =\emptyset$.

\end{comment}

Up to a generic linear change of coordinates we can assume that the points 
$(0,0, \pm 1)$ belong to $A\setminus \Gamma$.

Since $(0,0, \pm 1)\in A$, the projection $p(x,y,z)=z$ has only finitely 
many critical points in  $S \setminus Sing\; S$, which are non-degenerate, 
say $Q_1, \ldots, Q_m$. Up to a generic linear change of coordinates in the 
$(x,y)$-plane, we can also assume that all the critical points for $p$, i.e. 
$Q_1, \ldots, Q_m,P_1, \ldots, P_t$, either have distinct $z$-coordinates or 
have distinct $y$-coordinates.

By Lemma \ref{Gauss} for each $j=1, \ldots, m$ there exists an 
open neighborhood $V_j$ of $Q_j$ such that 
$G | _{V_j} \colon V_j \to G(V_j)$ 
is a diffeomorphism. Let $V= \bigcup_{j=1}^m V_j$; let also $B=\bigcap 
_{j=1}^m (G(V_j) \cup ap(G(V_j))$, which
is an open subset of ${\bf S^2}$ containing  $(0,0, 1)$ and $(0,0, -1)$. 
Since $(0,0, \pm 1)\notin \Gamma$, up 
to shrinking the $V_j$s we can assume that $\ol B \cap \Gamma =\emptyset$.

As a consequence of the observation made above, for any $i=1, \ldots, t$  there 
exists an open neighborhood $U_i$ of the singular point $P_i$ such that 
$v$ is not parallel to $\nabla f(X)$  for all $v\in \ol B$ and all 
$X\in U_i\setminus \{P_i\}$. Let 
$U= \bigcup_{i=1}^t U_i$. 


Since $S\setminus U$ is compact, $G|_{S\setminus U}$ is a proper map. Moreover, since 
$V$ is an  open neighborhood of $(G|_{S\setminus U})^{-1} (0,0, \pm 1)$, then 
there exists an  open neighborhood $B' \subseteq B$ of $(0,0, \pm 1)$ such 
that $(G|_{S\setminus U})^{-1} (B')\subseteq V$. In other words 
for any $v'\in B'$ the points $X\in S\setminus U$ where $\nabla f(X)$ is 
parallel to $v'$ lie in $V$. Moreover, since $B' \subseteq B$,  $\nabla f(X)$ is 
not parallel to $v'$ for all $X\in U\setminus Sing\; S$. 
Hence the only non-singular points $X\in S$ where 
 $\nabla f(X)$ is parallel to $v'$ lie in $V$; in particular $\phi_{v'}$ has as many 
 critical points as $\phi_{(0,0,1)}=p$ had. 
\medskip

For $\eps \in \R$ consider the linear change of coordinates 
$\Phi \colon \R^3_{(X,Y,Z)} \to \R^3_{(x,y,z)}$ given by 
$$\Phi(X,Y,Z)= (X, Y+\eps Z, Z- \eps Y).$$ 
With respect to the coordinates $(X,Y,Z)$ the surface $S$ is defined by 
$V(g)$ where $g= f \circ \Phi$. A point $Q\in S$ is critical for 
$p'(X,Y,Z)=Z$ if and only if $g_X(Q)=g_Y(Q)=0$, which happens 
if and only if 
$\nabla f( \Phi(Q))$ is parallel to $v_\eps = \frac{(0, \eps, 1)}{\|(0, \eps, 1)\|} \in 
{\bf S^2}$. In this case the critical value w.r.t. $p'$ is $Z=\frac{z+\eps y}{1+\eps ^2}$.

Whenever  $v_\eps$ lies in $B'$, $v_\eps$ is a normal direction to $S$ in exactly 
one point in each of the neighborhoods $V_j$. In particular the total number of 
critical points for $p'$ coincides with  the total number of 
critical points for $p$. If $(x_i, y_i, z_i)$ are the critical points for $p$, the critical 
values for $p'$ are therefore the values  $\frac{z_i +\eps y_i}{1+\eps ^2}$ which 
are distinct for $\eps$ small enough.
\end{proof}


\subsection{Computation of a Milnor radius}

Let $Q=(\alpha,\beta,\gamma)$ be a  point of $S$; we want to compute a 
Milnor radius for $S$ at $Q$, i.e   a positive real number $r_0$ such that, for 
all positive $\eps \leq r_0$, \ $S \cap D(Q,\eps)$ does not contain any 
 critical point for the polynomial 
function $\rho(x,y,z)=(x-\alpha)^2 +(y-\beta)^2+(z-\gamma)^2$ except $Q$ itself.

In order to compute an $r_0$ with the previous property, we can start by 
computing  a $\delta \in \R^+$ such that 
$B(Q,\delta) \setminus \{Q\}$ does not contain any non-singular critical  
point for $\rho$.

A point $P=(x,y,z)\in S$ is critical for $\rho$ if the  rank of the matrix
$M=\left(
\begin{array}{ccc}
f_x          &  f_y      &  f_z  \\
x-\alpha     &  y-\beta  &  z-\gamma  
\end{array} \right)$ 
is less than  or equal to 1. 

Let $L=(f, M_1, M_2, M_3)$ denote the ideal of  $\C[x,y,z]$ generated by $f$ 
and by  the determinants $M_1, M_2, M_3$
of the three square submatrices of order 2 of $M$. Then 
$Sing\,S_\C \subseteq V(L)$. More precisely, if we denote by 
$\Gamma$ the set of the non-singular points of $S_\C$ which 
are critical for $\rho$, then $V(L)= \Gamma \cup Sing\,S_\C$.

Recall that in our situation the set $Sing\, S_\C$ is not necessarily finite and 
hence $\rho(Sing\, S_\C)$ might coincide with $\C$. However 
(see \cite{Milnor} Corollary 2.8) the restriction 
of $\rho$ to $S_\C \setminus Sing\, S_\C$ 
can have at most finitely many critical values, 
i.e. $\rho(\Gamma )$ is finite in $\C$; in particular it coincides
with its Zariski closure $\ol{\rho(\Gamma)}^Z$.  Then
$\rho(\Gamma) \subseteq \rho(\ol{\Gamma}^Z) \subseteq
\ol{\rho(\Gamma)}^Z =\rho(\Gamma)$ and hence
$\rho(\Gamma)=\rho(\ol{\Gamma}^Z)$.

Since $\ol{\Gamma}^Z= \ol{V(L) \setminus Sing\, S_\C}^Z$, 
the algebraic set $\ol{\Gamma}^Z$ is the zero-set of the
ideal $(L \colon J^{\infty})$, where $J=(f, f_{x}, f_{y},f_{z})\subseteq \C[x,y,z]$ is the 
ideal defining the singular locus $Sing\,S_\C$. Thus there exists a non-zero 
polynomial $P(t)\in \C[t]$ whose roots are the critical values of  
$\rho$ on $S_\C \setminus Sing\, S_\C$; we can compute such a $P(t)$ by 
standard methods, for instance using Gr\"obner bases or via linear 
algebra, searching for a relation of linear dependence among the powers 
of $\rho$ modulo $(L \colon J^{\infty})$. 


If we take  $\delta ^2=\min \{ \theta\in \R \ |\ P(\theta)=0, \theta > 0\}$, 
the punctured real ball $B(Q,\delta)\setminus \{Q\}$ does not contain any
real critical point of $S$ for $\rho$, but it can still possibly contain some
point in $Sing\,S$. We can easily avoid this: since in our
hypothesis the real singular locus $ Sing\,S$ is finite, it
is sufficient to compute $\eta= \min \{ \|Q -P_i\|\ |\ P_i \in
Sing\,S, \ P_i \ne Q\}$and then to take  $r_0<\min
(\delta,\eta)$.

If we require that $r_0$ is also a Milnor radius at $Q$ for the plane curve 
$S_{\gamma}\subseteq \{z=\gamma\}$ defined by  $g(x,y)=f(x,y,\gamma)=0$, 
we need to compute a $\sigma \in \R^+$ such that the punctured plane disk 
$B((\alpha, \beta), \sigma) \setminus \{(\alpha, \beta)\}$ does not contain 
any critical  point for $\rho|_{S_{\gamma}}$. In this case the computation is simpler, 
because the critical points for $\rho|_{S_{\gamma}}$ are the points on $V(g)$ where 
$\det \begin{pmatrix}
g_x          &  g_y      \\
x-\alpha     &  y-\beta  &  
\end{pmatrix}  =0$. After computing $\sigma$ as above, it suffices to take  $r_0<\min
(\delta,\eta, \sigma)$.


\subsection{Recognizing connectivity relations}


In the description  of the algorithms in Section  \ref{affinecase}  we assumed 
the existence of a general 
function {\bf connComp} which takes as arguments a semialgebraic 
set $SAL$ in $\R^n$ and 
a finite set of points. Its job is to produce a partition of those points into 
maximal subsets which belong to the same connected component of $SAL$.
Our first use of {\bf connComp} in {\bf ccStrip} is to decide which points on the equator 
of a Milnor sphere belong to the same oval. In this case the semi algebraic set 
$SAL$ is a smooth one-dimensional curve in $\R^3$ defined by the equation of our 
surface  $S$ along with the equation of the Milnor sphere. In our other two uses of 
{\bf connComp}, the semialgebraic set is a two-dimensional subset of $S$.
We use Canny's concept (\cite{Canny}) of a ``roadmap'' to reduce the problem of deciding 
connectedness for these two-dimensional sets to the same problem on one-dimensional 
sets. In our case a roadmap for $S$ can be described by the union of a 
silhouette curve defined by $f$ and $f_x$ (which will be one-dimensional if our 
surface is in general position), along with suitable
sections of our surface for constant values of $z$. The essential property of our 
use of a roadmap is that points are pathwise connected on the roadmap if and only 
 if they 
are pathwise connected on $S$.
The next proposition shows that to form a roadmap for our surface, in addition to the 
silhouette curve, it is sufficient to take $z$ sections of $S$ through the 
critical points of the silhouette curve. 


\begin{propo}\label{constr-roadmap}
Assume that the  strip $\R^2 \times [a,b]$ contains only one critical point 
$Q=(\alpha,\beta,\gamma)$ for the projection $p(x,y,z)=z
$ which is not isolated in $S$, with $a< \gamma <b$. 
Let $R= (S_{\gamma} \setminus B(Q, \eps)) \cup C(Q, \eps)$ and 
$\Sigma= V(f, f_x)\cap \{a\leq z\leq b\}$. 
Denote by $c_1  < \ldots < c_k$ the critical values of the restriction of $p$ to 
$\Sigma \cap \{a < z <b\}$, $c_0=a$ and $c_{k+1}=b$. 
Let $\Gamma= \Sigma \cup \bigcup_{i=0}^{k+1} S_{c_i}$. Then:
\begin{enumerate}
\item for any $P\in S_a \cup S_b$ there exists an arc $\alpha_P$ contained in 
$\Gamma \setminus \{Q\}$ 
such that $\alpha_P (0) =P$ and $\alpha_P (1)\in R$,
\item two points $P_1, P_2 \in R$ lie in the same connected 
component of $T_{[a,b]}$ if and only if they lie in the same connected 
component of $R$,
\item two points $P_1, P_2 \in S_a \cup S_b$ lie in the same connected 
component of $T_{[a,b]}$ if and only if the points $\alpha_{P_1} (1)$ and 
$\alpha_{P_2} (1)$ lie in the same connected 
component of $R$.
\end{enumerate} 
\end{propo}
\begin{proof} 
(1) It suffices to consider the case when $P\in S_a$. 

Let $W$ be the connected component of $S_{[a,\gamma]}\setminus \{Q\}$ 
containing $P$. Then, for any $c\in [a, \gamma)$, $W_c$ is a single oval which intersects 
$\Sigma$ in at least two points. In particular $W_a$ is an oval $\sigma$ 
containing $P$.

Since $\gamma$ is a critical value for $p$, there exists $h$ with $1\leq h \leq k$ 
such that $\gamma=c_h$. 
Let $\beta_1$ be a  connected component of $\Sigma \cap W_{(a,c_1)}$.
Since $p_{| \beta_1}$ has no critical point, for any $u_1\in (c_0,c_1)$ 
the arc $\beta_1$ meets $\{z=u_1\}$ in a single point; moreover, since 
$\ol{W_{(c_0,c_1)}}$ is compact, $\ol {\beta_1}$ intersects $\{z=c_0\}$ in a point 
belonging to the oval $\sigma$ and intersects $\{z=c_1\}$ in a point $P_1$. 
Thus there exists an arc joining $P$ with $P_1$ and contained in 
$\sigma \cup \Sigma$.

Iterating  the previous argument in the strips $\R^2 \times (c_1, c_2), \ldots, 
\R^2 \times (c_{h-1}, \gamma)$, eventually we find an arc $\beta \subseteq 
\Gamma \cap \ol W$ 
joining $P$ with a point of $S_{\gamma}$. If the final point $\beta(1)$ does 
not belong to $B(Q, \eps)$, we are done. Otherwise $\beta$ necessarily 
meets $S(Q, \eps)$ in some point: in this case, if $t_0$ is the smallest real 
number in $[0,1]$ such that $\beta(t_0)\in  S(Q, \eps)$, 
it suffices to take as $\alpha_P$ the restriction of $\beta$ to $[0,t_0]$.

(2) The points $P_1, P_2 \in R$ lie in the same connected 
component of $T_{[a,b]}$ if and only if they lie in the same connected 
component of $S_{[a,b]}\setminus B(Q, \eps)$. Then the result follows from 
the fact that $R$ is a deformation retract of $S_{[a,b]}\setminus B(Q, \eps)$ by 
Theorem \ref {teoLazzeri} (5).

(3) The point $P_1 \in S_a \cup S_b$ (resp. $ P_2$) lies in the same connected 
component of $T_{[a,b]}$ as $\alpha_{P_1} (1)$ (resp. 
$\alpha_{P_2} (1)$). Then the result follows from (2).
\end{proof}


\begin{Rem}
For space algebraic curves  it is possible to decide  whether two points of the 
curve belong to the same connected component, using any algorithm 
that computes the connectivity graph of the curve. The curve $R$ found in 
Proposition \ref{constr-roadmap}  is not algebraic but it is a deformation 
retract of $(S_{\gamma} \setminus \{Q\})  \cup C(Q, \eps)$. Thus  
two points $P_1, P_2 \in R$ belong to the same connected component of $R$ 
if and only if they belong to the same connected component of 
$(S_{\gamma} \setminus \{Q\})  \cup C(Q, \eps)$, which can be decided 
by computing the connectivity graph of the 
algebraic curve $S_{\gamma}   \cup C(Q, \eps)$ and by recovering from it 
the connectivity graph of $(S_{\gamma} \setminus \{Q\})  \cup C(Q, \eps)$. 
Observe that $(\Gamma \setminus \{Q\}) \cup C(Q,\eps)$ satisfies 
Canny's roadmap conditions for $S_{[a,b]}\setminus \{Q\}$ and thus for $T_{[a,b]}$. 
\end{Rem}

All of our calls to {\bf connComp} can be 
reduced to the study of connectedness of points lying on curves in $\R^3$.
There are many approaches to solving connectivity on curves 
(for references see for instance \cite{Roy-book}), one in particular is due
to El Kahoui (\cite{El Kahoui}). His original purpose was to study the topology 
of arbitrary space 
curves in $\R^3$. In order to do this he studies the connectivity between 
points in adjacent critical fibers.
This provides us with a general technique for studying connectivity 
between points on a space curve.
We just add the fibers containing these points of interest to the set of 
fibers  being studied by El Kahoui's algorithm. Once we know 
the connectivity between points in adjacent fibers, we can 
compute the transitive closure of the connectivity graph to decide the connectivity of our 
original points. Since his algorithm is based on connectivity between adjacent fibers, 
if we want to restrict to paths not passing though a particular point in a fiber, we can remove 
that point from the connectivity graph before computing the transitive closure. 
This gives us a complete implementation of the {\bf connComp} algorithm 
needed for our purposes. 
The basic construction used in El Kahoui's algorithm is to count the 
number of real points in a 
fiber and to determine the position of the critical point in the fiber. 
We should point out that his algorithm does require some strong general 
position hypotheses in 
order to work correctly. In addition his algorithm for studying connectedness of points on 
space curves can also be used to decide whether our surface has a finite number of real 
critical points, and if so locate them. 
If the critical point ideal is zero-dimensional complex, this is relatively easy; 
when the critical point ideal is one-dimensional complex, then we can use 
El Kahoui's algorithm 
to decide whether the real points in the critical fibers are isolated or not. 
Finally El Kahoui's algorithm can be adapted to computing the topology of plane curves, 
i.e. implementing {\bf planeGraph}. 
The structure of the ovals can be computed from El Kahoui's fiber counts, 
and in the case of non-singular curves adjacency graphs can be obtained.


\subsection{Comments on computational complexity} 

Assuming our compact affine surface is described by a square-free polynomial $f$ of 
degree $d$, by \cite{Coste}
the number of connected components of $S=V(f)$ can be $\mathcal O(d^3)$. 
By our general
position hypothesis there are at most $d(d-1)^2$ critical points, which bounds 
the number of top level iterations of our algorithm (calls to {\bf lift}). 
The silhouette curve $(f, f_x)$ will have degree $d(d-1)$ and 
our choice of El Kahoui's algorithm
for analyzing this space curve requires a generic projection to a plane
curve of the same degree, whose discriminant will have degree $\mathcal O(d^4)$.
His algorithm studies the real points in the fibers above all the real roots
of the discriminant, i.e. $\mathcal O(d^6)$ points. 
Although we could need $\mathcal O(d^3)$ calls to {\bf connComp}, 
the silhouette curve is the same for
all these calls and only the points to test for connectedness change. 
In each strip we only need to test connectedness among $\mathcal O(d^2)$ 
points, since this is a bound on the number
of intersections of the silhouette curve with the planes $z=a_i$ and $z=a_{i+1}$. 
El Kahoui's algorithm
requires us to study the fibers over the projection of each of these points; 
we can have $\mathcal O(d^2)$  points in each fiber and the total number
of points to be studied in each strip is $\mathcal O(d^4)$. So the total number of points
required for all the calls to {\bf connComp} is $\mathcal O(d^6) + 
(d(d-1))^2\mathcal O(d^3)$ = $\mathcal O(d^7)$.
This the is same as required by the algorithm of Alberti, Mourrain and Tecourt 
(\cite{Mourrain}) for the triangulation 
of affine real algebraic surfaces and compares very favorably with their 
$\mathcal O(d^{13})$ 
estimate for the number of points required to compute a Cylindrical Algebraic 
Decomposition of the surface. 

Our algorithm requires some preliminary constructions to find the critical points 
and a Milnor radius at each such point; once this is done, we claim 
that the cost of the calls to {\bf connComp} 
dominates the cost of rest of our entire algorithm. Here we show that the
cost of testing connectedness between components in the strip and the
cost of merging components are both dominated by $\mathcal O(d^7)$ operations,
and thus dominated by the cost of computing the points needed 
by the various calls to {\bf connComp}.
For example  each of our $\mathcal O(d^3)$ calls to 
{\bf mergeComponents} requires collapsing
at most $\mathcal O(d)$ components in each strip, since that is a 
bound on the number of components of
type $(+-)$ in each strip (derived from the $2d$ bound on the number 
of intersections of the surface
and the equator of a Milnor sphere). Assuming we first compute a list of 
the components which need to be
merged, each fold requires $\mathcal O(d)$ comparisons and $\mathcal O(d^3)$ integer
arithmetic operations to combine labels, and we do at most $\mathcal O(d)$ 
folds per strip, for a total cost of $\mathcal O(d^5)$ point comparisons 
and $\mathcal O(d^7)$ integer operations to combine the relevant data. 
Similarly for the problem of matching connected components, 
although the total number of components of our surface
is bounded by $\mathcal O(d^3)$, the $\mathcal O(d^2)$ bound for the number 
of ovals in a section 
shows that at most $\mathcal O(d^2)$ components meet any section. 
Thus we can see that in each strip the cost
of testing which of the $\mathcal O(d^2)$ components in a strip $T_{[a,b]}$ 
connect to one of the $\mathcal O(d^2)$ 
components from $T_{\le a}$ which meet $z=a$ requires at most 
$\mathcal O(d^4)$ comparisons per strip, again
for a overall cost of $\mathcal O(d^7)$. Our preliminary implementation of this algorithm also demonstrates
that nearly all the time is spent in calls to {\bf connComp}; we intend to investigate seminumerical approaches to
improve this performance.


\section*{References}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Bibliografia
%%%%%%%%%%%%%%%%%%%%%%%%
\begin{thebibliography}{00}

\bibitem{Mourrain}
{\sc Alberti, L., Mourrain, B., T{\'e}court, J.-P.}:
Isotopic triangulation of a real algebraic surface.
{\em J. Symbolic Comput.}, vol. 44, n. 9 (2009), pp. 1291--1310


\bibitem{Roy-book} 
{\sc Basu, S., Pollack, R., and Roy, M.-F.}:
Algorithms in Real Algebraic Geometry.  {\em Algorithms and
Computat. in Mathematics, Vol. 10}, Springer-Verlag, 2003

\bibitem{Berberich} 
{\sc Berberich, E., Kerber, M., Sagraloff, M.}:
An efficient algorithm for the stratification and triangulation of an algebraic surface.
{\em Comput. Geom.}, vol. 43, n. 3 (2010),
pp. 257--278 

\bibitem{BCR} 
{\sc Bochnak, J., Coste, M,. Roy, M.-F.}:
Real algebraic geometry. 
{\em Ergebnisse der Mathematik und ihrer Grenzgebiete (3), Vol. 36},
Springer-Verlag,  Berlin, 1998

\bibitem{Canny} 
{\sc Canny, J.}:
The complexity of robot motion planning. 
MIT Press, Cambridge, MA, USA


\bibitem{Cheng}
{\sc  Cheng, J.-S.,  Gao, X.-S.,  Li, M.}:
Determining the topology of real algebraic surfaces. 
In {\em 11 IMA Conference on the Mathematics of Surfaces. R. Martin, H. Bez, M. Sabin (Eds.)}. In: LNCS, vol. 3604, 2005, pp. 121--146.

\bibitem{Collins} 
{\sc Collins, G.}: 
Quantifier elimination for
real closed fields by cylindrical algebraic decomposition.  In:
Proc. of Automata theory and formal languages (Second GI Conf.,
Kaiserslautern, 1975), 
{\em Lecture Notes in Comput. Sci., Vol. 33}, Springer, Berlin,
 1975
  
\bibitem{Coste}
{\sc Coste, M.}:
An Introduction to Semialgebraic Geometry.
Dottorato di Ricerca in Matematica.
Universit\`a di Pisa, Pisa, Italy (2000)

\bibitem{Diatta} 
{\sc Diatta, D. N., Mourrain, B., Ruatta, O.}:
On the isotopic meshing of an algebraic implicit surface.
{\em J. Symbolic Comput.}, vol. 47, n. 8 (2012), pp. 903--925

%\bibitem[DC]{Donald} 
%{\sc Donald, B. R., Chang, D. R.}: 
%On the complexity of computing the homology type of a
%              triangulation.  In:
%32nd {A}nnual {S}ymposium on {F}oundations of {C}omputer 
%{S}cience ({S}an {J}uan, {PR}, 1991), pp. 650--661,
%{\em  IEEE Comput. Soc. Press}, Los Alamitos, CA, 1991

\bibitem{Durfee} 
{\sc Durfee, A.H.}:
Neighborhoods of algebraic sets.
Trans. Amer. Math. Soc., vol. 276, n. 2 (1983), pp. 517-530 

\bibitem{El Kahoui} 
{\sc El Kahoui, M.}:
Topology of real algebraic space curves.
{\em J. Symbolic Comput.}, vol. 43, n. 4 (2008), pp. 235 - 258

\bibitem{FGL} 
{\sc Fortuna, E., Gianni, P. and Luminati, D.}:
Algorithmical determination of the topology of real algebraic surfaces.
{\em J. Symbolic Comput.}, vol. 38, n. 6 (2004), pp. 1551-1567

\bibitem{FGLP} 
{\sc Fortuna, E., Gianni, P., Luminati, D. and Parenti, P.}:
The adjacency graph of a real algebraic surface.
AAECC, vol. 16, n. 5 (2005), pp. 271-292 

\bibitem{FGPT}
{\sc Fortuna, E., Gianni, P., Parenti, P.  and Traverso, C.}:
Algorithms to compute the topology of orientable real algebraic surfaces.
{\em J. Symbolic Comput.}, vol. 36, n. 3-4 (2003), pp. 343-364

\bibitem{GT}
{\sc Gianni, P., Traverso, C.}:
Shape determination for real curves and surfaces.
{\em Ann. Univ. Ferrara Sez. VII}, vol. 29, (1983), pp. 87-109

%\bibitem{GMcP}
%{\sc Goresky, M., MacPherson, R.}:
%Stratified Morse theory.
%{\em Ergebnisse der Mathematik und ihrer Grenzgebiete (3), Vol. 14},
%Springer-Verlag, Berlin, 1988

%\bibitem{Kerber}
%{\sc Kerber, M.}:
%Geometric Algorithms for Algebraic Curves and Surfaces. 
%Ph.D. thesis. Universit\"at des Saarlandes, Germany (2009)

\bibitem{L}
{\sc Lazzeri, F.}:
Morse theory on singular spaces. In: 
Singularit\'es \`a Carg\`ese (Rencontre Singularit\'es G\'eom.
              Anal., Inst. \'Etudes Sci., Carg\`ese, 1972), 
{\em Ast\'erisque}, n. 7-8 (1973), pp. 263--268

%\bibitem[M1]{MilnorMorse}
%{\sc Milnor, J.}:
%Morse theory.
%Princeton University Press, Princeton, N.J., 1963


\bibitem{Milnor}
{\sc Milnor, J.}:
Singular points of complex hypersurfaces.
{\em Annals of Mathematics Studies, No. 61,}
Princeton University Press, Princeton, N.J. 1968


%\bibitem[R]{RoyCurves}
%{\sc Roy, M.-F.}
%Computation of the topology of a real curve.
%Algorithmique, topologie et g\'eom\'etrie alg\'ebriques (Seville,
%  1987 and Toulouse, 1988).
%{\em Ast\'erisque}, n. 192 (1990), pp. 17--33

\bibitem{Viro-mutual}
{\sc Viro, O.}:
Mutual position of hypersurfaces in projective space.
{\em Amer. Math. Soc. Transl. Ser. 2}, vol. 186, (1998), pp. 161--176

\end{thebibliography}





\end{document}

