Crates.io | ultragraph |
lib.rs | ultragraph |
version | 0.5.2 |
source | src |
created_at | 2023-08-17 03:33:18.439183 |
updated_at | 2024-01-14 14:42:41.09666 |
description | Hypergraph data structure. |
homepage | https://github.com/deepcausality/deep_causality/tree/main/ultragraph |
repository | https://github.com/deepcausality/deep_causality.rs |
max_upload_size | |
id | 946615 |
size | 87,881 |
Ultragraph aims to simplify working with directed graph data structures by adding more features such as storing and retrieving nodes directly from the graph, getting all neighbors of a node, and some basic algorithm such as shortest path.
Just run:
cargo add ultragraph
Alternatively, add the following to your Cargo.toml
ultragraph = "current_version"
See:
use ultragraph::prelude::*;
#[derive(Default, Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct Data {
x: u8,
}
pub fn main() {
let mut g = ultragraph::with_capacity::<Data>(10);
// Add nodes to the graph
let root_index = g.add_root_node(Data { x: 3 });
let node_a_index = g.add_node(Data { x: 7 });
let node_b_index = g.add_node(Data { x: 9 });
let node_c_index = g.add_node(Data { x: 11 });
// Link nodes together
// Link root node to node a
let res = g.add_edge(root_index, node_a_index);
assert!(res.is_ok());
// Link node a to node b
let res = g.add_edge(node_a_index, node_b_index);
assert!(res.is_ok());
// Link node root to node c
let res = g.add_edge(root_index, node_c_index);
assert!(res.is_ok());
// Get node a
let node = g.get_node(node_a_index);
assert!(node.is_some());
let data = node.unwrap();
assert_eq!(data.x, 7);
// get all outgoing_edges of root node
let neighbors = g.outgoing_edges(root_index).unwrap();
// root node has 2 outgoing_edges: node a and node b
assert_eq!(neighbors.len(), 2);
// neighbors is just a vector of indices
// so you can iterate over them to get the actual nodes
println!("Neighbors of root node: ");
for n in neighbors{
let node = g.get_node(n).unwrap();
println!("node: {:?}", node);
}
}
The project took inspiration from:
Contributions are welcomed especially related to documentation, example code, and fixes. If unsure where to start, just open an issue and ask.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in deep_causality by you, shall be licensed under the MIT licence, without any additional terms or conditions.
This project is licensed under the MIT license.