A Python library of common algorithms and data structures for competitive programming.
This library provides modular, tested implementations of frequently used components.
| Tier | File Name | Description | Key Complexity |
|---|---|---|---|
| 🥇 Tier 1 | union_find.py |
Disjoint Set Union (DSU) | |
mod_pow.py |
Modular Exponentiation (a^b % m) | ||
mod_inverse.py |
Modular Multiplicative Inverse (a^-1 % m) | ||
prime_factorization.py |
Prime factorization of an integer | ||
| 🥈 Tier 2 | dijkstra.py |
Single-Source Shortest Path (SSSP) | |
sieve_of_eratosthenes.py |
Sieve for primes and minimum prime factor table | ||
combination.py |
nCk % M using precomputed factorials |
|
|
| 🥉 Tier 3 | binary_indexed_tree.py |
Point updates, range sum queries (BIT/Fenwick) | |
segment_tree.py |
Point updates, range queries (e.g., min, max, sum) |
This library is structured as a Python package. To use it in your own scripts, make sure your script is in a directory that can see the my_python_library package.
For example, if your script is in the project root (/Users/mei/my_python_library/), you can import modules like this:
import sys
# It may be necessary to increase Python's recursion limit for graph problems
sys.setrecursionlimit(2000000)
from my_python_library.union_find import UnionFind
from my_python_library.dijkstra import dijkstra
def solve():
# ... your solution code using the imported modules ...
uf = UnionFind(10)
uf.unite(0, 1)
# ...The library includes a suite of unit tests to ensure correctness. To run the tests, execute the following command from the project root directory:
python3 -m unittest discover tests