---
title: 'In-and-Out Algorithm: Cross-Domain Techniques'
url: https://www.emergentmind.com/topics/in-and-out-algorithm
type: topic
---

# In-and-Out Algorithm: Cross-Domain Techniques

Searching arXiv for recent and directly relevant papers on “In-and-Out” terminology and algorithms across the domains represented in the source material.
arXiv search: query = "In-and-Out Algorithm OR In-n-Out OR point in polygon even-odd semicomplete digraph out-branching in-branching stochastic matching convex bodies"
“In-and-Out Algorithm” does not denote a single canonical procedure across the literature. Rather, the label is used for several technically distinct algorithmic patterns organized around an alternation between entering a local structure and exiting with a classification, branching, sample, or local certificate. In computational geometry it is closely associated with the even–odd rule for point-in-polygon queries; in directed graph theory it describes the search for an out-branching arc-disjoint from an in-branching; in stochastic matching it appears as “in-n-out” local computation; and in convex-body sampling it names a specific Gaussian forward/backward Markov chain with uniform stationary distribution on a convex body [1207.3502], [2302.06177], [2411.08805], [2405.01425].

## 1. Terminological scope

The term is best read as a family of domain-specific meanings rather than as the name of one universally standardized algorithmic object. The usages represented in the literature are summarized below.

| Domain | Meaning of “in-and-out” | Representative paper |
|---|---|---|
| Point-in-polygon | Even–odd parity rule: boundary crossings toggle inside/outside | [1207.3502] |
| Convex polygons | Perpendicular-based in/out judgment reduced to a quadrilateral | [2201.11542] |
| Semicomplete digraphs | Arc-disjoint out-branching and in-branching with prescribed roots | [2302.06177] |
| Semicomplete compositions | Polynomial construction or certification of good \((u,v)\)-pairs | [2302.08283] |
| Stochastic matching | “In-n-out” LCAs with bounded in-queries and out-queries | [2411.08805] |
| Convex-body sampling | Two-step Gaussian walk with rejection-conditioned return to the body | [2405.01425] |

A useful unifying observation is that all of these formulations alternate between a local excursion and a global decision rule. In geometry, a ray crosses a boundary and parity toggles. In digraph theory, an outward-oriented spanning structure is paired with an inward-oriented one. In local computation, outward probes and inward dependencies are jointly controlled. In sampling, a point diffuses outward into ambient space and is then drawn back into the feasible region. This suggests a recurrent conceptual motif, but not a single shared formalism.

## 2. Geometric uses: parity rules and convex-polygon localization

In computational geometry, the phrase “in-and-out” most directly refers to the classical even–odd rule for the point-in-polygon problem. A ray is cast from the query point \(Q\), the number of polygon-edge intersections is counted, and the point is classified as inside when the count is odd and outside when it is even. The 2012 treatment formalizes this rule for arbitrary polygons, including complex polygons whose edges intersect each other creating holes, and proves correctness in linear time. Boundary points are treated as inside. The algorithm translates the polygon so that \(Q=(0,0)\), uses the positive \(x\)-axis as the primary ray, skips vertices on the \(x\)-axis, and replaces runs of such vertices by auxiliary edges between the nearest non-\(x\)-axis vertices. When skipped vertices include at least one with positive \(x\)-value, the intersection test is taken against the complete \(x\)-axis rather than only its positive part, which preserves the correct parity in degenerate configurations [1207.3502].

The same literature distinguishes simple polygons from complex polygons and emphasizes that self-intersections, holes, overlapping regions, vertices on the ray, and edges lying on the ray are precisely the configurations that make naive implementations error-prone. The robust formulation avoids double counting by ensuring that every counted edge either clearly intersects the \(x\)-axis or clearly does not. Its correctness proof combines the general correctness of even–odd classification with a case analysis showing that auxiliary edges preserve the desired parity for all possible quadrant configurations of the non-\(x\)-axis endpoints [1207.3502].

A different geometric use appears in work specialized to simple convex polygons. There, the in/out decision is organized around a perpendicular from the query point \(P\) to the line through a chosen edge, a test against the segment connecting the outer points of the neighboring edges, and a reduction of the global problem to a point-in-quadrilateral test decided by a standard ray algorithm. The method is explicitly designed for simple convex polygons, not general non-convex or self-intersecting polygons, and its reported complexity ranges from \(O(1)\) to \(O(n)\). The paper states that the algorithm has fewer intersections and greatly improves the efficiency of the judgment [2201.11542].

## 3. Directed graphs: out-branchings, in-branchings, and good \((u,v)\)-pairs

In digraph theory, the in-and-out problem is the existence of an out-branching \(B_u^+\) rooted at \(u\) and an in-branching \(B_v^-\) rooted at \(v\) that are arc-disjoint. An out-branching is a connected spanning subdigraph in which every vertex except the root has in-degree one; dually, an in-branching is a connected spanning subdigraph in which every vertex except the root has out-degree one. A good \((u,v)\)-pair is a pair \((B_u^+,B_v^-)\) with no arc in common. The general decision problem is sharply harder than the classical problem of finding many out-branchings of the same orientation: it is already NP-complete to decide whether a digraph has one out-branching arc-disjoint from some in-branching [2302.06177].

For semicomplete digraphs, however, the structure becomes tractable. A complete characterization states that a semicomplete digraph \(D\) has a good \((u,v)\)-pair if and only if, for every choice of \(z,w\in V\), there exist arc-disjoint paths \(P_{u,z}\) and \(P_{w,v}\), and \(D\) is not isomorphic to any of a fixed list of five small exceptional digraphs. The proof is constructive and yields a polynomial algorithm that either finds the two branchings or certifies that no such pair exists. The same work shows that for semicomplete digraphs the case of \(2\)-arc-strong connectivity is especially favorable: strong spanning arc-decompositions and the existence of good \((u,v)\)-pairs align in a way that confirms a conjecture of Bang-Jensen for this class [2302.06177].

The 2023 extension to semicomplete compositions replaces each vertex of a semicomplete digraph \(S\) by an arbitrary digraph \(H_x\). This produces a significantly richer class, encompassing quasi-transitive digraphs through known decomposition theorems. The main result is again polynomial solvability for prescribed \(u,v\): given a composition \(D=S[H_1,\ldots,H_s]\), where \(S\) is semicomplete or transitive, there is a polynomial algorithm deciding whether \(D\) has a good \((u,v)\)-pair and outputting one when it exists. In particular, the result implies polynomial-time solvability for quasi-transitive digraphs and confirms a conjecture of Bang-Jensen and Gutin from 1998 [2302.08283].

## 4. “In-n-out” local computation in stochastic matching

In stochastic matching, the phrase is used in a more specialized form: “in-n-out LCAs.” The underlying problem is to choose, non-adaptively, a sparse subgraph \(H\) of a graph \(G\) so that the realized edges in \(H\) contain an approximate maximum matching of the unknown random realization \(G_p\). The conceptual contribution is to connect local computation algorithms to stochastic matching for the first time and to refine the LCA model by simultaneously bounding two dual notions: out-queries and in-queries [2411.08805].

For a queried vertex \(v\), the out-query set \(Q^+(v)\) is the set of vertices for which at least one incident edge is discovered while computing the output for \(v\). Dually, the in-query set \(Q^-(v)\) is the set of vertices \(u\) such that \(v\in Q^+(u)\). A correlated set \(\Psi(v)\) consists of all vertices whose query explorations overlap with that of \(v\). For natural LCAs, whose discovered prefixes always induce connected subgraphs, bounded expected in-queries and bounded expected out-queries imply bounded expected correlated-set size:
\[
\mathbb{E}[\psi(v)] \le Q^+Q^-.
\]
This is the technical form of “in-n-out”: local computation with bounded outward exploration and bounded inward dependence [2411.08805].

The stochastic matching application uses this limited-correlation property to control the dependence structure of random matching decisions. The global construction of the sparse queried subgraph is simple: sample \(R\) independent realizations of \(G\), compute a maximum matching in each, and output their union. The main theorem states that for every fixed \(\varepsilon>0\) and every \(p\in(0,1]\), every graph \(G\) has a subgraph \(H\) of maximum degree \((1/p)^{O_\varepsilon(1)}\) such that
\[
\mathbb{E}[\mu(H\cap G_p)] \ge (1-\varepsilon)\,\mathbb{E}[\mu(G_p)].
\]
Before this result, a \((1-\varepsilon)\)-approximation was known only with quasi-polynomial degree in \(1/p\); the in-n-out framework removes that gap by making the necessary correlation control polynomial in \(1/p\) rather than quasi-polynomial [2411.08805].

A recurring clarification in this line of work is terminological: “in-n-out” refers only to the dual bounds on in-queries and out-queries. It is not related to geometric in/out tests, queueing systems, or input/output throughput [2411.08805].

## 5. Convex-body sampling: the specific “In-and-Out” random walk

A distinct and explicit usage appears in high-dimensional sampling. Here “In-and-Out” is the name of a Markov chain for approximately uniform sampling from a convex body \(\mathcal K\subseteq\mathbb R^d\) accessible via a membership oracle. The target distribution is
\[
\pi^{\mathcal K}(x)=\frac{1_{\mathcal K}(x)}{\mathrm{vol}(\mathcal K)}.
\]
Each proper step consists of two Gaussian moves. From the current point \(x_i\in\mathcal K\), first sample
\[
y_{i+1}\sim \mathcal N(x_i,hI_d),
\]
then sample
\[
x_{i+1}\sim \mathcal N(y_{i+1},hI_d)
\]
conditioned on \(x_{i+1}\in\mathcal K\), implemented by rejection sampling. The chain can be interpreted as Gibbs sampling for the augmented density
\[
\pi(x,y)\propto \exp\!\left(-\frac{1}{2h}\|x-y\|^2\right)1_{\mathcal K}(x),
\]
and also as the composition of a forward heat flow and a backward heat flow [2405.01425].

The algorithm achieves state-of-the-art runtime complexity with stronger guarantees on the output than previously known, namely in Rényi divergence, which implies total variation, \(\mathcal W_2\), \(KL\), and \(\chi^2\). From an \(M\)-warm start, it reaches \(\eu R_q(\mu_m^X\|\pi^X)\le \varepsilon\) after
\[
m=\widetilde{\mathcal O}\!\bigl(qd^2\|\mathrm{Cov}(\pi^X)\|_{\mathsf{op}}\log\tfrac{M}{\eta}\bigr)
\]
iterations, and in the isotropic case after
\[
m=\widetilde{\mathcal O}\!\bigl(qd^2\log\tfrac{M}{\eta}\bigr)
\]
iterations, with corresponding expected membership-query bounds \(\widetilde{\mathcal O}(qMd^2\|\mathrm{Cov}(\pi^X)\|_{\mathsf{op}})\) and \(\widetilde{\mathcal O}(qMd^2)\) [2405.01425].

The proof departs from conductance-based analyses of Ball walk and Speedy walk. Instead, convergence is derived from a stochastic diffusion perspective: contraction toward the target is governed by the Poincaré and log-Sobolev constants of the stationary distribution. This makes the algorithm notable not only as a sampling routine but also as a bridge between oracle-based convex-body sampling and diffusion-based functional-inequality analysis [2405.01425].

## 6. Cross-cutting structure, distinctions, and related usage

Despite the shared label, these algorithms solve different problems on different state spaces. The geometric parity rule is a classification procedure on planar embeddings. The digraph formulations are spanning-structure packing problems in directed graphs. The in-n-out LCA framework is a dependence-control principle for local randomized algorithms. The convex-body sampler is a Markov chain on \(\mathbb R^d\) with rejection-conditioned Gaussian updates. Confusing these meanings obscures the fact that they share terminology but not a common operational core.

A common misconception is that “in-and-out” always means the same kind of boundary test. That is accurate for the even–odd point-in-polygon rule, where every boundary crossing toggles between outside and inside [1207.3502]. It is inaccurate for the digraph and stochastic-matching usages, where “in” and “out” refer to opposite arc orientations or to inward and outward query incidence [2302.06177], [2411.08805]. It is likewise inaccurate for convex-body sampling, where the name refers to an outward Gaussian diffusion followed by a return step conditioned on re-entering the body [2405.01425].

A related but looser descriptive use appears in deterministic in-place permutation inversion. One exposition states that the 2019 algorithm does not explicitly use the phrase “In-and-Out Algorithm,” but its control structure repeatedly goes into the permutation’s functional graph from local starting points, performs local restructuring through procedures such as Tortoise-and-Hare, Make-Segments, and Restore-Long-Cycle, and returns to global scans. Under the strict requirement that every array entry remain in \([1,\ldots,n]\), it achieves \(O(n^{3/2})\) time with \(O(\log n)\) bits of additional memory [1901.01926].

This suggests a broader interpretive pattern: “in-and-out” recurrently names algorithms that alternate between a global traversal and a local excursion whose outcome can be encoded compactly enough to support a global decision, update, or certificate. That interpretation is descriptive rather than formal, but it captures why the same phrase recurs across computational geometry, digraph theory, local computation, sampling, and related algorithmic constructions.

Source: https://www.emergentmind.com/topics/in-and-out-algorithm