The Algorithms - Python Python TheAlgorithms/Python

The Algorithms - Python

View on GitHub

Educational reference implementations of every classical algorithm in Python. Type-hinted, doctested, MIT-licensed. About 1,381 algorithms across 43 categories.

Python 221k stars MIT

About

TheAlgorithms/Python is a community-maintained reference of about 1,381 classical algorithm implementations in Python, organized into 43 top-level category directories under one repository. The project is MIT-licensed and sits near 220,000 GitHub stars at the pinned commit, with more than 1,900 contributors since the first commit in August 2016. The README disclaimer is one sentence and is not buried: "Implementations are for learning purposes only. They may be less efficient than the implementations in the Python standard library." That sentence defines the product: a reading corpus, not a runtime library.

Every file that merges must pass three automated gates. ruff enforces about forty rule groups covering style, naming, and security. mypy validates type-hint coverage on every parameter and return value. pytest --doctest-modules executes every >>> example in every module docstring as a real test. The doctest convention is load-bearing: the example a reader sees in a function's docstring is one the contributor proved correct before the PR merged. CONTRIBUTING.md also mandates a sourced docstring linking to the originating paper or Wikipedia article, and doctests for both valid and invalid inputs, including one that proves the function raises ValueError on bad input.

The 43 categories span the standard algorithms-textbook curriculum plus a few extras: sorting, searching, graphs, dynamic programming, divide-and-conquer, greedy methods, backtracking, ciphers, hashes, data structures, strings, matrix and linear algebra, machine learning, neural networks, bit manipulation, geometry, conversions, physics, Project Euler solutions, and more. Each directory is independent. Adding a new algorithm means dropping one .py file into the right category directory; the CI run then regenerates DIRECTORY.md automatically. Contributors are told not to create new top-level directories, which keeps the catalog from fragmenting.

What this repo is not: a production library, a benchmarking project, or a PyPI package. There is no installable distribution, no shared module between files, no common utils.py. Two implementations of the same algorithm in different categories will diverge in style because they were merged years apart by different contributors working against the same checklist. The deliberate redundancy is what makes a single file readable in isolation: open one file and you have the entire algorithm, end to end.

The navigation tour gives a category-by-category map of the repository, sampling one canonical file from each major directory. The first-contribution tour walks the full contribution gate from README disclaimer to local ruff and pytest commands, so a reader planning to submit a PR can follow the entire path before opening one.

Architecture

The repository root holds a thin layer of metadata (README.md, CONTRIBUTING.md, DIRECTORY.md, LICENSE.md, pyproject.toml) and 48 top-level directories. Forty-three of those directories hold algorithms, each named after a category in snake_case: sorts, searches, graphs, dynamic_programming, data_structures, ciphers, hashes, maths, strings, matrix, machine_learning, and so on. The remaining five (.devcontainer, .github, .vscode, docs, scripts) are tooling. scripts/build_directory_md.py regenerates DIRECTORY.md after every successful CI run, which is why the contribution guide tells you not to hand-edit it.

Inside a category directory, every file is one algorithm. sorts/quick_sort.py is one self-contained 52-line file; searches/binary_search.py is a 434-line collection of binary-search variants. There are no shared modules between leaves, no utils.py, no plugin registry. If two algorithms need the same helper, the helper gets copied. This deliberate redundancy is what makes a single file readable in isolation: open it and you have everything.

There is no runtime. The repository ships no main, no service, no installable package on PyPI. pyproject.toml declares requires-python = ">=3.14", lists scientific Python dependencies (numpy, scipy, scikit-learn, pandas, matplotlib) for the algorithms that use them, and configures pytest --doctest-modules --showlocals as the only entry into the code. CI runs that pytest invocation on every push, with about ten files excluded because they need network access or heavy ML weights. The default way to use any algorithm is to copy the file into your own project and adapt it.

Start here

TheAlgorithms/Python holds about 1,381 algorithm implementations across 43 categories. All 52 tours in this collection fall into one of three tiers. The two intro tours orient you to the codebase; the 30 marquee tours go deep on one algorithm each, covering its history and Python implementation; the 20 category tours walk 4-7 representative algorithms per category for breadth. Start with one intro, then pick a marquee tour for depth or a category tour for breadth.

Intro tours

  • First Contribution — walk the CONTRIBUTING.md checklist, the canonical insertion_sort.py file shape, and the pytest --doctest-modules gate before opening a PR.
  • Navigation — sample one file from each major category (sorts, searches, graphs, DP, ciphers) and land on the autoapi_dirs list that names all 43.

