How to use this book
This book teaches you to design algorithms, not to memorize them. It is built on two ideas. Algorithms are compositions of reusable primitives, and you learn those primitives best by struggling with problems before being shown the solution.
Algorithms are made of primitives
A handful of simple ideas keeps appearing across computing. This book calls them primitives. A random fingerprint lets you compare two huge things by comparing two small ones. A structure that leaves spare room where the next item is likely to land does not have to shift everything when that item arrives. A single well-chosen cut through a network can prove that nothing more can be pushed through it. These ideas show up in problems that, on the surface, have little to do with one another.
Famous algorithms are usually compositions of such ideas. Take a method for planning a delivery tour. The method is guaranteed to stay within 50% of the shortest possible route whenever going direct is never longer than taking a detour. The guarantee comes from three ordinary ideas fitted together in the right way.
Once you start seeing the primitives, you start seeing the seams. And once you see the seams, you can take algorithms apart and build new ones.
For every primitive, I want you to know four things:
- what it promises,
- what it assumes,
- what it costs,
- and how it fails.
The failure mode matters as much as the idea itself. It tells you when an idea can be trusted, when it can be composed with another, and when you should reach for something else.
You will try things before I tell you the answer
Most chapters begin with a problem before they give you the idea that solves it.
That is deliberate.
I want you to push against the problem first. Try something, get stuck, notice where your idea breaks. Then, when the new primitive arrives, it has somewhere to attach.
Two boxes protect that part of the process:
- Try first (no AI). Attempt the problem before reading on. Put the AI away. Reach for a pen.
- No-AI check. Solve this one unaided. It shows you that you can do it alone.
If you sit in one of those boxes for a while and nothing works, the book has not malfunctioned. That struggle is part of the teaching.
Everywhere else, use AI freely when it helps: ask for another example, get feedback on an idea, generate practice, inspect code, challenge an explanation.
When you read with an AI, give it the chapter. Ask it to keep to the chapter’s words, examples and boxes, and to tell you when it brings in something that the chapter does not have. Ask for a hint before an answer. Better still, ask it to question you until it finds the step you cannot explain, then work on that step.
If AI makes the thing you are supposed to be learning effortless, take that as a warning sign. The AI is probably doing too much of the work for you.
Being stuck is frustrating. I know. But the first failed attempt often does more for your understanding than another page of explanation.
Reading something clear and thinking yes, that makes sense is easy to mistake for knowing it. The No-AI checks are where you find out. The Come back in a week box, just before the frontier map, closes the chapter’s practice and asks you to answer again a week later, from memory.
Two layers, and how to read
Each chapter has a core path and a frontier map.
The core path contains the material I want you to understand and be able to use. Read it in order the first time through.
The frontier map looks outward: where these ideas are being pushed now, what they connect to, and what may come next. Vector databases, mechanism design, verifiable computation, and other directions live there. You can skip these sections on a first pass and return when curiosity pulls you back.
Some sections carry marks:
- ★ advanced safe to skip on a first reading
- ✎ practice exercises and puzzles to work through
- ◆ big picture where you step back and look at the chapter through design, trade-offs, and primitives
In a finished chapter, a first reading covers the unmarked sections and the ◆ ones. The ✎ sections are for working through, a few at a time. Their No-AI checks and the Come back in a week box belong to every reading. Do as many of the other exercises as your course or your goal requires.
Folded boxes hold proofs, side routes and answers. Open one when you want it, and read on when you don’t want it.
A small blue ⬢ after a few words marks a primitive where a chapter first puts it to work. It links to that primitive’s entry in the catalog. On the web, holding the pointer over it shows the primitive’s name and idea.
Read with paper beside you. When the text works through a step, write the numbers and symbols down as you go, then check them against the next figure or table. The chapters are written to be followed this way, pen in hand.
What you need to bring
It helps to be able to read a short Python function: a loop, an if, a function that calls itself. You do not need to write Python fluently. On the mathematics side, school algebra is enough, plus two things you may not have used much yet. The logarithm counts how many times you can divide by 2 (or by 3) before you reach 1. And \(n!\) is the number of ways to put \(n\) things in order.
If any of that is new to you, it is not a reason to wait. Ask an AI assistant to explain it, with examples, until it clicks. AI is good at filling a gap like this. Everything else is explained where it first appears, including the probability that the book uses and the notation for how costs grow.
The book and its library are one course
This book is half of the course. The other half is its companion library, github.com/mbrcic/algodesign. Each chapter gives the problem, the design and the reasons. The library holds the structures built in full, with their tests. You learn by moving between the two.
Where appropriate, a structure appears in three forms:
- Python: the version closest to the text, meant to be read and run;
- Rust: the implementation meant to be fast;
- Lean: a language whose proofs a machine checks, used here for the specification and the proofs of what the structure promises.
Code printed in a chapter follows the library’s Python. Each listing has notes beside its groups of lines, sometimes with a small picture. Read the notes first: on their own they give the steps, like pseudocode. Look at the code when you need the detail. Where a listing keeps only the heart of a function, ... stands for the lines left out, and a link leads to the whole function in the library.
Get the library before you start. Its README says how to install it and how to check that it works, and the README is kept up to date as the library grows.
In each finished chapter you will:
- try the problem before reading on (Try first, No-AI checks);
- run the chapter’s Python from the library, change it, and break it on purpose to see what fails;
- do the exercises (✎), whose answers are on the Answers page;
- do some of the projects, which ask you to specify, check and judge code that an AI writes;
- come back in a week.
The design box, and Valid
Each main structure in a chapter comes with a box titled The design, numbered like figures and tables (The design 2.2). It shows the design’s contract in the six parts from the Contracts chapter:
- the goal;
- the promise (what you get back);
- the idea;
- the invariants (what stays true after every operation);
- the cost;
- the assumptions, when they matter, and what happens without them.
The section then works out the design. The box states the contract used in that chapter, not everything one could ask of the structure.
The design box is also the preface’s representation, objective and verifier at the scale of one structure. The idea is the representation. The goal and the cost are the objective. The promise and the invariants are what a verifier has to check. For each claim that a box states, the library supplies evidence and names its kind. You still have to judge whether those claims express the problem you meant to solve.
In the library’s Lean, the predicate Valid holds the box’s checkable invariants, in the box’s order. For an algorithm, Valid is what holds after each of its steps. The implementer, often an AI, also supplies any extra conditions that the chosen representation needs. Some specifications name these conditions WellFormed. The implementer proves that building establishes Valid and any extra conditions, and that operations preserve them. None of these conditions may weaken what the box requires. If the implementation needs a new assumption about its inputs, the implementer must say why and propose a change to the box, and you decide. The library’s Lean theorems prove what the operations promise. To find what a structure keeps true, open its Spec.lean in the library and read its Valid.
For example, the box for the trie (a tree that stores words letter by letter) says that every branch leads to a stored word. In Lean that piece of Valid is NoDeadBranches. In Python and Rust it is is_no_dead_branches(). is_valid() checks every piece, and check_valid() names the first one that fails. The library’s README lists which structures have these so far.
In the library, each main structure’s Spec.lean opens with its box, quoted word for word, and then goes through it field by field. Each field carries one word saying how far it is checked:
- proved: a Lean theorem;
- tested: only tests in Python and Rust;
- measured: a number from a run, such as a timing or a count;
- argued: a reason in prose, left unproved on purpose because a proof would cost more than it gives;
- mentioned: stated, but not something code can check, such as an assumption about the workload;
- required: something every implementation must supply;
- none: the box says nothing there.
A field checked in several ways carries the weakest of them, in the order proved, tested, measured, argued. Required, mentioned and none stand outside that order, because they say what a field is. Contract gathers the box’s checkable parts as Lean statements, and the theorem contract proves that the library’s operations meet them.
To read a spec, start at the bottom. Contract is the last thing in the file, with Valid just above it. Contract has the box’s shape. The promise, the invariants and the cost are written out, one line of Lean for each line of the box that Lean can state, with the box’s words beside it. The goal, the idea and the assumption appear as comments in their places. Where Lean states a cost, the cost line bounds a count. For a trie lookup, the number of child steps is at most the word’s length.
Ops lists the operations a design must supply, with the routine proofs they owe, such as that each operation keeps Valid and a build stores what it was given. So the same contract can be asked of any code, yours or an AI’s. Of Lean you need four symbols: ∀ is “for every”, ∧ is “and”, → is “if … then”, ↔︎ is “exactly when”.
This is how you work with an AI. Give it the box and let it write the code. Its code won’t come with is_valid, so ask for one that checks each invariant, and break one by hand to see it fail. Keep some of your tests to yourself. Code checked only against tests it has seen will often fit only those tests. When an answer looks wrong, decide which of three things happened. If the code breaks a promise in the box, fix the code. If the code keeps the box but misses what you wanted, fix the box. If the design keeps its promise but its assumptions or its cost no longer fit the job, change the design. Writing the code can be the AI’s job. Deciding what the box says is still your job.
When you design something new, start with what you know, leave the other choices open, and revise the contract as you find out what matters. The boxes show the finished design, at the end of that process.
How the code is checked
Code printed in a chapter is run against the companion library every time a change to the book is proposed or pushed. If the two drift apart, the change is marked as failing.
That does not mean every claim in the book has been formally verified. The Lean proofs are about the Lean definitions, not about the Python or the Rust. The comparisons between the three languages test agreement on the inputs they run. They are not proofs. Wherever a chapter says something has been checked, it says how, and where something is proved, it says what the proof covers. A proof checks the properties someone thought to state, and is silent about the ones nobody stated. What to state is for you to decide.
Each structure in the library follows a named textbook version, so you can compare the code with a source written independently of it.
In a chapter that prints its code, every listing is run against the library. Chapters not yet finished show short excerpts instead. Those are not run, and the full code they come from is in the library. A listing may be pseudocode, leaving out details that a real language would need. In that case the text says so beside it, and the check only parses it, because running it would mean inventing what the text left out.
Every timing in this book comes from one reference machine, running the library’s Rust, except the times that the projects’ benches report, which are Python’s, on your machine. When the book reports an average speedup, it is the geometric mean, so a gain and an equal loss cancel.
Sharing it
The book is free to read online. Link to any part of it from anywhere. If you teach, you may give your students unchanged PDFs of the book or individual chapters, put figures on your slides with a credit line (Figure 2.4, from Modern Algorithm Design by Mario Brcic, modernalgorithmdesign.com), and copy and adapt the exercises with the same credit, as long as your students pay nothing extra for them. For anything else, such as posting the text elsewhere, printing copies to sell or using it in paid material, ask me first. The companion library is open source under the Apache 2.0 license. Everything else is © 2026 Mario Brcic, all rights reserved.
Citing it
To cite the book, use its DOI. It covers every version, and it leads to an archived copy on Zenodo. The website always holds the newest version.
Brcic, M. (2026). Modern Algorithm Design. Zenodo. https://doi.org/10.5281/zenodo.23457837
@book{brcic2026mad,
author = {Brcic, Mario},
title = {Modern Algorithm Design},
year = {2026},
publisher = {Zenodo},
doi = {10.5281/zenodo.23457837},
url = {https://modernalgorithmdesign.com/}
}To cite one chapter, add its number and title, for example Chapter 2, Search.