Skip to content

Steins algo #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
cdecccd
init commit, code cleanup
Jan 29, 2023
788f7f3
used newer convention for modules
Jan 30, 2023
63e1233
Update README.md
legalup Jan 30, 2023
eb93296
Update README.md
legalup Feb 3, 2023
c4f2d6e
new graph classes. still, i think tests are incorrect
legalup1729 Feb 4, 2023
28798a9
Merge branch 'master' of github.com:legalup/rust-llama
legalup1729 Feb 4, 2023
eaa2f06
changed data representation of directed graph to something cleaner, s…
Feb 11, 2023
571f68e
udg now works too...as if by miracle
Feb 12, 2023
2576ee8
udg now works too...as if by miracle
Feb 12, 2023
bbb8db5
used internal edge weights for mst. alleviates need to understand dat…
Feb 13, 2023
6ee1d6a
Merge branch 'master' of github.com:legalup/rust-llama-cookbook
Feb 13, 2023
0f23b85
Merge branch 'master' of github.com:legalup/rust-llama-cookbook
Feb 13, 2023
b450889
Update README.md
legalup Feb 13, 2023
2f90101
Update README.md
legalup Feb 13, 2023
918a902
Update README.md
legalup Feb 13, 2023
72b75a1
Update README.md
legalup Feb 13, 2023
0fc219a
Update README.md
legalup Feb 13, 2023
267bdd2
Merge branch 'master' of github.com:legalup/rust-llama-cookbook
Feb 14, 2023
c9f67c0
changed edge of undirected graph to set
Feb 14, 2023
6c45242
changed edge of undirected graph to set
Feb 14, 2023
42c2c0e
cleaned up dijkstra impl
Feb 15, 2023
269f713
Merge branch 'master' of github.com:legalup/rust-llama-cookbook
Feb 15, 2023
f697e79
Merge branch 'master' of github.com:legalup/rust-llama-cookbook
Feb 15, 2023
097dd49
Merge branch 'master' of github.com:legalup/rust-llama-cookbook
Feb 15, 2023
9784cc7
floyd warshall algo
Feb 15, 2023
d306487
Update README.md
legalup Feb 15, 2023
f3cccbb
Update README.md
legalup Feb 15, 2023
6b73af7
Update README.md
legalup Feb 15, 2023
71efc8e
Update LICENSE
legalup Feb 15, 2023
e990191
Create LICENSE.md
legalup Feb 15, 2023
3357eda
Merge pull request #1 from legalup/add-license-1
legalup Feb 15, 2023
3840dd8
did licensing properly
legalup1729 Feb 15, 2023
3cb3131
added test for floyd warshall
legalup1729 Feb 16, 2023
fbce395
added levenshtein distance
legalup1729 Feb 17, 2023
8292e95
flakes
Apr 25, 2024
a44a043
adding steins algo for fast gcd computation
Dec 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "contest-algorithms"
name = "contest-llamas"
version = "0.3.1-alpha.0"
authors = ["Aram Ebtekar"]
authors = ["Luis Galup"]
edition = "2021"

description = "Common algorithms and data structures for programming contests"
repository = "https://github.com/EbTech/rust-algorithms"
repository = "https://github.com/legalup/rust-llama-cookbook"
readme = "README.md"
keywords = ["competitive", "programming", "codeforces"]
categories = ["algorithms", "data-structures"]
Expand Down
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

29 changes: 29 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) for portions of project Foo are held by Aram Ebtekar 2017 as part of project rust-algorithms. All other copyright for project rust-ars-algorithmica are held by Luis Galup 2023.


Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 changes: 10 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
# Contest Algorithms in Rust
# Rust /\r5 /\lgorithmic/\

[![Crates.io Version](https://img.shields.io/crates/v/contest-algorithms.svg)](https://crates.io/crates/contest-algorithms)
[![Documentation](https://docs.rs/contest-algorithms/badge.svg)](https://docs.rs/contest-algorithms)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/bevyengine/bevy/blob/master/LICENSE)
[![Crates.io Downloads](https://img.shields.io/crates/d/contest-algorithms.svg)](https://crates.io/crates/contest-algorithms)
[![Build Status](https://travis-ci.org/EbTech/rust-algorithms.svg?branch=master)](https://travis-ci.org/EbTech/rust-algorithms)
[![Gitter](https://badges.gitter.im/rust-algos/community.svg)](https://gitter.im/rust-algos/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
This is a collection of classic data structures and interesting algorithms, emphasizing clarity, elegance and understanding over generality and speed. One design criterion is transparency: explain the names and concepts, annotate with complexity, be obvious in intention. Another is simplicity: the Rust ecosystem is full of well meaning crates, and I have intentionally decided to stick with data structures and algorithms only found in std for universality.

A collection of classic data structures and algorithms, emphasizing usability, beauty and clarity over full generality. As such, this should be viewed not as a blackbox *library*, but as a whitebox *cookbook* demonstrating the design and implementation of algorithms. I hope it will be useful to students and educators, as well as fans of algorithmic programming contests.
This is a fork of EbTech's amazing repo. My intention is to change the fundamental graph base classes to be more in keeping with our intuition and understanding of graphs, do more data representation encapsulation, decouple algorithm implementation from knowledge of the internals of their datastructures, put in more tests of algorithms validity and speed,and when happy with that, start adding far more algorithms.

This repository is distributed under the [MIT License](LICENSE). Contest submissions need not include the license text. Enjoy!
My hope is that someday my kids will use this to learn about algorithms, something that I have always been obsessed about. Its also intended for students/teachers of algorithmica. I also think that its useful for competive programming; and that Rust is in many ways a good language for that. Except for the fact that its not easy to make a linked list...

## For Students and Educators
Rust is a language that makes algorithms smaller and simpler, eliminating some deep bugs that are inevitable from the complexity in other languages (like c++). Its functional nature enforces elegance and compactness, its compiler assists in correctness.

When learning a new algorithm or data structure, it's often helpful to see or play with a concrete implementation. As such, this repository catalogues several classic algorithms in their simplest forms.

In addition, the Rust language has outstanding pedagogical attributes. Its compiler acts as a teacher, enforcing strict discipline while pointing to clearer ways to structure one's logic.

## For Programming Contests

The original intent of this project was to build a reference for use in programming contests. As a result, it contains algorithms that are frequently useful to have in one's toolkit, with an emphasis on code that is concise and easy to modify under time pressure.

Most competitive programmers rely on C++ for its fast execution time. However, it's notoriously unsafe, diverting a considerable share of the contestant's time and attention on mistake prevention and debugging. Java is the next most popular choice, offering a little safety at some expense to speed of coding and execution.

To my delight, I found that Rust eliminates entire classes of bugs, while reducing visual clutter to make the rest easier to spot. And, it's *fast*. There's a learning curve, to be sure. However, a proficient Rust programmer stands to gain a competitive advantage as well as a more pleasant experience!
![llama](https://user-images.githubusercontent.com/9121210/218507152-5a9646d5-c8bb-4937-acfb-8834410975fd.jpg)

Some contest sites and online judges that support Rust:
- [Codeforces](https://codeforces.com)
Expand All @@ -34,22 +20,11 @@ Some contest sites and online judges that support Rust:
- [HackerRank](https://www.hackerrank.com/contests)
- [Timus](http://acm.timus.ru/help.aspx?topic=rust)

The following support pre-2018 versions of Rust:
- [Google Kick Start and Code Jam](https://codingcompetitions.withgoogle.com)

For help in getting started, you may check out [some of my past submissions](https://codeforces.com/contest/1168/submission/55200038).

## Programming Language Advocacy

My other goal is to appeal to developers who feel limited by ancient (yet still mainstream) programming languages, by demonstrating the power of modern techniques.

Rather than try to persuade you with words, this repository aims to show by example. If you'd like to learn the language, I recommend [the official book](https://doc.rust-lang.org/book/) or [Programming Rust](https://www.amazon.com/Programming-Rust-Fast-Systems-Development-dp-1492052590/dp/1492052590).

# Contents

## [Graphs](src/graph/)

### [Graph representations](src/graph/mod.rs)
### [Graph representations](src/graph/graph.rs)

- Integer index-based adjacency list representation
- Disjoint set union
Expand All @@ -60,6 +35,7 @@ Rather than try to persuade you with words, this repository aims to show by exam
- Kruskal's minimum spanning tree
- Dijkstra's single-source shortest paths
- DFS pre-order traversal
- Floyd warshall shortest paths

### [Connected components](src/graph/connectivity.rs)

Expand All @@ -74,12 +50,12 @@ Rather than try to persuade you with words, this repository aims to show by exam

- Dinic's blocking maximum flow
- Minimum cut
- Hopcroft-Karp bipartite matching
- Hopcroft-Karp bipartite maximum matching O(\sqrt(V)*E)
- Minimum cost maximum flow

## [Math](src/math/)

### [Number theory](src/math/mod.rs)
### [Number theory](src/math/division.rs)

- Greatest common divisor
- Canonical solution to Bezout's identity
Expand Down
133 changes: 133 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
rust-overlay.url = "github:oxalica/rust-overlay";
};

outputs = inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
systems = [ "x86_64-linux" ];
perSystem = { config, self', pkgs, lib, system, ... }:
let
runtimeDeps = with pkgs; [ alsa-lib speechd ];
buildDeps = with pkgs; [ pkg-config rustPlatform.bindgenHook ];
devDeps = with pkgs; [ gdb ];

cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
msrv = cargoToml.package.rust-version;

rustPackage = features:
(pkgs.makeRustPlatform {
cargo = pkgs.rust-bin.stable.latest.minimal;
rustc = pkgs.rust-bin.stable.latest.minimal;
}).buildRustPackage {
inherit (cargoToml.package) name version;
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
buildFeatures = features;
buildInputs = runtimeDeps;
nativeBuildInputs = buildDeps;
# Uncomment if your cargo tests require networking or otherwise
# don't play nicely with the Nix build sandbox:
# doCheck = false;
};

mkDevShell = rustc:
pkgs.mkShell {
shellHook = ''
export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc}
'';
buildInputs = runtimeDeps;
nativeBuildInputs = buildDeps ++ devDeps ++ [ rustc ];
};
in {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = [ (import inputs.rust-overlay) ];
};

packages.default = self'.packages.example;
devShells.default = self'.devShells.nightly;

packages.example = (rustPackage "foobar");
packages.example-base = (rustPackage "");

devShells.nightly = (mkDevShell (pkgs.rust-bin.selectLatestNightlyWith
(toolchain: toolchain.default)));
devShells.stable = (mkDevShell pkgs.rust-bin.stable.latest.default);
devShells.msrv = (mkDevShell pkgs.rust-bin.stable.${msrv}.default);
};
};
}
5 changes: 4 additions & 1 deletion src/caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,33 @@ where
U: std::cmp::Eq + std::hash::Hash + Copy,
V: Copy,
{
/*
/// Constuctor for the Casher
/// # Examples
/// ```
/// # use contest_algorithms::caching::Cacher;
/// let mut squared = Cacher::new(|n: u32| n*n);
*/
/// ```
pub fn new(calculation: F) -> Cacher<F, U, V> {
Cacher {
calculation,
values: HashMap::new(),
}
}

/// Performs a lookup into the HashMap to see if the value has already
/// been calculated. If it has, returns the value. If it has not,
/// calls the function, stores the value, then returns the value.
/// # Examples
/// ```
/*
/// # use contest_algorithms::caching::Cacher;
/// let mut squared = Cacher::new(|n: u32| n*n);
///
/// // This is where we call the function
/// let sixteen = squared.call(4);
/// ```
*/
// TODO: whenever Rust's Entry API gains the ability to take ownership of
// arg only when necessary, this method should follow the same practice.
// Also, Cacher should implement Fn(U)->V once this is possible.
Expand Down
5 changes: 5 additions & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod connectivity;
pub mod disjoint_set;
pub mod flow;
pub mod graph;
pub mod util;
Loading