Marquee algorithm tours

Sorts

  • Bubble Sort — adjacent swaps, O(n squared) worst case, O(n) best case
  • Insertion Sort — shift-and-insert loop, Comparable Protocol, six doctests
  • Selection Sort — find minimum, swap to front, scan the remainder
  • Merge Sort — divide in half, merge sorted halves, O(n log n) guaranteed
  • Quicksort — random pivot, two-pass partition, list-spread recursive return
  • Heap Sort — build a max-heap, extract-max loop, in-place O(n log n)
  • Radix Sort — digit-by-digit counting sort, LSD order, no comparisons
  • Counting Sort — frequency array, prefix-sum accumulation, O(n + k)

Searches

  • Linear Search — one pass, index on hit, -1 on miss, no ordering required
  • Binary Search — halving loop, pairwise sort guard, -1 on miss
  • Jump Search — block jumps then linear scan, O(sqrt(n)) average

Graphs

  • Breadth-First Search — queue, visited set, adjacency dict, doctest with cycle
  • Depth-First Search — stack or recursion, visited set, pre-order traversal
  • Dijkstra — priority queue, relaxation loop, non-negative weights only
  • A* — admissible heuristic, f = g + h, open set as a min-heap
  • Bellman-Ford — edge relaxation for n-1 rounds, negative-cycle detection
  • Kruskal MST — sort edges by weight, union-find, greedy spanning tree

Dynamic programming

  • Fibonacci DP — class-based memoization, walrus assignment, incremental extension
  • 0/1 Knapsack — 2D DP table, include-or-skip recurrence, item reconstruction
  • Longest Common Subsequence — 2D matrix fill, character-match branch, traceback
  • Edit Distance — Levenshtein recurrence, three-way min, empty-string base cases

Cryptography and hashing

  • Caesar Cipher — alphabet shift modulo length, encrypt/decrypt pair
  • RSA Cipher — key generation, modular exponentiation, public/private pair
  • SHA-1 — 512-bit blocks, 80 rounds, five 32-bit state words
  • MD5 — four-round mixing, non-linear functions, 128-bit digest

Strings

  • KMP String Match — failure function, O(n + m), no backtracking in the text
  • Rabin-Karp — rolling hash, sliding window, O(n + m) average

Math

Data structures

  • Bloom Filter — k hash functions, bit array, probabilistic membership with no false negatives
  • LRU Cache — doubly-linked list plus dict, O(1) get and put, capacity eviction

Category tours

  • Sorts — bubble, insertion, selection, merge, quicksort, heap, radix: seven sorting algorithms side by side
  • Searches — linear, binary, jump, interpolation, exponential, quick-select, ternary
  • Ciphers — Caesar, Vigenere, RSA, Diffie-Hellman, Playfair, Hill, Enigma
  • Hashes — MD5, SHA-1, SHA-256, Adler-32, Luhn, DJB2
  • Data Structures — linked list, BST, AVL tree, heap, hash table, disjoint set, trie
  • Conversions — decimal-to-binary, decimal-to-hex, binary-to-decimal, Roman numerals, temperature, RGB-to-HSV, length
  • Bit Manipulation — popcount, Brian Kernighan, single-bit ops, reverse bits, find unique, power-of-two, Gray code
  • Strings — KMP, Rabin-Karp, Boyer-Moore, Manacher, Z-function, Levenshtein, Aho-Corasick
  • Maths — sieve, prime check, GCD, Fibonacci, Euclidean distance, binary exponentiation, CRT
  • Geometry — geometry primitives, Graham scan, Jarvis march convex hull
  • Matrix — matrix operations, rotate, spiral traversal, sorted search, inverse, Pascal triangle
  • Graphs — BFS, DFS, Dijkstra, Bellman-Ford, A*, Kruskal, Prim across the 64-file graphs/ directory
  • Backtracking — N-Queens, Sudoku, Knight Tour, Rat-in-Maze, permutations, word search, Hamiltonian cycle
  • Greedy Methods — fractional knapsack, coin change, stock buy-sell, gas station, optimal merge
  • Divide and Conquer — merge sort, convex hull, closest pair, Strassen, max subarray, power
  • Dynamic Programming — Fibonacci, knapsack, LCS, edit distance, matrix chain, LIS, Floyd-Warshall
  • Linear Algebra — Gaussian elimination, LU, Jacobi, matrix inverse, conjugate gradient, power iteration, rank
  • Machine Learning — linear regression, logistic regression, k-means, kNN, decision tree, gradient descent, SVM
  • Neural Networks — simple NN, backpropagation, two-hidden-layer, ReLU, Swish
  • Project Euler — Problems 1-7, each with the cleanest solution file in the problem directory

