Home

Awesome

Rusty Algorithms & Data Structures for Learners

<!-- [![Crates.io](https://img.shields.io/crates/d/algorithms-edu.svg)](https://crates.io/crates/algorithms-edu) -->

Crates.io Crates.io Continuous Integration Coverage Status lines of code

This repository presents Rust implementations of common algorithms and data structures, many of which are based on William Fiset's Java implementation: https://github.com/williamfiset/Algorithms . I highly recommend his YouTube channel, where he explains many of these algorithms in detail using illustrations, animations and pseudocode. I recommend that you implement these algorithms by yourself before comparing them to my or William's implementations, since the best way to learn is by doing, and it's likely that you discover ways in which the code can be written in a more efficient, robust and/or readable way, in which case you're welcome to submit a PR!

I also write algorithms that's not yet available in William's repository. When I do so I attach references (most of which are freely accessible) that I used and hopefully they should be sufficient to guide you to write your own implementations.

<!-- In addition to implementing W. Fiset's algorithms, I also write solutions to competitive programming problems. Some representative problems are explained in `src/problems`, and there is also a `leetcode` folder for my leetcode solutions. Both are far from completion. particularly in Leetcode. Where appropriate, I will note, or `use`, the relevent algorithm/data structure(s) in this crate. -->

Usage

The implementation details are explained in comments and docs and the example usage is implied in unit tests. To run tests:

cargo test

I use LaTeX to write some math expression in docs. To render them correctly in docs, run:

RUSTDOCFLAGS="--html-in-header katex-header.html" cargo doc --no-deps --open

or an alias for this command:

./doc

Recommended Environment

This simple setup provides most features a decent IDE would provide (importantly, jump to definition and type labelling)

<!-- ## Rusticity This is not a verbatim translation of W. Fiset's Java implementation. Instead, I try to make the code idiomatic in Rust, according to these rules: ### Avoid Long Names Using `mod`s For example, perfer ``` crate::algo::graph::bfs::adjacency_list_iterative::fast_deque ``` over ``` com.williamfiset.algorithms.graphtheory.BreadthFirstSearchAdjacencyListIterativeFastQueue ``` ### Custom Data Structures Have Unsurprising Method Names and Behaviour Follow the conventions of `std` types as much as possible. For example, when implementing a `Queue`, prefer ```rust pub fn push_back(&mut self, value: T); pub fn pop_front(&mut self) -> Option<T>; ``` over ```rust pub fn enqueue(&mut self, value: T); pub fn dequeue(&mut self) -> T; // or pub fn offer(&mut self, value: T); pub fn poll(&mut self) -> T; ``` ### Use `Option<T>` to Represent Nullable Values Genrerally, `Option::None` is an idiomatic representation of `null`. This makes the code work better with the standard library and cause less surprises. -->

Contents

Search Algorithms

Sort Algorithms

Graph

Graph Representations

Fundamental Graph Algorithms

Tree Algorithms

Minimum Spanning Tree/Forest

Network Flow

Shortest Path

Others

Data Structures

Machine Learning

Clustering

K Nearest Neighbour (KNN)

Math

Linear Algebra (docs/annotations WIP)

Problems

Dynamic Programming

Back Tracking

Graph

Network Flow