The Problem
The Leibniz formula expresses the determinant of a square matrix as a sum over all $n!$ permutations:
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:
- 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.
- 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:
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 →