Skip to content

tekawade/algs4-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

algs4-rust

High-performance Rust implementation of algorithms and data structures from "Algorithms, 4th Edition" by Robert Sedgewick and Kevin Wayne

License: GPL v3 Rust Build Status Tests

Overview

algs4-rust is a comprehensive, production-ready Rust port of the acclaimed kevin-wayne/algs4 Java library. This project brings the fundamental algorithms and data structures from Algorithms, 4th Edition to the Rust ecosystem with:

  • Complete Implementation: 150+ algorithms and data structures across 7 modules
  • Production Quality: 1,500+ passing tests, comprehensive documentation, and benchmarks
  • Educational Value: Maintains the clarity and accessibility of the original textbook
  • Rust Performance: Zero-cost abstractions with memory safety guarantees

Project Status

🚀 95% Complete - Ready for Publication

All major modules are implemented, tested, and documented. The project is prepared for publication to crates.io.

Phase Module Algorithms Status
1-2 Fundamentals 23+ data structures & utilities ✅ Complete
3-4 Sorting 19 sorting algorithms ✅ Complete
5-6 Searching 21 search trees, hash tables, tries ✅ Complete
7 Graphs 48 graph algorithms ✅ Complete
8 Strings 16 string processing algorithms ✅ Complete
9 Geometry 7 geometric algorithms ✅ Complete
10 Advanced 16 advanced algorithms ✅ Complete

Total: 150+ implementations across 156 source files

Features

Why algs4-rust?

  • Memory Safe: Leverages Rust's ownership system to prevent buffer overflows, use-after-free, and data races
  • Zero-Cost Abstractions: Performance matching hand-written C while maintaining high-level expressiveness
  • Type Safe: Compile-time guarantees catch errors before runtime
  • Comprehensively Tested: 1,500+ unit and integration tests with 100% pass rate
  • Thoroughly Documented: Every public API includes examples and performance characteristics
  • Idiomatic Rust: Follows Rust API guidelines with proper trait implementations
  • Benchmarked: Criterion-based performance benchmarks for critical algorithms
  • Educational: Maintains the pedagogical clarity of the original textbook

Installation

Add the modules you need to your Cargo.toml:

[dependencies]
algs4-fundamentals = "0.1"
algs4-sorting = "0.1"
algs4-searching = "0.1"
algs4-graphs = "0.1"
algs4-strings = "0.1"
algs4-geometry = "0.1"
algs4-advanced = "0.1"

Or use the workspace locally:

git clone https://github.com/tekawade/algs4-rust.git
cd algs4-rust
cargo build --release

Quick Start

Basic Data Structures

use algs4_fundamentals::collections::{LinkedStack, ResizingArrayQueue};

// Stack operations
let mut stack = LinkedStack::new();
stack.push(1);
stack.push(2);
assert_eq!(stack.pop(), Some(2));

// Queue operations
let mut queue = ResizingArrayQueue::new();
queue.enqueue("first");
queue.enqueue("second");
assert_eq!(queue.dequeue(), Some("first"));

Sorting

use algs4_sorting::{quick_sort, merge_sort, heap_sort};

let mut data = vec![3, 1, 4, 1, 5, 9, 2, 6];

quick_sort(&mut data);
// data is now [1, 1, 2, 3, 4, 5, 6, 9]

// Or use other algorithms
merge_sort(&mut data);
heap_sort(&mut data);

Searching

use algs4_searching::{BST, RedBlackBST, SeparateChainingHashST};

// Binary Search Tree
let mut bst = BST::new();
bst.put("key1", 100);
bst.put("key2", 200);
assert_eq!(bst.get("key1"), Some(&100));

// Red-Black BST (self-balancing)
let mut rbbst = RedBlackBST::new();
rbbst.put(1, "one");
rbbst.put(2, "two");

// Hash Table
let mut ht = SeparateChainingHashST::new();
ht.put("name", "Alice");

Graphs

use algs4_graphs::{Graph, DepthFirstPaths, BreadthFirstPaths, DijkstraSP};

// Undirected graph
let mut g = Graph::new(6);
g.add_edge(0, 1);
g.add_edge(0, 2);
g.add_edge(1, 3);

// Find paths using DFS
let dfs = DepthFirstPaths::new(&g, 0);
if let Some(path) = dfs.path_to(3) {
    println!("Path from 0 to 3: {:?}", path);
}

