Expand description
Algorithms Library for Rust
A comprehensive, zero-dependency collection of common algorithms across sorting, searching, graphs, dynamic programming, strings, greedy, and divide-and-conquer.
§Conventions
- Fallible operations return
Result<T, Error>; lookups that miss returnErr(Error::NotFound)rather than a sentinel index. *_searchfunctions on strings report byte offsets, not char indices.- Most sorts take
&[T]and returnVec<T>;*_in_placevariants sort&mut [T]without returning an allocation.
§Example
use algorithms_lib::{quick_sort, binary_search};
let sorted = quick_sort(&[3, 1, 4, 1, 5]);
assert_eq!(sorted, vec![1, 1, 3, 4, 5]);
assert_eq!(binary_search(&sorted, &4), Ok(3));Structs§
- Edge
- Weighted edge for graph algorithms.
- MSTEdge
- An undirected, weighted edge for minimum spanning tree algorithms.
- Priority
Queue - A min-priority queue.
- Union
Find - Disjoint-set union–find with union by rank and path compression.
Enums§
- EditOp
- One step in an edit-distance script.
- Error
- Errors that can be returned by algorithms in this crate.
Functions§
- a_star
- A* shortest path.
- activity_
selection - Maximum compatible activity indices.
- bellman_
ford - Bellman-Ford shortest paths with negative cycle detection.
- bfs
- Breadth-first search.
- binary_
search - Binary search.
- binary_
search_ on_ answer - Binary search on answer for monotone predicates.
- boyer_
moore_ search - Boyer-Moore pattern matching with the bad-character and good-suffix rules.
- count_
inversions - Inversion count.
- dfs
- Depth-first search.
- dijkstra
- Dijkstra’s shortest paths.
- edit_
distance - Levenshtein distance.
- edit_
distance_ reconstruction - Levenshtein distance and one optimal edit script.
- fast_
power - Exponentiation by squaring. Returns
Err(Error::InvalidInput)whenbaseis zero andexponentis negative. By convention,0^0returns1.0. - floyd_
warshall - Floyd–Warshall all-pairs shortest paths.
- fractional_
knapsack - Fractional knapsack maximum value.
- gcd
- Greatest common divisor.
- has_
cycle - Returns whether the directed graph contains a cycle.
- heap_
sort - Binary max-heap sort.
- heap_
sort_ in_ place - Sorts
itemsin place using a binary max-heap. True in-place sort: no auxiliary buffer proportional toitemsis allocated. - huffman_
coding - Huffman coding.
- huffman_
decode - Decode a Huffman bit string using a code table.
- huffman_
encode - Encode a sequence of symbols using a Huffman code table.
- interpolation_
search - Interpolation search for numeric data.
- jump_
search - Jump search.
- kmp_
search - Knuth-Morris-Pratt pattern matching.
- knapsack_
01 - 0/1 knapsack.
- lcm
- Least common multiple.
- longest_
common_ subsequence - Longest common subsequence length.
- longest_
common_ subsequence_ reconstruction - Longest common subsequence length and one actual subsequence.
- max_
subarray - Maximum subarray sum.
- merge_
sort - Stable merge sort.
- merge_
sort_ in_ place - Sorts
itemsin place using stable merge sort. - minimum_
spanning_ tree - Kruskal’s minimum spanning tree (or forest) algorithm.
- modular_
fast_ power - Modular exponentiation by squaring:
(base^exponent) % mod. - native_
sort - Standard-library sort wrapper.
- native_
sort_ in_ place - Sorts
itemsin place using the standard library sort (slice::sort). - quick_
select - Returns the k-th smallest element (0-indexed) of
items. - quick_
sort - Quick sort. Returns a new sorted
Vec; seequick_sort_in_placeto sort a mutable slice without allocating the result. - quick_
sort_ in_ place - Sorts
itemsin place using quick sort’s partitioning result. - rabin_
karp_ search - Rolling-hash pattern matching.
- radix_
sort - LSD radix sort for non-negative integers.
- sieve_
of_ eratosthenes - Sieve of Eratosthenes. Returns all prime numbers
<= n. - strongly_
connected_ components - Kosaraju’s strongly connected components algorithm.
- topological_
sort - Topological sort using Kahn’s algorithm.