Finding shortest non-trivial cycles in directed graphs on surfaces

Let D be a weighted directed graph cellularly embedded in a surface of genus g, orientable or not, possibly with boundary. We describe algorithms to compute a shortest non-contractible and a shortest surface non-separating cycle in D. This generalizes previous results that only dealt with undirected graphs. Our first algorithm computes such cycles in O(n2 log n) time, where n is the total number of vertices and edges of D, thus matching the complexity of the best known algorithm in the undirected case. It revisits and extends Thomassen's 3-path condition; the technique applies to other families of cycles as well. We also give an algorithm with subquadratic complexity in the complexity of the input graph, if g is fixed. Specifically, we can solve the problem in O(√g n3/2 log n) time, using a divide-and-conquer technique that simplifies the graph while preserving the topological properties of its cycles. A variant runs in O(ng log g + n log2 n) for graphs of bounded treewidth.


INTRODUCTION
Graphs embedded in the plane have been studied in depth.
For several computational problems, we know algorithms for planar graphs that are faster than a generic algorithm for arbitrary graphs: shortest paths, minimum spanning trees, (sub)graph isomorphism, maximum flow, minimum cut, etc. For some NP-hard problems we can obtain approximation schemes when restricted to planar graphs. Most of, if not all, these algorithms ultimately rely on topological properties of the plane and on the Jordan curve theorem.
Graphs embedded on surfaces are a natural generalization of planar graphs that have attracted much attention. From a theoretical perspective, they play a prominent role in particular in the graph minor theory developed by Robertson and Seymour (see e.g. Mohar and Thomassen [27]). From an applied perspective, efficient algorithms are needed to process non-planar surfaces: texture mapping, topological denoising, remeshing, and visualization (see e.g. the discussion in Erickson and Har-Peled [16]). In particular, when the embedded graph is weighted, the computation of shortest cycles satisfying some prescribed topological properties has been studied in a series of papers (see references below).
Considering digraphs (directed graphs) embedded in a surface is a natural next step in this area. This setting remains largely unexplored; except for the case of planar graphs, we are only aware of the following results. The data structures of Cabello and Chambers [3] to represent distances from the vertices of a face in an embedded graph can be easily extended to digraphs. Chambers et al. give an algorithm to compute maximum flows in undirected surfaceembedded graphs, and claim their approach extends to the directed case [9,Conclusion]. Erickson has recently shown that the best known algorithm to compute maximum s-t flows in planar directed graphs [2] does not extend to directed graphs on the torus [15,Section 3].
In this paper, we study the problem of finding shortest non-contractible or non-separating cycles in weighted digraphs embedded on surfaces. Like in previous works, we assume that the weights of a graph or digraph are always non-negative. This is the same as looking for shortest non-trivial closed walks (where trivial means either contractible or separating), since (as we will see shortly) shortest non-trivial closed walks are simple cycles; that is, they do not repeat any vertex. However, in contrast to the undirected case, shortest non-trivial walks through a given vertex may have several self-crossings.
Background on Shortest Non-Trivial Cycles. In the directed setting, the only case where the problem of computing shortest non-trivial walks is solved is the annulus [23]. In contrast, in the undirected setting, the problem for general surfaces has been largely explored; one reason is that cutting along such a cycle is the simplest way of simplifying the topology of a surface. Additionally, this is the basic ingredient for several more complex algorithms.
Thomassen [31] shows that shortest non-trivial cycles in undirected graphs can be found in near-cubic time (see also Mohar and Thomassen [27,Chapter 4]). The approach actually works for certain families of cycles that satisfy the socalled 3-path condition, which we revisit later in this paper. Erickson and Har-Peled [16] provide an algorithm to find shortest non-trivial cycles in near-quadratic time; this is the best current result. Cabello and Mohar [5] propose considering the problem parameterized by the genus. Kutz [25] propose an algorithm for orientable surfaces with running time O(g O(g) n log n), subsequently improved to O(g 3 n log n) by Cabello and Chambers [3]. In our companion paper [4] we consider the restricted scenario of unweighted, undirected graphs, and show that a shortest non-trivial cycle can be found in O(gnk) time, where k is the length of the output cycle. Here and in the sequel, n is the number of vertices and edges of the graph, assumed to be cellularly embedded on the surface (i.e., each face of the graph is a disk).
Many other more complex algorithms need to cut the surface along shortest non-trivial cycles (possibly through a given vertex) as a subroutine [17,10,8,7].
Several authors also considered computing shortest cycles with different topological properties, such as splitting cycles [8] and essential cycles [18]. The techniques of the present paper do not apply to these problems.
Overview of our Results. In Section 3, we first revisit the concept of 3-path condition given by Thomassen [31]. Specifically, we extend this notion to a family L of closed loops based at a given vertex s in a digraph. Note that the loops need not be simple and that the graph is directed and needs not be embedded. In an imprecise sense, it is convenient to think of the elements of L as "trivial" loops. This concept captures and extends the families of topologically trivial loops we want to study for embedded graphs. We provide a generic algorithm to find a shortest loop based at s outside the family L. The algorithm basically produces a candidate family of loops that should contain a shortest desired loop, and calls an oracle for each loop to decide membership in L. This algorithm is extended in Section 4 to the computation of a shortest closed walk (without fixing a basepoint) We then move, in Section 5, to the particular case of topologically non-trivial loops. Here we use techniques from our companion paper [4] to determine in amortized constant time whether a given candidate loop based at s belongs to L. This leads to an algorithm that finds the shortest non-trivial loop through s in O(n log n) time, and (repeating the procedure for each vertex) a shortest non-trivial cycle in O(n 2 log n) time, thus matching the complexity of the best known algorithm solving this problem in the undirected case.
In Section 6, we next turn our attention to the case where the genus is small, and give an algorithm that finds a shortest non-trivial cycle in O( √ g n 3/2 log n) time. The algorithm is based on a technique we could name topologically preserving divide-and-conquer. The idea is to use small separators in the graph to divide the problem into balanced subproblems, but each subproblem is kept in the original surface to avoid changing the topological character of the closed walks. The idea is reminiscent of the numerous algorithms for planar graphs that divide-and-conquer using a cycle separator; see for example [29,6,19,24]. However, in these cases, the preservation of the topology is implicit. We believe that our new approach is very natural and could be useful for other problems as well. For graphs with bounded treewidth, which admit constant-size separators, this strategy also leads to an O(ng log g + n log 2 n)-time algorithm. We also give an O(bn log n)-time algorithm for the special case where g = 0.