// Find shortest path using BFS
let bfs = BreadthFirstPaths::new(&g, 0);
println!("Distance: {}", bfs.dist_to(3).unwrap());

Weighted Graphs

use algs4_graphs::{EdgeWeightedGraph, Edge, PrimMST, DijkstraSP};

// Minimum Spanning Tree
let mut ewg = EdgeWeightedGraph::new(5);
ewg.add_edge(Edge::new(0, 1, 1.0));
ewg.add_edge(Edge::new(0, 2, 2.0));
ewg.add_edge(Edge::new(1, 3, 3.0));

let mst = PrimMST::new(&ewg);
println!("MST weight: {}", mst.weight());

// Shortest paths with Dijkstra
let mut ewd = EdgeWeightedDigraph::new(5);
// ... add edges
let sp = DijkstraSP::new(&ewd, 0);

String Algorithms

use algs4_strings::{KMP, BoyerMoore, RabinKarp, Huffman, LZW};

// Pattern matching
let text = "AABRAACADABRAACAADABRA";
let pattern = "AACAA";

let kmp = KMP::new(pattern);
if let Some(pos) = kmp.search(text) {
    println!("Pattern found at position {}", pos);
}

// Compression
let data = b"ABABABABAB";
let compressed = Huffman::compress(data);
let decompressed = Huffman::decompress(&compressed);

Module Overview

Fundamentals (algs4-fundamentals)

23 implementations - Basic building blocks and utilities

Collections

  • Stacks: LinkedStack, ResizingArrayStack
  • Queues: LinkedQueue, ResizingArrayQueue
  • Bags: LinkedBag, ResizingArrayBag

Priority Queues

  • MaxPQ, MinPQ - Binary heap implementations
  • IndexMaxPQ, IndexMinPQ - Indexed priority queues

Union-Find

  • QuickFindUF - O(1) find, O(n) union
  • QuickUnionUF - Tree-based
  • WeightedQuickUnionUF - Weighted with path compression

I/O Utilities

  • StdIn - Input parsing
  • StdOut - Formatted output
  • StdRandom - Random number generation

Statistical Tools

  • Stopwatch - Timing measurements
  • Counter - Event counting
  • Accumulator - Running statistics
  • Transaction - Data objects

Tests: 136 passing | Documentation: Complete with examples


Sorting (algs4-sorting)

19 sorting algorithms - Complete sorting algorithm suite

Elementary Sorts

  • Selection Sort - O(n²) comparisons, O(n) swaps
  • Insertion Sort - O(n²) average, O(n) best case
  • Binary Insertion Sort - Uses binary search for insertion
  • Shell Sort - O(n^(3/2)) with h-sorting

Advanced Sorts

  • Merge Sort - O(n log n) guaranteed
  • Merge Bottom-Up - Non-recursive merge sort
  • Merge X - Optimized with cutoff to insertion sort
  • Quick Sort - O(n log n) average, fastest in practice
  • Quick 3-Way - Handles duplicate keys efficiently
  • Quick X - Optimized with cutoff and median-of-three
  • Quick Bentley-McIlroy - 3-way partitioning variant
  • Heap Sort - O(n log n) in-place

Radix Sorts

  • LSD - Least-significant-digit-first string sort
  • MSD - Most-significant-digit-first string sort
  • MSD In-Place - Space-efficient MSD variant
  • Quick 3-String - 3-way string quicksort

Utilities

  • Inversions - Count inversions in O(n log n)

Tests: 185 passing | Performance: Benchmarked for all major algorithms


Searching (algs4-searching)

21 data structures - Symbol tables and search algorithms

Ordered Symbol Tables

  • Binary Search ST - Sorted array implementation
  • BST - Binary search tree
  • Red-Black BST - Self-balancing BST (guaranteed O(log n))
  • AVL Tree - Height-balanced BST
  • B-Tree - Multi-way balanced tree

Hash Tables

  • Separate Chaining - Linked list collision resolution
  • Linear Probing - Open addressing with linear probing

Tries (Prefix Trees)

  • Trie ST - R-way trie symbol table
  • Trie SET - R-way trie set
  • Patricia ST - Practical algorithm for compressed tries
  • Patricia SET - Space-efficient trie set

