{'fraction_non_alphanumeric': 0.04046746903889761, 'fraction_numerical': 0.013605442176870748, 'mean_word_length': 3.834176503653738, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
On a numerical algorithm for computing topological characteristics of three-dimensional bodies

We present an algorithm for computing the main topological characteristics of three-dimensional bodies. The algorithm is based on a discretization of Morse theory and uses discrete analogs of smooth functions with only nondegenerate (Morse) and the simplest degenerate critical points.


Introduction
The problem of computing topological characteristics of geometrical objects of complicated shape appears in many applications. This led to the rise of computational topology which is fast developing now [1,2,3]. Usually a problem of computational topology reduces to effective computation of Betti numbers, i.e. the ranks of homology groups.
One of the most typical approaches to computing homology groups is based on Morse theory [4,5] which has to be modified for studying objects that are defined by discrete data. Discrete approaches to the Morse theory were systematically studied in [1] and [6] and various discrete realizations of Morse theory were considered in [7,8,9,10].
In this article we propose a method for computing the Betti numbers of three-dimensional bodies. Therewith the bodies are defined as unions of unit cubes with integer-valued coordinate of vertices in the three-dimensional Euclidean space. The main feature of the approach is • the use of discrete analogs of smooth functions (of two variables) which in addition to standard nondegenerate (Morse) critical points have the simplest degenerate critical points of the type of the "monkey saddle" in the case under study.
The scheme of this approach is as follows. Let M be a three-dimensional body with a boundary in the three-dimensional Euclidean space. We consider the critical points of a differentiable (smooth) function f on M , i.e. the points at which the gradient of f vanishes. The values of f at these points are called critical. When the value of f passes through a critical value the topology of the excursion set {f ≤ const } changes. By using critical points we construct a chain complex C * = C 0 ⊕ C 1 ⊕ C 2 which is a graded commutative group. To the critical points there correspond the basic elements (generators) of the complex C * as of a vector space over the commutative field Z 2 which consists of two elements. To a nondegenerate critical point of index i there corresponds a generator of C i and to a critical point of the "monkey saddle" type there correspond two generators of C 1 . The gradient flow of f defines the boundary operators which are homomorphisms of commutative groups ∂ i : C i → C i−1 . The final part of this algorithm is standard: since the kernel Ker ∂ of a homomorphism ∂ : C * → C * lies in its image Im ∂ the homology groups (with coefficients in Z 2 ) are correctly defined as the quotient spaces The dimension of the i-th homology group as of a vector space over Z 2 is called the i-th Betti number of M and it is denoted as b i . The Betti numbers b 0 , b 1 , and b 2 which are interpreted as the number of connected components, the number of linearly independent one-dimensional cycles and the number of holes in M and their alternated sum Clearly, we consider a discrete version of this algorithm. Note that since a body is three-dimensional it is enough to restrict consideration to homology with Z 2 coefficients for a complete description of homological characteristics of M . Also we may use the Alexander duality and compute only the homomorphism ∂ 1 : C 1 → C 0 . We will discuss that here in morein detail.
Since we consider only bounded bodies, every such a body is assumed to be embedded into a cube K in the three-dimensional space. The algorithm of computing the matrices of boundary operators has complexity O(n) where n is the number of vertices, points with integer-valued coordinates, in K, and the complexity of computing Betti numbers is equal to O(n 3 c ), where n c is the number of critical points of f . Remark 1. There exist infinitely many different topological types of critical points of analytic functions of n (n ≥ 2) variables and their classification is very complicated. However there are finitely many Morse singularities and topological surgeries of corresponding level surfaces are completely well described. Therefore in computational topology it was supposed to use only the functions with Morse singularities for computing topological characteristics (see the scheme below). However it appears that after discretization the classification of critical points becomes finite which is quite natural and by describing all topological surgeries corresponding to non-Morse singularities one may use more general functions for realization of the scheme mentioned above and we demonstrate that in the three-dimensional case.
Remark 2. There are algorithms for computing Betti numbers with complexity asymptotically linear in n, 1 which substantially use the specifics of the three-dimensional case (see, for instance, [11]). In applications [12] in which our algorithm was used the number n c of critical points is small as compared with n and that gives a practical advantage. Moreover the proposed method may be generalized to higher dimensions.

Critical points of the diagonal function on a Euclidean cube
By an elementary interval I ⊂ R we mean the set I = [l, l + 1] for some l ∈ Z. We analogously define the elementary square Q = I 1 × I 2 ⊂ R 2 and the elementary cube C = I 1 × I 2 × I 3 ⊂ R 3 , where I k are elementary intervals. We consider three-dimensional bodies M formed by elementary cubes and lying inside the bounded domain for some natural number N .
In fact we compute homology not of M but of the body M which is constructed from M by unstacking joint vertices and boundary edges for such elementary cubes, in M , which intersect only at these joint vertices and edges.
We make to remarks: 1) we consider M due to applications of the algorithm; 2 2) the body M itself, or more precisely a topologically equivalent body, may be realized by unions of elementary cubes in a cube K ′ of a larger size, as shown by the following construction. Let us consider the cube . M ′ is a subdivision of M such that every elementary edges splits into three parts and every elementary cube splits into 9 parts. Then we remove from m ′ all elementary cubes such that they have a joint face with ∂M ′ . We obtain the new cubical set M . In the sequel we assume that M is obtained, if need be by such a surgery.
Let us take for a function f on K the "diagonal" function As usual, we put It is clear that if a 1 < a 2 and the set f −1 ([a 1 , a 2 ]) ∩ M does not contain points with integer-valued coordinates, then M a 1 and M a 2 are topologically equivalent (see, for instance, [4]). Therefore, under a continuous variation of a from 0 to 3N the topological type of M a changes only when a passes through a value of the form a = f ( Hence N (v) consists of eight elementary cubes surrounding v and lying in M . For a = f (v) and |b − a| < 1 6 we put 2 In [12] this algorithm is used for evaluation of topological characteristics of oil reservoirs. Such a reservoir is modeled by a three-dimensional body that is a union of threedimensional cells. Therewith it is assumed that a passing of oil from one cell to another is possible only though the joint two-dimensional face, and there is no passing through a joint vertex or a joint edge. To this end, we have to make a preliminary processing of M .
We say that v ∈ M is a critical point of a function f if for sufficiently small positive ε < 1 6 the sets N v M a−ε and N v M a+ε are not topologically equivalent.
For distinguishing the critical point type we code the cubes from N (v) as follows. This neighborhood consists of eight elementary cubes and with respect to the central integer-valued point every cube may be defined by changes of the coordinates whose values are either greater or less inside the cube than at v. Hence each cube is coded by a triple of signs "± ± ±". Every such a cube may lie or not lie in M . Hence we may correspond to N (v) the pair of matrices where t * * * = 1 if the cube lies in M and t * * * = 0 otherwise. We say that a critical point v ∈ M is nondegenerate if N v M a+ε topologically looks like the body N v M a−ε with a handle of index i, i = 0, 1, 2, glued to its boundary, i.e. one of the three possibilities holds: 2) (a handle of index 1) neighborhoods of two points on the boundary of N v M a−ε are connected by a thickened interval; 3) (a handle of index 2) to a tubular (ribbon) neighborhood of a circle in the boundary of N v M a−ε it is glued a thickened two-dimensional disc therewith the boundary of disc is glued to the circle.
We call a critical point degenerate otherwise. The number i is called the index of a critical point.
The following theorem is proved straightforwardly by the enumerating combinatorial types of critical points.
Theorem 1 All possible type of critical points are given in Table 1.
The types 1-14 correspond to nondegenerate critical points. The last type correspond to the "monkey saddle" which in the smooth case if given by a critical point (x, y) = (0, 0) of the function f (x, y) = x 3 − xy 2 . The passing through this value results in gluing two handles of index 1.
Let M i , i = 0, 1, 2, be the sets of critical points of index i. Every monkey saddle v contributes to M 1 the two points: the point v itself and its formal "double" v ′ . For every i = 0, 1, 2 let us consider the vector space C i over Z 2 with generators from M i . To construct a chain complex we have to define the linear boundary operators ∂ 1 : C 1 → C 0 and ∂ 2 : C 2 → C 1 .