Tours

Getting Started with Your First Contribution to TheAlgorithms/Python

beginner

Walk the README disclaimer, the CONTRIBUTING.md algorithm requirements, the canonical insertion_sort file shape, the auto-generated DIRECTORY.md, the pytest --doctest-modules gate, and the path to deeper algorithm tours.

6 stops ~16 min
contributiononboardingpythonalgorithmstheAlgorithms

Navigating TheAlgorithms/Python: A Tour of the 43 Categories

beginner

Sample one canonical algorithm from each major category (a sort, a search, a graph traversal, a DP class, a cipher) and end on the autoapi_dirs list that names every category.

7 stops ~18 min
navigationoverviewpythonalgorithmstheAlgorithms

How Bubble Sort Works

intermediate

Compare adjacent elements, swap if out of order, repeat. The algorithm that appears in every first-year course because the swapping motion is easy to picture.

6 stops ~14 min
sortingalgorithmpythonalgorithmstheAlgorithms

How Insertion Sort Works

intermediate

Pick the next element, shift everything larger one position right, drop the element into the gap. The algorithm that outperforms quicksort on small runs.

6 stops ~14 min
sortingalgorithmpythonalgorithmstheAlgorithms

How Selection Sort Works

intermediate

Find the minimum of the unsorted region, swap it to the front, repeat. O(n squared) always, but only O(n) swaps total.

5 stops ~10 min
sortingalgorithmpythonalgorithmstheAlgorithms

How Mergesort Works

intermediate

Split the list in half, sort each half recursively, merge the two sorted halves. The first algorithm written for a stored-program computer.

6 stops ~14 min
sortingalgorithmpythonalgorithmstheAlgorithms

How Quicksort Works

intermediate

A random pivot, a partition into lesser and greater, two recursive calls. The 1959 algorithm that still drives most stdlib sorts.

6 stops ~14 min
sortingalgorithmpythonalgorithmstheAlgorithms

How Heapsort Works

intermediate

Build a max-heap over the array, then repeatedly swap the root to the back and restore the heap. O(n log n) worst case with no extra memory.

6 stops ~16 min
sortingalgorithmpythonalgorithmstheAlgorithms

How Radix Sort Works

intermediate

Sort by least significant digit, then next digit, repeat. Non-comparison sort that runs in O(d times n) rather than O(n log n).

5 stops ~12 min
sortingalgorithmpythonalgorithmstheAlgorithms

How Counting Sort Works

intermediate

No comparisons, no pivots. Count key frequencies, prefix-sum them, then place each element at its exact output index in one backward pass.

6 stops ~15 min
sortingalgorithmpythonalgorithmstheAlgorithms

How Linear Search Works

intermediate

Scan every element in order, return the index on match, return -1 on miss. The lower bound for searching unsorted data.

5 stops ~12 min
searchalgorithmpythonalgorithmstheAlgorithms

How Binary Search Works

intermediate

Halve the search space on every step. The 1946 algorithm that still trips up professionals on overflow and off-by-one errors.

6 stops ~16 min
searchalgorithmpythonalgorithmstheAlgorithms

How Jump Search Works

intermediate

Jump through sorted data in square-root-sized blocks, then linear-search the candidate block. O(sqrt(n)) with no recursion, no pivots.

5 stops ~14 min
searchalgorithmpythonalgorithmstheAlgorithms

How BFS Works

intermediate

Visit every neighbor before going deeper. The FIFO queue invariant that guarantees shortest-path distances on unweighted graphs.

6 stops ~14 min
searchalgorithmpythonalgorithmstheAlgorithms

How DFS Works

intermediate

Follow a path as deep as it goes before backtracking. The iterative stack-based implementation that mirrors the recursive structure without blowing the call stack.

5 stops ~12 min
searchalgorithmpythonalgorithmstheAlgorithms

How Dijkstra's Algorithm Works

intermediate

A min-heap of (cost, node) tuples, a visited set, and a single relaxation loop. The 1956 algorithm at the core of OSPF and IS-IS routing.

7 stops ~18 min
pythonalgorithmstheAlgorithms

How A* Search Works

intermediate

Combine actual path cost with a heuristic estimate of remaining distance. The 1968 algorithm built for a robot, now in every map and game engine.

6 stops ~18 min
pythonalgorithmstheAlgorithms

How Bellman-Ford Works

intermediate

