Skip to main content

hypergraph_rs/
lib.rs

1//! # hypergraph-rs
2//!
3//! A hypergraph library for Rust. A hypergraph is a graph whose edges can join any number
4//! of nodes, not just two. This crate is a port of [XGI](https://github.com/xgi-org/xgi),
5//! the Python hypergraph library, and it builds on
6//! [rustworkx-core](https://crates.io/crates/rustworkx-core). Node and edge ids are strings,
7//! and every listing keeps the order in which things were added.
8//!
9//! Add it with `cargo add hypergraph-rs`.
10//!
11//! ## Quick start
12//!
13//! ```
14//! use hypergraph_rs::algorithms::connected::number_connected_components;
15//! use hypergraph_rs::Hypergraph;
16//!
17//! // Nodes, edges and memberships carry no value here, so all three types are `()`.
18//! let mut h: Hypergraph<(), (), ()> = Hypergraph::new();
19//!
20//! // An edge joins any number of nodes. Nodes that do not exist yet are created.
21//! h.add_edge(
22//!     vec!["ana".into(), "bo".into(), "cy".into()],
23//!     Some("club".into()),
24//!     (),
25//! )
26//! .unwrap();
27//! h.add_edge(
28//!     vec!["bo".into(), "cy".into(), "di".into(), "ed".into()],
29//!     Some("choir".into()),
30//!     (),
31//! )
32//! .unwrap();
33//! h.add_edge(vec!["fay".into(), "gus".into()], Some("pair".into()), ())
34//!     .unwrap();
35//!
36//! // Listings keep the order in which things were added.
37//! assert_eq!(h.node_ids(), ["ana", "bo", "cy", "di", "ed", "fay", "gus"]);
38//! assert_eq!(h.members("choir").unwrap(), ["bo", "cy", "di", "ed"]);
39//! assert_eq!(h.memberships("bo").unwrap(), ["club", "choir"]);
40//!
41//! // One statistic: "pair" shares no member with the other two edges, so there are
42//! // two connected components.
43//! assert_eq!(number_connected_components(&h), 2);
44//! ```
45//!
46//! ## What is in it
47//!
48//! - [`Hypergraph`]: undirected edges. [`DiHypergraph`]: each edge has a tail and a head.
49//!   [`SimplicialComplex`]: a hypergraph that keeps every subface of each simplex.
50//! - A hypergraph has three type parameters: the value on a node, on an edge and on a
51//!   membership. Each defaults to a JSON value. The statistics, readers, writers and
52//!   conversions work on that default type.
53//! - Modules, switched by the feature flags below: [`algorithms`], `generators`, `stats`,
54//!   `readwrite`, `layout`, `viz`, `raster`, `geometry`, `convert`, `dynamics`, `communities`. The
55//!   modules [`core`], [`linalg`] and [`utils`] are always there.
56//!
57//! ## A value on a membership
58//!
59//! A membership is the fact that one node belongs to one edge. XGI has no value on a
60//! membership. The membership value is this crate's own addition: the third type parameter,
61//! read and written with `member_data`, `set_member_data`, `members_with_data` and
62//! `add_node_to_edge_with_data` (on [`DiHypergraph`], `member_data`, `set_member_data` and
63//! `add_node_to_edge_with_data` also take the role, and `dimembers_with_data` lists the tail
64//! and the head together). A new membership carries `M::default()`. The XGI-compatible
65//! formats, the Python package and the WASM bindings do not carry the value.
66//!
67//! ```
68//! use hypergraph_rs::Hypergraph;
69//!
70//! let mut h: Hypergraph<(), (), u64> = Hypergraph::new();
71//! h.add_node_to_edge_with_data("council", "teachers", 3).unwrap();
72//! h.add_node_to_edge_with_data("union", "teachers", 40).unwrap();
73//! assert_eq!(h.member_data("council", "teachers"), Some(&3));
74//! assert_eq!(h.member_data("union", "teachers"), Some(&40));
75//! ```
76//!
77//! ## Feature flags
78//!
79//! The default features are `algorithms`, `generators`, `stats`, `readwrite`, `layout`,
80//! `viz`, `raster` and `geometry`.
81//!
82//! | Feature | Turns on |
83//! | --- | --- |
84//! | `algorithms` | connected components, shortest paths, clustering, centrality, assortativity, simpliciality |
85//! | `generators` | classic, random, lattice, uniform and simplicial generators (seeded) |
86//! | `stats` | node and edge statistics, for the default type of hypergraph |
87//! | `readwrite` | edge list, bipartite edge list, JSON, HIF and incidence matrix readers and writers, and a BiGG dictionary reader |
88//! | `layout` | deterministic layouts: node positions and the shape drawn for each edge |
89//! | `viz` | a scene graph built from a layout, with JSON and SVG output (needs `layout`) |
90//! | `raster` | the base of the terminal-text rasterizer (needs `viz` and `algorithms`) |
91//! | `dynamics` | Kuramoto synchronization |
92//! | `communities` | spectral clustering |
93//! | `convert` | conversions between hypergraphs and edge lists, dictionaries and graphs |
94//! | `network` | declares `readwrite::bigg::load_bigg_data`, which is not implemented and always returns an error |
95//! | `cells3d` | hexagonal-cell scenes and instruments for the rasterizer (needs `raster`, `communities` and the `h3o` crate) |
96//! | `raster-png` | PNG output for the rasterizer (needs `cells3d` and the `png` crate) |
97//! | `geometry` | outlines of a weighted set (ring, capsule, membrane), placement at a weighted mix of fixed points, spreading in fixed steps; the same bits on every machine |
98//! | `all` | every feature above |
99//!
100//! Every feature builds on its own, without the defaults.
101//!
102//! ## Minimum Rust version
103//!
104//! Rust 1.90, for the workspace and for every feature of this crate.
105
106// docs.rs builds with `--cfg docsrs` on nightly; `doc_cfg` labels each item with its feature.
107#![cfg_attr(docsrs, feature(doc_cfg))]
108
109#[cfg(feature = "algorithms")]
110pub mod algorithms;
111pub mod core;
112#[cfg(feature = "generators")]
113pub mod generators;
114#[cfg(feature = "geometry")]
115pub mod geometry;
116#[cfg(feature = "layout")]
117pub mod layout;
118pub mod linalg;
119#[cfg(feature = "raster")]
120pub mod raster;
121#[cfg(feature = "readwrite")]
122pub mod readwrite;
123#[cfg(feature = "stats")]
124pub mod stats;
125pub mod utils;
126#[cfg(feature = "viz")]
127pub mod viz;
128
129#[cfg(feature = "convert")]
130pub mod convert;
131
132#[cfg(feature = "dynamics")]
133pub mod dynamics;
134
135#[cfg(feature = "communities")]
136pub mod communities;
137
138pub use core::dihypergraph::DiHypergraph;
139pub use core::error::{EdgeError, MembershipError, NodeError, SimplexError, SwapError};
140pub use core::globalviews::subhypergraph;
141pub use core::hypergraph::{CleanupOptions, Hypergraph, MergeRename, MergeRule};
142pub use core::kinds::{DiMembershipEdge, DiRole, MembershipEdge, NodeKind};
143pub use core::simplicialcomplex::{FaceLattice, SimplicialComplex};
144pub use core::views::{EdgeView, FilterMode, NodeView};