Repository navigation
Migrate the randomized-algorithm framework into AlgoLib - #24
Open
AntoineduFresne wants to merge 1 commit into
Open
AntoineduFresne wants to merge 1 commit into
AntoineduFresne wants to merge 1 commit into
Conversation
Bring in the framework of the master's thesis "Analysis of Randomised Algorithms in Lean 4" (ETH Zurich, 2026). Source: github.com/AntoineduFresne/Master-Thesis, commit 765761e. - AlgoLib/Theory/Randomized: the framework (randomness, cost, correctness) - AlgoLib/Theory/Graph/MultiGraph.lean: multigraphs with contraction - AlgoLib/Util: Partition, Counting, HarmonicSums - AlgoLib/Algorithms/Graph/MinCut/Karger.lean: Karger's minimum cut - AlgoLib/Algorithms/Randomized: eight more algorithms and a tutorial Adapted to Lean v4.30.0-rc2, that is: universes are declared in TimeMT.lean and Tactics.lean (autoImplicit is off), and MultiGraph.lean imports Mathlib.Data.Nat.Lattice. Importantly, no theorem statement were changed.
AntoineduFresne
marked this pull request as ready for review
October 13, 2026 09:17
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR brings in the randomized-algorithm framework of my master's thesis, Analysis of Randomised Algorithms in Lean 4 (ETH Zürich, 2026), with its nine case studies. Karger's minimum cut is one of them. The code comes in as it is, and we refactor afterwards.
765761e, the commit the thesis cites.DESIGN.mdexplains the design choices.What this PR adds
30 Lean files (about 11,000 lines) and their 30 imports in
AlgoLib.lean. There is no new dependency: the code uses Mathlib and cslib'sTimeM, which AlgoLib already requires.ARA/Infrastructure/(the framework)AlgoLib/Theory/Randomized/ARA/Helpers/MultiGraph.leanAlgoLib/Theory/Graph/MultiGraph.leanARA/Helpers/(Partition,Counting,HarmonicSums)AlgoLib/Util/ARA/Algorithms/Karger/Karger.leanAlgoLib/Algorithms/Graph/MinCut/Karger.leanTutorial.leanAlgoLib/Algorithms/Randomized/ARA/FutureWork/SPMF.leanAlgoLib/Theory/Randomized/FutureWork/SPMF.leanThe entry point is
AlgoLib/Algorithms/Randomized/Tutorial.lean. It builds small algorithms step by step and names the file where each tool is defined.Design in brief
An algorithm is written once, polymorphic in its monad:
{M} [Monad M] [RandMonad M] [MonadCost ℕ M]RandMonadhas one primitive, the uniform drawrandFin n.MonadCosthas one primitive,tick, which by default does nothing. Proofs useLawfulRandMonad, whosetoPMF : M α → PMF αis a monad morphism. So a theorem proved once holds in every lawful monad.PMFandTimeMT ℕ PMFare lawful;IOis not, by design.The same definition has four readings:
MIOPMFTimeMT ℕ IOTimeMT ℕ PMFTimeMTis a writer-style monad transformer over cslib'sTimeM. Expected costs are computed in the last reading.Correctness comes in three tiers. Dirac: the output distribution is a point mass at a specification. Exact distribution: the output distribution itself is the theorem. Support and probability: every possible output satisfies a property, and the right answer has a probability bound. One-sided error is stated this way. Cost comes in three readings, from strongest to weakest: the cost distribution, the expected cost, and a tail bound by Markov's inequality.
Main results
Here
nis the input length, or the number of vertices for Karger, andmthe number of edges. Costs are expected numbers of ticks unless stated otherwise.karger_isCut). With probability at least2/(n(n−1)), each side is a minimum cut (karger_finds_min). Running itktimes and keeping the smallest reported value gives the minimum value with probability at least1 − (1 − 2/(n(n−1)))^k(karger_amplified).≤ (n−2)·m(karger_cost_le).k+1runs:≤ (k+1)·(n−2)·m(karger_amplified_cost_le).quicksort_correct).= 2(n+1)H(n) − 4non distinct inputs (quicksort_cost_exact).≤ C(n,2)on all inputs (quicksort_cost_le). Tail:ℙ[cost > k] ≤ C(n,2)/(k+1)(quicksort_runtime_tail).k-th order statistic (quickselect_correct).quickselect_cost_exact).≤ 4non distinct inputs (quickselect_cost_le_linear).ℙ[output = some a] = count(a)/n(reservoir_correct).= n − 1(reservoir_cost_exact).shuffle_uniform).P = 0givestruewith probability 1 (schwartzZippel_complete). Over an integral domain,P ≠ 0givestruewith probability≤ P.totalDegree / S.card(schwartzZippel_sound).= 1(schwartzZippel_cost_exact).A * B = Cgivestruewith probability 1 (freivalds_complete).A * B ≠ Cgivestruewith probability≤ 1/2(freivalds_sound).= 3n²(freivalds_cost_exact).= Σ_{r<n} n/(r+1), that isn·H(n)(couponCollector_cost_exact).treap_correct).≤ 3·log₂(n+3) + 4on distinct keys (treap_expected_height_le).Changes from the thesis code
The namespace
ARAbecameAlgoLib. Imports and the source paths quoted in docstrings follow the new layout, andLawfulRandMonad.leanbecameRandomness/RandMonad.lean.Five docstring passages spoke of ARA or of GraphLib, and were rewritten for AlgoLib. The largest is in
Theory/Graph/MultiGraph.lean: its section "Standing in for GraphLib" is now "Relation toAlgoLib.Graph".AlgoLib is on Lean
v4.30.0-rc2, the thesis code onv4.32.0. That took three lines:Theory/Randomized/Complexity/TimeMT.lean:21andTheory/Randomized/Tactics.lean:25declareuniverse u v wanduniverse u. The thesis code letautoImplicitbind them, and AlgoLib turns it off.Theory/Graph/MultiGraph.lean:8importsMathlib.Data.Nat.Lattice. Mathlib renamed that fileMathlib.Order.Lattice.Natin June 2026.No theorem statement changed.
Checks
lake buildsucceeds. There is nosorry.#print axiomson all 340 public theorems and lemmas: each uses at mostpropext,Classical.choiceandQuot.sound.showused to change the goal.For the refactor
AlgoLib, for exampleprob,expVal,FreshandrandIdx. They probably belong in sub-namespaces.TimeMTis declared in cslib's namespaceCslib.Algorithms.Lean, next toTimeM. It is astructure. Whether it should be adeflike Mathlib'sWriterT, so thatTimeMT T Idreduces toTimeM T, is the TimeT question still open between us.MonadCost,TimeMT) is independent of the RAM model onexperimental-RAM-model. How the two should relate is open.Tactics.leanadds 30 globalsimpandgrindattributes, almost all on Mathlib'sPMFlemmas. No other AlgoLib file usesPMFtoday, but every file that imports the framework inherits them.MultiGraphdoes not build onAlgoLib.Graph. Karger draws an edge uniformly at random, in a program, and counts crossing edges with multiplicity. So edges are aList (Sym2 α). The module docstring explains the choice and sketches two adapters.PMF, may belong inAlgoLib/Mathlib/, as CONTRIBUTING.md asks. I have not sorted them yet.Algorithms/Graph/MinCut/, the other algorithms inAlgorithms/Randomized/.Karger.leanis tidied up to line 579 andTutorial.leanis reviewed up to line 1307. Markers in the files show where.FutureWork/SPMF.leanis unfinished. It proves that retrying until success ends with probability 1 (mass_retry_eq_one), but it is not yet connected to the correctness and cost machinery.