Relax every edge V-1 times, then run one more pass to detect negative cycles. The algorithm Dijkstra cannot replace.

6 stops ~16 min
pythonalgorithmstheAlgorithms

How Kruskal's Algorithm Works

intermediate

Sort edges by weight, then add each one unless it creates a cycle. Union-find with path compression in 46 lines.

5 stops ~14 min
pythonalgorithmstheAlgorithms

How Fibonacci DP Works

intermediate

Cache the sequence on a class instance, extend it on demand, return a slice. The canonical example of memoization replacing exponential recursion.

5 stops ~14 min
pythonalgorithmstheAlgorithms

How the 0/1 Knapsack DP Works

intermediate

Fill a capacity-by-item table bottom-up, then trace back through it to recover which items go in the bag.

6 stops ~20 min
pythonalgorithmstheAlgorithms

How Longest Common Subsequence Works

intermediate

A 2-D DP table, one recurrence, and a traceback walk. The algorithm at the core of diff, git-merge, and DNA sequence alignment.

6 stops ~18 min
pythonalgorithmstheAlgorithms

How Edit Distance Works

intermediate

Count the minimum insertions, deletions, and substitutions to transform one string into another. The 1965 algorithm behind spell-check, fuzzy search, and DNA alignment.

6 stops ~18 min
pythonalgorithmstheAlgorithms

How the Sieve of Eratosthenes Works

intermediate

Mark every multiple of each found prime starting at its square. What remains is a list of primes, produced in O(n log log n).

5 stops ~14 min
pythonalgorithmstheAlgorithms

How the Caesar Cipher Works

intermediate

A modular shift over an alphabet, mirrored to decrypt, exhausted to brute-force. The oldest attested cipher in the Western record.

6 stops ~16 min
cryptographycipherpythonalgorithmstheAlgorithms

How RSA Encryption Works

intermediate

Split the message into fixed-size integer blocks, raise each block to a modular power, and undo it with the private exponent.

6 stops ~20 min
cryptographycipherpythonalgorithmstheAlgorithms

How SHA-1 Works

intermediate

Pad the message, split into 512-bit blocks, expand each block to 80 words, run 80 rounds of bit operations, accumulate five 32-bit words into a 160-bit digest.

6 stops ~20 min
pythonalgorithmstheAlgorithms

How MD5 Works

intermediate

Four 32-bit state words, 64 rounds, a sin-derived constant table. The 1991 algorithm that still shows up in checksums everywhere.

6 stops ~20 min
pythonalgorithmstheAlgorithms

How the Knuth-Morris-Pratt Algorithm Works

intermediate

Preprocess the pattern into a failure array, then scan the text without backtracking. The 1977 algorithm that makes grep and DNA search O(n+m).

6 stops ~16 min
stringsalgorithmpythonalgorithmstheAlgorithms

How Rabin-Karp String Matching Works

intermediate

Hash the pattern, slide a rolling hash across the text, confirm on match. The 1987 algorithm that makes multi-pattern search practical.

5 stops ~14 min
pythonalgorithmstheAlgorithms

How a Bloom Filter Works

intermediate

A bitarray, two hash functions, and a membership check that never gives false-negatives. Burton Bloom's 1970 data structure still in production databases.

6 stops ~14 min
pythonalgorithmstheAlgorithms

How an LRU Cache Works

intermediate

A doubly-linked list plus a hashmap gives O(1) get and put. The eviction policy at the core of CPU caches, browser caches, and functools.lru_cache.

6 stops ~18 min
pythonalgorithmstheAlgorithms

Sorting Algorithms in TheAlgorithms/Python

beginner

Seven sorting strategies side by side: from the classroom bubble loop to radix buckets.

7 stops ~14 min
category-toursortspythonalgorithmstheAlgorithms

Search Algorithms in TheAlgorithms/Python

beginner

Seven search strategies from a simple scan to partition-based selection, each with a different trade-off.

7 stops ~14 min
category-toursearchespythonalgorithmstheAlgorithms

Cipher Algorithms in TheAlgorithms/Python

intermediate

Seven ciphers from ancient Rome to Diffie-Hellman: each a different answer to the question of what makes a message unreadable.

7 stops ~16 min
category-tourcipherspythonalgorithmstheAlgorithms

Hash Functions in TheAlgorithms/Python

beginner

Six hash algorithms from a 13-byte checksum to 256-bit cryptographic digests: each trading reliability for speed differently.

6 stops ~12 min
category-tourhashespythonalgorithmstheAlgorithms

Data Structures in TheAlgorithms/Python

beginner

