ultragraph

Crates.ioultragraph
lib.rsultragraph
version0.5.4
sourcesrc
created_at2023-08-17 03:33:18.439183
updated_at2024-11-24 11:52:56.963281
descriptionHypergraph data structure.
homepagehttps://github.com/deepcausality/deep_causality/tree/main/ultragraph
repositoryhttps://github.com/deepcausality/deep_causality.rs
max_upload_size
id946615
size88,435
Marvin Hansen (marvin-hansen)

documentation

https://docs.rs/ultragraph

README

ultragraph

Crates.io Docs.rs MIT licensed Audit Clippy Tests

📣 Goal

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.

🎁 Features

  • Stores nodes directly in the graph for easy access
  • Access to all nodes and edges in the graph (get_node & get_all_nodes)
  • Access to all neighbors of a node (outgoing_edges)
  • Shortest path algorithm

⚡️ Implementation

  • Wraps petgraph under the hood
  • Stores relations in a matrix graph and nodes in a hashmap
  • Supports multiple implementations via storage trait pattern
  • Adds proper error handling

🚀 Install

Just run:

cargo add ultragraph

Alternatively, add the following to your Cargo.toml

ultragraph = "current_version"

⭐ Usage

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);
    }
}

🙏 Credits

The project took inspiration from:

👨‍💻👩‍💻 Contribution

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.

📜 Licence

This project is licensed under the MIT license.

💻 Author

  • Marvin Hansen, GitHub.
  • Github GPG key ID: 369D5A0B210D39BC
  • GPG Fingerprint: 4B18 F7B2 04B9 7A72 967E 663E 369D 5A0B 210D 39BC
Commit count: 1107

cargo fmt