contest-algorithms Rust Library

repository·master·Indexed 26 days ago

https://github.com/ebtech/rust-algorithms

A collection of classic data structures and algorithms implemented in Rust, designed as a 'whitebox cookbook' for students, educators, and competitive programmers. The library includes implementations for graph theory (connectivity, network flows, 2-SAT), mathematics (number theory, FFT, linear algebra), string processing (Trie, KMP, Aho-Corasick), associative range queries (ARQ), and search/ordering utilities. It also provides a Scanner for ergonomic I/O and a Cacher for memoization.

Tokens
5.6K
Snippets
6
Records
57
Agent score
87%

What's inside contest-algorithms

  1. Overview of Associative Range Query (ARQ) and Mo's Algorithm

    master
    This module provides implementations for Associative Range Query (ARQ) and Mo's Algorithm. The ARQ implementation is designed to be more general than standard segment tree implementations found in typical programming contest literature. For a deeper understanding of the specific implementation details used here, refer to the author's blog post on Codeforces.
  2. Overview of Contest Algorithms in Rust

    master
    The contest-algorithms crate is a collection of classic data structures and algorithms implemented in Rust. It is designed as a 'whitebox cookbook' rather than a blackbox library, prioritizing usability, clarity, and conciseness to help students, educators, and competitive programmers. The implementations are optimized for being easy to modify under time pressure in programming contests.
  3. Available Algorithm Modules

    master

    The repository contains implementations for several algorithmic domains:

    Graphs

    • Representations: Integer index-based adjacency lists, Disjoint Set Union (DSU).
    • Elementary Algorithms: Euler path/tour, Kruskal's MST, Dijkstra's shortest paths, DFS pre-order traversal.
    • Connectivity: Connected components, Strongly connected components, Bridges, 2-edge-connected components, Articulation points, 2-vertex-connected components, Topological sort, 2-SAT solver.
    • Network Flows: Dinic's maximum flow, Minimum cut, Hopcroft-Karp bipartite matching, Minimum cost maximum flow.

    Math

    • Number Theory: GCD, Bezout's identity, Miller's primality test.
    • FFT: Fast Fourier Transform, Number theoretic transform, Convolution.
    • Arithmetic: Rational numbers, Complex numbers, Linear algebra, Safe modular arithmetic.
    • Binary search (replacements for C++ lower_bound/upper_bound), Merge sort, Coordinate compression, Online convex hull trick.

    Associative Range Query (ARQ)

    • Binary indexed ARQ tree (Segment tree with lazy propagation), Dynamically allocated/sparse/persistent ARQ trees, Mo's algorithm.

    String Processing

    • Trie, Knuth-Morris-Pratt (KMP), Aho-Corasick, Suffix array, Longest common prefix, Manacher's algorithm.
  4. Implement Convex Hull Trick with PiecewiseLinearConvexFn

    master

    PiecewiseLinearConvexFn represents the maximum (upper envelope) of a collection of linear functions. It uses an online version of the convex hull trick with square root decomposition for efficient amortized performance.

    • max_with(new_m, new_b): Adds a new line with slope new_m and intercept new_b to the collection.
    • evaluate(x): Evaluates the current maximum function at point x with good amortized runtime.
  5. Analyze graph connectivity with ConnectivityGraph

    master

    The ConnectivityGraph struct provides several methods to query the connectivity properties of the graph it was initialized with:

    • cc: A Vec<usize> where cc[u] is the ID of vertex u's component (CC, SCC, or 2ECC). IDs range from 1 to num_cc.
    • vcc: A Vec<usize> where vcc[e] is the ID of edge e's 2VCC. IDs range from 1 to num_vcc.
    • num_cc: The total number of CCs, SCCs, or 2ECCs.
    • num_vcc: The total number of 2VCCs.
    • is_cut_vertex(u: usize) -> bool: Returns true if vertex u is an articulation vertex in an undirected graph.
    • is_cut_edge(e: usize) -> bool: Returns true if edge e is a bridge in an undirected graph.
  6. Compute Z-algorithm array

    master

    The z_algorithm(text) function computes the Z-array in $O(n)$ time. For each index i, Z[i] is the length of the longest prefix of text[i..] that is also a prefix of the entire text.

    use contest_algorithms::string_proc::z_algorithm;
    
    let z = z_algorithm(b"ababbababbabababbabababbababbaba");
  7. Compute maximum flow using Dinic's algorithm

    master

    The dinic(s, t) method implements Dinic's algorithm to find the maximum flow from source s to sink t (where s != t). It generalizes the Hopcroft-Karp algorithm for bipartite matching.

    Returns a tuple (max_flow, flow) where:

    • max_flow: The total amount of flow.
    • flow: A Vec<i64> containing the flow assigned to each edge index.

    Panics

    Panics if the maximum flow is $2^{63}$ or larger.

  8. Perform coordinate compression with SparseIndex

    master

    SparseIndex is a data structure used for coordinate compression.

    1. Initialize with SparseIndex::new(coords) where coords is a Vec<i64> of all possible coordinates.
    2. Use .compress(q) to find the compressed index of a coordinate q.

    compress returns:

    • Ok(i) if the coordinate q exists exactly at index i.
    • Err(i) if q falls between indices i-1 and i (useful for range queries).