PRELIMINARIES
We introduce the background material used in this paper. A large part of it was also presented in our companion paper [4], but we repeat it for completeness.

Graph Theory Definitions and Notations
Graphs considered in this paper may have parallel edges and loop edges. We recall some usual definitions related to walks in graphs; we want to emphasize that all walks considered here are oriented. Specifically, let G be a (directed or undirected) graph. We denote the vertex set of G by V (G) and the edge set of G by E(G).
A walk is a sequence of edges e1, . . . , em with the property that the target of ei is the source of ei+1, for i = 1, . . . , m−1. A cycle in G is an oriented closed walk without repeated vertices; a cycle has no distinguished vertex. In contrast, a loop based at a vertex s of G is an oriented closed walk with a distinguished occurrence of s. If every oriented edge of G has a non-negative weight, the weight of a closed walk or loop is the sum of the lengths of the corresponding oriented edges (counted with multiplicity).
Given two vertices x and y of G, we denote by xy the edge oriented from x to y in G. In presence of parallel edges or loop edges, this notation is ambiguous, but it will always be clear from the context which edge xy is meant. The weight of the oriented edge xy is denoted by (xy).
Given walks α and β, we denote by α −1 the reversal of α, and by α · β the concatenation of α and β. Furthermore, (α) denotes the length of α. For any vertices x, y of G, we use dG(x, y) to denote the minimum length of the walks from x to y in G. We emphasize that dG(·, ·) is not a distance in general (it does not necessarily satisfy symmetry); nevertheless, we still use the words "length" (as a synonym for "weight") and "shortest".