Seven fundamental data structures side by side: the pointer chain, the key-ordered tree, the self-balancing tree, the heap, the hash table, the union-find, and the prefix tree.

7 stops ~16 min
category-tourdata-structurespythonalgorithmstheAlgorithms

Conversion Algorithms in TheAlgorithms/Python

beginner

Seven base, numeral, and unit conversions showing how repeated division, recursion, lookup tables, and pivot factors each solve the same family of problems.

7 stops ~14 min
category-tourconversionspythonalgorithmstheAlgorithms

Bit Manipulation in TheAlgorithms/Python

beginner

Seven techniques that use bitwise operations to count, test, flip, reverse, and encode integers without arithmetic.

7 stops ~14 min
category-tourbit-manipulationpythonalgorithmstheAlgorithms

String Algorithms in TheAlgorithms/Python

intermediate

Seven string search and comparison algorithms from KMP failure arrays to Aho-Corasick automata.

7 stops ~14 min
category-tourstringspythonalgorithmstheAlgorithms

Maths Algorithms in TheAlgorithms/Python

intermediate

Seven mathematical algorithms from prime sieves to the Chinese Remainder Theorem.

7 stops ~14 min
category-tourmathspythonalgorithmstheAlgorithms

Geometry Algorithms in TheAlgorithms/Python

intermediate

Four stops from primitive shapes to convex hull algorithms, with cross-product orientation as the shared computational primitive.

4 stops ~10 min
category-tourgeometrypythonalgorithmstheAlgorithms

Matrix Algorithms in TheAlgorithms/Python

intermediate

Six matrix operations from elementwise addition to Pascal's triangle generation.

6 stops ~12 min
category-tourmatrixpythonalgorithmstheAlgorithms

Graph Algorithms in TheAlgorithms/Python

intermediate

Seven graph traversal and shortest-path strategies from BFS queues to Prim's greedy expansion.

7 stops ~14 min
category-tourgraphspythonalgorithmstheAlgorithms

Backtracking Algorithms in TheAlgorithms/Python

intermediate

Seven constraint-satisfaction problems solved by the same pattern: try a placement, recurse, and undo it when the branch fails.

7 stops ~14 min
category-tourbacktrackingpythonalgorithmstheAlgorithms

Greedy Algorithms in TheAlgorithms/Python

intermediate

Five problems where the locally optimal choice at each step produces the globally optimal result.

5 stops ~11 min
category-tourgreedy-methodspythonalgorithmstheAlgorithms

Divide and Conquer Algorithms in TheAlgorithms/Python

intermediate

Six algorithms that split a problem in half, solve each half independently, and combine the results: from sorting to matrix multiplication to geometry.

6 stops ~12 min
category-tourdivide-and-conquerpythonalgorithmstheAlgorithms

Dynamic Programming in TheAlgorithms/Python

intermediate

Seven DP patterns from memoized sequences to all-pairs shortest paths.

7 stops ~14 min
category-tourdppythonalgorithmstheAlgorithms

Linear Algebra Algorithms in TheAlgorithms/Python

intermediate

Seven direct solvers and iterative methods, from Gaussian elimination to conjugate gradient, each exposing a different trade-off in numerical linear algebra.

7 stops ~14 min
category-tourlinear-algebrapythonalgorithmstheAlgorithms

Machine Learning Algorithms in TheAlgorithms/Python

intermediate

Seven classical ML algorithms from linear models to kernel machines, each implemented from scratch in plain NumPy.

7 stops ~14 min
category-tourmachine-learningpythonalgorithmstheAlgorithms

Neural Networks in TheAlgorithms/Python

intermediate

Five files tracing how a network learns: from a single-neuron forward pass to backpropagation, multi-layer weight matrices, and the activation functions that make it possible.

5 stops ~12 min
category-tourneural-networkpythonalgorithmstheAlgorithms

Project Euler Solutions in TheAlgorithms/Python

intermediate

The first seven Project Euler problems, each solved in a single Python function with doctests, showing how mathematical insight reduces brute force to a handful of lines.

7 stops ~14 min
category-tourproject-eulerpythonalgorithmstheAlgorithms

Origin Story

TheAlgorithms/Python: A Living Textbook of Classical Algorithms

How a community of 1,900 contributors keeps about 1,381 algorithm files type-hinted, doctested, and readable end to end.

Read the full story

Related Projects

Your codebase next

Create code tours for your project

Intraview lets AI create interactive walkthroughs of any codebase. Install the free VS Code extension and generate your first tour in minutes.

Install Intraview Free