The gradient flow and the boundary operators
The gradient flow enables us to describe a gluing of handles and that allows us to compute the boundary operators in the chain complex C * . By a trajectory of the gradient flow we mean a sequence of vertices of M which we may pass going down along edges. The difference from the gradient flow of a smooth function consists in an indeterminacy, i.e., from every vertex we may descend into not a unique vertex that lies below. Since we use the Alexander duality, it is enough to define the gradient flow from critical points of index 1 to critical points of index 0.
By a tangent vector ξ = (v, e) ∈ T M we mean a pair consisting of a vertex v and an edge e ∋ v which lie in the boundary of M . We say that ξ is increasing if another end of e lies at a higher level of f ; otherwise, we say that ξ is a decreasing vector. Moreover on the set of increasing (or decreasing) tangent vectors at a given point we fix a natural order that is induced by ordering of the coordinate directions.
Let v be a critical point of f . If v has index 0, then exactly three tangent vectors are drawn from v and all of them are increasing. If v is a nondegenerate point of index 1, then to every edge of a decreasing tangent vector we assign some label S that is equal to ±1 and is as follows: The list of all types of critical points shows that all such edges lie in the boundary of M and there are at least two of them. If there are exactly two such edges then we mark one by +1 and another by −1. If there are three such edges then two of them belong to an elementary face from the boundary of M and we mark these two edges by the same label, say by +1, and then mark the left edge by −1.
Let v be a critical point of index 0, and v ′ be a critical point of index 1. A pair of tangent vectors ξ = (v, e) and ξ ′ = (v ′ , e ′ ) is called a gradient pair, if ξ is an increasing vector, ξ ′ is a decreasing vector, and the following conditions hold: there exists a sequence of increasing tangent vectors (e i , v i ), i = 0, . . . , k such that e 0 = e, v 0 = v, the edge e i connects v i and v i+1 , e k = e ′ , and for every vertex v i the vector (e i , v i ) is greater (with respect to the order) than all other tangent vectors which start at this vertex and lie in M .
Exactly in this case we will write that ξ < ξ ′ . We mean by the gradient flow of f on M the family of gradient pairs of tangent vectors: By definition, a path in M is a family of successively traversed edges from M . The path formed by edges e 0 , . . . , e k from the definition of a gradient pair is called gradient.
We define the boundary linear operator by the formula: We recall that M 0 and M 1 are the sets of critical points of indices 0 and 1.
Let us remark that this definition is correct because from every critical point of index 1 there are drawn two decreasing tangent vectors labeled by different signs. Therewith we may go down until we achieve (in finitely many steps) a critical point of index 0. Moreover v + 0 and v − 0 are uniquely defined because we always choose the highest tangent vector. If we consider two gradient paths γ 1 and γ ′ 1 going down to v + 0 and v − 0 , then together these paths form a path whose neighborhood forms a handle of index 1 attached when the value of f passes through f (v 1 ) (see §2).
Let us consider the three-dimensional body M ′ = K\M , the complement to M , and a function h = −f on M ′ . Clearly, where h achieves its minimal value. Let C ′ i be the vector space over Z 2 generated by the elements of M ′ i . We have the isomorphisms C ′ i = C 2−i , i = 1, 2, and C ′ 0 = C 2 ⊕ Z 2 , where the Z 2 component is generated by p 0 . By applying the construction of ∂ 1 to h we obtain the boundary operator ∂ 1 h : C ′ 1 → C ′ 0 . Let us consider the dual spaces C ′ * i , i = 0, 1, 2, consisting of Z 2 -valued linear functions on C ′ i . There are isomorphisms C ′ * i = C ′ i , i = 0, 1, 2, which assign to every Z 2 -function on C ′ i the formal sum of points from M ′ i , at which the function does not vanish. The operator ∂ 1 h defines the adjoint operator We put

