Expand description
The Graph type and igraph’s basic interface (igraph_interface.h).
Graph is the C struct igraph_t itself, made Rusty: it owns its data
(destroyed on Drop), it is Clone (via igraph_copy) and Send,
and its methods return Results instead of error codes. Vertices and
edges are identified by consecutive integer ids starting from zero, as in
igraph; ids are i64 (igraph_int_t) while counts are usize.
This module contains the fundamental operations: construction, adding
and removing vertices and edges, and queries about the structure.
Algorithms live in the other modules of the crate, most of them as further
methods of Graph.
use igraph::prelude::*;
// A directed triangle plus a pendant vertex.
let mut g = Graph::from_edges(&[(0, 1), (1, 2), (2, 0)], 4, true).unwrap();
g.add_edge(2, 3).unwrap();
assert_eq!((g.vcount(), g.ecount()), (4, 4));
assert_eq!(g.neighbors(2, NeighborMode::Out).unwrap(), vec![0, 3]);
assert_eq!(g.edge(3).unwrap(), (2, 3));
assert_eq!(g.get_eid(1, 2, true).unwrap(), Some(1));
assert_eq!(g.get_eid(2, 1, true).unwrap(), None);
let h = g.clone();
g.delete_vertices(3).unwrap();
assert_eq!((g.vcount(), h.vcount()), (3, 4));The functions of igraph_interface.h map as follows:
For raw FFI users, Graph::init_with turns any C function that
initializes an igraph_t into a Result<Graph>, and Graph::setup
initializes igraph for the calling thread (every safe wrapper does it
automatically, see crate::error::ensure_init).
Graph is Send (it can be moved to another thread) but not Sync:
igraph lazily caches properties inside the graph even through const
pointers.
§Where to go next
Most graphs are not built edge by edge:
- deterministic graphs (rings, lattices, trees, the named graphs of
Graph::famous, …) are inconstructors, random graph models ingames, and file readers and writers inforeign; - conversions to and from matrices and edge lists are in
conversion(e.g.Graph::get_adjacency) andconstructors(e.g.Graph::adjacency); - structural queries beyond degrees (strength, simplicity, multi-edges,
density, …) are in
structural, subgraphs and simplification inoperators, and vertex, edge and graph attributes inattributes; - for repeated neighborhood queries,
adjlistbuilds adjacency and incidence lists once.
use igraph::prelude::*;
// Zachary's karate club, one of igraph's named graphs.
let club = Graph::famous("Zachary").unwrap();
assert_eq!((club.vcount(), club.ecount()), (34, 78));
let degree = club.degree(.., NeighborMode::All, Loops::Twice).unwrap();
// The handshake lemma: every edge has two endpoints.
assert_eq!(degree.iter().sum::<i64>(), 2 * 78);
// The instructor (0) and the administrator (33) are the hubs.
assert_eq!((degree[0], degree[33]), (16, 17));
assert_eq!(club.maxdegree(.., NeighborMode::All, Loops::Twice).unwrap(), 17);Type Aliases§
- EdgeId
- Edge identifier (
igraph_int_t). - Graph
- An igraph graph (
igraph_t), see the module docs. - Vertex
Id - Vertex identifier (
igraph_int_t).