Applications

  • Binary Search - Search in sorted arrays
  • Sequential Search - Unordered linked list search
  • Count - Frequency counting
  • Dedup - Remove duplicates
  • File Index - Build file index
  • Frequency Counter - Word frequency analysis
  • KWIK - Keyword in context
  • Lookup CSV - CSV database lookup
  • Lookup Index - Index-based lookup

Tests: 77+ passing | Coverage: All major symbol table implementations


Graphs (algs4-graphs)

48 implementations - Most comprehensive graph algorithm library

Graph Representations (6 types)

  • Graph - Undirected graph with adjacency lists
  • Digraph - Directed graph
  • EdgeWeightedGraph - Weighted undirected graph
  • EdgeWeightedDigraph - Weighted directed graph
  • SymbolGraph - Graph with string vertex names
  • SymbolDigraph - Directed symbol graph

Graph Generators (2 powerful generators)

  • GraphGenerator - Simple, complete, bipartite, tree, cycle, wheel, star, regular graphs
  • DigraphGenerator - DAG, tournament, rooted trees, strong/weak digraphs

Traversal (4 algorithms)

  • DepthFirstPaths - DFS pathfinding
  • BreadthFirstPaths - BFS shortest paths
  • DepthFirstDirectedPaths - DFS for digraphs
  • BreadthFirstDirectedPaths - BFS for digraphs

Connectivity (4 algorithms)

  • CC - Connected components (union-find based)
  • KosarajuSharirSCC - Strongly connected components
  • TarjanSCC - SCC with single DFS
  • GabowSCC - Path-based SCC algorithm

Shortest Paths (7 algorithms)

  • DijkstraSP - Non-negative edge weights, O((E + V) log V)
  • DijkstraUndirectedSP - Dijkstra for undirected graphs
  • DijkstraAllPairsSP - All-pairs shortest paths
  • BellmanFordSP - Handles negative weights, O(VE)
  • AcyclicSP - Linear-time for DAGs
  • AcyclicLP - Longest paths in DAGs
  • FloydWarshall - All-pairs with negative edges, O(V³)

Minimum Spanning Trees (4 algorithms)

  • PrimMST - Eager Prim's algorithm
  • LazyPrimMST - Lazy Prim's with priority queue
  • KruskalMST - Edge-based MST
  • BoruvkaMST - Borůvka's algorithm

Topological Sort & DAGs (2 algorithms)

  • Topological - Topological order for DAGs
  • DepthFirstOrder - Preorder, postorder, reverse postorder

Cycle Detection (4 algorithms)

  • Cycle - Detect cycles in undirected graphs
  • DirectedCycle - Cycle detection in digraphs
  • DirectedCycleX - Enhanced cycle detection
  • EdgeWeightedDirectedCycle - Cycles in weighted digraphs

Special Problems (6 algorithms)

  • Bipartite - Two-colorability check
  • BipartiteX - Enhanced bipartite detection
  • EulerianCycle - Find Eulerian cycles
  • EulerianPath - Find Eulerian paths
  • DirectedEulerianCycle - Eulerian cycles in digraphs
  • DirectedEulerianPath - Eulerian paths in digraphs

Maximum Flow (1 algorithm)

  • FordFulkerson - Max flow / min cut

Other (1 algorithm)

  • TransitiveClosure - Reachability queries

Tests: 322 passing | Benchmarks: Comprehensive Criterion suite | README: Detailed module documentation


Strings (algs4-strings)

16 implementations - String processing and pattern matching

Pattern Matching

  • KMP - Knuth-Morris-Pratt, O(n + m)
  • Boyer-Moore - Sublinear average case
  • Rabin-Karp - Fingerprint-based search

Regular Expressions

  • NFA - Nondeterministic finite automaton

Compression

  • Huffman - Huffman coding compression
  • LZW - Lempel-Ziv-Welch compression
  • Run-Length - Run-length encoding

Suffix Arrays

  • SuffixArray - Suffix array construction
  • SuffixArrayX - Optimized suffix array

Substring Problems

  • Longest Common Substring - LCS in O(n)
  • Longest Repeated Substring - Find longest repeat

Utilities

  • Alphabet - Custom alphabet support

Tests: 227 passing | Coverage: All major string algorithms


Geometry (algs4-geometry)

7 implementations - Computational geometry

Primitives

  • Point2D - 2D point with geometric operations
  • Interval1D - 1D interval
  • Interval2D - 2D axis-aligned rectangle
  • RectHV - Rectangle with geometric queries