Theorem 2
The homology groups of the chain complex C * = (C i , ∂ i ) are isomorphic to the homology groups of M with coefficients in Z 2 : Proof. For every critical point of index 1 there exist two paths such that they go down to critical points of index 0 in the directions labeled by different marks. This pair of gradient paths is combined into the path that connects two critical points of index 0 and hence the formal sum of these points belongs to the image of , and, since we consider homology with coefficients in a field, H 1 (M ) = H 1 (C * ). The theorem is proved.

An algorithm for computing the boundary operators
The approach described above may be used for constructing an algorithm as follows: Initially we have some array M = M [i, j, k], where i, j, k = 1, . . . , N , whose entries are equal to 0 or 1. At the input of the algorithm we have a preprocessed array M . At the output we obtain the lists C0 and C1 of critical points of indices 0 and 1 (as it is described above every monkey saddle contributes into C1 two elements; these lists give bases for the groups C 0 and C 1 ) and the matrix D = D[i, j] that defines the boundary operator ∂ 1 .
To every vertex v we correspond a formally adjoint vertex v ′ . Let us consider the function Ind which assigns to every vertex v ∈ M either its index, if the vertex is a critical point of index 0 or 1; either −1, if it is a monkey saddle; either −2 otherwise. The list C0 contains the critical points of index 0, and the list C1 consists of the critical points of index 1 and the points v and v ′ which correspond to every monkey saddle v. The function N um returns the number of a critical point of index i in Ci. To every vertex v there corresponds GF (v), the number of a certain critical point of index 0 to which one may go down from v. Finally, to every vertex v the functions Down 1 and Down 2 return the ends of decreasing tangent vectors starting at v, where Down 1 corresponds to the highest vector, or the empty values if there are no such vectors. Therewith to the critical points of index 1 the functions Down 1 and Down 2 return vertices corresponding to decreasing vectors with different signs. If v is a monkey saddle, the we assume that Down 1(v) and Down 2(v) are the ends of vectors (−1, 0, 0) and (0, −1, 0) attached to v. For the adjoint vertex we put Down 1(v ′ ) = v and Down 2(v ′ ) is the end of vector (0, 0, −1) attached to v. The description of the algorithm is given in Table 2.
Applying this algorithm twice to M and its complement M ′ , obtain the matrices D1 and D2 describing the operators ∂ 1 and ∂ 2 . Afterwards the for for all integer-valued vertices v ∈ M do 3: if Ind(v) = 0 then 4: Add(C0, v) 5: GF (v) := {N um(v)} 6: if Ind(v) = 1 then 7: Add(C1, v) 8: GF (v) := GF (Down 1(v)) 9: if GF (Down 1(v)) = GF (Down 2(v)) then 10: D(N um(v), N um(GF (Down 1(v)))) := 1 11: D(N um(v), N um(GF (Down 2(v)))) := 1 12: if Ind(v) = −1 then 13: Add(C1, v) 14: Add ( The complexity of the algorithm for constructing the matrices of the boundary operators, including the preprocessing of data, is equal to O(n) where n = N 3 is the number of vertices in K. For a subsequent computation of Betti numbers we have to compute the ranks of Di, for instance, by the Gauss method, and that needs O(n 3 c ) operations where n c is the number of critical points. Generically the relation between n and n c depends on the initial data.