Skip to main content

Module graph

Module graph 

Source
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:

C functionRust
igraph_empty, igraph_createGraph::new, Graph::empty, Graph::from_edges, Graph::from_flat_edges
igraph_empty_attrsGraph::empty followed by set_graph_attr (see attributes)
igraph_copy, igraph_destroyClone, Graph::try_clone, Drop
igraph_add_vertices, igraph_add_edge(s)add_vertices, add_vertex, add_edge, add_edge_id, add_edges, add_edges_from_slice, add_edges_from_vector
igraph_delete_vertices(_map), igraph_delete_edgesdelete_vertices, delete_vertices_map, delete_edges
igraph_vcount, igraph_ecount, igraph_is_directedvcount, ecount, num_vertices, num_edges, vertices, edge_ids, is_directed
igraph_neighbors, igraph_incidentneighbors, neighbors_with, incident
igraph_degree(_1)degree, degree_of
igraph_edge(s)edge, edges, edges_flat, edge_list, edges_iter
IGRAPH_FROM, IGRAPH_TO, IGRAPH_OTHERedge_from, edge_to, other_endpoint
igraph_get_eid(s), igraph_get_all_eids_betweenget_eid, has_edge, get_eids, get_eids_opt, get_all_eids_between
igraph_is_same_graphis_same_graph, PartialEq
igraph_invalidate_cacheinvalidate_cache
igraph_vs_*, igraph_es_* (igraph_iterators.h)select_vertices, select_edges, vs_size, es_size, adjacent_vertices, incident_edges

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:

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.
VertexId
Vertex identifier (igraph_int_t).