Topological Background
We review some basic topology of surfaces, which will be needed only after Section 5 on. See any of the books by Hatcher [21], Massey [26], or Stillwell [30] for a comprehensive treatment.
A surface (or 2-manifold) Σ possibly with boundary is a compact, connected, topological space where each point has a neighborhood homeomorphic either to the plane or to the closed half-plane; the points without neighborhood homeomorphic to the plane comprise the boundary of Σ. A surface is non-orientable if it contains a subset homeomorphic to the Möbius band, and orientable otherwise. Here and in the sequel, surfaces are considered up to homeomorphism; in particular, a disk is just a surface homeomorphic to the standard unit disk in R 2 .
An orientable surface is homeomorphic to a sphere where g disjoint disks are removed, a handle (a torus with one boundary component) is attached to each of the remaining g circles, and then b disjoint disks are removed, for unique integers g, b ≥ 0. A non-orientable surface is homeomorphic to a sphere where g disjoint disks are removed, a Möbius band is attached to each of the remaining g circles, and then b disjoint disks are removed, for unique integers g ≥ 1 and b ≥ 0. In both cases, g is called the genus of the surface. Σ denotes the surface without boundary obtained by attaching a disk to each boundary component of Σ.
An embedding of a graph G (viewed as an undirected graph) in a surface Σ is a drawing of G on Σ without crossings. More formally, the vertices of G are mapped to distinct points of the interior of Σ; each edge is mapped to a path in the interior of Σ, such that the endpoints of the path agree with the points assigned to the vertices of that edge. Moreover, all the paths must be without intersection or selfintersection except, of course, at common endpoints. We sometimes identify a graph G with its embedding on Σ. The faces of G are the connected components of the complement of the image of G.
We only consider cellular embeddings of a graph. By this, we mean that Σ minus the image of the graph is a union of disks. In particular, each face of a cellular embedding on Σ is homeomorphic to an open disk with zero, one, or more disjoint open disks removed; the boundaries of these open disks belong to the boundary of Σ.
Let G be a graph cellularly embedded on Σ, with V vertices, E edges, and F faces; then Euler's formula states that To simplify our complexity results, we sometimes introduce n = V + E; by Euler's formula, we have g = O(n).
We assume that the embedding is represented in a suitable way, like for example the gem representation, using the incidence graph of flags (vertex-edge-face incidences) discussed by Eppstein [14], or rotation systems [27]. For orientable surfaces, one can also use the DCEL that is customary in Computational Geometry (see, e.g., de Berg et al. [12]). More precisely, we store the embedding of G on Σ, and mark within each face of the embedding the number of boundary components of Σ it contains.
Homotopy and homology are standard topological notions, which will not be formally defined here. Intuitively, two paths or loops are homotopic if one can be deformed continuously on the surface to become the other, keeping the endpoints fixed during the deformation. A loop is contractible, or homotopically trivial, if it is homotopic to the constant loop with the same basepoint. If α and β are contractible loops with the same basepoint, then so are α −1 and α · β. Similar properties hold for null-homologous, or homologically trivial, loops 1 . Let us only mention that homotopically trivial implies homologically trivial. Moreover, a closed walk or loop without repeated vertices is null-homologous on Σ if and only if its image is separating, that is, it splits Σ into two connected components.
In every cellular embedding there is a non-contractible loop (based at an arbitrary vertex), except if Σ is the sphere or the disk. Every cellular embedding has a non-separating loop (based at an arbitrary vertex), except if Σ is the sphere.

Reduction to an Undirected Graph with Asymmetric Weights
Let D be a directed graph (not necessarily embedded), where each arc has a non-negative length. Our goal is to compute shortest loops or cycles in D satisfying some given property. We first transform D into an undirected graph G with asymmetric lengths. Specifically, G is the undirected version of D; each oriented edge of G corresponds: • either to an arc of D with some length . In this case, the length of that oriented edge is ;

COMPUTING SHORTEST NON-TRIVIAL LOOPS
In this section and in Section 4, we consider the problem of computing the shortest loop or cycle in an undirected graph with asymmetric weights among a given family satisfying a certain property, the 3-path condition. This property can be expressed without reference to an embedding of the graph, so here and in the following section, our graph needs not be embedded.
Our primary goal is to compute shortest non-trivial cycles. This will be done by taking each vertex s of the input graph, computing a shortest non-trivial loop based at s, and returning the shortest such loop. So, in this section, we focus on computing shortest non-trivial loops.

The 3-Path Condition
Let G be an undirected graph. A family of loops L in G based at some vertex s satisfies the 3-path condition when the following two conditions are satisfied: sum of zeros is zero: if α, β, γ are s-to-x walks in G (for some vertex x) such that α · β −1 and β · γ −1 are in L, then α · γ −1 is also in L. Figure 1, left, illustrates the condition "sums of zero is zero". Some remarks on the definition may be convenient. Firstly, a clarification for the reader familiar with the work of Thomassen [31]: although the spirit of our definition is similar as that of Thomassen, we have exchanged the role of the family L and its complement. We believe that our presentation is more convenient, and is motivated by the following: the loops of L behave like the "trivial elements in a certain group", while the loops outside L behave like the "non-trivial elements". The main property of 3-path condition basically tells that the sum of two zero elements must be zero. Our aim is to find a shortest loop outside L, that is, a shortest "non-trivial element".
Secondly, we may mention some families of loops based at s that satisfy the 3-path condition: the family of contractible loops, the family of null-homologous loops, the family of loops with an even number of edges, and the family of two-sided loops [27,Section 4.3]. We will be discussing the problem of finding a shortest loop in the complement of such families.

