Clean, educational implementations of vital algorithms in Python.
This project aims to provide clean, tested and well-documented implementations of common and important algorithms in Python 3.
We strive to adhere to the following rules:
- No imports. Only Python standard library functions are used, except for
pytest
for testing andflake8
for PEP8 compliance checking. - Every algorithm must include at least 2 doctests/examples.
- Every algorithm must include at least 3 test cases, which should ideally test edge cases.
- The PEP8 style-guide should be followed, this may be checked using the
flake8
tool.
In addition, the following guidelines apply:
- Use explicit variable names, detailed and concise inline comments. Readability matters.
- Object orientation, generators, and so forth should be used when possible.
- Every algorithm should ideally include at least one literature reference, as well as algorithmic information about time and space usage.
You are encouraged to contribute, whatever your skill level is. Here's how to contribute:
- Fork the project to your GitHub account.
- Clone your fork to your local computer.
- Add this to as upstream using
git remote add upstream https://github.com/tommyod/PythonAlgorithms.git
. - Make your contribution to the project. Add tests, keep it clean, and follow the rules outlined above.
- Install flake8
by typing
pip install flake8
in the terminal, then runflake8 --show-source --ignore=F811,W293,W391,W292,W291
. Make sure there are no errors. - Run the tests using
pytest --doctest-modules -v
. Tests should run quickly, and every algorithm should include at least 5 tests and 2 doctests. - Run
git fetch upstream
to get the lastest changes from upstream (this repo), then rungit rebase upstream/master
to rebase your changes on top of this repository. - Create a pull request. Write in detail about what you did.
For more detailed information about how to contribute to an open source project, see the pandas docs, scikit-learn docs and the git book.
Here's a list of algorithms that should be considered. For more information, see this list of algorithms from Wikipedia. Although the code quality is dubious, GeeksforGeeks has a list of algorithms. Some "competing" GitHub repos are TheAlgorithms/Python and keon/algorithms, but again the code quality is debatable.
- Representation: directed graphs
- Representation: undirected graphs (weight and unweighted)
- Prim's algorithm (minimum spanning tree)
- Kruskal's algorithm (minimum spanning tree)
- Djikstra's algorithm (single source shortest paths)
- DFS
- BFS
- Connected components
- Checking if bipartite
- Topological sorting of a DAG
- Maximum flow / Ford-Fulkerson algorithm
- Fibonacci
- Binomial coeffs
- Prime numbers (generating primes)
- Prime factorization (factorize an integer)
- Extended Euclidean algorithm
- Optimal matrix multiplication (p.362 Halim)
- Primality testing (test if a number is prime)
- Maximal subset sum (weights equal values)
- Knapsack with repetition
- Knapsack without repetition
- Running mean (arithmetic and geometric means)
- Longest consecutive increasing subsequence
- Longest non-consecutive increasing subsequence
- Longest common substring
- KMP algorithm
- Edit distance
- Edit /w custom error function (i.e. wrong typing keyboard)
- Coin change (Halim p.109)
- Maximal range sum (Halim p.103)
- Elements by maxnorm
- Closest pairs of points on the line
- Closest pairs of points in the plane
- Interval covering (p.91 Halim)
- Bucket sort
- Mergesort
- Median finding in linear time (p.215 in Cormen)
- Union Find (used in Kruskal's algorithm)
- Range query tree