#graph-node #graph #directed-graph #node-index #map #edge #indices

no-std mapgraph

A directed graph that can also be used as an arbitrary map

14 releases (8 breaking)

new 0.10.1 Mar 26, 2024
0.9.0 Feb 27, 2024
0.6.1 Dec 29, 2023
0.4.1 Sep 4, 2023
0.3.1 Feb 14, 2023

#351 in Data structures

Download history 7/week @ 2023-12-07 16/week @ 2023-12-28 7/week @ 2024-01-04 4/week @ 2024-02-15 170/week @ 2024-02-22 120/week @ 2024-02-29 39/week @ 2024-03-07 8/week @ 2024-03-14 82/week @ 2024-03-21

303 downloads per month

MIT/Apache

185KB
3.5K SLoC

mapgraph

crate documentation

A powerful library for directed adjacency list graphs.

Graph is a complex data structure where relations between nodes may often be cyclic which doesn't go well with the Rust's type system. A typical approach to this is to refer to nodes and edges in a graph using node and edge indices (sometimes called IDs or keys) which are mapped to the actual nodes, edges and their weights instead of using references or pointers. Since we're mapping indices anyway why not make the maps we use generic and allow custom indices?

mapgraph provides the generic Graph type that is based on two maps: one for nodes and one for edges. By using the rich system of traits from the map submodule the Graph type may borrow some capabilities of the maps it's based on. For example, using a HashMap as a node map for a Graph makes it accept external indices for nodes, effectively making the graph an index for its own nodes.

use mapgraph::aliases::{SlotMapGraph, HashSlotMapGraph};

// Using a graph with a SlotMap backing its nodes. The node's index is produced internally.
let mut first_graph = SlotMapGraph::<String, ()>::default();
let node_index = first_graph.add_node("foo".to_string());
assert_eq!(first_graph.node_weight(node_index).unwrap(), "foo");

// Using a graph with a HashMap backing its nodes. The node's index is supplied externally.
let mut second_graph = HashSlotMapGraph::<&str, (), &str>::default();
second_graph.try_add_node_at_index("foo", "bar")?;
assert_eq!(second_graph.node_weight("foo").unwrap(), &"bar");

See the crate's documentation for more info.

License

Licensed under either of

at your option.

Contribution

Contributions are welcome and should be submitted as merge requests to this repository.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.3–0.9MB
~17K SLoC