Properties
Let L be the family of loops based at s that are not in L. Our objective is to find a shortest element in L.
Let T be the shortest path tree in G from the source s. (Recall that we assume that shortest paths are unique.) Similarly, let R be the reversed shortest path tree in G to the sink s; that is, R corresponds to a shortest path tree from s in the graph G obtained by exchanging the lengths (xy) and (yx) of each edge xy. Hence, for any vertex x of G, we have dG(s, x) = dT (s, x) and dG(x, s) = dR(x, s).
For a vertex x, we will use T [s, x] to denote the unique s-to-x path in T , and T [x, s] for the reversal of that path. Similarly, we will use R[x, s] to denote the unique x-to-s path in R, and R[s, x] for the reversal of that path. (So, the only relations among ( For any (oriented) loop α based at s and passing through vertex x, let α[s, x] denote the subwalk of α that goes from s to x, and α[x, s] be the complementary part of α. 4 For any oriented edge Let M be the family of loops of the form loop T,R (xy) that are in L: For the topological problems that we will study below, it is convenient to introduce the following family of loops based at s: Note that we decide to include a loop in N by checking membership in L of a different loop. The reason for this becomes apparent in the proof of the following lemma. A motivation to consider N is that, for certain topological properties to be considered below, we can manipulate N faster than M (see Section 5). Lemma 2. If L is non-empty, then a shortest element of N is a shortest element of L.
Proof. Assume that L is non-empty. By Lemma 1 the family L has a shortest element of the form α = loop T,R (xy) for some edge xy ∈ E(G) \ E(T ). We first prove that α belongs to N, or equivalently, that loop T (xy) ∈ L. See Figure 2. Since T [s, y] · R[y, s] is strictly shorter than α, it belongs to L. If loop T (xy) was also in L, then we could apply the 3-path condition to the s-to-y walks T [s, x] · xy, T [s, y], and R[y, s] −1 , and we would conclude that α is also in L, which contradicts our choice of α. Thus the loop α is in N.
Let β be a shortest element in N; we can write β as loop T,R (xy) for some edge xy ∈ E(G) \ E(T ) such that loop T (xy) ∈ L. There remains to prove that β belongs to L. See Figure 2. By the preceding paragraph β is no longer than α. Since T [s, y] · R[y, s] is strictly shorter than β, hence than α, it must be in L. If β was also in L, we could apply the 3-path condition to the s-to-y walks T [s, x] · xy, R[y, s] −1 , and T [s, y] to deduce that loop T (xy) is in L, and reach a contradiction. Thus β is in L, and since it is no longer than α, it is a shortest element of L.
The previous two lemmas show that finding a shortest element in M or in N is enough to find a shortest element in L. However, it should be pointed out that the role of N is surprising. In particular, N can contain loops from L. We only know that the shortest one is in L. In general, it does not hold that N ⊆ M or M ⊆ N. They just agree in having the same shortest element.

Algorithm
Theorem 1. Let G be an undirected graph with asymmetric weights, V vertices, and E edges, and let s be one of its vertices. Let L be a family of loops in G that satisfies the 3-path condition. Assume that there is an oracle that decides in time τ (k) if a loop with k edges is in L. Then we can find a shortest loop based at s not in L, or correctly report that no such loop exists, in time O(Eτ (E) + V log V ).
Proof. The algorithm is as follows. Firstly, we construct a shortest path tree T and a reversed shortest path tree R for vertex s. Secondly, for each oriented edge xy, we construct explicitly the closed walk loop T,R (xy), check its membership in L, and select the shortest one among the ones not in L (unless all of them are in L, in which case we report that no such loop exists).
Correctness is clear by Lemma 1. As for the running time, note that T and R can be constructed in time O(E+V log V ) time, using Dijkstra's algorithm [13] speeded up with Fibonacci heaps [20]. Once the trees T and R are available, we can preprocess the graph such that any distance query from or to vertex s takes O(1) time. For each oriented edge xy, we need time O(1 + τ (E)) = O(τ (E)) to compute the length of loop T,R (xy) and to check membership in L. The result follows.

COMPUTING SHORTEST NON-TRIVIAL CYCLES
Let G be an undirected graph. A family of closed walks W in G satisfies the 3-path condition when the following two conditions are satisfied (recall that, by definition, closed walks have no distinguished basepoint, in contrast to loops): invariant under reversal: if α ∈ W, then α −1 ∈ W; sum of zeros is zero: if α, β, γ are x-to-y walks in G (for some arbitrary vertices x and y) such that α · β −1 and β · γ −1 are in W, then α · γ −1 is also in W.
In particular, if G is embedded on a surface, the families of contractible, null-homologous, even, and two-sided closed walks satisfy this condition.

Lemma 3. A shortest closed walk not in W is a cycle.
Proof. Let α be a shortest element not in W and assume, for the sake of contradiction, that α is not a cycle. Let x be a vertex that is visited twice by the walk α. Let β1 be a closed subwalk of α between the first and the second appearance of x in α. Let β2 be the closed walk between the second and the first appearance of x in α. By construction it holds that α = β1 · β2. Since β1 and β2 are strictly shorter than α, they must belong to W. However, by the 3-path condition applied to the paths β1, the constant walk consisting of the single vertex x, and β −1 2 , it follows that the path β1 ·(β −1 2 ) −1 = α is also in W. This is a contradiction with the choice of α.
Theorem 2. Let G be an undirected graph with asymmetric weights, V vertices, and E edges. Let W be a family of closed walks in G that satisfies the 3-path condition. Assume that there is an oracle that decides in time τ (k) if a closed walk with k edges is in W. Then in time O(V Eτ (E)+ V 2 log V ) we can find a shortest closed walk not in W (actually, this is a cycle by Lemma 3), or correctly report that no such walk exists.
Proof. We run the algorithm of Theorem 1, taking for s each vertex in turn, and return the shortest closed walk found. This is valid since the family of loops in W with a given basepoint satisfies the 3-path condition for loops.

COMPUTING SHORTEST NON-TRIVIAL LOOPS AND CYCLES ON SURFACES
Let Σ be a compact surface, orientable or not, with genus g and b boundaries. Let G be a graph cellularly embedded on Σ. In this section, we present improved algorithms to compute a shortest non-contractible or non-separating loop or closed walk in G. Let L denote either the family of contractible loops or the family of non-separating loops based at some vertex s of G; this family satisfies the 3-path condition.

Computing Shortest Non-Trivial Loops on Surfaces
Recall the definition of N given in Equation (1). We want to find the oriented edges that define a loop in N. This is equivalent to the problem of deciding if loop T (xy) is in L.
For this purpose, we can regard T as an undirected graph, and decide if loop T (xy) is in L. This can be done efficiently using a characterization from our companion paper [4].
Proposition 1. In O(E +V log V ) time, we can compute the shortest path tree T and determine the set of edges xy of E(G) \ E(T ) such that loop T (xy) is contractible (resp. separating).
Proof. The techniques are very similar to our companion paper [4, Section 3]; we briefly indicate the general strategy for completeness.
In this proof we will need the the dual graph G * : it has for vertices the (dual) set of faces of G, for edges the (dual) set of edges of G, and two faces are adjacent if they share an edge of G. The edge dual to e is denoted by e * , and it connects the two faces adjacent to e in the embedding. Furthermore, for every face f of G containing a boundary component, we add to G * a loop edge. Such loop edges correspond to no edge of the primal graph G.) We compute the shortest path tree T in O(n log n) time using Dijkstra's algorithm. Let C * be the subgraph of G * with the same vertex set as G * and edge set E(G * ) \ {e * | e ∈ E(T )}. The graph C * can be constructed in linear time. Note that loop T (e) is non-contractible or non-separating if and only if cycle T (e) is non-contractible or non-separating, respectively.
In our companion paper [4], Corollary 2, we show the following characterization: The cycle cycle T (e) is non-separating if and only if C * −e * is connected. The cycle cycle T (e) is contractible if and only if C * − e * has a connected component that is a tree (possibly reduced to a single vertex) Therefore, the set of dual edges e * such that loop T (e) is non-contractible can be obtained in linear time from the set of edges of C * by repeatedly removing edges with an incident vertex of degree one. To obtain the set of edges e such that loop T (e) is non-separating, note that its dual set is precisely the set of non-bridge edges in C * . The computation of the bridge edges of a graph in linear time using depth-first search is part of the folklore.
Theorem 3. Let G be an undirected graph with asymmetric weights, V vertices, and E edges, that is cellularly embedded on Σ. Let s be one of its vertices. In time O(E+V log V ) we can find a shortest non-contractible, respectively nonseparating, loop based at s.
Proof. By Lemma 2, it suffices to compute the shortest element in N; that is, the shortest loop T,R (xy) such that xy ∈ E(G) \ E(T ) and loop T (xy) ∈ L. Proposition 1 allows to list the edges xy such that loop T (xy) ∈ L in O(E + V log V ) time. For each xy, the length of loop T,R (xy) can be computed in constant time. Hence, we can find the shortest element of N in O(E + V log V ) time.

Computing Shortest Non-Trivial Cycles on Surfaces
Theorem 4. Let G be an undirected graph with asymmetric weights, V vertices, and E edges, that is cellularly embedded on Σ. In O(E +V g +V 2 log V ) time, we can find a shortest non-contractible (resp. non-separating) closed walk; this is also a shortest non-contractible (resp. non-separating) cycle.
Proof. The algorithm consists essentially in applying V times Theorem 3, choosing each vertex in turn to be the basepoint. However, this gives a running-time of O(V E + V 2 log V ). To decrease this to O(E + V g + V 2 log V ), we use Proposition 2 below (which allows only for a slight improvement here, but will be used crucially in Section 6). This proposition says that, after O(E) pre-and post-processing, we may assume E = O(V + g). So the complexity of the algorithm is now O(E + V (V + g) + V 2 log V ), as claimed.
The output of the algorithm is a cycle by Lemma 3.
Proposition 2. Assume we want to compute a shortest non-contractible or non-separating cycle in G. With O(E) time pre-processing and post-processing, it is sufficient to compute the shortest such cycle on another graph G with V vertices and E = Θ(V + g) edges, cellularly embedded on a surface Σ , such that Σ and Σ have the same orientability character, the same genus, and the number of boundaries of Σ is at most the number of boundaries of Σ.
Proof. We focus on the non-separating case first. In this case, we may assume b = 0, since attaching a disk to each boundary component does not change the separability character of a cycle. We will choose Σ = Σ.
Whenever one face has degree one, we may remove the incident loop edge, which is contractible. Whenever one face has degree two, we may replace the two homotopic edges e1 and e2, with endpoints x and y, by an edge e with the same endpoints (removing the face in-between). The weight of e, oriented from x to y, is the minimum of the two weights of the edges of e1 and e2 from x to y, and similarly for the weights from y to x.
In linear time, we removed all the faces with degree one or two. The shortest cycle in G within a given homotopy class corresponds to a shortest cycle in G within that homotopy class. So the shortest non-separating cycle in G corresponds to the shortest non-separating cycle in G; recovering the cycle in G from the cycle in G takes O(E) time.
Clearly, G has the same number V of vertices as G; let E and F be its number of edges and faces. The bound V + g = O(E ) comes from Euler's formula. The reverse bound follows from F ≤ 2E /3 (by double-counting the incidences between the edges and the faces) and E = V + F + O(g) (by Euler's formula).
For the non-contractible case, the same algorithm works, but we cannot assume anymore that the surface has no boundary, so this proof only gives us E = O(V + g + b). We next show that a slightly more complicated argument still gives us E = O(V + g), as claimed. Recall that our representation of the embedded graph G on the surface Σ is obtained by attaching disks to each boundary component of Σ, and recording, in each face of G, the number of boundaries inside it. (Actually, the exact number is irrelevant as far as contractibility is concerned; only the presence or absence of boundaries matter.) The strategy is as in the non-separating case, except that we may remove some non-contractible cycles during the simplification from G to G . So our algorithm maintains (1) an embedded graph G , such that every non-contractible cycle in G corresponds to a non-contractible cycle in G of the same length, and (2) a non-contractible cycle α in G. The invariant is that the shortest non-contractible cycle in G either is α or corresponds to the shortest non-contractible cycle in G . Initially, G = G and α is undefined.
Assume one face f (of the embedded graph G on Σ) has degree one. If it contains no boundary component of Σ, we can remove it as before. Otherwise, the loop edge β incident to it is non-contractible; we remove β and the corresponding face f , and mark the other face incident to β as containing boundaries. Also, we let α be the shortest cycle among the current α and β.
Assume one face has degree two, and contains no boundary component of Σ. Then as before, we may remove that face, and replace both incident edges by a single edge e with appropriate weights: the weight of e, oriented from x to y, is the minimum of the two weights of the edges of e1 and e2, oriented from x to y, and similarly for the opposite orientation.
Assume that four edges e1, e2, e3, and e4 have the same endpoints and form three faces of degree two, whose boundaries are eiei+1 (i = 1, 2, 3). Then, assuming the previous step has been applied as much as possible, each face contains (at least) one boundary component of Σ. The shortest non-contractible cycle in G might be contained in e2 ∪ e3, so we let α be the shortest cycle among the current α, e2 · e −1 3 , and e −1 2 · e3. Otherwise, since it has no repeated vertices, it uses at most one of e2 and e3, and at most once, so we may remove the face between e2 and e3, merging e2 and e3 into a single edge e with appropriate weights. This is valid since any cycle without repeated vertices using e2 or e3 is noncontractible, so the shortest one uses whichever is cheaper.
The pre-processing and post-processing steps can be implemented to run in O(E) time. The new graph G has V vertices; let E and F be its number of edges and faces. It remains to prove that E = Θ(V + g). Again, the only nontrivial inequality is E = O(V + g). Let G be the graph G where we iteratively remove every edge incident to a face of degree two; it has V vertices; let E be its number of edges. Then G contains no face of degree one or two, so we have E = O(V + g) as before. Furthermore, transforming G into G removes at most two thirds of the edges, by the preceding paragraph. So E = O(E ), which concludes.

