Research Paper

berkeDet: A Dual Iterative Algorithm for Exact Determinant Computation via Block Replication and Index Mapping

A new approach to the Leibniz formula that achieves $O(n \cdot n!)$ complexity while preserving lexicographic order — by eliminating sorting, reversal, and per-permutation sign computation entirely.

Berke Gülmen  ·  Meryem Gülmen  ·  Ömer Gülmen
Authors Berke Gülmen, Meryem Gülmen, Ömer Gülmen
Date 26 February 2026
Keywords Leibniz Formula, Determinant, Permutation, Lexicographic Order, Sign Function
Complexity $O(n \cdot n!)$ total — $O(1)$ amortized per sign

The Problem

The Leibniz formula expresses the determinant of a square matrix as a sum over all $n!$ permutations:

$$\det(A) = \sum_{\sigma \in S_n} \operatorname{sgn}(\sigma) \prod_{i=1}^n a_{i,\sigma(i)}$$

To compute this directly, you need two things: all $n!$ permutations of $\{1, 2, \ldots, n\}$ in order, and the sign ($+1$ or $-1$) of each one.

The classical way to do this has two costly bottlenecks:

  1. Generating the next permutation — the naive lexicographic method requires sorting a suffix at each step, costing $O(n \log n)$ per permutation. The well-known Narayana Pandita optimization replaces sorting with a suffix reversal, but still requires scanning for a pivot, swapping, and reversing at every step.
  2. Computing the sign — the standard method counts inversions, costing $O(n^2)$ per permutation. Over all $n!$ permutations, this alone costs $O(n^2 \cdot n!)$.

Our Solution

berkeDet eliminates both bottlenecks using two subroutines that work by block replication and index mapping — no sorting, no reversal, no scanning, and no inversion counting at any step:

meryemSign generates all $n!$ signs in $\Theta(n!)$ total time — an amortized cost of $O(1)$ per sign. It exploits the recursive block structure of the sign sequence: when extending from $S_{n-1}$ to $S_n$, the signs follow a simple alternating pattern:
$$\underbrace{+\mathbf{s}}_{k=1},\quad \underbrace{-\mathbf{s}}_{k=2},\quad \underbrace{+\mathbf{s}}_{k=3},\quad \underbrace{-\mathbf{s}}_{k=4},\quad \ldots$$
where $\mathbf{s}$ is the sign list for $S_{n-1}$. The entire list is built by replicating and flipping blocks — no arithmetic beyond sign flips.
meryemPer generates all $n!$ permutations in lexicographic order in $O(n \cdot n!)$ total time. It builds the complete list layer by layer: for each possible first element, it remaps the previous layer's permutations onto the remaining elements. No sorting, no reversal, no pivot scanning appears at any step — the lexicographic order emerges naturally from the construction.

Why It Matters

The classical lexicographic approach costs $O(n^2 \cdot n!)$ total. berkeDet costs $O(n \cdot n!)$ — a factor of $n$ faster. This matches the speed of the Steinhaus–Johnson–Trotter algorithm, which is the fastest non-lexicographic method. But SJT does not produce permutations in lexicographic order.

berkeDet is the first method to achieve $O(n \cdot n!)$ total cost while preserving lexicographic order.

Comparison with Existing Methods

Method Tperm Tsign Total Lex. Sort-free
Naive lex. + inversion count $O(n \log n)$ $O(n^2)$ $O(n^2 \cdot n!)$ Yes No
Narayana + inversion count $O(n)$ $O(n^2)$ $O(n^2 \cdot n!)$ Yes No*
Narayana + merge-sort sign $O(n)$ $O(n \log n)$ $O(n \log n \cdot n!)$ Yes No*
SJT + sign flip $O(n)$ $O(1)$ $O(n \cdot n!)$ No Yes
Heap + bookkeeping $O(1)$ amort. $O(1)$ amort. $O(n!)$ No Yes
berkeDet (this work) $O(n)$ amort. $O(1)$ amort. $O(n \cdot n!)$ Yes Yes

*Narayana Pandita replaces sorting with suffix reversal — a non-trivial optimization that still requires per-step pivot scanning, swapping, and reversal.

Abstract

We introduce berkeDet, a dual iterative algorithm that computes the determinant of an $n \times n$ matrix via the Leibniz formula using two independent subroutines: meryemSign, which generates the complete list of permutation signs in lexicographic order, and meryemPer, which generates the corresponding permutations. Both subroutines operate by block replication and index-mapping operations, avoiding the per-step overhead inherent in classical approaches.

In the standard lexicographic implementation of the Leibniz formula, computing the sign of each permutation via inversion counting costs $O(n^2)$ per permutation, and advancing to the next permutation via the naive lexicographic generator requires an $O(n \log n)$ suffix sort. Even the well-known Narayana Pandita optimization, which replaces the sort with a suffix reversal, still requires per-step scanning and swapping operations. By contrast, meryemSign generates all $n!$ signs in $\Theta(n!)$ total time — an amortized cost of $O(1)$ per sign — and meryemPer generates all permutations in $O(n \cdot n!)$ total time without any sorting, reversal, or pivot scanning at any step. The combined berkeDet algorithm therefore runs in $O(n \cdot n!)$ time, improving the per-permutation cost by a factor of $n$ over the classical lexicographic approach. We prove correctness by induction and verify the algorithm computationally for matrices up to order 8.

Scope and Applicability

Like all Leibniz-based methods, berkeDet has factorial time complexity and is intended for exact determinant computation of small to moderate matrices, symbolic algebra, and educational contexts — not as a replacement for $O(n^3)$ numerical methods such as LU decomposition. The contribution is to provide the most efficient known implementation of the Leibniz formula in lexicographic order — without sorting, reversal, or per-permutation sign computation.

Lexicographic order is useful in deterministic testing, symbolic algebra systems that expect canonical ordering, educational settings where natural ordering aids comprehension, and parallel computation where lexicographic ranges can be cleanly partitioned.

Computational Verification

berkeDet has been verified against NumPy's numpy.linalg.det for randomly generated integer matrices of orders $n = 1$ through $n = 8$. In all cases the results agreed to within floating-point tolerance ($< 10^{-9}$ relative error).

Detailed descriptions, proofs, source code, and a beginner-friendly Python tutorial:
View the Algorithm →     View Resources →

♥ Support This Research ♥