Algorithms

  • Graham Scan - Convex hull in O(n log n)
  • Closest Pair - Divide and conquer, O(n log n)
  • Farthest Pair - Using convex hull

Tests: 329 passing | Documentation: Complete with geometric examples


Advanced (algs4-advanced)

16 implementations - Advanced topics and specialized algorithms

Linear Algebra

  • Complex - Complex number arithmetic
  • Vector - Dense vector operations
  • SparseVector - Sparse vector with dot products
  • Polynomial - Polynomial arithmetic and evaluation

Transforms

  • FFT - Fast Fourier Transform, O(n log n)

Matrix Operations

  • Gaussian Elimination - Solve linear systems
  • Gauss-Jordan Elimination - Matrix inversion

Optimization

  • Linear Programming - Simplex algorithm

Advanced Data Structures

  • Segment Tree - Range query data structure
  • Fenwick Tree - Binary indexed tree

Algorithms

  • ThreeSum - Find triplets summing to zero, O(n³)
  • ThreeSumFast - Optimized version, O(n² log n)

Applications

  • AllowFilter - Allowlist filtering
  • BlockFilter - Blocklist filtering
  • TopM - Track top M items

Tests: 101 passing | Coverage: All advanced algorithms


Project Structure

algs4-rust/
├── modules/
│   ├── fundamentals/     # Basic data structures (23 implementations)
│   │   ├── collections/  # Stacks, queues, bags
│   │   ├── union_find/   # Union-find algorithms
│   │   ├── priority_queue/ # Heap-based priority queues
│   │   ├── io/          # StdIn, StdOut, StdRandom
│   │   └── util/        # Stopwatch, Counter, etc.
│   ├── sorting/          # All sorting algorithms (19)
│   ├── searching/        # Search trees & hash tables (21)
│   ├── graphs/           # Graph algorithms (48)
│   │   └── benches/     # Criterion performance benchmarks
│   ├── strings/          # String processing (16)
│   ├── geometry/         # Geometric algorithms (7)
│   └── advanced/         # Advanced topics (16)
├── Cargo.toml           # Workspace configuration
├── README.md            # This file
└── LICENSE              # GPL-3.0 license

Building from Source

# Clone the repository
git clone https://github.com/tekawade/algs4-rust.git
cd algs4-rust

# Build all modules in release mode
cargo build --release --all

# Run all tests (1,500+ tests)
cargo test --all

# Run benchmarks (graphs module)
cargo bench -p algs4-graphs

# Generate and view documentation
cargo doc --open --no-deps

# Check code quality
cargo clippy --all -- -D warnings
cargo fmt --all --check

Development

Prerequisites

  • Rust: 1.70 or later
  • Cargo: Comes with Rust installation

Testing

# Run all tests
cargo test --all

# Run tests for specific module
cargo test -p algs4-fundamentals
cargo test -p algs4-sorting
cargo test -p algs4-graphs

# Run tests with output
cargo test --all -- --nocapture

# Run doc tests
cargo test --doc

Benchmarking

# Run benchmarks for specific module
cargo bench -p algs4-fundamentals
cargo bench -p algs4-sorting
cargo bench -p algs4-searching
cargo bench -p algs4-graphs
cargo bench -p algs4-strings
cargo bench -p algs4-geometry
cargo bench -p algs4-advanced

# Run all benchmarks (may take a while)
cargo bench --workspace

# Benchmarks include:
# - Fundamentals: Bags, Queues, Stacks, Union-Find
# - Sorting: All major sorting algorithms (Elementary, Merge, Quick, Heap, String Sorts)
# - Searching: BST, Red-Black BST, Hash Tables, Tries
# - Graphs: Traversal, Shortest Paths, MST, etc.
# - Strings: Pattern Matching (KMP), Tries
# - Geometry: Convex Hull
# - Advanced: Fenwick Tree, B-Tree

Documentation

Every public API is documented with:

  • Description and purpose
  • Time and space complexity
  • Usage examples (as doctests)
  • References to textbook sections
# Generate documentation
cargo doc --no-deps

# Open in browser
cargo doc --open --no-deps

Code Quality

# Format code
cargo fmt --all

# Lint code
cargo clippy --all -- -D warnings

# Check compilation without building
cargo check --all

Performance

All algorithms are implemented with performance as a priority:

  • Zero-cost abstractions: Generic code compiles to specialized machine code
  • Efficient data structures: Proper use of Vec, slices, and iterators
  • Minimal allocations: Reuse memory where possible
  • Profile-guided optimizations: Release builds use LTO and maximum optimization

Benchmark Results

The graphs module includes comprehensive Criterion benchmarks. Sample results (on modern hardware):

Operation Graph Size Time
Graph creation 1,000 vertices ~50 µs
DFS traversal 1,000 vertices ~80 µs
BFS shortest path 1,000 vertices ~90 µs
Connected components 1,000 vertices ~75 µs
Dijkstra (sparse) 1,000 vertices ~250 µs
Prim MST 1,000 vertices ~200 µs
Topological sort 1,000 vertices ~60 µs
Kosaraju SCC 1,000 vertices ~100 µs

Run cargo bench -p algs4-graphs to benchmark on your system.

Documentation Resources

Testing Philosophy

The project maintains high quality through comprehensive testing:

  • Unit Tests: Test individual functions and methods (1,500+ tests)
  • Integration Tests: Test module interactions and real-world scenarios
  • Doc Tests: Ensure documentation examples are correct and up-to-date
  • Property Tests: Verify algorithmic invariants
  • Regression Tests: Prevent bugs from reoccurring
  • Edge Cases: Empty inputs, single elements, large datasets

Current Status: 1,500+ tests, 100% passing

Contributing

Contributions are welcome! This project follows the Rust community guidelines:

Contribution Areas

  1. Algorithm Implementations: Add missing algorithms from the textbook
  2. Performance Optimizations: Improve algorithm efficiency
  3. Documentation: Enhance examples and explanations
  4. Tests: Add more test cases and property-based tests
  5. Benchmarks: Expand benchmark coverage
  6. Bug Fixes: Fix any issues you discover

Guidelines

  • Follow Rust API Guidelines
  • Write comprehensive tests for all changes
  • Document all public APIs with examples
  • Run cargo fmt and cargo clippy before submitting
  • Ensure all tests pass: cargo test --all
  • Add benchmarks for performance-critical code

Development Workflow

# Create a feature branch
git checkout -b feature/your-feature-name

# Make changes and test
cargo test --all
cargo clippy --all
cargo fmt --all

# Commit changes
git commit -m "feat: add new feature"

# Push and create PR
git push origin feature/your-feature-name

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

This license is inherited from the original kevin-wayne/algs4 repository to honor the authors' choice for educational software.

What This Means

  • ✅ Free to use for any purpose (commercial or personal)
  • ✅ Free to modify and distribute
  • ✅ Source code must remain open (copyleft)
  • ✅ Derivative works must use GPL-3.0

Credits

This project stands on the shoulders of giants:

  • Original Authors: Robert Sedgewick and Kevin Wayne
  • Textbook: Algorithms, 4th Edition by Sedgewick & Wayne
  • Original Implementation: kevin-wayne/algs4
  • Rust Implementation: algs4-rust contributors

Special thanks to Robert Sedgewick and Kevin Wayne for creating these algorithms and making them freely available to the programming community for educational purposes.

Related Projects

Rust Algorithm Libraries

Original algs4 Implementations

Acknowledgments

This project exists to bring the exceptional educational content of Algorithms, 4th Edition to the Rust community. We are grateful to:

  • Robert Sedgewick and Kevin Wayne for their seminal work in computer science education
  • The Rust community for creating an amazing language and ecosystem
  • All contributors who help improve this library

FAQ

Q: Is this project complete? A: The core implementation is ~95% complete with all major algorithms from the textbook. Some advanced features and optimizations are ongoing.

Q: Can I use this in production? A: Yes! The code is well-tested and follows Rust best practices. However, review the algorithms for your specific use case.

Q: How does performance compare to the Java implementation? A: Generally faster due to Rust's zero-cost abstractions and lack of garbage collection overhead. Benchmarks show comparable or better performance.

Q: Are the APIs stable? A: Currently at version 0.1, APIs may change before 1.0. Breaking changes will follow semantic versioning.

Q: Can I contribute? A: Absolutely! See the Contributing section above.

Q: Why GPL-3.0 license? A: We inherit the license from the original algs4 project to respect the authors' choice.


Note: This is an educational project implementing algorithms from Algorithms, 4th Edition. For production use, also consider specialized crates like petgraph for graphs or standard library collections.

Star this repo if you find it useful! ⭐

About

Rust implementation of https://github.com/kevin-wayne/algs4/

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages