114 releases (45 breaking)

new 0.46.0 May 17, 2024
0.44.0 May 15, 2024
0.2.3 Mar 31, 2024

#363 in Data structures

Download history 981/week @ 2024-03-29 993/week @ 2024-04-05 2195/week @ 2024-04-12 2196/week @ 2024-04-19 2949/week @ 2024-04-26 2267/week @ 2024-05-03 1387/week @ 2024-05-10

8,991 downloads per month

MIT/Apache

515KB
14K SLoC

Graaf!Crates.io Build status API reference Coverage Status

Work with directed graphs

Installation

Add the following to your Cargo.toml:

[dependencies]
graaf = "0.46.0"

Overview

Operations

Build and query graphs.

use {
    graaf::{
        gen::EmptyConst,
        op::*,
    },
    std::collections::BTreeSet,
};

let mut graph = <[BTreeSet<usize>; 3]>::empty();

graph.add_arc(0, 1);
graph.add_arc(0, 2);

assert_eq!(graph.degree(0), 2);
assert_eq!(graph.degree(1), 1);
assert_eq!(graph.degree(2), 1);

Algorithms

Search, traverse, and analyze graphs.

use graaf::algo::bfs::single_pair_shortest_path as spsp;

// 0  ←  1
// ↑     ↑
// 3  →  2

let graph = [Vec::new(), vec![0], vec![1], vec![0, 2]];

assert_eq!(spsp(&graph, 3, 0), Some(vec![3, 0]));
assert_eq!(spsp(&graph, 3, 1), Some(vec![3, 2, 1]));
assert_eq!(spsp(&graph, 3, 2), Some(vec![3, 2]));
assert_eq!(spsp(&graph, 0, 3), None);

Representations

An adjacency matrix representation is available with the adjacency_matrix feature.

use graaf::{
    op::*,
    repr::AdjacencyMatrix,
};

let mut graph = AdjacencyMatrix::<3>::new();

graph.add_arc(0, 1);
graph.add_arc(1, 1);

assert!(!graph.is_simple());

Generators

Generate parameterized graphs.

use graaf::gen::*;

assert_eq!(
    Vec::<Vec<usize>>::empty(2),
    vec![Vec::new(), Vec::new()]
);

assert_eq!(
    Vec::<Vec<usize>>::cycle(3),
    vec![vec![1], vec![2], vec![0]]
);

assert_eq!(
    <[Vec::<usize>; 3]>::complete(),
    [vec![1, 2], vec![0, 2], vec![0, 1]]
);

Features

These features require nightly Rust.

  • adjacency_matrix enables the adjacency matrix representation.

Changelog

See CHANGELOG.md.

License

Use graaf under either the Apache License, Version 2.0, or the MIT license at your option.

No runtime deps