IMPROVED ALGORITHM FOR SMALL GENUS OR CONSTANT TREEWIDTH
Let Σ be a surface (orientable or not) with genus g and b boundaries. In this section, we improve the result of the previous section: we obtain algorithms with running time O( √ g n 3/2 log n) to compute a shortest non-contractible or non-separating cycle on Σ. The same technique yields nearlinear-time algorithms for graphs with bounded treewidth, because they admit small separators.

Small Genus
Theorem 5. Let G be an undirected graph with asymmetric weights, V vertices, and E edges, that is cellularly embedded on Σ. We can compute a shortest non-contractible or non-separating cycle in G in O(E +V g log g+ √ g V 3/2 log V ) time.
The proof uses topology-preserving divide-and-conquer : we compute a separator of the embedded graph, and recurse on two instances in which a different part of the graph is simplified, but the topology of the surface is preserved. Figure 3 illustrates this strategy. Proof. The strategy is to compute an initially empty set F of edges of E(G) \ E(H) such that contracting all of them does not change the topology of the surface and removes all vertices of G \ H.
For every component K of the graph induced by the vertices in G\H, we compute a spanning tree T of K. This tree must be incident to at least one edge with a vertex in H; otherwise the embedding G would not be cellular. Let e be one such edge; we add the edges of T and the edge e to the set F .
The union of the edges in F and their incident vertices is a forest (any cycle using only these edges would enter and leave at least one tree T , which is connected to the remaining part of F by at most one edge). So we can perform the contraction of the edges of F topologically, on the embedding G. After this operation, the new graph contains only vertices of H.
The contraction of all the edges of F can be done in O(E) time. Note that, after the contraction of F , we can update the pointers from each vertex-edge-face incidence to its incident vertex in O(E) total time.
Proof of Theorem 5. By Proposition 2, using O(E) pre-processing and post-processing time, we may assume that E = Θ(V + g). If V = O(g), we just compute the optimal solution using Theorem 4. Thus, it only remains to consider the case V = Ω(g), which implies E = Θ(V ). The rest of the proof discusses this case. We describe the algorithm for non-separating cycles; the same argument applies to non-contractible cycles.
In O(V ) time, we find a graph separator X for G of size O( √ gV ) [14,Theorem 5]; that is, X is a set of O( √ gV ) vertices such that G − X is the disjoint union of two subgraphs H1 and H2, each with at most 2V /3 vertices. Now, by Theorem 3, we can compute the shortest non-separating loop α0 passing through any of the vertices in X, in O( 5 In O(V ) time, we simplify the embedding G to a cellular embedding G1 that contains H1 and has the same vertex set as H1 (Lemma 4), and put infinite weights on the edges not in H1. We can recursively compute a shortest non-separating cycle α1 in G1 (the pre-processing and post-processing step of Proposition 2 takes O(V ) time). Similarly, we simplify G to a cellular embedding G2 that contains H2 and has the same vertex set as H2, and compute a shortest non-separating cycle α2 in G2. The shortest of α0, α1, and α2 is a shortest nonseparating cycle in G. We stop the recursion when the number of vertices is Θ(g) (hence the number of edges is Θ(g)). In this base case, we compute the shortest non-separating cycle using Theorem 4. This costs O(g 2 log g) time for each subproblem. Note that there are O(V /g) base subproblems because every vertex of G is found in at most one base subproblem, so the total time spent in these base cases is O(V g log g).
Let T (V ) be the total time spent in the recursive calls, without counting the base subproblems, for an input with V vertices. We have where the maximum is taken over values V1, V2 that satisfy V1 + V2 ≤ V and 0 ≤ V1, V2 ≤ 2V /3.

Constant Treewidth
Similarly, we get an improvement when the treewidth is constant: Theorem 6. Let G be an undirected graph with asymmetric weights and V vertices that is cellularly embedded on Σ. Assume G has treewidth at most k0, where k0 is a constant. We can compute the shortest non-contractible or non-separating cycle in G in O(V g log g + V log 2 V ).
Proof. The proof is the same as that of Theorem 5. The only difference is that, for graphs of bounded treewidth, it holds E = O(V ) and we can compute separators of constant size in linear time [1]. The recurrence is now where the maximum is taken over values V1, V2 that satisfy V1 + V2 ≤ V and 0 ≤ V1, V2 ≤ 2V /3.

Improved Algorithm for Genus Zero
In the case of a sphere with boundaries (orientable, g = 0), if b is fixed, we can get an almost linear dependence on the complexity of the input graph. In this case, since every cycle is separating, only the non-contractible case is relevant.
Theorem 7. Assume Σ is a sphere with b boundaries. Let G be an undirected graph with asymmetric weights and E edges that is cellularly embedded on Σ. We can compute the shortest non-contractible cycle in G in O(bE log E) time.
Proof. Let B1, . . . , B b be the boundary components of Σ. A cycle on Σ is non-contractible if and only if it separates B1 from another boundary component of Σ. Let i ∈ {2, . . . , b}. We now prove that computing the shortest cycle that has B1 on its left and Bi on its right is possible in O(E log E) time, which concludes.
We view G as an undirected plane graph. Let G * be its dual graph with respect to the sphere without boundary obtained after attaching a disk to every boundary component; for each k, let B * k be the vertex of G * corresponding to B k . If e is an oriented edge of G, then e * is the dual edge of G * oriented so that e * crosses e from left to right. Let X be a set of oriented edges of G. Then X contains the oriented edges of some cycle with B1 on its left and Bi on its right if and only if every path from B * 1 to B * i in G * uses an oriented edge in X * [11,Lemma 7.2].
In O(E log E), we compute a minimum cut on G * from B * 1 to B * i , where the capacity of a dual oriented edge is the length of its primal oriented edge; this can be done either using the algorithm by Janiga and Koubek [23], speeded up with the linear time shortest path algorithm by Henzinger et al. [22], or by computing a maximum flow [2,15] and using the max-flow min-cut theorem. If all lengths are non-zero (which can be assumed by putting infinitesimally small lengths if necessary), this gives an inclusionwise minimum cut of smallest capacity, hence, by the preceding paragraph, a shortest cycle having B1 on its left and Bi on its right.
We first introduce a composite definition of length, denoted by λ-length. Let s be the vertex for which we want to compute a shortest non-trivial loop. Fix a shortest path tree T in G from the source s and also a reversed shortest path tree R in G to the sink s. For any loop α through s we define λT (α) to be the length of the longest segment of α starting from s and contained in T . Finally, let us define the λ-length of α to be the couple λ(α) = ( (α), −λT (α)). For the rest of this section, the length of two loops through s will be compared using the lexicographic order under λ(·). Thus, α is λ-shorter than another loop β through s if λ(α) λ(β). This means that to obtain a λ-shortest loop through s we want first to minimize the length of the loop, then to maximize the length of the segment starting from s and contained in T . We can now give a proof of Lemma 1.
Proof of Lemma 1. Assume that L is non-empty, and let α be a λ-shortest loop in L. Let x be the last vertex in α as we start walking from s that satisfies α[s, x] = T [s, x]. Let y be the next vertex in α after x. By definition of the λ-length, we have loop T,R (xy) α, where loop T,R (xy) = α[s, y] · R[y, s]. To prove the lemma, it is thus enough to show that loop T,R (xy) is in L, hence in M. We next note the following. In both cases λ(β) ≺ λ(α), and β cannot be in L.
• The loop T [s, y] · α[y, s] is in L. The same argument as above applies.