Computing the Geometric Intersection Number of Curves

Extracted source text · arXiv 1511.09327

\documentclass[a4paper,12pt,english]{article}
\usepackage{amsfonts,amsmath,latexsym,amsthm,fixmath}
\usepackage{amssymb,ifthen}
\usepackage[utf8]{inputenc}   
\usepackage{lmodern}
\usepackage[english]{babel}   
\usepackage[babel]{microtype}
\usepackage{color,graphicx}
\usepackage{url}
\usepackage[
    pdftex,
    pdfstartview={XYZ 0 1000 1.0},
    bookmarks=false,
    colorlinks,
    breaklinks=true,
    citecolor=citecolor,
    filecolor=blue,
    linkcolor=linkcolor,
    urlcolor=urlcolor,
    pdfborder={0 0 0}
]{hyperref}
\definecolor{urlcolor}{rgb}{0, 0.5, 0}
\definecolor{citecolor}{rgb}{.5,0,.25}
\definecolor{linkcolor}{rgb}{0,0,1}

\usepackage{geometry}
\geometry{margin=1in,includefoot}

\graphicspath{{figures/}}
\def\svgpath{figures/}

\newcommand{\includesvg}[2][]{%
\def\tempa{#1}\def\tempb{}%
\ifx\tempa\tempb\else\let\svgwidth\tempa\fi
\input{\svgpath#2.pdf_tex}%
}

%----------------
\newcommand{\col}{\textcolor}
\newcommand{\red}{\textcolor{red}}
\newcommand{\green}{\textcolor{green}}
\newcommand{\blue}{\textcolor{blue}}

\newtheorem{thm}{Theorem}
\newtheorem{df}[thm]{Definition}
\newtheorem{prop}[thm]{Proposition}
\newtheorem{cor}[thm]{Corollary}
\newtheorem{lem}[thm]{Lemma}
\newtheorem{claim}{Claim}
\newtheorem{conj}{Conjecture}
\newtheorem*{nonumberlem}{Lemma}
\newtheorem*{nonumberprop}{Proposition}

\theoremstyle{remark} 
\newtheorem{remark}{Remark} 


%---------------------
% A few shortcuts...
%---------------------
\def\C{\mathcal{C}}
\def\R{\mathbb{R}}
\def\Z{\mathbb{Z}}
\def\D{\mathbb{D}}
\def\dP{\mathbb{P}} %{\rm I\hskip-2pt P}
\def\define#1{\textbf{#1}}
\def\Ipath#1#2{[{#1}\stackrel{#2}{\to}]}
\def\Opath#1#2{({#1}\stackrel{#2}{\to})}
\def\Dpoint#1#2{({#1},{#2})}
\def\Aocc#1#2{[{#1},{#2}]}
\def\Focc#1{[{#1},{#1+1}]}
\def\Bocc#1{[{#1},{#1-1}]}
\def\Imath{i}
\def\Jmath{j}
\def\Ibar{i}
\def\Jbar{j}
\def\Overline{}
\def\ftsz{\footnotesize}
\def\Surf{$\Sigma$}
\def\zipper{unzip}
\newcommand{\inv}{\ensuremath{^{-1}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title

 \title{Computing the Geometric Intersection Number of Curves\thanks{This work was supported by the LabEx PERSYVAL-Lab ANR-11-LABX-0025-01.}}

\author{
Vincent Despr\'e%
    \thanks{GIPSA-Lab, Universit\'e Joseph Fourier, Grenoble, France,
\protect\url{Vincent.Despre@gipsa-lab.fr}}
  \and
Francis Lazarus%
    \thanks{GIPSA-Lab, CNRS, Grenoble, France,
\protect\url{Francis.Lazarus@gipsa-lab.fr}}
}


\begin{document}
\maketitle

\begin{abstract}
The geometric intersection number of a curve on a surface is the minimal number of self-intersections of any homotopic curve, i.e. of any curve obtained by continuous deformation. Likewise, the geometric intersection number of a pair of curves is the minimal number of intersections of any homotopic pair. 
Given two curves represented by closed walks of length at most $\ell$ on a combinatorial surface of complexity $n$ we describe simple algorithms to compute the geometric intersection number of each curve or of the two curves in $O(n+ \ell^2)$ time. We also  propose an algorithm of complexity $O(n+\ell\log^2\ell)$ to decide if the geometric intersection number of a curve is zero, i.e. if the curve is homotopic to a simple curve. 
\end{abstract}

\section{Introduction}
Let $S$ be a surface. Two closed curves $\alpha,\beta: \R/\Z\to S$ are \define{freely homotopic}, written $\alpha\sim \beta$, if there exists a continuous map $h: [0,1]\times\R/\Z$ such that $h(0,t)=\alpha(t)$ and $h(1,t)=\beta(t)$ for all $t\in \R/\Z$. Assuming the curves in generic position, their number of intersections is
\[ |\alpha\cap\beta| = |\{(t,t')\mid t, t' \in \R/\Z \text{ and } \alpha(t) = \beta(t')\}|.
\]
Their \define{geometric intersection number} only depends on their free homotopy classes and is defined as
\[i(\alpha,\beta) = \min_{\alpha'\sim\alpha, \beta'\sim\beta} |\alpha'\cap\beta'| 
\]
Likewise, the number of self-intersections of $\alpha$ is given by
\[\frac{1}{2}|\{(t,t')\mid t\neq t' \in \R/\Z \text{ and } \alpha(t) = \alpha(t')\}|,
\] 
and its minimum over all the curves freely homotopic to $\alpha$ is its \define{geometric self-intersection number} $i(\alpha)$. Note the one half factor that comes from the identification of $(t,t')$ with $(t',t)$.

The geometric intersection number is an important parameter that allows to stratify the set of homotopy classes of curves on a surface. The surface is usually endowed with a hyperbolic metric, implying that each homotopy class is identified by its unique geodesic representative.  Extending a former result by Mirzakhani~\cite{m-gnscg-08}, Sapir~\cite{s-bnnsc-15,m-cmcgo-16} has recently provided tight asymptotics for the number of closed geodesics with bounded length and bounded geometric intersection number. Chas and Lalley~\cite{cl-sicts-12} also proved that the distribution of the geometric intersection number with respect to the length of geodesics approaches the Gaussian distribution as the length grows to infinity. Other more experimental results were obtained with the help of a computer to show the existence of length-equivalent homotopy classes with distinct geometric intersection numbers~\cite{c-sinle-14}.
Hence, for both theoretical and practical reasons various aspects of 
the computation of geometric intersection numbers have been studied in the past including the algorithmic ones. Nonetheless, all the previous approaches rely on rather complex mathematical arguments and to our knowledge no exact complexity analysis has yet appeared.
 In this paper, we make our own the words of Dehn who noted that the metric on words (on some basis of the fundamental group of the surface) can advantageously replace the hyperbolic metric~\cite{l-ttgpd-10}. We propose a combinatorial framework that leads to simple algorithms of  low complexity to compute the geometric intersection number of curves or to test if this number is zero. Our approach is based on the computation of canonical forms as recently introduced in the purpose of testing whether two curves are homotopic~\cite{lr-hts-12,ew-tcsr-13}. Canonical forms are instances of combinatorial geodesics who share nice properties with the geodesics of a hyperbolic surface. On such surfaces each homotopy class contains a unique geodesic that moreover minimizes the number of self-intersections. 
Although a combinatorial geodesic is generally not unique in its homotopy class, it must stay at distance one from its canonical representative and a careful analysis of its structure leads to the first result of the paper.
 \begin{thm}\label{th:main-result}
   Given two curves represented by closed walks of length at most $\ell$ on a combinatorial surface of complexity $n$ we can compute the geometric intersection number of each curve or of the two curves in $O(n+ \ell^2)$ time.
 \end{thm}
As usual the complexity of a combinatorial surface stands for its total number of vertices, edges and faces. A key point in our algorithm is the ability to compute the primitive root of a canonical curve in linear time. We next provide an algorithm to compute an actual curve immersion that minimizes the number of self-interactions in its homotopy class. 
\begin{thm}\label{th:compute-immersion}
  Let $c$ be a closed walk of length $\ell$ in canonical form. We can compute a combinatorial immersion with $i(c)$ crossings in $O(\ell^4)$ time.
\end{thm}
We also propose a nearly optimal algorithm that answers an old problem studied by Poincar\'e~\cite[\S 4]{p-ccal-04}:  decide if the geometric intersection number of a curve is null, that is if the curve is homotopic to a simple curve. 
\begin{thm}\label{th:simple-curve}
  Given a curve represented by a closed walk of length $\ell$ on a combinatorial surface of complexity $n$ we can decide  if the curve is homotopic to a simple curve in  $O(n+ \ell\log^2\ell)$ time. In the affirmative we can construct an embedding of $c$ in the same amount of time.
\end{thm}

In the next section we review some of the previous relevant works. We introduce our combinatorial framework in Section~\ref{sec:framework} and discuss the structure of combinatorial geodesics in Section~\ref{sec:geodesics}. Section~\ref{sec:strategy} presents our general simple strategy to compute the geometric intersection number.
The proof of Theorem~\ref{th:main-result} is given in the next three Sections where the case of non-primitive curves is also treated. The computation of a minimally crossing immersion is presented in Section~\ref{sec:computing-immersions} and~\ref{sec:finding-bigons} with the proof of Theorem~\ref{th:compute-immersion}. We finally propose a simple algorithm to detect and embed curves homotopic to simple curves (Theorem~\ref{th:simple-curve}) in Section~\ref{sec:simple-curves}.

\section{Historical notes}
In the fifth supplement to its \emph{Analysis situs} Poincar\'e~\cite[\S 4]{p-ccal-04} describes a method to decide whether a given closed curve $\gamma$ on a surface can be continuously deformed to a simple curve. For this, he considers the surface as the quotient $\mathbb{D}/\Gamma$ of the Poincar\'e disk $\mathbb{D}$ by a (Fuchsian) group $\Gamma$ of hyperbolic transformations. The endpoints of a lift of $\gamma$ in the Poincar\'e disk are related by a hyperbolic transformation whose axis is a hyperbolic line $L$ representing the unique geodesic homotopic to $\gamma$. He concludes that $\gamma$ is homotopic to a simple curve if and only if all the transforms of $L$ by $\Gamma$ are pairwise disjoint or equal. This method was turned into an algorithm by Reinhart~\cite{r-ajccs-62} who worked out the explicit computations in the Poincar\'e disk using the usual representation of hyperbolic transformations by two-by-two matrices. The entries of the matrices being algebraic the computation could indeed be performed accurately on a computer. The ability to recognize curves that are  \emph{primitive}, i.e. whose homotopy class cannot be expressed as a proper power of another class, happens to be crucial in this algorithm though computationally expensive.

Birman ans Series~\cite{bs-ascs-84} subsequently proposed an algorithm for the case of surfaces with nonempty boundary that avoids manipulating algebraic numbers. While their arguments appeal to a hyperbolic structure, their algorithm is purely combinatorial. Intuitively, a surface with boundary deform retracts onto a fat graph (in fact a fat bouquet of circles) whose universal covering space embeds as a fat tree in the Poincar\'e disk. The successive lifts of a curve $\gamma$ trace a bi-infinite path in this tree. The limit points of this path belongs to the circle $\partial\mathbb{D}$ at infinity and coincide with the ideal endpoints of the axis of the hyperbolic transformation corresponding to any of the lifts of the curve in the path. The question as to whether two lifts give rise to intersecting axes can thus be reduced to test if the corresponding bi-infinite paths have separating limit points on $\partial\mathbb{D}$. As for Reinhart, Birman and Series assume that the homotopy class of $\gamma$ is given by a word $W$ on some given set of generators of the fundamental group of the surface. In turn, the above test on bi-infinite paths boils down to consider the cyclic permutations of $W$ and $W^{-1}$ in a cyclic lexicographic order and to check if this ordering is  well-parenthesized with respect to some pairing of the words.

Cohen and Lustig~\cite{cl-pggin-87} further observed that the approach of Birman and Series could be extended to count the geometric intersection number of one or two curves. 
In a second paper Lustig~\cite{l-pggin2-87} tackles the case where the curves are taken on a surface without boundary. He considers a closed surface with negative Euler characteristic as the quotient of the Poincar\'e disk by a group of transformations isomorphic to the fundamental group of the surface. The main contribution of the paper is to define a canonical representative for every free homotopy class $\alpha$ given as a word in some fixed system of loops generating the fundamental group. Lustig first notes that there is no known way of choosing a unique word for $\alpha$. In particular, the shortest words are far from unique. He rather represents (a lift of) $\alpha$ by a path in the union $G\cup N\cup H$ of three tessellations of $\mathbb{D}$, where the edges of $G$ are all the lifts of the generating loops, $N$ is the dual tessellation, and the edges of $H$ joins the vertices of $G$ with the vertices of $N$. (Although the graphs of $G$, $N$ and $H$ are embedded their union is not as every edge of $G$ crosses its dual edge in $N$.) Lustig gives a purely combinatorial characterization of canonical paths and argues that the method in his first paper~\cite{cl-pggin-87} can be applied to the canonical representative of $\alpha$. Overall the two papers adds up to 60 pages with essential arguments from hyperbolic geometry and the complexity of the whole procedure remains to be done.

Other approaches were developed without assuming any hyperbolic structure.
Based on the notion of winding number, Chillingsworth~\cite{c-sccs-69,c-wns2-72} provides an algorithm to test whether a curve is homotopic (this time with fixed basepoint) to a simple curve on a surface with nonempty boundary. He also proposed an algorithm for determining when a given set of \emph{simple} closed curves can be made disjoint by (free) homotopy~\cite{c-afdsc-71}.  While the winding number relies on a differentiable structure, Zieschang~\cite{z-aekf-65,z-aekf2-69} used the connection between the automorphisms of a topological surface and the automorphisms of its fundamental group in order to detect the homotopy classes of simple closed curves. 

A related work by Hass and Scott~\cite{hs-ics-85} is concerned with curves which have excess intersection, i.e. that can be homotoped so as to reduce their number of intersections. Hass and Scott introduce various types of monogons and bigons that can be either embedded, singular or weak.
A \emph{singular monogon} of a curve $\gamma: \R/\Z\to S$ is a contractible subpath of $\gamma$ whose endpoints define a self-intersection of $\gamma$.
A \emph{bigon} of $\gamma$ is defined by two self-intersections joined by two homotopic subpaths (with fixed endpoints) $\gamma|_\sigma$ and $\gamma|_{\sigma'}$ with $\sigma, \sigma' \subset \R/\Z$. 
The bigon is said \emph{singular} if the defining segments $\sigma,\sigma'$ are disjoint and \emph{weak} otherwise. Hass and Scott prove that a curve with excess self-intersection on an orientable surface must have a singular monogon or a singular bigon. Their result directly suggests an algorithm to compute the geometric intersection number of a curve: iteratively remove monogons or untie bigons until there is no more. The final configuration must have the minimal number of self-intersections. 
Designing an efficient procedure to find monogons and bigons remains the crux of this approach. In any case, the method cannot be extended to compute the geometric intersection number of two curves since Hass and Scott give two counter-examples to the fact that two curves with excess intersections should have a singular monogon or bigon. One of their counter-examples contains a curve with excess self-intersections and the other one contains a non-primitive curve. Our counter-example, Figure~\ref{fig:no-singular-bigon}, shows that even assuming each curve to be primitive and in minimal configuration, we may have excess intersections without singular bigons. 
\begin{figure}[h]
  \centering
\includesvg[.5\linewidth]{no-singular-bigon}
  \caption{The plain circles represent non-contractible curves. The two curves $\gamma$ and $\delta$ on the left have homotopic disjoint curves $\gamma'$ and $\delta'$. They thus have excess intersection although there is no singular bigon between the two. If $A=\gamma(0)=\delta(0)$ and $B=\gamma(u)=\delta(v)$ we nonetheless have $\delta|_{[0,v]}\sim \gamma|_{[0,1+u]}$  where $\gamma|_{[0,1+u]}$ is the concatenation of $\gamma$ with $\gamma|_{[0,u]}$. In particular, $\gamma|_{[0,1+u]}$ wraps more that once around $\gamma$.
}
  \label{fig:no-singular-bigon}
\end{figure}
Nonetheless, it was proved~\cite{hs-scs-94,gs-mcmcr-97,p-cails-02} that starting from any configuration of curves one may reach a configuration with a minimal number of intersections by applying a finite sequence of elementary moves involving monogons, bigons and trigons similar to the Reidemeister moves in knot theory. A surprising consequence was obtained by Neumann-Coto~\cite{n-csgs-01}. Define a \emph{cut and paste} on a family of curves by cutting the curves at some of their intersection points and glueing the resulting arcs in a different order. Neumann-Coto proves that any set of primitive curves can be brought to a homotopic set with minimal (self-)intersections by a set of cut and paste operations. Note that each intersection of two curve pieces can be re-arranged in three different manners, including the original one, by a cut and paste. Hence, if the curves have $I$ (self-)intersections we may find a minimal configuration out of the $3^I$ possible re-configurations! 

We also mention the algebraic approach of Gon\c{c}alves et al.~\cite{gkz-amnip-05} based on previous works by Turaev~\cite{t-iltdm-79} who introduced intersection forms over the integral group ring of the fundamental group of the surface. See~\cite{cl-pggin-87,gkz-amnip-05} for more historical notes.

\section{Background and framework}\label{sec:framework}
We introduce our combinatorial framework and show that it faithfully represents its continuous analogue.

\paragraph{Combinatorial surfaces.}
As usual in computational topology, we model a surface by a cellular embedding of a graph $G$ in a compact topological surface $S$. Such a cellular embedding can be encoded by a \define{combinatorial surface} composed of the graph $G$ itself together with a rotation system~\cite{mt-gs-01} that records for every vertex of the graph the clockwise cyclic order of the incident arcs. The \define{facial walks} are obtained from the rotation system by the face traversal procedure as described in~\cite[p.93]{mt-gs-01}.
In order to handle surfaces with boundaries we allow every face of $G$ in $S$ to be either an open disk or an annulus (open on one side). In other words $G$ is a cellular embedding in the closure of $S$ obtained by attaching a disk to every boundary of $S$. We record this information by storing a boolean for every facial walk of $G$ indicating whether the associated face is perforated or not. All the considered graphs may have loop and multiple edges. A directed edge will be called an \define{arc} and each edge corresponds to two opposite arcs. We denote by $a\inv$ the arc opposite to an arc $a$. For simplicity, \define{we shall only consider orientable surfaces in this paper}. Every combinatorial surface $\Sigma$ can be reduced by first contracting the edges of a spanning tree and then deleting edges incident to distinct faces. The resulting \define{reduced surface} has a single vertex and a single face. The combinatorial surface $\Sigma$ and its reduced version encodes different cellular embeddings on a same topological surface.

\paragraph{Combinatorial curves.}
Consider a combinatorial surface with its graph $G$. A combinatorial curve (or path) $c$ is a  walk in $G$, i.e. an alternating sequence of vertices and arcs, starting and ending with a vertex, such that each vertex in the sequence is the target vertex of the previous arc and the source vertex of the next arc. We generally omit the vertices in the sequence. A combinatorial curve is closed when additionally the first and last vertex are equal. When no confusion is possible we shall drop the adjective combinatorial. The \define{length} of $c$ is its total number of arc occurrences, which we denote by $|c|$. If $c$ is closed, we write $c(\Ibar)$, $\Ibar\in \Z/|c|\Z$, for the vertex of index $\Ibar$ of $c$ and $c\Focc{\Imath}$ for the  arc joining $c(\Ibar)$ to $c(\Ibar+1)$.  For convenience we set $c[\Ibar+1,\Ibar]=c[\Ibar,\Ibar+1]\inv$ to allow the traversal of $c$ in reverse direction. In order to differentiate the arcs with their occurrences we denote by $[\Ibar,\Ibar\pm 1]_c$ the corresponding occurrence of the arc $c[\Ibar,\Ibar\pm 1]$ in $c^{\pm 1}$, where $c\inv$ is obtained by traversing $c^{1}:=c$ in the opposite direction. More generally, for any non-negative integer $\ell$ and any sign $\varepsilon\in\{-1,1\}$,
The sequence of indices 
\[(\Ibar,\Overline{\Imath+\varepsilon},\Overline{\Imath+2\varepsilon},\dots,\Overline{\Imath+\varepsilon\ell})
\]
is called an \define{index path} of $c$ of length $\ell$. The index path can be \define{forward} ($\varepsilon =1$) or \define{backward} ($\varepsilon =-1$) and can be longer than $c$ so that an index may appear more than once in the sequence. We denote this path by $\Ipath{\Imath}{\varepsilon\ell}_{c}$. Its \define{image path} is given by the arc sequence
\[c\Ipath{\Imath}{\varepsilon\ell} := 
(c[\Ibar,\Overline{\Imath+\varepsilon}],c[\Overline{\Imath+\varepsilon},\Overline{\Imath+2\varepsilon}],\dots,c[\Overline{\varepsilon(\ell-1)},\Overline{\varepsilon\ell}]).
\]
The image path of a length zero index path is just a vertex.

A \define{spur} of $c$ is a subsequence of the form $(a,a\inv)$ where $a$ is an arc.
A closed curve is \define{contractible} if it is homotopic to a trivial curve (i.e., a curve reduced to a single vertex). We will  implicitly assume that a  homotopy has fixed endpoints when applied to paths and is free when applied to closed curves. 

\paragraph{Combinatorial immersions.}
Following the notion of a combinatorial set of loops as in~\cite{cl-oslos-05}, we define a \define{combinatorial immersion} of a set $\mathcal{C}$ of combinatorial curves 
as the data for each arc $a$ in $G$  of a left-to-right order $\preceq_a$ over all the occurrences of $a$ or $a\inv$ in the curves of $\mathcal{C}$. The only requirement is that opposite arcs should be associated with inverse orders.
Let $A_v$ be the set of occurrences of $a$ or $a\inv$ in the curves of $\mathcal{C}$ where $a$ runs over all arcs of $G$ with origin $v$. A combinatorial immersion induces for each vertex $v$ of $G$ a circular order $\preceq_{v}$ over $A_v$; if $a_1,\dots,a_k$ is the clockwise-ordered list of arcs of $G$ with origin $v$, then $\preceq_{v}$ is the cyclic concatenation of the orders $\preceq_{a_1},\dots,\preceq_{a_k}$. An immersion of $\mathcal{C}$ restricts to an immersion of each of its curves. Conversely, given an immersion of each curve in $\mathcal{C}$ we can built an immersion (in general many) of their union by merging for each arc $a$ the left-to-right orders $\preceq_a$ of each curve immersion.

\paragraph{Combinatorial crossings.}
Given an immersion $\mathcal I$ of two combinatorial closed curves $c$ and $d$ we define a \define{double point} of $(c,d)$ as a pair of indices $\Dpoint{\Imath}{\Jmath}\in \Z/|c|\Z\times \Z/|d|\Z$ such that $c(\Ibar)=d(\Jbar)$. Likewise, a double point of $c$ is a pair $\Dpoint{\Imath}{\Jmath}\in \Z/|c|\Z\times \Z/|c|\Z$ with $\Ibar\neq \Jbar$ and $c(\Ibar)=d(\Jbar)$ 
The double point $\Dpoint{\Imath}{\Jmath}$ is a \define{crossing} in $\mathcal I$ if the pairs of arc occurrences $([\Ibar-1,\Ibar]_c,[\Ibar,\Ibar+1]_c)$ and $([\Jbar-1,\Jbar]_d,[\Jbar,\Jbar+1]_d)$ are linked in the  $\preceq_{c(\Ibar)}$-order, i.e. if they appear in the cyclic order 
\[\cdots [\Ibar,\Ibar-1]_c\cdots [\Jbar,\Jbar-1]_d\cdots [\Ibar,\Ibar+1]_c\cdots [\Jbar,\Jbar+1]_d\cdots, 
\]
with respect to $\preceq_{c(\Ibar)}$ or the opposite order. An analogous definition holds for a self-crossing of a single curve, taking $c=d$ in the above definition. Note that the notion of (self-)crossing is independent of the traversal directions of $c$ and $d$. The number of crossings of $c$ and $d$ and of self-crossings of $c$ in $\mathcal I$ is denoted respectively by $i_{\mathcal{I}}(c,d)$ and $i_{\mathcal{I}}(c)$.

We define the \define{combinatorial self-crossing number} of $c$, denoted by $i(c)$, as the minimum of $i_{\mathcal{I}}(c')$  over all the combinatorial immersions $\mathcal{I}$ of any combinatorial curve $c'$ freely homotopic to $c$. The \define{combinatorial crossing number} of two combinatorial curves $c$ and $d$ is defined the same way taking into account crossings between $c$ and $d$ only.
\begin{lem}\label{lem:geometric-vs-combinatorial}
  The combinatorial (self-)crossing number coincides with the geometric (self-)intersection number, i.e. for every combinatorial closed curves $c$ and $d$:
\[i(c)=i(\rho(c))\qquad \text{ and } \qquad i(c,d)=i(\rho(c),\rho(d)).
\]
\end{lem}
\begin{proof}
 Every combinatorial immersion $\mathcal{I}$ of $c$ can be realized by a continuous curve $\gamma\sim \rho(c)$ with the same number of self-intersections as the number of self-crossings of $\mathcal{I}$. To construct $\gamma$ we consider small disjoint disks centered at the images of the vertices of $G$ in $S$. We connect those vertex disks by disjoint strips corresponding to the edges of $G$. For every arc $a$ of $G$ we  draw inside the corresponding edge strip parallel curve pieces labelled by the occurrences of $a$ or $a\inv$ in $c$ in the left-to-right order $\preceq_a$. The endpoints of those curve pieces  appear on the boundary of the vertex disks in the circular vertex orders induced by  $\mathcal I$. It remains to connect those endpoints by straight line segments inside each disk (via a parametrization over a Euclidean disk) according to the labels of their incident curve pieces. The resulting curve $\gamma$ can be homotoped to $\rho(c)$ and its self-intersections may only appear inside the vertex disks. Clearly, an intersection of two segments of $\gamma$ in a disk corresponds to linked pairs of arc occurrences, i.e. to a combinatorial crossing, and vice-versa. It follows that $i(c) \geq i(\rho(c))$. To prove the reverse inequality we show that for any continuous curve $\gamma$ in generic position there exists a combinatorial immersion of a curve $c'$ such that $\rho(c')\sim \gamma$ and the number of self-crossings $c'$ is at most the number of self-intersections of $\gamma$. By an isotopy we can enforce the self-intersections of $\gamma$ to lie inside the vertex disks. After removing the vertex disks from $S$, we are left with a set of disjoint and simple pieces of $\gamma$ that can be isotoped inside the edge strips and vertex disks without introducing any new intersections. If a piece of curve in a strip has its endpoints on the same end of the strip we can further isotope the piece inside the incident vertex disk. This way all the curve pieces join the two ends of their strip and can be ordered from left-to-right inside each strip (assuming a preferred direction of the strip). Replacing each curve piece by an arc occurrence we thus define a combinatorial immersion of a curve $c'$ with the required properties.
The same constructions apply to an immersion of two curves showing that  $i(c,d)=i(\rho(c),\rho(d))$. 
\end{proof}

\paragraph{Reduction to a system of quads.}
Let $\Sigma$ be a combinatorial surface with negative Euler characteristic. We describe the construction of a system of quads for a surface without boundary. A similar construction applies when $\Sigma$ has perforated faces. 
Following Lazarus and Rivaud~\cite{lr-hts-12} we start putting $\Sigma$ into a standard form called a \define{system of quads} by Erickson and Whittlesey~\cite{ew-tcsr-13}. After reducing $\Sigma$ to a surface $\Sigma'$ with a single vertex $v$ and a single face $f$ this system of quads is obtained by adding a vertex $w$ at the center of $f$, adding edges between $w$ and all occurrences of $v$ in the facial walk of $f$, and finally deleting the edges of $\Sigma'$. The graph of the resulting system of quads, called the \define{radial graph}~\cite{lr-hts-12}, is bipartite. It contains two vertices, namely $v$ and $w$, and $4g$ edges, where $g$ is the genus of $\Sigma$. All its faces are quadrilaterals. Note that the system of quads can be deduced from $\Sigma$ by a sequence of edge contractions, deletions and insertions. Every cycle $c$ of $\Sigma$ can thus be modified accordingly to give a cycle $\varphi(c)$ in the system of quads. Clearly, there exist cellular  embeddings $\rho, \rho'$ of the graphs of $\Sigma$ and of the system of quads in a same topological surface such that $\rho(c)\sim\rho'(\varphi(c))$. 
\begin{lem}[\cite{dg-tcs-99,lr-hts-12}]\label{lem:systems-of-quads}
  Let $n$ be the number of edges of the combinatorial surface $\Sigma$. The above construction of a system of quads can be performed in $O(n)$ time so that for every closed curve $c$ of length $\ell$ in $\Sigma$, we can compute in $O(\ell)$ time a curve $c'$ of length at most $2\ell$ in the system of quads such that $c'\sim \varphi(c)$.
\end{lem}
For the rest of the paper we shall assume that \define{all surfaces have negative Euler characteristic}. 

\paragraph{Diagrams.}
A \define{disk diagram} over the combinatorial surface $\Sigma$ is a combinatorial sphere $\Delta$ with one perforated face together with a labelling of the arcs of $\Delta$ by the arcs of $\Sigma$ such that
\begin{enumerate}
\item opposite arcs receive opposite labels, 
\item the facial walk of each non-perforated face of $\Delta$ is labelled by the facial walk of some non-perforated face of $\Sigma$.
\end{enumerate}
The diagram is \define{reduced} when no edge of $\Delta$ is incident to two non-perforated faces labelled by the same facial walk (with opposite orientations) of $\Sigma$. An \define{annular diagram} is defined similarly by a combinatorial sphere with two distinct perforated faces. A vertex of a diagram that is not incident to any perforated face is said \define{interior}.
\begin{lem}[van Kampen, See~{\cite[Sec. 2.4]{ew-tcsr-13}}] \label{lem:diagram}
  A cycle of $\Sigma$ is contractible if and only if it is the label of the facial walk of the perforated face of a reduced disk diagram over $\Sigma$. Two cycles are freely homotopic if and only if the facial walks of the two perforated faces of a reduced annular diagram over $\Sigma$ are labelled by these two cycles respectively.
\end{lem}
Note that two non-perforated faces that are adjacent and consistently oriented in a reduced diagram are labelled by adjacent faces that are consistently oriented in $\Sigma$. Moreover, the degree of an interior vertex of the diagram is a multiple of the degree of  the corresponding vertex in $\Sigma$.  In the sequel, \define{all the considered diagrams will be supposed reduced}. 

\paragraph{Spurs, brackets and canonical curves.}
Thanks to Lemma~\ref{lem:systems-of-quads} we may assume that our combinatorial surface $\Sigma$ is a system of quads. Moreover, the construction of this system of quads with the assumption on the Euler characteristic implies that \define{all interior vertices have degree at least 8}.
Following the terminology of Erickson and Whittlesey~\cite{ew-tcsr-13}, we define the \define{turn} of a pair of arcs $(a_1,a_2)$ sharing their origin vertex $v$ as the number of face corners between $a_1$ and $a_2$ in clockwise order around $v$. Hence, if $v$ is a vertex of degree $d$ in $\Sigma$, the turn of $(a_1,a_2)$ is an integer modulo $d$  that is zero when $a_1=a_2$. The \define{turn sequence} of a subpath $(a_i,a_{i+1},\dots,a_{i+j-1})$ of a closed curve of length $\ell$ is the  sequence of $j+1$ turns of $(a_{i+k}\inv,a_{i+k+1})$ for $-1\leq k< j$, where indices are taken modulo $\ell$. The subpath may have length $\ell$, thus leading to a sequence of $\ell+1$ turns. Note that the turn of $(a_{i+k}\inv,a_{i+k+1})$ is zero precisely when $(a_{i+k},a_{i+k+1})$ is a spur. A \define{bracket} is any subpath whose turn sequence has the form $12^*1$ or $\bar{1}\bar{2}^*\bar{1}$ where $t^*$ stands for a possibly empty sequence of turns $t$ and $\bar{x}$ stands for $-x$.  
It follows from Lemma~\ref{lem:diagram} and a simple combinatorial Gauss-Bonnet theorem~\cite{gs-sctag-90} that
\begin{thm}[\cite{gs-sctag-90,ew-tcsr-13}]\label{th:four-vertex}
A nontrivial contractible closed curve on a system of quads must have either a spur or four brackets. Moreover, if the curve is the label of the boundary of a disk diagram with at least one interior vertex, then the curve must have either a spur or five brackets.
\end{thm}

Lazarus and Rivaud~\cite{lr-hts-12} have introduced a canonical form for every free homotopy class of closed curves in a system of quads. In particular, two curves are freely homotopic if and only if their canonical forms are equal (up to a circular shift of their vertex indices). It was further characterized by Erickson and Whittlesey~\cite{ew-tcsr-13} in terms of turns and brackets.
\begin{thm}[\cite{lr-hts-12,ew-tcsr-13}]\label{th:canonical}
  The canonical form of a combinatorial closed curve of length $\ell$ on a system of quads can be computed in $O(\ell)$ time. It is the unique homotopic curve that contains no spurs or brackets and whose turning sequence contains  no $-1$'s and contains at least one turn that is not $-2$.
\end{thm}

\section{Geodesics}\label{sec:geodesics}
The canonical form is an instance of a \define{combinatorial geodesic}, i.e. a curve that contains no spurs or brackets. The canonical form is the rightmost homotopic geodesic. The definitions of a geodesic and of a canonical form extend trivially to paths. In particular, the canonical form of a path is its unique rightmost homotopic geodesic and is characterized as for closed curves.
\begin{thm}\label{th:canonical-path}
  The canonical form of a combinatorial path is the unique homotopic path that contains no spurs or brackets and whose turning sequence contains  no $-1$'s.
\end{thm}
Although we cannot claim in general the uniqueness of geodesics in a homotopy class, homotopic geodesics are almost equal and have the same length. Specifically, define a \define{(quad) staircase} as a planar sequence of quads obtained by stitching an alternating sequence of rows and columns of quads  to form a staircase. Assuming that the staircase goes up from left to right, we define the \define{initial tip} of a quad staircase as the lower left vertex of the first quad in the sequence. The \define{final tip}  is defined as the upper right vertex of the last quad. A \define{closed staircase} is obtained by identifying the two vertical arcs incident to the initial and final tips of a staircase.
\begin{thm}\label{th:geodesic}
  Let $c,d$ be two non-trivial homotopic combinatorial geodesics. 
If $c,d$ are closed curves, then they label the two boundary cycles of an annular diagram composed of a unique closed staircase or of an alternating sequence of paths (possibly reduced to a vertex) and quad staircases connected through their tips. 
Likewise, if $c,d$ are paths, then 
the closed curve $c\cdot d\inv$ labels the boundary of a disk diagram composed of an alternating sequence of paths (possibly reduced to a vertex) and quad staircases connected through their tips. 
\end{thm}
\begin{proof}
We only detail the proof when $c,d$ are paths. See Figure~\ref{fig:lentille-plate}.
\begin{figure}
  \centering
  \includesvg[.6\textwidth]{lentille-plate}
  \caption{A disk diagram for two homotopic paths $c$ and $d$ composed of paths and staircases.}
  \label{fig:lentille-plate}
\end{figure}
The similar case of closed curves is covered in~\cite{ew-tcsr-13}. By Lemma~\ref{lem:diagram}, $c\cdot d\inv$ is the label of the facial walk of the perforated face of a disk diagram $\Delta$. This diagram has a cactus-like structure composed of 2-cells subdivided into quads and connected by trees. A vertex $v$ such that $\Delta\setminus v$ is not connected is called a \emph{cut vertex}. We also consider as cut vertices the endpoints of $c$ in $\Delta$. A 2-cell of $\Delta$ must have more than one cut vertex on its boundary. Otherwise, this boundary is entirely labelled by a subpath of either $c$ or $d$ and Theorem~\ref{th:four-vertex} implies the existence of four brackets, one of which (in fact two) must avoid the cut vertex, hence be contained in the interior of this subpath. This would contradict that $c$ and $d$ are geodesic. Moreover, because $c$ and $d$ have no spur, $\Delta$ cannot have more than two degree one vertices. It follows that $\Delta$ is an alternating sequence of paths and 2-cells. In particular, each 2-cell has exactly two cut vertices. No 2-cell in this sequence has an interior vertex. For otherwise, by the second part of Theorem~\ref{th:four-vertex}, the boundary of this 2-cell would contain five brackets one of which would be contained in the interior of either $c$ or $d$ and this would again contradict that $c$ and $d$ are geodesic.
It follows that the dual of a 2-cell, viewed as an assembling of quads, is a tree. We finally remark that this tree must be a path with a staircase shape. Indeed, any other shape would imply the existence of a bracket in either $c$ or $d$.
\end{proof}
\begin{cor}\label{cor:equal-length}
 With the hypothesis of Theorem~\ref{th:geodesic}, $c$ and $d$ have equal length which is minimal among homotopic curves.
\end{cor}
\begin{cor}\label{cor:no-monogon}
  A combinatorial geodesic has no non-trivial index path whose image path is contractible.
\end{cor}
The next two remarks follow directly from the characterization of geodesics and canonical forms in terms of spurs, brackets and turns.
\begin{remark}\label{rem:sub-geodesic}
  The image path of any index path of a combinatorial geodesic is geodesic. If the combinatorial geodesic is in canonical form, so is the image path.
\end{remark}

\begin{remark}\label{rem:geodesic-power}
 Likewise, any power $c^k$ of a combinatorial closed geodesic $c$ is also a combinatorial geodesic. Moreover, if $c$ is in canonical form, so is $c^k$.
\end{remark}

\medskip
Let $c_R$ and $c\inv_L$ be canonical paths such that $c_R\sim c_L$. In other words, $c_L$ is the leftmost geodesic homotopic to $c_R$. By Theorem~\ref{th:geodesic}, there is a disk diagram $\Delta$ composed of quad staircases and paths and whose left and right boundaries $\Delta_L$ and $\Delta_R$ are labelled by $c_L$ and $c_R$ respectively. 
The next technical Lemma will be used in Proposition~\ref{prop:counting-crossings} to analyze the intersection of canonical curves.
A \define{spoke} is a non-boundary edge of $\Delta$.
\begin{lem}\label{lem:double-path-struct}
Let $v,w$ be two vertices, one on each side of $\Delta$. Then $\Delta$ contains a path  $p$ from $v$ to $w$ labelled by a canonical path. 
Moreover, $p$ can be uniquely decomposed as either $\lambda.\rho$, $\rho.\lambda$,   $\lambda.e.\rho$  or $\rho.e.\lambda$, where
\begin{enumerate}
\item\label{it:lambda}  $\lambda$ is a subpath (possibly reduced to a vertex) of $\Delta_L$ or $\Delta\inv_L$,
\item $\rho$ is a subpath (possibly reduced to a vertex) of $\Delta_R$ or $\Delta\inv_R$, 
\item $e$ is a spoke,
\item\label{it:neg-L} if $\lambda$ is a subpath of $\Delta\inv_L$ of positive length then $p\cap \Delta\inv_L = \lambda$,
\item\label{it:pos-R} if $\rho$ is a subpath of $\Delta_R$ of positive length then $p\cap \Delta_R = \rho$.
\item\label{it:both-neg} if $\rho$ is a subpath of $\Delta\inv_R$ and $\lambda$ is a subpath of $\Delta_L$ then either $\rho$ is reduced to a vertex and $p\cap \Delta_R = \rho$ or $\lambda$ is reduced to a vertex and $p\cap \Delta\inv_L = \lambda$.
\end{enumerate}
\end{lem}
\begin{proof}
  We assume that $v$ is on the left side and $w$ on the right side of $\Delta$, the other case being symmetric. Let $i,j$ be such that $v=\Delta_L(i)$ and $w=\Delta_R(j)$. We first consider the case where $j\geq i$. If $\Delta_L$ and $\Delta_R$ coincide at $v$, then we trivially obtain the desired decomposition as $p=\Delta_L\Ipath{i}{0}.\Delta_R\Ipath{i}{j-i}$. Otherwise, $v$ may be incident to $0,1$ or $2$ spokes.
  \begin{itemize}
  \item If $v$ is not incident to a spoke then $\Delta_L(i-1)$ is either the initial tip of a staircase or is incident to a spoke $e$. We set $q = \Delta_L\Ipath{i}{-1}.\Delta_R\Ipath{i-1}{j-i+1}$ in the first case and $q =\Delta_L\Ipath{i}{-1}.e.\Delta_R\Ipath{i-1}{j-i+1}$ otherwise. The path $q$ has no spurs or $-1$ turns but may start with a bracket. If not, by the characterization of Theorem~\ref{th:canonical-path}, $q$ is already canonical and we can set $p=q$. Otherwise, we short cut the bracket in $q$ to obtain a canonical path $p$ satisfying the above points~\ref{it:lambda} to~\ref{it:pos-R}. See Figure~\ref{fig:structure-geodesic}.
    \begin{figure}
      \centering
      \includesvg[\textwidth]{structure-geodesic}
      \caption{The canonical path $p$ from $v$ to $w$ when $v$ is not incident to a spoke.}
      \label{fig:structure-geodesic}
    \end{figure}
\item If $v$ is incident to exactly one spoke $e$,
then $e$ connects $v$ to either  $\Delta_R(i-1)$ or $\Delta_R(i+1)$. In the former case we set $q=e.\Delta_R\Ipath{i-1}{j-i+1}$. As above, $q$ may be canonical or starts with a bracket and we easily obtain the path $p$ with the desired properties. See Figure~\ref{fig:structure-geodesic-I}. 
    \begin{figure}
      \centering
      \includesvg[\textwidth]{structure-geodesic-I}
      \caption{The canonical path $p$ from $v$ to $w$ when $v$ is incident to exactly one spoke.}
      \label{fig:structure-geodesic-I}
    \end{figure}
If $e$ is incident to $\Delta_R(i+1)$ and
$j>i$ the path $p=\Delta_L\Ipath{i}{0}.e.\Delta_R\Ipath{i+1}{j-i-1}$ has the required properties. 
When $e$ is incident to $\Delta_R(i+1)$ and $j=i$, then either 
 $\Delta_L(i-1)$ is incident to a spoke $e'$ and we must have $p=\Delta_L\Ipath{i}{-1}.e'.\Delta_R\Ipath{i}{0}$, or $\Delta_L(i-1)$  must be the initial tip of a staircase and we must have $p=\Delta_L\Ipath{i}{-1}.\Delta_R\Ipath{i-1}{1}$. 
\item If $v$ is incident to two spokes, then one of them, $e_-$, connects $v$ to $\Delta_R(i-1)$ and the other $e_+$ connects $v$ to $\Delta_R(i+1)$. We can directly set $p=e_-.\Delta_R\Ipath{i-1}{1}$ if $j=i$ and $p=e_+.\Delta_R\Ipath{i+1}{j-i-1}$ if $j>i$.
  \end{itemize}
For Point~\ref{it:both-neg} in the Lemma, we note that we cannot have $p = \lambda.\rho$ or $p=\lambda.e.\rho$  with both $\lambda$ and $\rho$ being  subpaths of positive length of $\Delta_L$  and $\Delta\inv_R$ respectively. Indeed, $p$ would have a spur or a $\bar{1}$ turn in the first case and a bracket in the other case. Moreover, if $\rho$ is reduced to a vertex  and $\lambda$ is a subpath of $\Delta_L$,  we may assume that  the intersection $p\cap\Delta_R$ is reduced to $\rho$. Otherwise, the canonical path between any other intersection point and $\rho$ would have to follow $\Delta_R$ and we could express $p$ so that $\rho$ is a subpath of positive length of $\Delta_R$. An analogous argument holds to show that we can assume $p\cap\Delta\inv_L=\lambda$ if $\lambda$ is reduced to a vertex.

We next consider the case $i>j$. When $\Delta_L$ and $\Delta_R$ coincide at $w$, we obtain the desired decomposition as $p=\Delta_L\Ipath{i}{j-i}.\Delta_R\Ipath{i}{0}$. Otherwise, $w$ may be incident to $0,1$ or $2$ spokes. Similar arguments as in the case $j\geq i$ allow to conclude the proof. 
\end{proof}

\section{Our strategy for counting intersections}\label{sec:strategy}
Following Poincaré's original approach we   
represent the surface $S$ as the hyperbolic quotient surface $\D/\Gamma$ where $\Gamma$ is a discrete group of hyperbolic motions of the Poincaré disk $\D$.
We denote by $p: \D\to \D/\Gamma = S$ the universal covering map. Any closed curve $\alpha:\R/\Z\to S$ gives rise to its infinite power $\alpha^\infty:\R\to\R/\Z \to S$ that wraps around $\alpha$ infinitely many times. A \define{lift} of $\alpha$ is any curve $\tilde{\alpha}:\R\to \D$ such that $p\circ \tilde{\alpha} = \alpha^\infty$ where the parameter of $\tilde{\alpha}$ is defined up to an integer translation (we thus identify the curves $t\mapsto \tilde{\alpha}(t+k)$, $k\in\Z$). Note that $p\inv(\alpha)$ is the union of all the images $\Gamma\cdot \tilde{\alpha}$ of $\tilde{\alpha}$ by the motions in $\Gamma$. 
The curve $\tilde{\alpha}$ has two limit points on the boundary of $\D$ which can be joined by a unique hyperbolic line $L$.
The projection $p(L)$ wraps infinitely many times around the unique geodesic homotopic to $\alpha$. In particular, the limit points of $\tilde{\alpha}$ are independent of the chosen representative in the homotopy class of $\alpha$. 

No two motions of $\Gamma$ have a limit point in common unless they are powers of the same motion. This can be used to show that when $\alpha$ is primitive, its lifts are uniquely identified by their limit points~\cite{fm-pmcg-12}.
Let $\alpha$ and $\beta$ be two primitive curves. We fix a lift $\tilde{\alpha}$ of $\alpha$ and denote by $\tau\in\Gamma$ the hyperbolic motion sending  $\tilde{\alpha}(0)$ to $\tilde{\alpha}(1)$. Let $\Gamma\cdot \tilde{\beta}$ be the set of lifts of $\beta$. We consider the subset of lifts 
\[B=\{\tilde{\beta}'\in \Gamma\cdot \tilde{\beta}\mid \text{ the limit points of } \tilde{\beta}' \text{ and }  \tilde{\alpha} \text{ alternate along } \partial\D\}.
\]
\begin{lem}[\cite{r-ajccs-62}]\label{lem:strategy}
  \[i(\alpha,\beta) = |B/\tau|, \]
where $B/\tau$ is the set of equivalence classes of lifts generated by the relations $\tilde{\beta}'\sim \tau(\tilde{\beta}')$.
\end{lem}
\begin{proof}
  Put $I(\alpha,\beta) =\{(t,t')\mid t, t' \in \R/\Z \text{ and } \alpha(t) = \beta(t')\}$. 
Define a map $\varphi: I(\alpha,\beta)\to B/\tau$ as follows. 
Given $(u\bmod 1, v\bmod 1)\in I(\alpha,\beta)$ there is, by the unique lifting property of coverings, a unique lift $\tilde{\beta}'$ of $\beta$ that satisfies $\tilde{\beta}'(v) = \tilde{\alpha}(u)$. We set $\varphi(u\bmod 1, v\bmod 1)$ to the class of this lift. Note that changing $u$ to $u+k$, $k\in\Z$, leads to the lift $\tau^k(\tilde{\beta}')$, so that $\varphi$ is well-defined. The map $\varphi$ is onto. Indeed, if $\tilde{\beta}'\in B$ then $\tilde{\beta}'$ and $\tilde{\alpha}$ must intersect at some point $\tilde{\beta}'(v) = \tilde{\alpha}(u)$. It follows that $\varphi(u\bmod 1, v\bmod 1)$ is the class of $\tilde{\beta}'$. As an immediate consequence,
\[ |I(\alpha,\beta)| \geq |B/\tau|
\]
When $\alpha$ and $\beta$ are geodesics all their lifts are hyperbolic lines and $\varphi$ is a bijection. We conclude that $|I(\alpha,\beta)|$ is minimized among all homotopic curves so that
$i(\alpha,\beta) = |B/\tau|$.
\end{proof}
When $\alpha$ and $\beta$ are hyperbolic geodesics, their lifts being hyperbolic lines have alternating limit points exactly when they have a non-empty intersection and they have a unique intersection point in that case. This point projects to a crossing of $\alpha$ and $\beta$ that actually identifies the corresponding element of $B/\tau$.
When $\alpha$ and $\beta$ are not geodesic the situation is more ambiguous and their lifts may have multiple intersection points. 
Those intersection points project to crossings of $\alpha$ and $\beta$, so that the elements of $B/\tau$ are now identified with subsets of crossing points (with odd cardinality) rather than single crossing points. The induced partition is generated by the following relation: two crossings are equivalent if they are connected by a pair of homotopic subpaths of $\alpha$ and $\beta$, namely one of the two subpaths of $\alpha$ and one of the two subpaths of $\beta$ cut by the two crossings. Indeed, if the two crossings are projections of intersections of two lifts, then the paths between those intersections in each lift project to homotopic paths. Conversely, homotopic paths lift to paths with common endpoints that can be seen as subpaths of two intersecting lifts. In order to compute the above partition, we thus essentially need an efficient procedure for testing if two paths are homotopic. This homotopy test can indeed be performed in linear time according to Theorem~\ref{th:canonical}. Since a combinatorial curve of length $\ell$ may have $O(\ell^2)$ crossings, we directly obtain an algorithm with time complexity $O(\ell^5)$  to compute the above partition.

When dealing with combinatorial (canonical) geodesics, the situation is more constrained and somehow intermediate between the ideal hyperbolic case and the most general situation. Thanks to Theorem~\ref{th:geodesic}, we know that homotopic paths must stay parallel and at distance at most one. See Figure~\ref{fig:strategy}. 
\begin{figure}
  \centering
  \includesvg[\linewidth]{strategy}
  \caption{Left, two intersecting hyperbolic lines. Middle, two lifts of non-geodesic curves may intersect several times. Right, lifts of combinatorial geodesics.}
  \label{fig:strategy}
\end{figure}
This allows us to identify equivalent crossings more efficiently. These ideas are formalized in the next sections.

\section{Crossing Double-paths}\label{sec:double-paths}
Let $c,d$ be two combinatorial closed curves on a combinatorial surface. 
A \define{double-path} of $(c,d)$ of length $\ell$ is a pair of forward index paths $(\Ipath{\Imath}{\ell}_c,\Ipath{\Jmath}{\ell}_d)$ with the same
image path $c\Ipath{\Imath}{\ell}=d\Ipath{\Jmath}{\ell}$. If $\ell=0$ then  the double path is just a double point. A double path of $c$ 
is defined similarly, taking $c=d$ and assuming $\Imath\neq \Jmath$. 
The next Lemma follows from Remark~\ref{rem:sub-geodesic}.
\begin{lem}\label{lem:double-path}
  Let $\Ipath{\Imath}{\ell}_c$ and $\Ipath{\Jmath}{k}_d$ be forward index paths of two canonical curves $c$ and $d$ such that the image paths $c\Ipath{\Imath}{\ell}$ and $\Ipath{\Jmath}{k}$ are homotopic. Then $k=\ell$ and $(\Ipath{\Imath}{\ell}_c,\Ipath{\Jmath}{\ell}_d)$ is a double path.
\end{lem}
A double path $(\Ipath{\imath}{\ell}_c,\Ipath{\jmath}{\ell}_d)$ gives rise to a sequence of $\ell+1$ double points $\Dpoint{\imath+k}{\jmath+k}$ for $k\in [0,\ell]$. A priori a double point could occur several times in this sequence. The next two lemmas claim that this is not possible when the curves are primitive.
Recall that a curve is \define{primitive} if its homotopy class cannot be expressed as a proper power of another class.
\begin{lem}\label{lem:short-double-path-c}
  A double path of a primitive combinatorial curve $c$ cannot contain a double point more than once in its sequence. In particular, a double path of $c$ must be strictly shorter than $c$.
\end{lem}
\begin{proof}
Suppose that a double path $\dP$ of $c$ contains two occurrences of a double point $(\Ibar,\Jbar)$. Because the couples $(\Ibar,\Jbar)$ and $(\Jbar,\Ibar)$ represent the same double point there are two cases to consider. 
\begin{itemize}
\item If $\dP$ contains the couple $(\Ibar,\Jbar)$ twice then it must contain a subsequence of length $|c|$ starting with $(\Ibar,\Jbar)$. We thus have $c\Ipath{\imath}{|c|}=c\Ipath{\jmath}{|c|}$. This implies that $c$ is equal to some non-trivial circular permutation of itself. It is a simple exercise to check that $c$ must then be a proper power of some other curve, contradicting that $c$ is primitive. 
\item Otherwise $\dP$ contains $(\Ibar,\Jbar)$ and $(\Jbar,\Ibar)$. Let $\ell$ be the distance between these two occurrences in $\dP$. We thus have $c\Ipath{\Imath}{\ell} = c\Ipath{\Jmath}{|c|-\ell}$ from which we deduce that  $c$ is a square (and $\ell=|c|/2$), contradicting that $c$ is primitive. 
\end{itemize}
\end{proof}

\begin{lem}\label{lem:short-double-path-c-d}
  Let $c$ and $d$ be two non-homotopic primitive combinatorial curves. A double path of $(c,d)$ cannot contain a double point more than once in its sequence. Moreover, the length of a double path of $(c,d)$ must be less than $|c|+|d|-1$.
\end{lem}
\begin{proof}
  Suppose that a double path of $(c,d)$ contains two occurrences of a double point. After shortening the double path if necessary, we may assume that these two occurrences are the first and the last double points of the double path. Its length must accordingly be a nonzero integer multiple $p$ of $|c|$ as well as a nonzero integer multiple $q$ of $|d|$. It follows that for some circular permutations $c'$ of $c$ and $d'$ of $d$ we have $c'^p = d'^q$. By a classical result of combinatorics on words~\cite[Prop. 1.3.1]{l-cw-97} this implies that $c'$ and $d'$ are powers of a same curve, in contradiction with the hypotheses in the lemma. In fact, by a refinement due to Fine and Wilf~\cite[Prop. 1.3.5]{l-cw-97} it suffices that $c'^p$ and $d'^q$ have a common prefix of length $|c|+|d|-1$ to conclude that $c'$ and $d'$ are powers of a same curve. This proves the second part of the lemma.
\end{proof}

A double path whose index paths cannot be extended is said \define{maximal}. 
As an immediate consequence of Lemmas~\ref{lem:short-double-path-c} and ~\ref{lem:short-double-path-c-d} we have:
\begin{cor}\label{cor:partition-double-paths}
  The maximal double paths of a primitive curve or of two primitive curves in canonical form induce a partition of the double points of the curves. 
\end{cor}
Let $(\Ibar,\Jbar)$ and $\Dpoint{\Imath+\ell}{\Jmath+\ell}$ be the first and the last double points of a maximal double path of $(c,d)$, possibly with $c=d$. When $\ell\geq 1$ the arcs $c[\Ibar,\Ibar-1]$, $d[\Jbar,\Jbar-1]$, $c[\Ibar,\Ibar+1]$ must be pairwise distinct because canonical curves have no spurs, and similarly for the three arcs $c[\Overline{\Imath+\ell},\Overline{\Imath+\ell+1}]$, $d[\Overline{\Jmath+\ell},\Overline{\Jmath+\ell+1}]$, $c[\Overline{\Imath+\ell},\Overline{\Imath+\ell-1}]$. We declare the maximal double path to be a \define{crossing double path} if the circular ordering of the first three arcs at $c(\Ibar)$ and the circular ordering of the last three arcs at $c(\Overline{\Imath+\ell})$ are either both clockwise or both counterclockwise with respect to the rotation system of the system of quads. When $\ell=0$, that is when the maximal double path is reduced to the double point\footnote{Those crossing double points should not be confused with the combinatorial crossings of an immersion as defined in Section~\ref{sec:framework}. Which notion of crossings is used should always be clear from the context.} $(i,j)$, we require that the arcs  $c[i,i-1],d[j,j-1], c[i,i+1], d[j,j+1]$ are \emph{pairwise distinct} and appear in this circular order, or its opposite, around the vertex $c(i)=d(j)$.

\section{Counting intersections combinatorially}
Let $c,d$ be primitive combinatorial curves such that $d$ is canonical and let $c_R$ and $c_L\inv$ be the canonical curves homotopic to $c$ and $c\inv$ respectively. We denote by $\Delta$ the annular diagram corresponding to $c_R$ and $c_L$. When the two boundaries $\Delta_R$ and $\Delta_L$ of $\Delta$ have a common vertex we implicitly assume that $c_R$ and $c_L$ are indexed so that this vertex corresponds to the same index along $\Delta_R$ and $\Delta_L$.
We consider the following set of double paths:
\begin{itemize}
\item $\mathcal{D}_+$ is the set of crossing double paths of positive length of $c_R$ and $d$,
\item $\mathcal{D}_0$ is the set of crossing double paths $(i,j)$ of zero length of $c_R$ and $d$ such that either 
  \begin{itemize}
\item the two boundaries of $\Delta$ coincide at $\Delta_L(i) = \Delta_R(i)$ and $d[j-1,j] = c_L[i-1,i]$ or $d[j,j+1] = c_L[i,i+1]$, or
\item one of $d[j,j-1]$ or $d[j,j+1]$ is the label of a spoke $(\Delta_R(i),\Delta_L(i'))$ of $\Delta$ and $d[j-2,j-1] = c_L[i'-1,i']$ in the first case or $d[j+1,j+2] = c_L[i',i'+1]$ in the other case.
  \end{itemize}
\item $\mathcal{D}_-$ is the set of crossing double paths  $(\Ipath{\Imath}{\ell}_{c_L\inv},\Ipath{\Jmath}{\ell}_d)$ ($\ell \geq 0$) of $c_L\inv$ and $d$ such that \emph{none} of the following situations occurs:
  \begin{itemize}
\item the two boundaries of $\Delta$ coincide at $\Delta_L\inv(i) = \Delta_R(i')$ and $d[j-1,j] = c_R[i'-1,i']$,
\item  the two boundaries of $\Delta$ coincide at $\Delta_L\inv(i+\ell) = \Delta_R(i')$  and $d\Focc{j+\ell} = c_R\Focc{i'}$,
\item $d[j-1,j]$ is the label of a spoke $(\Delta_L\inv(i), \Delta_R(i'))$ of $\Delta$ and $d[j-2,j-1] = c_R[i'-1,i']$,
\item $d[j+\ell,j+\ell+1]$ is the label of a spoke $(\Delta_L\inv(i+\ell), \Delta_R(i'))$ of $\Delta$ and $d[j+\ell+1,j+\ell+2] = c_R[i',i'+1]$.
  \end{itemize}
\end{itemize}
Those definitions allow the case $c\sim d$, recalling that the index paths of a double path of $c$ must be distinct by definition.
Referring to Section~\ref{sec:strategy}, we view the underlying surface of the system of quads $\Sigma$ as a quotient $\D/\Gamma$ of the Poincar\'e disk. The system of quads lifts to a quadrangulation of $\D$ and the lifts of a combinatorial curve in $\Sigma$ are combinatorial bi-infinite paths in this quadrangulation. By Remark~\ref{rem:geodesic-power}, if the combinatorial curve is geodesic (resp. canonical) so are its lifts. In this case, each lift is simple by Corollary~\ref{cor:no-monogon}. We fix a lift $\tilde{c_R}$ of $c_R$ and consider the set $B/\tau$ of Lemma~\ref{lem:strategy} corresponding to the classes of lifts of $d$ whose limit points alternate with the limit points of $\tilde{c_R}$ along $\partial\D$. 
\begin{prop}\label{prop:counting-crossings}
  $B/\tau$ is in 1-1 correspondence with the disjoint union $\mathcal{D}_+\cup \mathcal{D}_0\cup  \mathcal{D}_-$.
\end{prop}
\begin{proof}
  Let $\tilde{c_L}$ be the lift of $c_L$ with the same limit points as $\tilde{c_R}$. These two lifts  project onto the boundaries of the annular diagram $\Delta$ and thus form an infinite strip $\tilde{\Delta}$ of width at most 1 in $\D$ composed of paths and quad staircases (possibly a single infinite staircase). We shall define a correspondence between $B/\tau$ and $\mathcal{D}_+\cup \mathcal{D}_0\cup  \mathcal{D}_-$.
To this end we consider 
a lift $\tilde{d}$ of $d$ whose limit points alternate with those of $\tilde{c_R}$. In other words, $\tilde{d}\in B$. The lift $\tilde{d}$ must cross $\tilde{\Delta}$. Let $i$ and $j$ be respectively the smallest and largest index $k$ such that $\tilde{d}(k)$ is in $\tilde{\Delta}$. By Remark~\ref{rem:sub-geodesic}, the corresponding subpath $\tilde{d}[i,j]$ of $\tilde{d}$ is canonical. Since $\D$ is simply connected, $\tilde{d}[i,j]$ is homotopic to any path joining the same extremities and we can apply Lemma~\ref{lem:double-path-struct} to show that $\tilde{d}[i,j]$ is actually contained in $\tilde{\Delta}$ and that it can be decomposed as either $\lambda.\rho$, $\rho.\lambda$,   $\lambda.e.\rho$  or $\rho.e.\lambda$ where $e$ is a spoke of $\tilde{\Delta}$, $\rho=\tilde{c_R}\Ipath{a}{r}$ and $\lambda=\tilde{c_L}\Ipath{b}{\ell}$ for some $a,b,r,\ell\in \Z$. 
  \begin{itemize}
  \item If $r>0$ then by Point~\ref{it:pos-R} of Lemma~\ref{lem:double-path-struct} we have $\tilde{d}\cap \tilde{c_R}=\rho$ so that this intersection defines a maximal double path of  $\tilde{d}$ and $\tilde{c_R}$. It must be crossing since  $\tilde{d}\in B$. Its projection on $\Sigma$ is a crossing double path of length $r>0$ of $d$ and $c_R$, hence in $\mathcal{D}_+$, to which we map $\tilde{d}$.
\item If $r=0$ and $\ell>0$, then Point~\ref{it:both-neg} of Lemma~\ref{lem:double-path-struct} implies $\tilde{d}\cap \tilde{c_R}=\rho$. As above, $\rho$ must define a crossing double path of  length zero of $\tilde{d}$ and $\tilde{c_R}$. We map $\tilde{d}$ to the projection of this crossing double point on $\Sigma$ and remark that this projection is in $\mathcal{D}_0$.
\item Otherwise, we must have $r\leq 0$ and $\ell\leq 0$ by Point~\ref{it:both-neg} of Lemma~\ref{lem:double-path-struct}. If $\ell <0$ then Point~\ref{it:neg-L} of Lemma~\ref{lem:double-path-struct} implies $\tilde{d}\cap \tilde{c_L}\inv = \lambda$. This intersection defines a crossing double path of $\tilde{d}$ and $\tilde{c_L}\inv$ and we map $\tilde{d}$ to its projection on $\Sigma$. If $\ell=0$ and $r<0$ then Point~\ref{it:both-neg} of Lemma~\ref{lem:double-path-struct} implies $\tilde{d}\cap \tilde{c\inv_L}=\lambda$, which also holds true if $r=\ell=0$. In both cases $\lambda$ corresponds to a crossing double path of  length zero of $\tilde{d}$ and $\tilde{c\inv_L}$ and we map $\tilde{d}$ to its projection on $\Sigma$. We finally remark that this last projection or the above one belong to $\mathcal{D}_-$.
  \end{itemize}

Because $\tilde{\Delta}$ is left globally invariant by $\tau$ (the hyperbolic motion that sends $\tilde{c_R}(0)$ to $\tilde{c_R}(|c_R|)$), we have $\tau(\tilde{d})\cap\tilde{\Delta}= \tau(\tilde{d})\cap\tau(\tilde{\Delta})=\tau(\tilde{d}\cap\tilde{\Delta})$.
It follows that $\tilde{d}$ and $\tau(\tilde{d})$ are mapped to the same crossing double path by the above rules. We thus have a well defined map $B/\tau\to \mathcal{D}_+\cup \mathcal{D}_0 \cup \mathcal{D}_-$. The uniqueness of the decomposition in Lemma~\ref{lem:double-path-struct} implies that this map is 1-1.
In order to check that the map is onto we  consider a maximal crossing double path $\dP=(\Ipath{\Imath \mod |c_R|}{\ell}_{c_R},\Ipath{\Jmath\mod |d|}{\ell}_d)$ of $c_R$ and $d$ in $\mathcal{D}_+$. By the unique lift property of coverings there is a unique lift $\tilde{d}$ of $d$ such that $\tilde{d}(j)=\tilde{c_R}(i)$ and $\dP$ lifts to a crossing double path of $\tilde{d}$ and $\tilde{c_R}$. By Lemma~\ref{lem:double-path} this double path is the only intersection of $\tilde{d}$ and $\tilde{c_R}$ so these lifts must have alternating limit points. In other words $\tilde{d}$ is in $B$ and is mapped to $\dP$. A similar argument applies to the crossing double paths of $\mathcal{D}_0$ and $\mathcal{D}_-$.
\end{proof}
This leads to a simple algorithm for computing combinatorial crossing numbers.
\begin{cor}\label{cor:crude-result}
  Let $c,d$ be primitive curves of length at most $\ell$ on a combinatorial surface with complexity $n$. The crossing numbers $i(c,d)$ and $i(c)$ can be computed in $O(n+\ell^2)$ time.
\end{cor}
\begin{proof}
By Lemma~\ref{lem:systems-of-quads} we may assume that the surface is a system of quads. By Theorem~\ref{th:canonical} we may compute the canonical forms of $c, c\inv$ and $d$ in $O(\ell)$ time. According to Proposition~\ref{prop:counting-crossings}, we have
\[i(c,d) = |\mathcal{D}_+|+|\mathcal{D}_0|+|\mathcal{D}_-|
\]
The set $\mathcal{D}_+$ can be constructed in $O(\ell^2)$ time. Indeed, 
since the maximal double paths of $c$ and $d$ form disjoint sets of double points by Corollary~\ref{cor:partition-double-paths}, we just need to traverse the grid $\Z/|c|\Z\times \Z/|d|\Z$ and group the double points into maximal double paths. Those correspond to diagonal segments in the grid that can be computed in time proportional to the size of the grid. We can also determine which double paths are crossing in the same amount of time. Likewise, we can construct the sets $\mathcal{D}_0$ and $\mathcal{D}_-$ in $O(\ell^2)$ time. We can also compute $i(c)$ in quadratic time using that $i(c,c)=2i(c)$.
\end{proof}

\section{Non-primitive curves and proof of Theorem~\ref{th:main-result}}\label{sec:non-primitive}
In order to finish the proof of Theorem~\ref{th:main-result}, we need to tackle the case of non-primitive curves. Thanks to canonical forms, computing the primitive root of a curve becomes extremely simple.
\begin{lem}\label{lem:non-primitive}
  Let $c$ be a combinatorial curve of length $\ell>0$ in canonical form. A primitive curve $d$ such that $c$ is homotopic to $d^k$ for some integer $k$ can be computed in $O(\ell)$ time.
\end{lem}
\begin{proof}
  By Theorem~\ref{th:canonical}, we may assume that $c$ and $d$ are in canonical form. By Remark~\ref{rem:geodesic-power}, the curve $d^k$ is also in canonical form. The uniqueness of the canonical form implies that $c=d^k$, possibly after some circular shift of $d$. It follows that $d$ is the smallest prefix of $c$ such that $c$ is a power of this prefix. It can be found in $O(\ell)$ time using a variation of the Knuth-Morris-Pratt algorithm to find the smallest period of a word~\cite{kmp-fpms-77}.  
\end{proof}

The geometric intersection number of non-primitive curves is related to the geometric intersection number of their primitive roots. The next result is rather intuitive, see Figure~\ref{fig:non-primitive}, and is part of the folklore although we could only find references in some relatively recent papers.
\begin{prop}[\cite{gs-mcmcr-97,gkz-amnip-05}]\label{prop:non-primitive-formulas}
  Let $c$ and $d$ be primitive curves and let $p,q$ be integers. Then,
\[
i(c^p) = p^2 \times i(c) + p-1 \quad \text{ and }\quad i(c^p,d^q) = \left\{
              \begin{array}{ll}
                   2pq \times i(c) & \text{if } c\sim d \text{ or } c\sim d\inv, \\
                   pq \times i(c,d) & \text{otherwise.}
              \end{array}
       \right. 
\]
\end{prop}
\begin{figure}
  \centering
  \includesvg[\linewidth]{non-primitive}
  \caption{Left, each self-intersection of $c$ gives rise to $p^2$ self-intersections of its $p$th power obtained by wrapping $p$ times around $c$ in a small tubular neighborhood. The starting basepoint also adds $p-1$ self-intersections. Middle, each intersection of $c$ and $d$ gives rise to $pq$ intersections of (homotopic perturbations of) $c^p$ and $d^q$. Right, when $c$ and $d$ are homotopic, one should count $2pq$ intersections per self-intersection of $c$.}
  \label{fig:non-primitive}
\end{figure}
\begin{proof}[Proof of Theorem~\ref{th:main-result}]
Let $c,d$ and $\Sigma$ be the two combinatorial curves and the combinatorial surface as in the Theorem.
We first assume that the $\Sigma$ has negative Euler characteristic. 
We can compute the canonical forms of $c$ and $d$ in $O(\ell)$ time after $O(n)$ time preprocessing by 
Lemma~\ref{lem:systems-of-quads}. Thanks to Lemma~\ref{lem:non-primitive} we can further determine primitive curves $c'$ and $d'$ and integers $p,q$ such that $c\sim c'^p$ and $d\sim d'^p$ in $O(\ell)$ time. We can compute $i(c',d')$ and $i(c')$ in $O(\ell^2)$ time according to Corollary~\ref{cor:crude-result}. We finally use the formulas in the previous proposition to deduce $i(c,d)$ and $i(c)$ from $i(c',d')$ and $i(c')$.

If $\Sigma$ is a sphere or a disk, then every curve is contractible and $i(c,d)=i(c)=0$. If $\Sigma$ is a cylinder, then every two curves can be made non crossing so that $i(c,d)=0$ while $i(c)=p-1$. Finally, if $\Sigma$ is a torus, the radial graph of the system of quads can be decomposed into two loops $\alpha,\beta$ such that $c\sim \alpha^x\cdot\beta^y$ and $d\sim \alpha^{x'}\cdot\beta^{y'}$. We may then use the classical formulas: $i(c)=\gcd(x,y)-1$ and $i(c,d)=|\det\big((x,y),(x',y')\big)|$.
\end{proof}

\section{Computing a minimal immersion}\label{sec:computing-immersions}
In the subsequent sections we only deal with the self-intersection number of a single curve. We thus drop the subscript $c$ to denote an index path $\Ipath{\Imath}{\ell}$ or an arc occurrence $\Focc{\Imath}$. We also implicitly consider intersections as self-intersections.
By Theorem~\ref{th:main-result} we can compute the geometric intersection number of a curve efficiently. Here, we describe a way to compute a minimal immersion, that is an actual immersion with the minimal number of intersections. We refer to the combinatorial framework of Section~\ref{sec:framework} to describe such an immersion combinatorially. Thanks to Lemma~\ref{lem:geometric-vs-combinatorial}, we know that this framework faithfully encodes the topological configurations.
\paragraph{Bigons and monogons.}
A \define{bigon} of an immersion $\mathcal I$ of  $c$ is a  pair of index paths $(\Ipath{\Imath}{\ell},\Ipath{\Jmath}{k})$ whose \define{sides} $c\Ipath{\Imath}{\ell}$ and $c\Ipath{\Jmath}{k}$ have strictly positive lengths, are homotopic, and whose \define{tips}
$(i,j)$ and $(i+\ell,j+k)$ are combinatorial crossings for $\mathcal I$.
A \define{monogon} of $\mathcal I$ is an index path $\Ipath{\Imath}{\ell}$ of strictly positive length such that $(\Ibar,\Overline{\Imath+\ell})$ is a combinatorial crossing and the image path $c\Ipath{\Imath}{\ell}$ is contractible.
\begin{prop}\label{prop:bigon}
  A combinatorial immersion of a primitive curve has excess self-crossing if and only if it contains a bigon or a monogon.
\end{prop}
\begin{proof}
We just need to prove the existence of a bigon or monogon for a continuous realization $\gamma: \R/\Z\to S$ of the combinatorial immersion. This bigon or monogon corresponds to a combinatorial bigon or monogon as claimed in the Proposition. To prove the topological counterpart, we first note as in Section~\ref{sec:strategy} that the minimal number of intersections is counted by the lifts of $\gamma$ whose limit points alternate along $\partial\mathbb{D}$. 
It follows that $\gamma$ is minimally crossing when its lifts are pairwise disjoint unless the corresponding limit points alternate in $\partial\mathbb{D}$ and they should intersect exactly once in that case. When $\gamma$ is not minimally crossing  there must exist two lifts intersecting more than necessary, thus forming a bigon in $\D$. The projection of such a bigon on $S$ gives the desired result. Note that a statement similar to the one in the Proposition holds for an immersion of a pair of primitive curves $c$  and $d$ with excess crossing.  In Appendix~\ref{app:bigon}, we provide a purely combinatorial proof of the direct implication of the Proposition in the case of two curves. 
\end{proof}

Suppose that $\mathcal{I}$ is a combinatorial immersion of a primitive geodesic $c$. It cannot have a monogon by Corollary~\ref{cor:no-monogon}. Hence, according to Proposition~\ref{prop:bigon} the immersion $\mathcal{I}$ is minimal unless it contains a bigon. The two sides of such a bigon may share some common part that would prevent us from swapping these two sides.  In agreement with the terminology of Hass and Scott~\cite{hs-ics-85}, a bigon $(\Ipath{\Imath}{\ell},\Ipath{\Jmath}{k})$ is said \define{singular} if 
\begin{enumerate}
\item its two index paths have disjoint interiors, i.e. they do not share any arc occurrence;
\item when $\Jbar=\Overline{\Imath+\ell}$ the following arc occurrences
\[\Bocc{\Imath}, [j,j-1],\Focc{\Imath},[\Jmath+k,\Jmath+k+1],[j,j+1],[\Jmath+k,\Jmath+k-1]
\]
do not appear in this order or its opposite in the circular ordering induced by $\mathcal{I}$ at $c(j)$;
\item when $\Ibar=\Overline{\Jmath+k}$ the following arc occurrences
\[\Bocc{\Imath}, \Bocc{\Jmath},\Bocc{\Imath+\ell},\Focc{\Imath},\Focc{\Imath+\ell},\Focc{\Jmath}
\]
do not appear in this order or its opposite in the circular ordering  induced by $\mathcal{I}$ at $c(i)$.
\end{enumerate}
Note that when $c$ is geodesic and $k=-\ell$ there cannot be any identification between $\{\Ibar, \Overline{\Imath+\ell}\}$ and $\{\Jbar, \Overline{\Jmath-\ell}\}$. For instance, $\Ibar=\Jbar$ implies $c\Ipath{\Imath}{\ell}\sim c\Ipath{\Jmath}{-\ell}=c\Ipath{\Imath}{-\ell}$ so that $c\Ipath{\Imath-\ell}{2\ell}\sim 1$, while $\Jbar=\Overline{\Imath+\ell}$ implies $c\Ipath{\Imath}{\ell} \sim c\Ipath{\Jmath}{-\ell} = c\inv\Ipath{\Imath}{\ell}$ so that $c^2\Ipath{\Imath}{\ell}\sim 1$. In both cases $c$ would have a monogon which would contradict Lemma~\ref{cor:no-monogon}.

When $c$ is primitive and geodesic, we cannot have $\Jmath=\Imath+\ell$ and $\Imath=\Jmath+\ell$ at the same time. Otherwise, we must have $k=\ell$ by the preceding paragraph, and $c$ would be a circular shift of $c\Ipath{\Imath}{\ell}\cdot c\Ipath{\Jmath}{\ell}$ and thus homotopic to a square. Also remark that when one of the last two conditions in the definition is not satisfied, the bigon maps to a non-singular bigon in the continuous realization of $\mathcal I$ as in Lemma~\ref{lem:geometric-vs-combinatorial}. See Figure~\ref{fig:singular-def}.
\begin{figure}
  \centering
  \includesvg[\linewidth]{singular-def}
  \caption{Right, the realization of the bigon $(\Ipath{\Imath}{\ell},\Ipath{\Jmath}{k})$ where $\Jbar=\Overline{\Imath+\ell}$ and $\Bocc{\Imath}$ is in-between $\Bocc{\Imath+\ell}$ and $\Bocc{\Jmath+k}$, and $\Focc{\Jmath+\ell}$ is  in-between $\Focc{\Imath}$ and $\Focc{\Jmath}$.  The small purple part is at the same time the beginning of the red side of the bigon and at the end of the blue side. Swapping this bigon does not reduce the number of crossings.}
  \label{fig:singular-def}
\end{figure}

When the bigon is singular we can swap its two sides by exchanging the two arc occurrences $[\Overline{\Imath+p},\Overline{\Imath+p+1}]$ and $[\Overline{\Jmath+\varepsilon p},\Overline{\Imath+\varepsilon(p+1)}]$, for $0\leq p< \ell$ and $k=\varepsilon\ell$. 
\begin{lem}\label{lem:swap}
   Swapping the two sides of a singular bigon of an immersion of a geodesic primitive curve decreases its number of crossings by at least two. 
\end{lem}
This is relatively obvious if one considers a continuous realization of the immersion, performs the swapping and comes back to a combinatorial immersion as in the proof of Lemma~\ref{lem:geometric-vs-combinatorial}. We nonetheless provide a purely combinatorial proof in Appendix~\ref{app:swap}. 

Hence, by swapping singular bigons we may decrease the number of crossings until there is no more singular bigons. It follows from the next lemma that the resulting immersion has no excess crossing.
\begin{thm}[Hass and Scott~{\cite[Th. 4.2]{hs-ics-85}}]\label{th:singular-bigon}
  An immersion of a primitive geodesic curve has excess crossing if and only if it contains a singular bigon.
\end{thm}
\begin{proof}
We realize $\mathcal I$ by a continuous curve $\gamma$ with the same construction as in Lemma~\ref{lem:geometric-vs-combinatorial}. By Theorem 4.2 in~\cite{hs-ics-85} the curve $\gamma$ has a singular bigon. In turn this bigon corresponds to a singular bigon in $\mathcal I$. 
\end{proof}
In the next section we describe how to detect bigons in practice.

\section{Finding Bigons}\label{sec:finding-bigons}
If an immersion $\mathcal{I}$ of a primitive geodesic $c$ has a bigon then its two sides have equal length according to Corollary~\ref{cor:equal-length} and form a sequence of paths and staircases following the description of Theorem~\ref{th:geodesic}. In particular, the vertices of the two sides can be put in 1-1 correspondence and corresponding vertices are at distance at most one. Hence, when looking for bigons in $\mathcal{I}$, we can start from any crossing $(i,j)$ and walk in parallel along the two sides from $i$ and $j$, checking that the two sides stay at distance one. However, we do not know a priori if the two sides will cross again to form a bigon and the search may be unsuccessful. In order to analyze the complexity of the bigon search we thus consider pair of paths that could potentially be part of a bigon, but that are not necessarily so. Formally, a pair $(\Ipath{\Imath}{\ell},\Ipath{\Jmath}{\varepsilon\ell})$ of distinct index paths such that $c\Ipath{\Imath}{\ell}$ and $c\Ipath{\Jmath}{\varepsilon\ell}$ are corresponding subpaths of homotopic geodesic paths is called a \define{thick double path} of length $\ell$. In other words, a thick double path is such that its image paths can be extended to form homotopic geodesic paths. As for a double path, a thick double path gives rise to a sequence of $\ell+1$ index pairs $\Dpoint{\Imath+k}{\Jmath+\epsilon k}$ for $k\in [0,\ell]$. Let $\Delta$ be a disk diagram whose boundary is labelled by homotopic geodesic paths extending the thick double path as illustrated on Figure~\ref{fig:thick}. 
\begin{figure}
  \centering
  \includesvg[.6\textwidth]{thick}
  \caption{A thick double path extends to a pair of homotopic geodesics and bound a partial diagram.}
  \label{fig:thick}
\end{figure}
The restriction of $\Delta$ to the part delimited by the thick double path is called a \define{partial diagram}. In a partial diagram each index pair $\Dpoint{\Imath+k}{\Jmath+\epsilon k}$ may be in at most one of five \define{configurations}: either the corresponding vertices coincide in the partial diagram, or they appear diagonally opposite in a quad; in turn this quad may be labelled by one of the (at most) four quads incident to either $c[i,i+1]$ or $c[j,j+\varepsilon]$. A partial diagram  is \define{maximal} if it cannot be extended to form a partial diagram of a longer thick double path. 
\begin{lem}\label{lem:configurations}
  If $c$ is a primitive geodesic curve each configuration of an index pair may occur at most once in a partial diagram.
\end{lem}
\begin{proof}
  Suppose for a contradiction that the index pair $(i,j)$ occurs twice in the same configuration in a partial diagram of a thick double path of $c$. Exchanging the roles of $i$ and $j$ if necessary, we may assume that the indices $i,j$ are represented by integers such that $0< j-i \leq |c|/2$.
The two  occurrences of $(i,j)$  are separated by a path whose length is an integer multiple of $|c|$, say $k|c|$. We first assume that the two index paths of the thick double path are both forward. In the partial diagram, $c(i)$ and $c(j)$ label vertices that are either identical or opposite in a quad. They are thus connected by a path $p$ of length zero or two. We infer that 
\[c\Ipath{\Imath}{k|c|}\cdot p \sim p\cdot c\Ipath{\Jmath}{k|c|}\sim p\cdot c[i,j]\inv\cdot c\Ipath{\Imath}{k|c|}\cdot c[i,j]
\]
This can be written as $c'^k\cdot q \sim q\cdot c'^k$ where $c'=c\Ipath{\Imath}{|c|}$ is a cyclic permutation of $c$ and $q = p\cdot c[i,j]\inv$ is a (closed) path of length at most $j-i+2$. Since the homotopy classes of the loops $c'^k$ and $q$ are commuting they must admit a common primitive root~\cite{r-ajccs-62}. We thus have {\it (i)} $c'^k \sim u^s$ and {\it(ii)} $q\sim u^t$ for some primitive curve $u$ and some $s,t\in \Z$. Relation {\it(i)} implies that $c'$ and $u^s$ are also commuting so that $c'$ is homotopic to a power of the primitive curve $u$. But $c'$ being also primitive we actually have $c'\sim u^{\pm 1}$. By Remark~\ref{rem:geodesic-power}, $c'^t$ is geodesic, hence has minimal length in its homotopy class. From {\it(ii)} we obtain $j-i+2 \geq |t|.|c|$. This is only possible if $t=0$ or if $|t|=1$ and $j-i=|c|-2$ (indices in a pair have the same parity). 
\begin{itemize}
\item If $t=0$, then $q\sim 1$, so that $p\sim c[i,j]$. Since $c$ is  geodesic, we must have $j-i\leq |p| \leq 2$, hence $j=i+2$ and the corresponding vertices must be diagonally opposite in a quad. Two homotopic paths each of length two bound a disk diagram composed of either a length two path or a single quad. In other words,  $c[i,j]$ and $p$ are either equal or bound the first quad in the partial diagram. In either case we infer that $c[i,j+1]= c[i,j].c[j,j+1]$ contains a spur or a bracket, leading to a contradiction.
\item If $|t|=1$ and $j-i=|c|-2$, then $|c|=4$ (recall that $0<j-i\leq |c|/2$). A simple case study of the possible partial diagrams for such a $c$ lead to a contradiction in each case. See the Appendix~\ref{sec:end-proof-lem-config} for a proof. 
\end{itemize}

We now assume that the thick double path is composed of a forward and a backward index paths. Similarly to the previous case, we infer that $c'\sim q \cdot (c')\inv \cdot q\inv$, where $c'=c\Ipath{\Imath}{|c|}$  and $q = p\cdot c[i,j]\inv$. Equivalently, $c$ and its inverse are freely homotopic. This is however impossible unless $c$ is contractible.
\end{proof}
\begin{cor}\label{cor:index-pairs}
  Each configuration of an index pair may occur at most once in the set of maximal partial diagrams.
\end{cor}
\begin{proof}
  By the preceding Lemma, we only need to check that a configuration of an index pair cannot occur twice in distinct partial diagrams of maximal thick double paths. This is essentially the unique lift property of coverings.
\end{proof}

\begin{proof}[Proof of Theorem~\ref{th:compute-immersion}]
  By Theorem~\ref{th:canonical} we can assume that $c$ is geodesic. We first consider the case where $c$ is primitive. Let $\mathcal I$ be a randomly chosen immersion of $c$. We store the induced vertex orderings of the arc occurrences in arrays so that we can test if a double point is a crossing in constant time using pointer arithmetic. By Theorem~\ref{th:singular-bigon}, if $\mathcal I$ has excess crossing it has a singular bigon. This singular bigon defines a thick double path that must appear in some maximal partial diagram of $\mathcal I$ as two boundary paths with common extremities.
Hence, in order to find a singular bigon we just need to scan the index pairs in every maximal partial diagram and test whether the two sides between consecutive index pairs define a singular bigon.  By the very definition of a singular bigon, each test can be easily performed in constant time. The number of tests is itself bounded by the number of index pairs in all the maximal partial diagrams. This is $O(\ell^2)$ by Corollary~\ref{cor:index-pairs}. Once a singular bigon is found we swap its sides in $O(\ell)$ time. This preserves the geodesic character and, by Lemma~\ref{lem:swap}, the number of crossings is reduced by at least two.
Since $\mathcal I$ may have $O(\ell^2)$ excess crossings, we need to repeat the above procedure $O(\ell^2)$ times and we may conclude the theorem in the case of primitive curves. When $c=d^p$, with $d$ primitive, we  first find a minimally crossing immersion for $d$ by the above procedure. We further traverse the immersion $p$ times duplicating $d$ as many times. As we start each traversal we connect the last arc occurrence of the previously traversed copy with the first occurrence of the next copy. We continue this copy by duplicating each traversed arc occurrence to its right. It is easily seen that the number of crossings of the final immersion of $c$, after connecting the last traversed arc with the first one, satisfies the formula in Proposition~\ref{prop:non-primitive-formulas}.
\end{proof}
We end this section with some refinements of Lemma~\ref{lem:configurations} and its Corollary. We first highlight a simple property of the system of quads.
\begin{lem}\label{lem:facial-walk}
  The facial walk of a quad cannot contain an arc twice, either with the same or the opposite orientations.
\end{lem}
\begin{proof}
  If the facial walk of a quad contains two occurrences of an arc with the same orientation, then their identification creates a Möbius strip in the system of quads, in contradiction with its orientability. If the facial walk contains two consecutive occurrences of an arc with opposite orientations, then their common endpoint must have degree one in the system of quads. This contradicts the minimal degree 8 in our system of quads. Finally, if the facial walk contains non consecutive occurrences of an arc with opposite orientations, then their identification creates a cylinder bounded by loop edges. However, the radial graph being bipartite, this cannot occur.
\end{proof}
\begin{lem}\label{lem:no-common-index}
  Let $c$ be a primitive geodesic curve. If a forward index path $\Ipath{i}{k}$ and a backward index path $\Ipath{j}{-k}$ have homotopic image paths $c\Ipath{i}{k}\sim c\Ipath{j}{-k}$, then they cannot share any index.
\end{lem}
\begin{proof}
  Remark that we cannot have $i=j$ for otherwise $c\Ipath{j-k}{2k} = (c\Ipath{j}{-k})\inv.c\Ipath{i}{k} = 1$ in contradiction with Corollary~\ref{cor:no-monogon}. Likewise, we cannot have $i+k=j-k$. By way of contradiction, suppose that the index paths $\Ipath{i}{k}$ and $\Ipath{j}{-k}$ have a common index $i+r=j-t$ for some integers $0\leq r,t\leq k$. By the previous remark we have $0<\frac{r+t}{2}<k$. It follows that $(m,m)$ is an interior index pair of $(\Ipath{i}{k}, \Ipath{j}{-k})$, where
$m=i+\frac{r+t}{2} = j-\frac{r+t}{2}$ (recall that $i$ and $j$ have the same parity). Consider a disk diagram for $c\Ipath{i}{k}\sim c\Ipath{j}{-k}$. The index pair $(m,m)$ cannot correspond to the same vertex in the diagram since the path $c[i,j]=c[i,m].c[m,j]$ would then label a closed path in the diagram, hence be contractible. This would again contradict  Corollary~\ref{cor:no-monogon}. It ensues that $(m,m)$ corresponds to opposite vertices in a quad of the diagram. This quad is part of a staircase where the arc $c[m,m+1]$ labels two arcs on either sides of the partial diagram. Figure~\ref{fig:m-m-opposite-1} depicts the four possible configurations. 
\begin{figure}
  \centering
  \includesvg[\linewidth]{m-m-opposite-1}
  \caption{When the index pair $(m,m)$ corresponds to distinct vertices (the thick $m$ dots) in the diagram, the arc $c[m,m+1]$ labels two arcs that may be in one of four possible configurations. The whole diagram is only represented in the left case. The red strip projects to a Möbius strip in the system of quads.}
  \label{fig:m-m-opposite-1}
\end{figure}
In each case we infer that the system of quads would contain a Möbius strip, in contradiction with its orientability. 
\end{proof}
\begin{lem}\label{lem:ell-plus-un}
  Two distinct index paths of a non-trivial primitive geodesic curve $c$ having homotopic image paths have length distinct from $|c|$ and at most $|c|+1$.
\end{lem}
\begin{proof}
When the index paths have opposite orientations, the previous lemma directly implies the result. 
Let $\ell=|c|$ and let $\Ipath{i}{k},\Ipath{j}{k}$ be two index paths such that 
$c\Ipath{i}{k}\sim c\Ipath{j}{k}$. Clearly, $k\neq \ell$ since otherwise $c$ would be homotopic to a conjugate of itself by the shorter curve $c[i,j]$, in contradiction with the primitivity of $c$.
For the sake of a contradiction, suppose that $k>\ell+1$. Consider a diagram $\Delta$ with sides $\Delta_R$ and $\Delta_L$ for the homotopic paths $c\Ipath{i}{k}\sim c\Ipath{j}{k}$. By Lemma~\ref{lem:configurations} the vertices $\Delta_R(i+\ell)$ and $\Delta_L(j+\ell)$ must be diagonally opposite in a quad of $\Delta$. Using that $\Delta_R\Ipath{i}{2}$ and $\Delta_R\Ipath{i+\ell}{2}$ are labelled by the same edges and similarly for $\Delta_L\Ipath{j}{2}$ and $\Delta_L\Ipath{j+\ell}{2}$ we easily deduce that the system of quads has a quad with two occurrences of a same arc in its facial walk, or has a vertex of degree at most $5$, or is non-orientable. This would contradict Lemma~\ref{lem:facial-walk} or the hypotheses on the system of quads. In details, Figure~\ref{fig:lengthbigon} depicts the five possible configurations (a,b,c,d) for $\Delta_R\Ipath{i}{2}$  and $\Delta_L\Ipath{j}{2}$ in the diagram $\Delta$ and the eight possible configurations (A1-2, B1-3, C1-3) for $\Delta_R\Ipath{i+\ell}{2}$  and $\Delta_L\Ipath{j+\ell}{2}$.
  \begin{figure}
    \centering
    \includesvg[\textwidth]{lengthbigon}
    \caption{Upper row, the five possible configurations at the beginning of the disk diagram for $c\Ipath{i}{k}\sim c\Ipath{j}{k}$. Lower row, the eight possible configurations in the neighborhood of the index pair $(i,j)$ in this diagram.}
    \label{fig:lengthbigon}
  \end{figure}
 Remark that cases C1-3 are symmetric to cases B1-3, so that we only need to consider the five configurations A1-2, B1-3. We denote by $(\text{x,Y}) \in \{\text{a,b,c,d,e}\}\times\{\text{A1-2, B1-3}\}$ the conjunction of configurations $x$ and $Y$ in $\Delta$. The quads in configurations a,b,c,d are part of an initial staircase in $\Delta$ that may have a positive or negative orientation according to whether or not its orientation is consistent with some default orientation of the quad system and similarly for the final staircase containing $\Delta_R(i+\ell)$ and $\Delta_L(j+\ell)$. 
\begin{itemize}
\item 
If the orientations of the two staircases have the same sign, then the face to the right of $\Delta_L[j,j+1]$ has the same facial walk as the face to the right of $\Delta_L(j+\ell, j+\ell+1)$.  The induced identifications imply that for $(\text{x,Y})\in \{\text{a,b,c,d}\}\times\{\text{A1-2, B1-3}\}\cup \{\text{e}\}\times\{\text{A1-2,B1}\}$ a quad is bounded twice by the same arc (with or without the same orientation) in contradiction with Lemma~\ref{lem:facial-walk}. If $(\text{x,Y}) \in \{\text{e}\}\times\{\text{B2-3}\}$, we easily deduce that $c(i+1)=c(j+1)$ has degree four in the system of quads, again a contradiction. 
\item
When the initial and final staircases have opposite orientations the faces to the right of $\Delta_L[j,j+1]$ and to the right of $\Delta_L(j+\ell, j+\ell+1)$ (resp. to the left of $\Delta_R[i,i+1]$ and  to the left $\Delta_R[i+\ell,i++\ell+1]$) correspond to the two faces incident to $c[j,j+1]$ (resp. $c[i,i+1]$). We further split case $A2$ into two variants A$2'$ and A$2''$ according to whether the two quads (see Figure~\ref{fig:lengthbigon}) have consistent orientations or not. 
By the induced identifications we derive the following forbidden situations: two occurrences of a same arc in a quad for $(\text{x,Y}) \in \{\text{a,d,e}\}\times\{\text{A1, A}2''\}\cup \{\text{e}\}\times \{\text{B1}\}$, a degree two vertex (namely $c(j+1)$) for $(\text{x,Y}) \in \{\text{a,c}\}\times\{\text{B1,B3}\}$, a degree three vertex for $(\text{x,Y}) \in \{\text{b}\}\times\{\text{B1-3}\}\cup \{\text{a,c}\}\times\{\text{B2}\}\cup \{(\text{e,B1})\}$, a degree four vertex for $(\text{x,Y}) \in \{\text{b,c}\}\times\{\text{A1}\}\cup \{\text{e}\}\times\{\text{B2-3}\}\cup \{\text{(a,A$2'$),(d,B1)}\}$, a degree five vertex for $(\text{x,Y}) \in \{\text{b,c}\}\times\{\text{A}2'\}\cup \{\text{d}\}\times\{\text{B2-3}\}$, and finally a Möbius band for $(\text{x,Y}) \in \{\text{b,c}\}\times\{\text{A}2''\}$.
\end{itemize}
\end{proof}
When the initial and final staircases in the above proof have consistent orientations it is sufficient to use that $\Delta_R\Ipath{i}{1}$ and $\Delta_R\Ipath{i+\ell}{1}$ are labelled by the same edge, and similarly for $\Delta_L\Ipath{j}{1}$ and $\Delta_L\Ipath{j+\ell}{1}$, in order to reach a contradiction for cases $A1$ and $A2$. In other words, the two  homotopic paths
$c\Ipath{i}{k}\sim c\Ipath{j}{k}$ cannot have length $\ell+1$. This leads to the following refinement.
\begin{lem}\label{lem:short-bigon}
  A bigon $(\Ipath{i}{k}, \Ipath{j}{k})$ of an immersion of a primitive geodesic curve $c$ without intermediate crossings (i.e., no index pair $(i+r, j+r)$, $1<r<k$, is a combinatorial crossing) has length $k<|c|$.
\end{lem}

\begin{lem}\label{lem:flat-bigons}
  If two forward index paths of a canonical curve have homotopic image paths then they actually have the same image paths, i.e. they form a double path. In particular, bigons composed of two forward paths must be flat.\footnote{In a preliminary version of this work posted on arXiv the authors erroneously claimed an analogous property for the case of a bigon composed of a forward and a backward index paths.}
\end{lem}
\begin{proof}
  By Theorem~\ref{th:geodesic} the image paths bound a disk diagram composed of paths and staircase. Remark that a staircase with both sides directed forward must have a $\bar{1}$ turn on its left side. Since the curve is canonical we infer that the diagram cannot have any staircase, implying that the two sides coincide. 
\end{proof}

\section{The {\zipper} algorithm}\label{sec:simple-curves}
We now turn to the original problem of Poincar\'e~\cite[\S 4]{p-ccal-04}, deciding whether a given curve $c$ is homotopic to a simple curve. In the affirmative we know by Lemma~\ref{lem:swap} and Theorem~\ref{th:singular-bigon} that some geodesic homotopic to $c$ must have a (combinatorial) \define{embedding}, i.e. an immersion without crossings. Rather than swapping the sides of a singular bigon as in Lemma~\ref{lem:swap} we can choose to switch one side along the other side. This will also decrease the number of crossings if the bigon contains no other interior bigons. By considering interior-most bigons only, we can thus enforce a given edge of $c$ to stay fix as we remove crossings. This suggests an incremental computation of an embedding in which the image of the first arc occurrence is left unchanged: we assume that $c$ is canonical and consider the trivial embedding of its first arc occurrence $[0,1]$. We next insert the successive arc occurrences incrementally to obtain an embedding of the path formed by the already inserted arcs. When inserting the occurrence $[i,i+1]$ we need to compare its left-to-right order with each  already inserted arc occurrence $\beta$ of its supporting arc. If $\beta\neq [0,1]$ we can use the comparison of the occurrence $[i-1,i]$ with the occurrence $\gamma$ preceding $\beta$ (or succeeding $\beta$ if it is a backward occurrence). If $[i-1,i]$ and $\gamma$ have the same supporting arc, we just propagate their relative order to $[i,i+1]$ and $\beta$. Otherwise, we use the circular ordering of the supporting arcs of $[i-1,i]$, $\gamma$ and $[i,i+1]$ in order to conclude. When $\beta= [0,1]$, we cannot use the occurrence preceding $[0,1]$ as it is not yet inserted. We rather compare $[i,i+1]$ and $[0,1]$ as follows. In the Poincar\'e disk, we consider two lifts $\tilde{d}_i$ and $\tilde{d}_0$ of $c$ such that $\tilde{d}_i[i,i+1]=\tilde{d}_0[0,1]$. We decide to insert $[i,i+1]$ to the left (right) of  $[0,1]$ if one of the limit points of $\tilde{d}_i$ lies to the left (right) of $\tilde{d}_0$. Note that when $c$ is homotopic to a simple curve the two limit points of $\tilde{d}_i$ should lie on the same side of $\tilde{d}_0$.

After comparing $[i,i+1]$ with all the occurrences of its supporting arc, we can insert it in the correct place. If no crossings were introduced this way, we proceed with the next occurrence $[i+1,i+2]$. It may happen, however, that no matter how we insert  $[i,i+1]$ in the left-to-right order of its supporting arc, the resulting immersion of $\Ipath{0}{i+1}$ will have a combinatorial crossing. In order to handle this case, we first check if $[i,i+1]$ is \define{switchable}, i.e. if for some $k\geq 0$ and some turns $t,u$ the subpath $p:=c\Ipath{\Imath}{k+2}$ has turn sequence $t2^k1u$ and the index path  $\Ipath{\Imath}{k+2}$ does not contain the arc occurrence $[0,1]$. When $[i,i+1]$ is switchable we can switch $p$ to a new subpath $p'$ with turn sequence $(t-1)\bar{1}\bar{2}^{k}(u-1)$ such that  $p$ and $p'$ bound a diagram composed of a single horizontal staircase. 
See Figure~\ref{fig:switch-path}.
\begin{figure}
  \centering
  \includesvg[\textwidth]{switch-path}
  \caption{The arc $[i,i+1]$ is switchable.}
  \label{fig:switch-path}
\end{figure}
We indeed perform the switch if some intersection is actually avoided this way. More formally, this happens when there is a crossing $(i,j)$ such that the turn of $(c[j,j+1], c[i,i+1])$ or of $(c[j,j-1], c[i,i+1])$ is one as illustrated on Figure~\ref{fig:switch-path-1}.
\begin{figure}
  \centering
  \includesvg[\textwidth]{switch-path-1}
  \caption{A switch may avoid a crossing.}
  \label{fig:switch-path-1}
\end{figure}
Note that replacing $p$ by $p'$ leads to a curve $c'$ that is still geodesic and homotopic to $c$. Moreover, \emph{the part of $c'$ that remains to be embedded is in canonical form} as it contains no $\bar{1}$ turns. We then insert the arc occurrence $[i,i+1]$ and proceed with the algorithm using $c'$ in place of $c$. 
The successive switches in the course of the computation untangle  $c$ incrementally and we call our embedding procedure the \define{{\zipper} algorithm}. For further reference we make some observations that easily follows from the above switch procedure and the fact that $c$ is initially canonical.
\begin{remark}\label{rem:switchable}
  If $[i,i+1]$ is switchable but not switched at the time of being processed it remains true until the end of the algorithm, meaning that the vertex of index $i$ is directly followed by a subpath $p$ with a turn sequence of the form $2^k1$ ($k$ may get smaller if some other arc of $p$ is switched). Similarly, if $[i,i+1]$ is not switchable because of an inappropriate turn sequence, it remains true until the end of the algorithm.
\end{remark}
\begin{remark}\label{rem:01-right-side}
   Let $c'$ be a curve homotopic to $c$ obtained after a certain number of switches during the execution of the {\zipper} algorithm. Then $c'$ cannot contain a subpath with turn sequence $\bar{1}\bar{2}^*$ that ends at index $0$, nor can it contain a subpath with turn sequence $\bar{2}^*\bar{1}$ that starts at index $0$.
\end{remark}
\begin{lem}\label{lem:zipper-complexity}
  The {\zipper} algorithm applied to a canonical primitive curve $c$ of length $\ell$ can be implemented to run in $O(\ell\log^2\ell)$ time.
\end{lem}
\begin{proof}
We first traverse $c$ in reverse direction to mark all the switchable arcs. In the course of the algorithm,
each time a switchable arc triggers the switch of a subpath $p$ we unmark all the arcs in $p$ except the last one that may become switchable (depending on the last turn of $p$ and on the status of the arc following $p$). It easily follows that an arc can be switched at most twice and that the amortized cost for the switches is linear. Alternatively, we could use the run-length encoded turn sequence of $c$ (as defined in~\cite{ew-tcsr-13}) to detect each switchable arc  and update the turn sequence in constant time per switches.
In a preprocessing phase we also compute the relative order of $[0,1]$ with all the other occurrences of the same arc in $c$ as follows. If $c[i,i+1]=c[0,1]$, the corresponding arc occurrences form a double path of length one and we compute their relative order by extending maximally this double path in the backward direction. Looking at the tip of this double path, say $\Dpoint{j}{k}$, we can decide which side is to the left of the other. Indeed, the three arcs $c\Bocc{\Jmath}$, $c\Bocc{k}$ and $c\Focc{j}=c\Focc{k}$ must be pairwise distinct  and their circular order about their common origin vertex in the system of quads provides the necessary information as follows from Lemma~\ref{lem:flat-bigons}. 

The computation of the maximal extensions in the backward direction amounts to evaluate the longest common prefix of $c\inv$ with all its circular shifts. Overall, this can be done in $O(\ell)$ time thanks to a simple variation of the Knuth-Morris-Pratt algorithm. We next initialize an empty (balanced) binary search tree for every edge of the system of quads. Assuming a default orientation of each edge, its search tree should eventually contain the set of occurrences of the corresponding arc using the left-to-right order induced by the embedding. 

We now traverse $c$ in the forward direction starting with  $[0,1]$ and insert each traversed arc occurrence $\alpha=[i,i+1]$ in its tree. If the search tree is empty, we just insert $\alpha$ at the root. Otherwise, either $\alpha$ is switchable and needs to be switched to a new arc $a$, or it should be inserted into a non-empty search tree. The former case can be  easily detected in $O(\log\ell)$ time using a dichotomy over the occurrences of $a$: \emph{$\alpha$ needs to be switched when 
one of these occurrences defines a crossing with $\alpha$ at their common endpoint}. In the latter case, we perform the usual tree insertion: each time $\alpha$ must be compared with some already inserted arc occurrence $\beta\neq [0,1]$ we can use the comparison of the occurrence preceding $\alpha$ with the occurrence preceding $\beta$ (or succeeding $\beta$ if it is a backward occurrence). This takes $O(\log\ell)$ time. When $\beta=[0,1]$ we can use our  precomputed comparisons unless $\alpha$ was previously switched, thus not compared with $[0,1]$ during the preprocessing phase. Since $c$ is canonical, arcs may only be switched to their left in one of four possible configurations as on Figure~\ref{fig:under_a0} 
\begin{figure}
  \centering
  \includesvg[.8\textwidth]{under_a0}
  \caption{Top: the arc $c(\alpha)$ may be in one of four possible configurations with respect to the first arc $c[01]$. Middle and bottom row: from its initial canonical position, $\alpha$ is switched to the same arc as $[0,1]$.}
  \label{fig:under_a0}
\end{figure}
and we infer that $\alpha$ must lie to the right of $[0,1]$ as justified by Lemma~\ref{lem:relative-pos} below.  In conclusion, each comparison costs $O(\log\ell)$ time so that $\alpha$ can be inserted in its search tree in $O(\log^2\ell)$ time.

Note that after $\alpha$ is inserted we do not try to determine if the current immersion has a crossing or not. This will be checked in a second  step after the {\zipper} algorithm is completed. 
\end{proof}
\begin{lem}\label{lem:relative-pos}
  Let $\tilde{c}$ and $\tilde{d}$ be two lifts of a primitive canonical geodesic in the Poincar\'e disk $\D$. The limit points of $\tilde{c}$ cut the boundary circle $\partial \D$ into two pieces. By the \emph{right piece}, we mean the piece of $\partial \D$ that bounds the part of $\D\setminus \tilde{c}$ to the right of $c$. Suppose that $\tilde{c}$ contains an arc $a$ and that $\tilde{d}$ contains an arc $b$ such that $a$ and $b$ are in one of the four relative positions depicted on Figure~\ref{fig:relative-pos}. Then one of the limit points of $\tilde{d}$ is in the right piece of $\partial \D$.
\end{lem}
\begin{figure}
  \centering
  \includesvg[.7\linewidth]{relative-pos}
  \caption{The relative positions of $a$ and $b$.}
  \label{fig:relative-pos}
\end{figure}
\begin{proof}
  We first note that $b$ itself lies to the right of $\tilde{c}$. Indeed, $\tilde{c}$ would have to use a $\bar{1}$ turn to see $b$ on its left or to pass along $b$, in contradiction with its canonicity. If both limit points of $\tilde{d}$ were to the left of $\tilde{c}$, then  $\tilde{c}$ and $\tilde{d}$ would form a bigon with one staircase part containing $b$. The $\tilde{c}$ side of this staircase would thus see $b$ on its right, which is impossible since $\tilde{c}$ is canonical (here, we have not used that $\tilde{d}$ is canonical but only geodesic).
\end{proof}

\begin{prop}\label{prop:zipper}
  If $i(c)=0$ the {\zipper} algorithm returns an embedding of a geodesic homotopic to $c$.
\end{prop}
\begin{proof}
  Let $\mathcal I$ be the immersion computed by the {\zipper} algorithm. We denote by $c_k$ the geodesic homotopic to $c$ resulting from the first switches in the algorithm up to the insertion of the arc occurrence $[k,k+1]$. Note that $k>r$ implies $c_k\Ipath{0}{r+1}=c_r\Ipath{0}{r+1}$.
Suppose that $\mathcal I$ has a crossing. For the rest of this proof, we denote by $i$ the smallest index such that the insertion of $[i,i+1]$ creates a crossing, i.e. the restriction of $\mathcal I$ to $\Ipath{0}{i+1}$ has a crossing double point while its restriction to $\Ipath{0}{i}$ is an embedding. By convention we set $i=\ell$ if the crossing appeared after the last arc insertion. We shall show that $c_{\ell-1}$ has two lifts whose limit points are alternating on the boundary of the Poincar\'e disk. It will follow from Section~\ref{sec:strategy}  that $i(c)=i(c_{\ell-1})>0$ thus proving the Proposition. We first establish some preparatory claims.
  \begin{claim}\label{claim:branched-crossing}
    If $(i,j)$ is a crossing of $\mathcal I$ with $0<j<i$, then the backward arc $c_i[j,j-1]$ and the forward arc $c_i[j,j+1]$ are distinct from the supporting arc $c_i[i,i+1]$ of $[i,i+1]$. Moreover, if $[i,i+1]$ was switched just before its insertion, then $c_i[j,j-1]$ and $c_i[j,j+1]$ are also distinct from the supporting arc $c_{i-1}[i,i+1]$ of $[i,i+1]$ \emph{just before its switch}.
  \end{claim}
  \begin{proof}
    The first part of the claim follows directly from our insertion procedure and the fact that the restriction of $\mathcal I$ to $\Ipath{0}{i}$ is an embedding. 
Moreover, assume that $[i,i+1]$ was switched just before its insertion.
By the insertion procedure, this means that the insertion of $[i,i+1]$ before the switch would have induced a crossing 
$(i,k)$ where $0<k<i$ and $c_{i-1}[k,k+1], c_{i-1}[k,k-1] \neq c_{i-1}[i,i+1]$ by the first part of the claim. By the same first part we have $c_i[j,j-1], c_i[j,j+1]\neq c_i[i,i+1]$. Since the restriction of $\mathcal I$ to $c_i\Ipath{0}{i}$ has no crossing, the length 2 path $c_{i-1}\Ipath{k-1}{2}=c_{i}\Ipath{k-1}{2}$ separates $c_{i}\Ipath{j-1}{2}$ from $c_{i-1}[i,i+1]$, implying $c_i[j,j-1], c_i[j,j+1]\neq c_{i-1}[i,i+1]$ as desired.
  \end{proof}
  \begin{claim}\label{claim:parallel-right}
    If $c_i$ has a $\bar{1}$ turn at index $k+1$, with $0<k< i$, then there is an index $r$ with $0< r<k$ such that
    \begin{itemize}
    \item $c_{k}[k,k+1]=c_{k}[r,r-1]$,
    \item the arc $c_i[r,r-1]$ lies to the right of $c_i[k,k+1]$,
\item $c_{k}[r,r-1]\neq c_{k-1}[k,k+1]$.
    \end{itemize}
  \end{claim}
  \begin{proof}
A $\bar{1}$ turn can only occur at the destination of an arc that has been switched and such that the next arc was not switched (otherwise, we would get a $\bar{2}$ turn). If $c_i$ has a $\bar{1}$ turn at $k+1$ then so has $c_{k}$ since the algorithm never backtracks. It follows that the arc $c_{k}[k,k+1]$ must have been switched just before its insertion, thus witnessing the existence of an arc $a$ of the form $c_{k}[u,u-1]$ or $c_{k}[u,u+1]$, with $0<u<k$, that lies parallel to and to the right of $c_{k}[k,k+1]$ (with respect to $\mathcal I$). Moreover, according to Claim~\ref{claim:branched-crossing} we can also assume $c_{k}[u,u-1]$ and $c_{k}[u,u+1]$ to be distinct from the supporting arc of $[k,k+1]$ just before it was switched, namely $c_{k-1}[k,k+1]$. In the case $a=c_{k}[u,u-1]$, we may set $r=u$ and conclude. 
Otherwise, $a=c_{k}[u,u+1]$ and, 
recalling that the restriction of $\mathcal I$ to $\Ipath{0}{k+1}$ has no crossing, it must be that $c_{k}\Ipath{u}{2}$ also lies parallel to and to the right of $c_{k}\Ipath{k}{2}$.  So $c_i$ has a $\bar{1}$ turn at index $u+1$, and we are back to the hypothesis of the claim, decreasing the value of $k$ to $u$. We can repeat the same arguments inductively, each time decreasing the value of $k$ in the claim. Since $k>1$, the process must stop, implying that we have reached the former case.
 \end{proof}

Recall that $i$ is the smallest index such that the restriction of $\mathcal I$ to $\Ipath{0}{i+1}$ has a crossing. We denote by $c'=c_{\ell-1}$ the geodesic homotopic to $c$ resulting from all the switches in the course of the algorithm execution.  Let $(i,j)$ be a crossing of $\mathcal I$ with $0<j<i$. 
We consider two lifts $\tilde{d_i}$ and $\tilde{d}_j$ of $c'$ in the Poincar\'e disk such that $\tilde{d}_i(i) = \tilde{d}_j(j)$. We first suppose $i<\ell$. 
\begin{claim}\label{claim:no-crossing-after-i}
  We can choose $j$ so that $\tilde{d}_i\Ipath{i}{+\infty}$ has no crossing with $\tilde{d}_j$. (Crossings are defined with respect to the lift of $\mathcal I$ in the Poincaré disk.)
\end{claim}
\begin{proof}
  Fix any $j$ such that $(i,j)$ is a crossing of $\mathcal I$ ($0<j<i$) and suppose that $\tilde{d}_i\Ipath{i}{+\infty}$ and $\tilde{d}_j$ cross. Let $s$ be the smallest positive integer such that $\tilde{d}_i$ and $\tilde{d}_j$ crosses at $\tilde{d}_i(i+s)$. We thus have a bigon of the form $(\Ipath{i}{s},\Ipath{j}{\varepsilon s})$ for some $\varepsilon\in \{-1,1\}$. Moreover, this bigon has no intermediate crossings by the choice of $s$. Let $\Delta$ be a disk diagram for this bigon, oriented consistently with the system of quads.  $\Delta$ must start with a staircase part by Claim~\ref{claim:branched-crossing}. In particular, the turn $t$ between $c'[j,j+\varepsilon]$ and $c'[i,i+1]$ should be $\pm 1$.
The {\zipper} algorithm may have run across four possible situations at step $i$.
\begin{enumerate}
\item\label{enum:1} Either $[i,i+1]$ was switchable just before its insertion but was not switched, 
\item \label{enum:2} or it was switchable and switched, 
\item \label{enum:3} or it was not switchable because of an inappropriate turn sequence, 
\item \label{enum:4} or it was not switchable because the part to be switched contains $[0,1]$. 
\end{enumerate}
In the first situation, we know by Remark~\ref{rem:switchable} that $c'(i)$ is followed by a turn sequence of the form $2^*1$. Hence, $t$ is exactly $1$; but this contradicts the fact that $[i,i+1]$ was not switched though switchable. The third situation together with Remark~\ref{rem:switchable} also lead to a contradiction as the inappropriate turn sequence prevents $c\Ipath{i}{s}$ from being part of any staircase. Thanks to Claim~\ref{claim:branched-crossing}, the second situation equally prevents $c\Ipath{i}{s}$ from being part of any staircase. It remains to consider the fourth situation. We first suppose $\varepsilon=-1$, i.e. that the diagram $\Delta$ corresponds to the bigon $(\Ipath{i}{s},\Ipath{j}{-s})$. By Lemma~\ref{lem:short-bigon}, we have $s<\ell$ and the fourth situation implies that the $\Ipath{i}{s}$ side of the bigon contains $[0,1]$. These two properties imply that $0<i+s-\ell<i$. It ensues that the $\Ipath{j}{-s}$ side contains index $i$, for otherwise $(i+s,j-s)$ would be a crossing occurring before step $i$. However, the occurrence of $i$ on both sides of the bigon contradicts Lemma~\ref{lem:no-common-index}. Hence, it must be that $\varepsilon=1$, i.e. that $\Delta$ is bounded by two forward paths.
Since the $\Ipath{i}{s}$ side of $\Delta$ contains $[0,1]$ and since $i\neq \ell$, it ensues from Remark~\ref{rem:01-right-side} that $\mathcal I$ has a $\bar{1}$ turn at index $j+1$. Claim~\ref{claim:parallel-right} ensures the existence of a smaller index $r$ such that $c_{j}[j,j+1]=c_{j}[r,r-1]$ and $c_{j}[r,r-1]\neq c_{j-1}[j,j+1]$. Since $c_{j}[j,j+1]$ makes a one turn with both $c_{j-1}[j,j+1]$ and $c'[i,i+1]$, these last arcs are equal and, since $(j,r)$ is not a crossing, $(i,r)$ must be a crossing of $\mathcal I$. We replace $j$ by $r$ to obtain another lift $\tilde{d}_r$ that crosses $\tilde{d}_i$ at the same point $\tilde{d}_i(i)=\tilde{d}_r(r)$ with the additional property that $\tilde{d}_r[r,r-1]$ and $\tilde{d}_i[i,i+1]$ make a one turn. We finally observe that $\tilde{d}_r$ satisfies the claimed property. Indeed, the first staircase of $\Delta$ must lie to the left of its $\Ipath{i}{s}$ side by Remark~\ref{rem:01-right-side}, hence the other side of $\Delta$ must start with $[r,r-1]$. This is however impossible by the preceding argument for $\varepsilon=-1$. It follows that $\tilde{d}_i\Ipath{i}{+\infty}$ and $\tilde{d}_r$ have no crossing.
\end{proof}
We next consider the smallest positive integer $r$ such that $\tilde{d}_i$ and $\tilde{d}_j$ crosses at $\tilde{d}_i(i-r)$. If no such $r$ exists, then by the above Claim~\ref{claim:no-crossing-after-i}, $\tilde{d}_j$ and $\tilde{d}_i$ have a unique intersection point and we may conclude that their limit points alternate. We can thus assume the existence of a bigon  $(\Ipath{i-r}{r},\Ipath{j-\varepsilon r}{\varepsilon r})$ with $r>0$ and $\varepsilon\in\{-1,1\}$, and without intermediate crossings.
We first examine the case $\varepsilon=-1$ where the bigon $(\Ipath{i-r}{r},\Ipath{j+ r}{-r})$ has oppositely oriented index paths. We must have $r\geq i-j$ for otherwise the tip $(i-r,j+r)$ of the bigon would define a crossing occurring before step $i$, contradicting the choice of $i$. Hence, $\Ipath{i-r}{r}$ contains $j$. This is however impossible by Lemma~\ref{lem:no-common-index}.
We now look at the case of two forwards index paths ($\varepsilon=1$). 
We must have $r\geq j$ for otherwise the tip $(i-r,j-r)$ of the bigon would define a crossing occurring before step $i$, again contradicting the choice of $i$. It follows that $\Ipath{j-r}{r}$ contains $[0,1]$ and that the forward branches $\tilde{d}_j\Ipath{i-j}{+\infty}$ and $\tilde{d}_i\Ipath{0}{+\infty}$ have a unique intersection point. The bigon labels a disk diagram $\Delta$ composed of paths and staircases as described in Theorem~\ref{th:geodesic}. 
\begin{itemize}
\item 
If $[0,1]$ and $\alpha:= [i-j,i-j+1]$ label the same arc of a path part in $\Delta$ there are two possibilities: either  it holds initially that $c[0,1]=c(\alpha)$ or $\alpha$ was switched in the course of the algorithm. In the former case, we know by the preprocessing phase and Lemma~\ref{lem:flat-bigons} that the left-to-right order of $[0,1]$ and $\alpha$ is coherent with its extension in the backward direction. This implies that ultimately in the backward direction $\tilde{d}_i$  lies on the same side of $\tilde{d}_j$ as does $\alpha$. In the latter case, as described in the end of the proof of Lemma~\ref{lem:zipper-complexity}, we know by the insertion procedure that at least one of the limit points of $\tilde{d}_i$  lies on the same side of $\tilde{d}_j$ as does $\alpha$. 
Since $(i,j)$ is the only crossing in the forward direction, we conclude in both cases that $\tilde{d}_j$  and $\tilde{d}_i$ have alternating limit points. 
\item Otherwise, $[0,1]$ and $\alpha$ label two distinct arcs, say $a_0$ and $a_{i-j}$, of a staircase part $\sigma$ of $\Delta$.  Let $[i-v,i-u]$ and  $[j-v,j-u]$, $0\leq u<v\leq r$ be the index paths corresponding to the sides $\sigma_L$ and $\sigma_R$ of $\sigma$. By Remark~\ref{rem:01-right-side}, $[j-u,j-v]$ must label the right side $\sigma_R$ while  $[i-v,i-u]$ cannot contain $[0,1]$. It follows that $v<i$, whence $[i-v,i-u]\subset [1,i]$. If $a_{i-j}$ belongs to a horizontal part of $\sigma_L$, the first vertex in this  part has a $\bar{1}$ turn and we let $m$ be the index of this vertex. Otherwise, $a_{i-j}$ belongs to a vertical part whose last vertex has a $\bar{1}$ turn. It follows that the vertex of index
$i-j+1$ had a $\bar{1}$ turn at step $i-j$ of the algorithm and we set $m=i-j+1$. By Claim~\ref{claim:parallel-right}, there is an arc occurrence $[x,x-1]$, with $x< m-2$, that lies to the right of $a_{i-j}$ and such that the turn at $x+1$ is not one. We view $\Delta$ as a subset of the Poincaré disk so that $\Delta_L$ and  $\Delta_R$ can be seen as portions of $\tilde{d}_i$ and $\tilde{d}_j$ respectively. Let $\tilde{q}$ be the lift of $c'$ that extends the above occurrence $[x,x-1]$ in $\D$. See Figure~\ref{fig:push-1}. 
  \begin{figure}
    \centering
    \includesvg[\textwidth]{push-1}
    \caption{$a_{i-j}$ may belong to a horizontal (left figure) or vertical (right figure) part of $\sigma_L$.}
    \label{fig:push-1}
  \end{figure}
We denote by ${q}_+:=\tilde{q}\Ipath{x}{+\infty}$ and ${q}_-:=\tilde{q}\Ipath{x}{-\infty}$ the portion of $\tilde{q}$ respectively after and before its vertex with index $x$.

We claim that $q_+$ cannot cross $\tilde{d}_i$.
Otherwise, we would get a pair of homotopic paths, one piece of $q_+$ starting at index $x$ and one piece of 
$\tilde{d}_i$ ending at index $m$, with opposite orientations. By Lemma~\ref{lem:no-common-index}, the $q_+$ piece would not contain index $m$ and would thus have length at most $m-x < m$. In turn, this would imply that the $\tilde{d}_i$ piece would only contain vertices with indices in $[1,m]$. The crossing of $q_+$ and $\tilde{d}_i$ would thus occur before step $i$, a contradiction.

We next claim that $q_-$ cannot cross $\tilde{d}_j$. 
To see this, first note that the index path corresponding to the part of $q_-$ inside $\Delta$ must contain $0$. Indeed, $q_-$ must cross $\Delta_L$ or  the part of $\Delta_R$ after $a_0$ and, in any case, the index along $q_-$ of the corresponding crossing should be at least $i$, since no crossing occurs before step $i$.

It follows from the above claims that $\tilde{q}$ and $\tilde{d}_i$ have alternating limit points. See Figure~\ref{fig:push-2}.
  \begin{figure}
    \centering
    \includesvg[.6\textwidth]{push-2}
    \caption{$q_+$ cannot cross $\tilde{d}_i$, $q_-$ cannot cross $\tilde{d}_j$, and $\tilde{d}_j$ cannot cross $\tilde{d}_i\Ipath{i}{+\infty}$.}
    \label{fig:push-2}
  \end{figure}
\end{itemize}

It remains to consider the case $i=\ell$ where the first crossing appears after the last arc insertion. Since all the arcs have been inserted without introducing crossings it means that the crossings of the computed immersion $\mathcal I$  must have the form $(0,j)$. We first claim that each bigon of $\mathcal I$ must have its two sides oriented the same way. Otherwise, the tips of the bigon must have the form $(0,j)$ and $(j,0)$ for some index $j\neq 0$, implying that $c_\ell\Ipath{0}{\pm j}$ is contractible. This would contradict Corollary~\ref{cor:no-monogon}. 
Let $\tilde{d}_\ell$ and $\tilde{d}_j$ be two intersecting lifts of $c'$ in the Poincar\'e disk. If  $\tilde{d}_\ell$ and $\tilde{d}_j$ intersect only once, then we are done as they must have alternating limit points. We now suppose that $\tilde{d}_\ell$ and $\tilde{d}_j$ intersect at least twice and we consider the bigon $\Delta$ between their last two intersections in the forward direction. 
By Lemma~\ref{lem:short-bigon}, the length of $\Delta$ is smaller than $\ell$. Let $(0,j)$ and $(\ell-j,0)$ be the tips of $\Delta$, so that $\tilde{d}_\ell\Ipath{0}{j}\sim \tilde{d}_j\Ipath{\ell-j}{j}$. 
\begin{itemize}
\item 
If $[0,1]$ and $\alpha=[\ell-j,\ell-j+1]$ label the same arc $\tilde{d}_\ell[0,1] = \tilde{d}_j(\alpha)$ then, by the insertion procedure, one of the limit points of $\tilde{d}_\ell$ is on the same side of $\tilde{d}_j$ as does $\alpha$. The same argument as in the general case $i<\ell$ allows to conclude that $\tilde{d}_\ell$ and $\tilde{d}_j$ have alternating limit points. 
\item
We finally suppose that $[0,1]$ and $\alpha$ label distinct arcs, say $a_0$ and $a_{\ell-j}$, of a staircase part $\sigma$ of $\Delta$. 
As in the general case $i<\ell$,  $a_0$ must see $\sigma$ on its left, while $a_{\ell-j}$ must see $\sigma$ on its right. Hence, the $\tilde{d}_j$ side of $\sigma$ is canonical while the side along $\tilde{d}_\ell$ is not and must have been switched. By Claim~\ref{claim:parallel-right}, there is an arc occurrence $\beta$ to the right of $\tilde{d}_\ell[\ell-j,\ell-j+1]$ with the opposite orientation. Let $\tilde{q}$ be the lift of $c'$ that extends $\beta$. As in the general case $i<\ell$, we can show that the part of $\tilde{q}$ after $\beta$ cannot cross $\tilde{d}_\ell$, while the part before $\beta$ cannot cross $\tilde{d}_j$. Using that $(0,j)$ is the last crossing along $\tilde{d}_\ell$ and $\tilde{d}_j$, we equally conclude that $\tilde{d}_\ell$ and $\tilde{q}$ have alternating limit points. See Figure~\ref{fig:push-2} with $i=\ell$.
\end{itemize}
\end{proof}

\begin{proof}[Proof of Theorem~\ref{th:simple-curve}]
  Let $c$ be a combinatorial curve of length $\ell$ on a combinatorial surface of size $n$. We compute its canonical form in $O(n+\ell)$ time and check in linear time that $c$ is primitive. In the negative, we conclude that either $c$ is contractible, hence reduced to a vertex, or that  $c$ has no embedding by Proposition~\ref{prop:non-primitive-formulas}. In the affirmative, we apply the {\zipper} algorithm to compute an immersion $\mathcal I$ of some geodesic $c'$ homotopic to $c$. According to Proposition~\ref{prop:zipper}, we have $i(c)=0$ if and only if $\mathcal I$ has no crossings. This is easily verified in $O(n+\ell)$ time by checking for each vertex $v$ of the system of quads that the set of paired arc occurrences with $v$ as middle vertex form a well-parenthesized sequence with respect to the local ordering $\prec_v$ induced by $\mathcal I$. We conclude the proof thanks to  Lemma~\ref{lem:zipper-complexity}.
\end{proof}

A related problem was tackled by Chang \emph{et al.}~\cite[Th. 8.2]{cex-dwsp-15} who try to find an embedding of a given closed path on a combinatorial surface. In their formulation, though, the path is not authorized to be modified. In our terminology, they only look for the existence of a combinatorial immersion without crossings. They suggest a linear time complexity for this problem and it seems likely that we could also eliminate the $\log^2\ell$ factor in our complexity.

\section{Concluding remarks}
The existence of a singular bigon claimed in Theorem~\ref{th:singular-bigon} relies on Theorem 4.2 of Hass and Scott~\cite{hs-ics-85}. As noted by the authors themselves this result is ``surprisingly difficult to prove''. Except for this result and the recourse to some hyperbolic geometry in the general strategy of Section~\ref{sec:strategy}
our combinatorial framework allows to provide simple algorithms and to give simple proofs of results whose known demonstrations are rather involved. 
Concerning Proposition~\ref{prop:bigon}, the existence of an immersion without bigon could be achieved in our combinatorial viewpoint by showing that if an immersion has bigons, then one of them can be swapped to reduce the number of crossings. (The example in Figure~\ref{fig:no-singular-bigon} shows that such a bigon need not be singular.) What is more, if those swappable bigons could be found easily this would provide an algorithm to compute a minimally crossing immersion of two curves by iteratively swapping bigons as in Section~\ref{sec:computing-immersions} for the case of a single curve. However, we were unable to show the existence or even an appropriate definition of a swappable bigon. Note that in the analogous approaches using Reidemeister-like moves by de Graff and Schrijver~\cite{gs-mcmcr-97} or by Paterson~\cite{p-cails-02}, the number of moves required to reach a minimal configuration is unknown. Comparatively, the number of bigon swapping would be just half the excess crossing of a given immersion.

Although the geometric intersection number of a combinatorial curve of length $\ell$ may be $\Omega(\ell^2)$, it is not clear that the complexity in Theorem~\ref{th:main-result} is optimal. In particular, it would be interesting to see if the {\zipper} algorithm of Section~\ref{sec:simple-curves} yields minimally crossing curves even with curves that are not homotopic to simple curves, thus improving Theorem~\ref{th:compute-immersion}. This would lead to an algorithm that is faster to find an immersion with the minimal number of crossings than to actually count them! It is also tempting to check whether the {\zipper} algorithm applies to compute the geometric intersection number of two curves rather than a single curve.
Another intriguing question related to the computation of minimally crossing immersions comes from the fact that they are not unique. Given a combinatorial immersion we can construct a continuous realization as described in the proof of Lemma~\ref{lem:geometric-vs-combinatorial}. Say that two realizations are in the \define{same configuration} if there is an ambient isotopy of the surface where they live that brings one realization to the other. It was shown by Neumann-Coto~\cite{n-csgs-01} that every minimally crossing immersion is in the configuration of shortest geodesics for some Riemaniann metric $\mu$, but Hass and Scott~\cite{hs-ccgs-99} gave counterexamples to the fact that we could always choose $\mu$ to be hyperbolic. \\
\textit{Is there an algorithm to construct or detect combinatorial immersions that have a realization in the configuration of geodesics for some hyperbolic metric?}\\

\bibliographystyle{alpha}
% \bibliography{biblio}
\begin{thebibliography}{CdVL05}

\bibitem[BS84]{bs-ascs-84}
Joan~S. Birman and Caroline Series.
\newblock An algorithm for simple curves on surfaces.
\newblock {\em J. London Math. Soc.}, 29(2):331--342, 1984.

\bibitem[CdVL05]{cl-oslos-05}
{\'E}ric Colin~de Verdi\`ere and Francis Lazarus.
\newblock Optimal {S}ystem of {L}oops on an {O}rientable {S}urface.
\newblock {\em Discrete \& {C}omputational {G}eometry}, 33(3):507 -- 534, March
  2005.

\bibitem[CEX15]{cex-dwsp-15}
Hsien-Chih Chang, Jeff Erickson, and Chao Xu.
\newblock Detecting weakly simple polygons.
\newblock In {\em Proc. 26th Ann. ACM-SIAM Symp. Discrete Algorithms}, pages
  1655--1670, 2015.

\bibitem[Cha14]{c-sinle-14}
Moira Chas.
\newblock Self-intersection numbers of length-equivalent curves on surfaces.
\newblock {\em Experimental Mathematics}, 23(3):271--276, 2014.

\bibitem[Chi69]{c-sccs-69}
David~R.J. Chillingworth.
\newblock Simple closed curves on surfaces.
\newblock {\em Bulletin of the London Mathematical Society}, 1(3):310--314,
  1969.

\bibitem[Chi71]{c-afdsc-71}
David~R.J. Chillingworth.
\newblock An algorithm for families of disjoint simple closed curves on
  surfaces.
\newblock {\em Bulletin of the London Mathematical Society}, 3(1):23--26, 1971.

\bibitem[Chi72]{c-wns2-72}
David~R.J. Chillingworth.
\newblock Winding numbers on surfaces. {II}.
\newblock {\em Mathematische Annalen}, 199(3):131--153, 1972.

\bibitem[CL87]{cl-pggin-87}
Marshall Cohen and Martin Lustig.
\newblock Paths of geodesics and geometric intersection numbers: {I}.
\newblock In {\em Combinatorial group theory and topology}, volume 111 of {\em
  Ann. of Math. Stud.}, pages 479--500. Princeton Univ. Press, 1987.

\bibitem[CL12]{cl-sicts-12}
Moira Chas and Steven~P Lalley.
\newblock Self-intersections in combinatorial topology: statistical structure.
\newblock {\em Inventiones mathematicae}, 188(2):429--463, 2012.

\bibitem[DG99]{dg-tcs-99}
Tamal~K. Dey and Sumanta Guha.
\newblock Transforming {C}urves on {S}urfaces.
\newblock {\em Journal of {C}omputer and {S}ystem {S}ciences}, 58(2):297--325,
  1999.

\bibitem[dGS97]{gs-mcmcr-97}
Maurits de~Graaf and Alexander Schrijver.
\newblock Making curves minimally crossing by {R}eidemeister moves.
\newblock {\em Journal of {C}ombinatorial {T}heory, {S}eries {B}},
  70(1):134--156, 1997.

\bibitem[DLH10]{l-ttgpd-10}
Pierre De~La~Harpe.
\newblock Topologie, th{\'e}orie des groupes et probl{\`e}mes de d{\'e}cision:
  c{\'e}l{\'e}bration d'un article de max dehn de 1910.
\newblock {\em Gazette des math{\'e}maticiens}, 125:41--75, 2010.

\bibitem[EW13]{ew-tcsr-13}
Jeff Erickson and Kim Whittelsey.
\newblock Transforming curves on surfaces redux.
\newblock In {\em Proc. 24th Ann. ACM-SIAM Symp. Discrete Algorithms}, 2013.

\bibitem[FM12]{fm-pmcg-12}
Benson Farb and Dan Margalit.
\newblock {\em A primer on mapping class groups}.
\newblock Princeton university Press, 2012.

\bibitem[GKZ05]{gkz-amnip-05}
Daciberg~L. Gon\c{c}alves, Elena Kudryavtseva, and Heiner Zieschang.
\newblock An algorithm for minimal number of (self-)intersection points of
  curves on surfaces.
\newblock In {\em Proceedings of the Seminar on Vector and Tensor Analysis},
  volume~26, pages 139--167, 2005.

\bibitem[GS90]{gs-sctag-90}
Steve~M. Gersten and Hamish~B. Short.
\newblock Small cancellation theory and automatic groups.
\newblock {\em Inventiones mathematicae}, 102:305--334, 1990.

\bibitem[HS85]{hs-ics-85}
Joel Hass and Peter Scott.
\newblock Intersections of curves on surfaces.
\newblock {\em Israel {J}ournal of {M}athematics}, 51(1-2):90--120, 1985.

\bibitem[HS94]{hs-scs-94}
Joel Hass and Peter Scott.
\newblock Shortening curves on surfaces.
\newblock {\em Topology}, 33(1):25--43, 1994.

\bibitem[HS99]{hs-ccgs-99}
Joel Hass and Peter Scott.
\newblock Configurations of curves and geodesics on surfaces.
\newblock {\em Geometry and Topology Monographs}, 2:201--213, 1999.

\bibitem[KMP77]{kmp-fpms-77}
Donald~E. Knuth, James~H. Morris, Jr, and Vaughan~R. Pratt.
\newblock Fast pattern matching in strings.
\newblock {\em SIAM journal on computing}, 6(2):323--350, 1977.

\bibitem[Lot97]{l-cw-97}
M.~Lothaire.
\newblock {\em Combinatorics on words}.
\newblock Cambridge University Press, 1997.

\bibitem[LR12]{lr-hts-12}
Francis Lazarus and Julien Rivaud.
\newblock On the homotopy test on surfaces.
\newblock In {\em Proc. 53rd Ann. IEEE Symp. Foundations of Computer Science},
  pages 440--449, 2012.

\bibitem[Lus87]{l-pggin2-87}
Martin Lustig.
\newblock Paths of geodesics and geometric intersection numbers: {II}.
\newblock In {\em Combinatorial group theory and topology}, volume 111 of {\em
  Ann. of Math. Stud.}, pages 501--543. Princeton Univ. Press, 1987.

\bibitem[Mir08]{m-gnscg-08}
Maryam Mirzakhani.
\newblock Growth of the number of simple closed geodesies on hyperbolic
  surfaces.
\newblock {\em Annals of Mathematics}, pages 97--125, 2008.

\bibitem[Mir16]{m-cmcgo-16}
Maryam Mirzakhani.
\newblock Counting mapping class group orbits on hyperbolic surfaces.
\newblock Preprint \href{http://arxiv.org/pdf/1601.03342}{arxiv:1601.03342},
  January 2016.

\bibitem[MT01]{mt-gs-01}
Bojan Mohar and Carsten Thomassen.
\newblock {\em Graphs on Surfaces}.
\newblock Studies in the Mathematical Sciences. Johns Hopkins University Press,
  2001.

\bibitem[NC01]{n-csgs-01}
Max Neumann-Coto.
\newblock A characterization of shortest geodesics on surfaces.
\newblock {\em Algebraic and {G}eometric {T}opology}, 1:349--368, 2001.

\bibitem[Pat02]{p-cails-02}
J.M. Paterson.
\newblock A combinatorial algorithm for immersed loops in surfaces.
\newblock {\em Topology and its Applications}, 123(2):205--234, 2002.

\bibitem[Poi04]{p-ccal-04}
Henri Poincar{\'e}.
\newblock Cinqui{\`e}me compl{\'e}ment {\`a} l'analysis situs.
\newblock {\em Rendiconti del Circolo Matematico di Palermo}, 18(1):45 --110,
  1904.

\bibitem[Rei62]{r-ajccs-62}
Bruce~L. Reinhart.
\newblock Algorithms for jordan curves on compact surfaces.
\newblock {\em Annals of Mathematics}, pages 209--222, 1962.

\bibitem[Sap15]{s-bnnsc-15}
Jenya Sapir.
\newblock Bounds on the number of non-simple closed geodesics on a surface.
\newblock Preprint \href{http://arxiv.org/pdf/1505.07171}{arxiv:1505.07171},
  May 2015.

\bibitem[Tur79]{t-iltdm-79}
Vladimir~G. Turaev.
\newblock Intersections of loops in two-dimensional manifolds.
\newblock {\em Mathematics of the USSR-Sbornik}, 35(2):229, 1979.

\bibitem[Zie65]{z-aekf-65}
Heiner Zieschang.
\newblock Algorithmen f{\"u}r einfache kurven auf fl{\"a}chen.
\newblock {\em Mathematica Scandinavica}, 17:17--40, 1965.

\bibitem[Zie69]{z-aekf2-69}
Heiner Zieschang.
\newblock Algorithmen f{\"u}r einfache kurven auf fl{\"a}chen {II}.
\newblock {\em Mathematica scandinavica}, 25:49--58, 1969.

\end{thebibliography}

\newpage
\appendix
\noindent
\textbf{\huge Appendix}
\section{Proof of the direct implication of Proposition~\ref{prop:bigon}}\label{app:bigon}
\begin{nonumberprop}
  If a combinatorial immersion of one or primitive curves has excess (self-)crossing then it contains a bigon or a monogon.
\end{nonumberprop}
We first introduce some terminology. 
An \define{elementary homotopy} on a combinatorial curve $c$ consists in adding or removing a spur, or replacing in $c$ a possibly empty part of a facial walk by its complementary part. For a free elementary homotopy we can also apply a circular shift to the indices of the closed curve $c$. The equivalence relation generated by (free) homotopies is called combinatorial (free) homotopy. 

Let $\rho: G\to S$ be a cellular embedding of $G$ in a topological surface $S$ corresponding to the  combinatorial surface we are working with. Every combinatorial curve $c$ can be realized by a continuous curve on $S$ by replacing each arc in $c$ with a continuous arc deduced from the restriction of $\rho$ to the corresponding arc. We denote this continuous realization by $\rho(c)$. It is part of the folklore that (free)  combinatorial homotopy coincides with continuous (free) homotopy, meaning that $c$ is (freely) homotopic to $d$ if an only if $\rho(c)$ is (freely) homotopic to $\rho(d)$. 

An \define{elementary move} of a combinatorial immersion consists either in an elementary homotopy or in an \define{adjacent transposition}, i.e. in exchanging the left-to-right order of two occurrences in a same arc where one occurrence is next to the right of the other. We further require before performing an elementary homotopy that the immersion is in \define{good position}. This means, if the elementary homotopy applies to a nonempty part $u$ of a facial walk of some face, that each arc occurrence in $u$ should be the rightmost element of its arc, i.e. the most interior to the face. When removing a spur, we just require that the two arc occurrences to be removed are adjacent in left-to-right order.  See Figure~\ref{fig:elementary-moves}.  
\begin{figure}[h]
  \centering
\includesvg[.85\linewidth]{elementary-moves}
\caption{Left, the removal of a spur in good position. Right, two elementary homotopies replacing a subpath $u$ of a facial walk by the complementary subpath $v$. In the upper right, $v$ is empty. The immersions being in good positions, crossings may only appear or disappear by pairs.
}
  \label{fig:elementary-moves}
\end{figure}
The elementary homotopy does not modify the order of the remaining arc occurrences. When inserting a spur we make the inserted arc occurrences adjacent and when inserting part of a facial walk we insert each arc occurrence after the rightmost element of its arc. Remark that by a sequence of adjacent transpositions we can always enforce an immersion to be in good position. 

\begin{proof}[Proof of the Proposition]
We consider the case of an immersion $\mathcal I$ of two curves $c$ and $d$. Suppose that $\mathcal I$ has $p$ excess crossings. We shall prove the stronger claim that $\mathcal I$ has at least $\lceil p/2\rceil$ bigons whose tips are pairwise distinct. Let $\mathcal J$ be an immersion of two curves $c'$ and $d'$ respectively homotopic to $c$ and $d$ such that $\mathcal J$ has no excess crossings. Consider a sequence of elementary homotopies from $(c', d')$ to $(c,d)$. Following the above remark, we can insert adjacent transpositions between each elementary homotopy in order to obtain a sequence of elementary moves from $\mathcal J$ to $\mathcal I$. See the above remark. The claim is trivially verified by $\mathcal J$. We now check that the claim remains true after each elementary move. If the move is an adjacent transposition we may assume that we exchange an arc occurrence of $c$ with an arc occurrence of $d$ since we only consider intersections between $c$ and $d$. Without loss of generality we also assume exchanging the forward occurrences $[\Ibar,\Ibar+1]_c$ and $[\Jbar,\Jbar+1]_d$. There are three cases to consider. 
  \begin{enumerate}
  \item If none of $\Dpoint{\Imath}{\Jmath}$ and $\Dpoint{\Imath+1}{\Jmath+1}$ is a crossing then the transposition adds two crossings that we may pair as the tips of a new bigon. We now have $p+2$ excess crossings with at least $\lceil (p+2)/2\rceil$ bigons as required. 
\item\label{it:one-of-two} If $\Dpoint{\Imath}{\Jmath}$ is a crossing but $\Dpoint{\Imath+1}{\Jmath+1}$ is not and if $\Dpoint{\Imath}{\Jmath}$ is paired to form the tips of a bigon, say $(\Ipath{\Imath}{\ell}_c,\Ipath{\Jmath}{k}_d)$, we may just replace this bigon by $(\Ipath{\Imath+1}{\ell-1}_c,\Ipath{\Jmath+1}{k-1}_d)$ sliding its tip  $\Dpoint{\Imath}{\Jmath}$ to $\Dpoint{\Imath+1}{\Jmath+1}$. A similar procedure applies when $\Dpoint{\Imath+1}{\Jmath+1}$ is a crossing but $\Dpoint{\Imath}{\Jmath}$ is not. In each case the number of  excess crossings and bigons is left unchanged. 
\item\label{it:two-of-two} It remains the case where both $\Dpoint{\Imath}{\Jmath}$ and $\Dpoint{\Imath+1}{\Jmath+1}$ are crossings. If none of the two is paired to form the tips of a bigon, then the transposition removes two crossings and no pairing, so that the claim remains trivially true. If exactly one of the two is paired or if the two are paired together, we loose one bigon and two crossings after the transposition so that the claim remains true. Otherwise, there are two bigons of the form $(\Ipath{\Imath}{\ell}_c,\Ipath{\Jmath}{k}_d)$ and $(\Ipath{\Imath+1}{\ell'}_c,\Ipath{j+1}{k'}_d)$ that we can recombine to form the bigon $(\Ipath{\Imath+\ell}{1+\ell'-\ell}_c,\Ipath{j+k}{1+k'-k}_d)$. We again have one less bigon and two less crossings. 
  \end{enumerate}
We now consider the application of an elementary homotopy as described before the proof. There are again three possibilities.
\begin{enumerate}
\item 
If the homotopy replaces a nonempty subpath $u$ of a facial walk $uv\inv$ by the nonempty complementary part $v$ then no crossing may appear or disappear as we assume the immersion in good position. In particular, no crossing may use an internal vertex of $u$ and $u$ is either entirely included in or excluded from any side of any bigon. We can replace $u$ by $v$ in any bigon side where $u$ occurs to obtain valid bigons in the new immersion after the elementary homotopy is applied. The number of  excess crossings and bigons is left unchanged. 
\item When $u$  is empty in the above replacement, or when inserting a spur, we may only add crossings by pairs forming bigons with one zero-length side and the complement of $u$ or the spur as the other side. A similar analysis as in case~\eqref{it:one-of-two} of a transposition applies to take care of each pair.
\item When $v$ is empty in the above replacement, or when removing a spur, we may only remove crossings by pairs and a similar analysis as in case~\eqref{it:two-of-two} of a transposition applies to take care of each pair.
\end{enumerate}
This ends the proof for the case of two curves.
A similar proof holds for the existence of a bigon or a monogon in an immersion with excess self-intersection. This time the excess crossings are either paired to form bigons or left alone as the tips of monogons.
 \end{proof}

\section{Proof of Lemma~\ref{lem:swap}}\label{app:swap}
\begin{nonumberlem}
   Swapping the two sides of a singular bigon of an immersion of a geodesic primitive  curve decreases its number of crossings by at least two. 
\end{nonumberlem}
\begin{proof}
  Consider a singular bigon $(\Ipath{\Imath}{\ell},\Ipath{\Jmath}{\ell})$ of an immersion $\mathcal I$ of a closed primitive canonical curve $c$. Let $\mathcal J$ be the immersion after the bigon has been swapped. We shall partition the set of potential double points and show that the net change of the number of crossings with respect to $\mathcal I$ and $\mathcal J$ is non positive in each part. As the tips of the bigon are not crossings in $\mathcal J$, this will prove the lemma. 
We set for $\Overline{x}\in\Z/|c|\Z\setminus (\Ipath{\Imath}{\ell}\cup\Ipath{\Jmath}{\ell})$ and $0< p,q<\ell$:
\[\small
  \begin{array}{ll}
\D_{p,q}=\{\Dpoint{\Imath+p}{\Jmath+q}, \{\Dpoint{\Imath+q}{\Jmath+p}\},& \D_{x,p}=\{\Dpoint{x}{\Imath+p}, \Dpoint{x}{\Jmath+p}\}, \\
\D_{p,0}=\{\Dpoint{\Imath}{\Imath+p},\Dpoint{\Imath}{\Jmath+p},\Dpoint{\Jmath}{\Imath+p}, \Dpoint{\Jmath}{\Jmath+p}\}, &
\D_{x,0}=\{\Dpoint{x}{\Imath}, \Dpoint{x}{\Jmath}\}, \\
\D_{p,\ell}=\{\Dpoint{\Imath+\ell}{\Imath+p},\Dpoint{\Imath+\ell}{\Jmath+p},
\Dpoint{\Jmath+\ell}{\Imath+p}, \Dpoint{\Jmath+\ell}{\Jmath+p}\}, &
 \D_{x,\ell}=\{\Dpoint{x}{\Imath+\ell}, \Dpoint{x}{\Jmath+\ell}\},\\
\D'_{p,q}=\{\Dpoint{\Imath+p}{\Imath+q}, \{\Dpoint{\Jmath+q}{\Jmath+p}\}, 
  & \hspace{-1cm} \D_1=\{\Dpoint{y}{z}\mid y,z \not\in(\Ipath{\Imath}{\ell}\cup\Ipath{\Jmath}{\ell}) \}, \\
\D_2=\{\Dpoint{\Imath}{\Imath+\ell}, \Dpoint{\Imath}{\Jmath+\ell}, \Dpoint{\Jmath}{\Jmath+\ell}, \Dpoint{\Jmath}{\Imath+\ell}\},&
\D_3 =\{\Dpoint{\Imath}{\Jmath}, \Dpoint{\Imath+\ell}{\Jmath+\ell}\}
\end{array}
\]
When $\Ibar=\Overline{\Jmath+\ell}$ or $\Jbar=\Overline{\Imath+\ell}$, the set $\D_2$ consists of three double points only (or zero if $c(\Ibar)\neq c(\Overline{\Imath+\ell})$). Note that we cannot have both equalities as this would imply $\Jbar=\Overline{\Jmath+2\ell}$, whence $\ell=|c|/2$ and $c$ would be a square, in contradiction with the hypothesis that $c$ is primitive.
For each double point in $\D_1$ the four incident arc occurrences are  left in place in $\mathcal I$ and $\mathcal J$. It ensues that $\D_1$ has the same crossings in $\mathcal I$ and $\mathcal J$.  The double points in $\D_3$ are the tips of the bigon. They are crossings in $\mathcal I$ and not in $\mathcal J$, whence a net change of $-2$ crossings. For each of $\D_{x,p}, \D_{p,q}$ or $\D'_{p,q}$, the first double point in their above definition is a crossing in $\mathcal I$ (resp. $\mathcal J$) if and only if the second one is a crossing in $\mathcal J$ (resp. $\mathcal I$). Their net change of crossings is thus null. For $\D_{x,0}$, $\D_{x,\ell}$, $\D_{p,0}$ and $\D_{p,\ell}$  there are a few case analysis depending on the relative ordering of the two arc occurrences incident to $\Overline{x}$ (resp. $\Overline{\Imath+p}$, $\Overline{\Jmath+p}$) with respect to the crossing $\Dpoint{\Imath}{\Jmath}$ (resp. $\Dpoint{\Imath+\ell}{\Jmath+\ell}$. In each case (see Figure~\ref{fig:Dx1}) the number of crossings cannot increase from $\mathcal I$ to $\mathcal J$.
\begin{figure}
  \centering
  \includegraphics[width=\linewidth]{Dx1}
  \caption{Up to some obvious symmetries the case $\D_{x,0}$, $\D_{x,\ell}$, $\D_{p,0}$ or $\D_{p,\ell}$ has four distinct possible configurations. The blue and red strands represent the crossing $\Dpoint{\Imath}{\Jmath}$ (resp. $\Dpoint{\Imath+\ell}{\Jmath+\ell}$) and each arrow links the left configuration before the bigon swap (in $\mathcal I$) with the right configuration after the swap is applied (in $\mathcal J$).}
  \label{fig:Dx1}
\end{figure}
For $\D_2$ there are three cases according to whether $\Overline{\Imath+\ell}=\Jbar$, $\Overline{\Jmath+\ell}=\Ibar$, or none of the two identifications occurs. Recall from the paragraph before the lemma that we cannot have both identifications. The first two  cases can be treated the same way. See Figure~\ref{fig:D2special}. 
\begin{figure}
  \centering
  \includegraphics[width=.8\linewidth]{D2special}
  \caption{When $\Overline{\Imath+\ell}=\Jbar$, the end of $\Ipath{\Imath}{\ell}$ overlaps with the beginning of $\Ipath{\Jmath}{\ell}$ (in the purple strand). The third configuration on the right is actually forbidden by the definition of a singular bigon! It corresponds to the non-singular bigon on Figure~\ref{fig:singular-def}.}
  \label{fig:D2special}
\end{figure}
The number of possible configurations is much larger in the last case, i.e. when $\Overline{\Imath+\ell}\neq\Jbar$ and $\Overline{\Jmath+\ell}\neq\Ibar$. We essentially have to shuffle two circular orderings of length four corresponding to the two crossing tips. Without loss of generality we can assume that the path from $\Ibar$ to $\Overline{\Imath+\ell}$ is the right side of the bigon. We consider the following arc occurrences: 
\[
\begin{array}{llll}
  \alpha=\Focc{\Imath},&\alpha'=\Bocc{\Imath}, & \beta=\Focc{\Jmath},& \beta'=\Bocc{\Jmath}\\
\gamma=\Focc{\Imath+\ell},& \gamma'=\Bocc{\Imath+\ell},&  \delta=\Focc{\Jmath+\ell},& \delta'=\Bocc{\Jmath+\ell}
\end{array}
\]
Since $\Dpoint{\Imath}{\Jmath}$ and $\Dpoint{\Imath+\ell}{\Jmath+\ell}$ are crossings we must see $(\alpha,\beta,\alpha',\beta')$ in this counterclockwise order around the vertex $c(\Ibar)=c(\Overline{\Imath+\ell})$ and similarly for $(\gamma,\delta,\gamma',\delta')$. We denote by $S_1$ and $S_2$, respectively, these two circular sequences. We need to consider the effect of the bigon swapping on all the possible shuffles of $S_1$ and $S_2$. The restriction of these shuffles to $\alpha,\beta,\gamma$ and $\delta$ gives the 6 possible shuffles of $(\alpha,\beta)$ and $(\gamma,\delta)$. Among them the order $(\alpha,\delta,\gamma,\beta)$ cannot occur. Indeed, since the bigon is a thick double path the arcs $c(\alpha)$ and $c(\beta)$ either coincide or form a corner of a quad. This would force  $c(\delta)$ and $c(\gamma)$ to lie in a similar configuration. In turn, the constrained order $S_2$ would also enforce  $c(\delta')$ and $c(\delta)$ to coincide or form a corner of a quad in contradiction with the hypothesis that $c$ has no spurs or brackets. Similar arguments show that the orders $(\alpha,\gamma,\beta,\delta)$, $(\alpha,\delta,\beta,\gamma)$, $(\alpha,\gamma,\delta,\beta)$ and $(\alpha,\beta,\delta,\gamma)$ can only occur as factors in the possible shuffles of $S_1$ and $S_2$. These 4 orders thus leads to 24 distinct shuffles of $S_1$ and $S_2$ by factoring with the 6 shuffles of $(\alpha',\beta')$ with $(\gamma',\delta')$. By exchanging the roles of $(\alpha,\beta)$ and $(\gamma,\delta)$ and by turning clockwise instead of counterclockwise we see that $(\alpha,\delta,\beta,\gamma)$ leads to the same orders as $(\alpha,\gamma,\beta,\delta)$ and a similar correspondence holds for $(\alpha,\gamma,\delta,\beta)$ and $(\alpha,\beta,\delta,\gamma)$. We thus only need to check the 12 configurations depicted on Figure~\ref{fig:D2quater}. 
\begin{figure}
  \centering
  \includegraphics[width=\linewidth]{D2quater}
  \caption{Effect of swapping the singular bigon $(\Ipath{\Imath}{\ell},\Ipath{\Jmath}{\ell})$ on $\D_2$ for all the orders including the factor $(\alpha,\gamma,\beta,\delta)$ or  $(\alpha,\gamma,\delta,\beta)$. The blue strand represents $(\alpha, \alpha')$, the red one $(\beta, \beta')$, the orange one $(\gamma, \gamma')$ and the green one $(\delta, \delta')$.}
  \label{fig:D2quater}
\end{figure}
It remains to consider the order $(\alpha,\beta,\gamma,\delta)$.  Because of the constrained order $S_2$, $\gamma'$ and $\delta'$ cannot lie between $\gamma$ and $\delta$. This leaves out $\binom{4}{2}=6$ possible shuffles of $(\gamma',\delta')$ and $(\alpha,\beta,\gamma,\delta)$:
\[
\begin{array}{lll}
S'_1: (\alpha,\beta,\gamma,\delta,\gamma',\delta') &
S'_2: (\alpha,\beta,\delta',\gamma,\delta,\gamma') &
S'_3: (\alpha,\beta,\gamma',\delta',\gamma,\delta) \\
S'_4: (\alpha,\delta',\beta,\gamma,\delta,\gamma') &
S'_5: (\alpha,\gamma',\beta,\delta',\gamma,\delta) &
S'_6: (\alpha,\gamma',\delta',\beta,\gamma,\delta)
\end{array}
\]
We finally shuffle each of these orders with $(\alpha', \beta')$. Since $\alpha'$ and $\beta'$ cannot lie between $\alpha$ and $\beta$, we obtain $\binom{6}{2}=15$ possible shuffles when considering either $S'_1, S'_2$ or $S'_3$, $\binom{5}{2}=10$ possible shuffles with $S'_4$ or $S'_5$, and $\binom{4}{2}=6$ possible shuffles with  $S'_6$. By swapping left and right and turning clockwise instead of counterclockwise, we remark that $S'_1$ and $S'_3$ lead to the same orders and similarly for $S'_4$ and $S'_5$. We thus only need to consider the shuffles of $(\alpha', \beta')$ and $S'_1, S'_2, S'_4$ or $S'_6$ to complete the inspection of all the cases. Those shuffles are represented on Figures~\ref{fig:D2-S1},~\ref{fig:D2-S2},~\ref{fig:D2-S4},~\ref{fig:D2-S6} respectively.
\begin{figure}
  \centering
  \includesvg[\linewidth]{D2-S1}
  \caption{Effect of the bigon swapping on the 15 shuffles of $S'_1$ and $(\alpha', \beta')$.}
  \label{fig:D2-S1}
\end{figure}
\begin{figure}
  \centering
  \includesvg[\linewidth]{D2-S2}
  \caption{Effect of the bigon swapping on the 15 shuffles of $S'_2$ and $(\alpha', \beta')$.}
  \label{fig:D2-S2}
\end{figure}
\begin{figure}
  \centering
  \includesvg[\linewidth]{D2-S4}
  \caption{Effect of the bigon swapping on the 10 shuffles of $S'_4$ and $(\alpha', \beta')$.}
  \label{fig:D2-S4}
\end{figure}
\begin{figure}
  \centering
  \includesvg[\linewidth]{D2-S6}
  \caption{Effect of the bigon swapping on the 6 shuffles of $S'_6$ and $(\alpha', \beta')$.}
  \label{fig:D2-S6}
\end{figure}

In each of the configurations, we trivially check that the number of crossings is not increasing. This allows to conclude the lemma when the two index paths of a singular bigon are directed the same way. A similar analysis can be made when their directions are opposite, that is when the considered  bigon has the form $(\Ipath{\Imath}{\ell},\Ipath{\Jmath}{-\ell})$.
\end{proof}

\section{End of proof of Lemma~\ref{lem:configurations}}\label{sec:end-proof-lem-config}

\emph{Claim: Let $c$ be a primitive geodesic of length four and let $k$ be a positive integer. Then $c\Ipath{0}{4k}$ and $c\Ipath{2}{4k}$ cannot bound a partial diagram where the index pairs $(0,2)$ and $(4k, 4k+2)$ are in the same configuration.}
\begin{proof}
  Consider a partial diagram $\Delta$ for the thick double path $\dP=(\Ipath{0}{4k},\Ipath{2}{4k})$ and suppose that the index pairs $(0,2)$ and $(4k, 4k+2)$ are in the same configuration. In order to reach a contradiction we will use the fact that $c\Ipath{0}{2}=c\Ipath{4k}{2}$ label the first arcs of the $\Ipath{0}{4k}$ side of $\dP$ as well as the last arcs of the $\Ipath{2}{4k}$ side, and similarly for $c\Ipath{2}{2}=c\Ipath{4k-2}{2}$.

If the first and last index pairs of $\dP$ correspond to the same vertex in $\Delta$, then there are five possible configurations for the first two arcs on each side of $\dP$ (depicted on Figure~\ref{fig:length-partial-diag}, b1-5) and five possible configurations for the last two arcs on each side of $\dP$ (depicted on Figure~\ref{fig:length-partial-diag}, e1-5). 
\begin{figure}
  \centering
  \includesvg[\linewidth]{length-partial-diag}
  \caption{The possible configurations for the first two arcs and the last two arcs on each side of $\dP$ in the corresponding partial diagram.}
  \label{fig:length-partial-diag}
\end{figure}
Clearly, $\Delta$ cannot start with a path part of length two (case b1), as this would imply $c\Ipath{0}{2}=c\Ipath{2}{2}$ and in turn $c=c\Ipath{0}{2}.c\Ipath{2}{2}$ would be a square; this would contradict the fact that $c$ is primitive. Case b2 leads to the same contradiction  noting that $c\Ipath{0}{2}\sim c\Ipath{2}{2}$. Cases e1 and e2 can be dealt with analogously. As in Lemma~\ref{lem:ell-plus-un}, we denote by $(x,y)$ the conjunction of configurations $x$ and $y$. 
\begin{itemize}
\item If the orientations of the first and last staircases in $\Delta$ coincide, we easily obtain that $c(1)$ has degree 2 for the conjunctions $(b3, e3)$, $c(3)$ has degree 2 for $(b4, e4)$, 
$c(1)$ has degree 3 for $(b3, e4)$ and $(b4, e3)$ and $c(1)$ has degree 4 for $(b3, e5)$, $(b4, e5)$, $(b5, e3)$ and $(b5, e4)$. In case $(b5, e5)$ a quad has two sides labelled by the same arc, which is forbidden by Lemma~\ref{lem:facial-walk}.
\item  If the orientations of the first and last staircases in $\Delta$ are opposite then each conjunction of $b3,4,5$ with $e3,4,5$ implies that a quad has two sides labelled by the same arc, which is again forbidden.
\end{itemize}
When the first and last index pairs of $\dP$ correspond to diagonally opposite vertices in a quad, we get eight possible configurations for the first two arcs on each side of $\dP$ (depicted on Figure~\ref{fig:length-partial-diag}, B1-8) and eight possible configurations for the last two arcs on each side of $\dP$ (depicted on Figure~\ref{fig:length-partial-diag}, E1-8). All the pink quads in the figure denote the same quad with the same orientation, recalling that the configuration for the first and last index pairs of $\dP$ is the same. We easily get in each case that a vertex has degree at most 5, or that a quad has two sides labelled by the same arc, or that $c$ is a square (case $(B2, E2)$). In each case, we have thus reached a contradiction.
\end{proof}


\end{document}



%%% Local Variables:
%%% ispell-local-dictionary: "english"
%%% End: