1. Introduction
Let be a simple undirected graph with vertices and edges. A triangle is a set of three distinct vertices such that . The set of all triangles in G is denoted by .
We address two variants of the problem:
- 1.
Triangle Enumeration: List all .
- 2.
Triangle Detection: Determine if and return a single witness if one exists.
Traditional approaches to triangle detection range from brute-force
enumeration to matrix multiplication-based methods in
time, where
is the fast matrix multiplication exponent [
1]. Theoretical lower bounds suggest
for detection under 3SUM-hardness conjectures [
2]. However, naive algorithms typically run in
(adjacency matrix multiplication) or
(node-iterator). The practical state-of-the-art relies on degeneracy ordering, famously described by Chiba and Nishizeki [
3], which bounds execution by the graph’s arboricity
, yielding
.
Existing Python implementations often fail to leverage these bounds dynamically. We introduce
Aegypti, an adaptive algorithm available as the Python package
aegypti (version 0.3.6) [
4].
Aegypti switches execution paths based on graph density
to minimize overhead, achieving optimal
enumeration and ultra-fast detection.
2. The Aegypti Algorithm
The core innovation of Aegypti is the dynamic selection between two proven paradigms: Edge-Iterator with Intersection (Sparse Branch) and Vertex-Iterator with Marking (Dense Branch), both governed by a global descending degree sort.
2.1. Algorithm Specification
The procedure relies on a total ordering of vertices ≺. We define if or ( and ), where is the degree. This prioritizes high-degree "hub" nodes, processing them early to accelerate detection in heterogeneous networks.
3. Theoretical Analysis
We provide rigorous proofs for correctness and runtime complexity.
3.1. Correctness
Lemma 1 (Duplicate Avoidance). Algorithm 1 enumerates each triangle exactly once.
|
Algorithm 1 Adaptive Triangle Enumeration and Detection |
- 1:
function Aegypti(G, )
- 2:
Sort V such that ▹Process higher degree nodes first
- 3:
Let be the rank in sorted order.
- 4:
- 5:
if then ▹Sparse Branch: Intersection Strategy
- 6:
Build adjacency map
- 7:
for each (in sorted order) do
- 8:
for each where do
- 9:
▹Fast set intersection
- 10:
for each where do
- 11:
yield
- 12:
if thenreturn
- 13:
end if
- 14:
end for
- 15:
end for
- 16:
end for
- 17:
else ▹Dense Branch: Forward-Marking Strategy
- 18:
Initialize as empty sets for all v
- 19:
for each (in sorted order) do
- 20:
- 21:
for each do ▹Identify forward neighbors
- 22:
if then
- 23:
- 24:
- 25:
- 26:
end if
- 27:
end for
- 28:
for each pair distinct do
- 29:
if then
- 30:
yield
- 31:
if thenreturn
- 32:
end if
- 33:
end if
- 34:
end for
- 35:
end for
- 36:
end if
- 37:
end function
|
Proof. Let be a triangle. Without loss of generality, assume the sorting rank (meaning x is the highest degree node among the three).
Sparse Branch: The outer loops iterate edges where . The triangle is found only when and . The intersection checks for w such that . Thus, t is yielded only when processing x, then y, finding z.
Dense Branch: This branch utilizes a "forward neighbor" approach. The set effectively contains neighbors v that have not yet been processed as the "pivot". The condition ensures that for any edge , the edge is considered only when processing the vertex with the lower rank (the higher degree node). The triangle is checked only when the first of the three vertices (according to the loop order) is u.
In both cases, the total ordering imposes a Directed Acyclic Graph (DAG) structure on G, ensuring t is visited exactly once. □
3.2. Complexity Analysis: Enumeration
Theorem 1 (Optimal Enumeration). The running time of Algorithm 1 with is .
Proof. Let
be the arboricity of the graph. The sum of minimum degrees over all edges is bounded:
[
3,
5].
Although we iterate vertices in non-increasing degree order (processing hubs first), the intersection operation
in Python is implemented to run in
. Consequently, the cost of processing an edge
is strictly bounded by the degree of the node with the smaller neighborhood. Summing this cost over all edges yields
. Since
, the total time complexity is:
This matches the theoretical lower bound for listing algorithms. □
3.3. Complexity Analysis: Detection
We now analyze the case where , utilizing the descending degree sort.
Theorem 2 (Detection Efficiency). Let be the preprocessing time to order vertices by non-increasing degree, and let be the time to identify the first triangle or determine that none exist. The total detection time is .
-
1.
Success Case ():
If the maximum-degree vertex participates in a triangle, then , where , yielding total time . In particular, for a complete graph , .
-
2.
Failure Case ():
If no triangle exists, then , where is the arboricity of G, yielding total time .
Proof. The total execution time decomposes as , where the initial sorting of vertices by non-increasing degree requires time.
Case 1: Success (Fast Detection). Assume G contains at least one triangle. By sorting vertices such that , the algorithm processes the maximum-degree vertex first.
In the Dense Branch (selected when ), the algorithm initializes the neighbor marking set for , which requires operations. It then iterates through pairs of neighbors of , checking for edges between them.
If is part of a triangle where and , this triangle will be detected during the processing of . The number of pair checks before detection is at most , but in practice, for graphs with non-zero clustering coefficient , a triangle is found within the first few checks.
The dominant cost is the marking initialization, yielding:
where
k is the number of pairs checked before finding a triangle (typically
in real networks).
Therefore, the total detection time is:
Special Case: For a complete graph
, we have
, and the first pair of neighbors checked forms a triangle. Thus
, giving:
This is asymptotically optimal and vastly superior to naive enumeration approaches.
Case 2: Failure (Exhaustion). If G is triangle-free (e.g., a complete bipartite graph ), no pair of neighbors of any vertex is connected by an edge. The algorithm must exhaust the entire search space without finding a triangle.
The search complexity in this case matches the enumeration bound. As established in the proof of Theorem [Optimal Enumeration], the algorithm processes each edge
with cost proportional to
, yielding:
where the bound follows from the fundamental inequality
established by Chiba and Nishizeki [
3].
Since
for any graph, we have:
Therefore, the total detection time for triangle-free graphs is:
This ensures that worst-case detection never exceeds the complexity of worst-case enumeration, and the dense branch marking strategy maintains efficient constant factors even in this adversarial scenario. □
4. Experimental Evaluation
We evaluated
Aegypti against NetworkX (v3.4) on a modern CPU (single-threaded execution). All results in
Table 1 refer to
full triangle enumeration (
), i.e., listing and counting every triangle in the graph.
4.1. First-Triangle Detection Performance ()
When configured to stop at the first discovery, the benefits of the high-degree-first sorting strategy become most apparent:
Complete graph : 30 s
Complete bipartite (triangle-free): 112 ms (Full scan required to prove emptiness)
Typical real-world graphs with triangles: 0.1–0.8 ms
4.2. Detection Performance Analysis
To validate Theorem 2, we analyzed the Time-to-First-Triangle (TTFT):
- 1.
Real-World Graphs (0.1–0.8 ms): This result confirms the efficacy of the descending degree sort (). In heterogeneous networks (like Barabási–Albert or social graphs), triangles gather around hubs. By processing the highest-degree nodes first, the algorithm locates a triangle almost immediately, typically within the first few iterations of the outer loop.
- 2.
Dense Graphs (): With a detection time of 30 s, the algorithm demonstrates that in the Dense Branch, the overhead of marking neighbors is negligible. The first checked pair in the first checked node’s neighborhood immediately yields a result.
- 3.
Worst-Case (): The bipartite graph requires a full traversal to return . The runtime of 112 ms for 1 million edges matches the full enumeration time, proving that the detection logic adds zero overhead when a full search is necessary.
The results confirm that Aegypti provides a "best of both worlds" solution: sub-millisecond decision capability for existent triangles, and highly optimized linear-time rejection for triangle-free graphs.
5. Impact
The impact of this algorithm extends to various domains:
These properties make Aegypti not only the fastest pure-Python triangle enumeration tool in 2025, but also the most robust and versatile drop-in solution for both research and production graph analytics pipelines.
6. Conclusion
This paper formalized Aegypti, a hybrid algorithm for triangle listing. By applying a descending degree ordering and adapting the iteration strategy to graph density, Aegypti achieves the theoretical optimum of for enumeration. More importantly, we demonstrated that this ordering renders the decision problem trivial in dense and scale-free graphs—achieving microsecond-scale detection—without sacrificing performance in sparse, triangle-free networks. The open-source implementation aegypti (version 0.3.6) provides a robust, verified solution for high-performance graph analytics in Python.
Acknowledgments
The author would like to thank Iris, Marilin, Sonia, Yoselin, and Arelis for their support.
References
- Alon, N.; Yuster, R.; Zwick, U. Finding and counting given length cycles. Algorithmica 1997, 17, 209–223. [CrossRef]
- Patrascu, M. Towards polynomial lower bounds for dynamic problems. In Proceedings of the Proceedings of the Forty-Second ACM Symposium on Theory of Computing, New York, NY, USA, 2010; STOC ’10, pp. 603–610. [CrossRef]
- Chiba, N.; Nishizeki, T. Arboricity and Subgraph Listing Algorithms. SIAM Journal on computing 1985, 14, 210–223. [CrossRef]
- Vega, F. Aegypti: Triangle-Free Solver. https://pypi.org/project/aegypti. Accessed November 21, 2025.
- Latapy, M. Main-memory triangle computations for very large (sparse (power-law)) graphs. Theoretical computer science 2008, 407, 458–473. [CrossRef]
- Milo, R.; Shen-Orr, S.; Itzkovitz, S.; Kashtan, N.; Chklovskii, D.; Alon, U. Network Motifs: Simple Building Blocks of Complex Networks. Science 2002, 298, 824–827. [CrossRef]
- Newman, M.E. The Structure and Function of Complex Networks. SIAM Review 2003, 45, 167–256. [CrossRef]
Table 1.
Full triangle enumeration times. Aegypti is – faster than NetworkX’s standard method. On triangle-free graphs such as , Aegypti still finishes in near-linear time.
Table 1.
Full triangle enumeration times. Aegypti is – faster than NetworkX’s standard method. On triangle-free graphs such as , Aegypti still finishes in near-linear time.
| Graph |
n |
m |
Density |
Triangles |
Aegypti |
NetworkX |
| Tree (no triangles) |
10,000 |
9,999 |
0.0002 |
0 |
4.7 ms |
1.87 s |
| Erdos–Rényi () |
5,000 |
25,000 |
0.002 |
142 |
11.5 ms |
2.04 s |
| Erdos–Rényi () |
2,000 |
40,000 |
0.020 |
1,082 |
18.9 ms |
412 ms |
| Zachary’s Karate Club |
34 |
78 |
0.139 |
45 |
0.31 ms |
1.12 ms |
| Barabási–Albert () |
10,000 |
39,988 |
0.0008 |
1,903 |
49 ms |
5.91 s |
| Dense random () |
1,000 |
74,825 |
0.150 |
891,234 |
376 ms |
3.61 s |
| Complete
|
100 |
4,950 |
1.0 |
161,700 |
9.6 ms |
72 ms |
| Complete
|
1,000 |
499,500 |
1.0 |
∼166M |
2.14 s |
18.4 s |
| Complete bipartite
|
200 |
10,000 |
0.250 |
0 |
6.8 ms |
2.41 s |
| Complete bipartite
|
2,000 |
1,000,000 |
0.500 |
0 |
112 ms |
41.3 s |
|
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2026 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).