Skip to main content

Module constructors

Module constructors 

Source
Expand description

Deterministic graph generators (igraph_constructors.h).

This module turns igraph’s deterministic constructors into associated functions of Graph returning Result<Graph>: given the same arguments they always build the very same graph, with the same vertex and edge ids. (Random generators live in crate::games.)

use igraph::prelude::*;

// The Petersen graph, three ways: by name, as G(5, 2), and from its LCF code.
let by_name = Graph::famous("Petersen").unwrap();
let gp = Graph::generalized_petersen(5, 2).unwrap();
assert_eq!((by_name.vcount(), by_name.ecount()), (10, 15));
assert_eq!((gp.vcount(), gp.ecount()), (10, 15));
// Both are 3-regular.
for g in [&by_name, &gp] {
    let degrees = g.degree(VertexSelector::All, NeighborMode::All, Loops::Twice).unwrap();
    assert!(degrees.iter().all(|&d| d == 3));
}

// A 3x4 grid, a 6-cycle and a star with 5 leaves.
let grid = Graph::square_lattice(&[3, 4], 1, false, false, None).unwrap();
assert_eq!((grid.vcount(), grid.ecount()), (12, 17));
let c6 = Graph::cycle_graph(6, false, false).unwrap();
assert_eq!(c6.ecount(), 6);
let star = Graph::star(6, StarMode::Undirected, 0).unwrap();
assert_eq!(star.degree_of(0, NeighborMode::All, Loops::Twice).unwrap(), 5);

§Provided functionality

FamilyFunctions
From matricesGraph::adjacency, Graph::weighted_adjacency, Graph::sparse_adjacency, Graph::sparse_weighted_adjacency
From edge listsGraph::small (see also Graph::from_edges)
Paths, cycles, starsGraph::ring, Graph::path_graph, Graph::cycle_graph, Graph::star, Graph::wheel
Complete graphsGraph::full, Graph::full_citation, Graph::full_multipartite, Graph::turan
LatticesGraph::square_lattice, Graph::triangular_lattice, Graph::hexagonal_lattice, Graph::hypercube
TreesGraph::kary_tree, Graph::symmetric_tree, Graph::regular_tree, Graph::tree_from_parent_vector, Graph::from_prufer
Circulant-likeGraph::circulant, Graph::generalized_petersen, Graph::lcf, Graph::extended_chordal_ring
Word graphsGraph::de_bruijn, Graph::kautz
Named graphsGraph::famous (with FamousGraph), Graph::atlas, Graph::mycielski_graph
Degree sequencesGraph::realize_degree_sequence, Graph::realize_bipartite_degree_sequence
Derived graphsGraph::linegraph

The corresponding chapter of the C documentation is Deterministic graph generators.

§Conventions

  • Vertex counts and sizes are usize; values that may legitimately be negative (shifts, parent ids, degrees coming from Graph::degree) are i64.
  • Every function returns an Error rather than panicking on invalid input; most errors are ErrorKind::InvalidValue.

§See also

NeedWhere
Random graphs (Erdős–Rényi, random regular, random trees, degree sequences, …)crate::games, e.g. Graph::erdos_renyi_game_gnm, Graph::k_regular_game, Graph::tree_game, Graph::degree_sequence_game
The inverse conversions (graph → matrix, tree → Prüfer code)Graph::get_adjacency, Graph::get_adjacency_sparse, Graph::to_prufer in crate::conversion
Bipartite constructorsGraph::full_bipartite, BipartiteGraph in crate::bipartite
Graphs derived from other graphsGraph::complementer, Graph::mycielskian, Graph::disjoint_union in crate::operators
Is a degree sequence realizable at all?is_graphical, is_bigraphical
Comparing generated graphs up to relabellingGraph::isomorphic, Graph::count_automorphisms in crate::isomorphism
Drawing themGraph::layout_circle (rings), Graph::layout_star, Graph::layout_grid (lattices), Graph::layout_reingold_tilford (trees) in crate::layout
Reading graphs from filescrate::foreign
use igraph::prelude::*;

// LCF notation and the named graph agree up to relabelling: the Heawood
// graph two ways.
let heawood = Graph::famous("Heawood")?;
assert!(Graph::lcf(14, &[5, -5], 7)?.isomorphic(&heawood)?);
// Its automorphism group PGL(2, 7) has order 336, and its girth is 6.
assert_eq!(heawood.count_automorphisms(None)?, 336.0);
assert_eq!(heawood.girth()?, Some(6));

// Round trip through the adjacency matrix of the conversion module.
let a = heawood.get_adjacency(GetAdjacency::Both, None, Loops::Twice)?;
assert_eq!(Graph::adjacency(&a, Adjacency::Undirected, Loops::Twice)?, heawood);

§Differences from the C library

A few defects of igraph 1.0.0 and 1.0.1 (the generator sources are unchanged in 1.0.1) are worked around on the Rust side, so the wrappers behave as documented:

  • Graph::sparse_adjacency / Graph::sparse_weighted_adjacency agree with their dense counterparts even when an entry below the diagonal has no mirror entry (igraph drops it in the unweighted Max mode and in the weighted Max, Min and Plus modes), and when explicit zeros are given in the Undirected mode (igraph’s structural symmetry test would reject them).
  • Graph::adjacency and Graph::sparse_adjacency reject NaN, infinite, negative and fractional edge counts (igraph casts them to integers unchecked).
  • Graph::star and Graph::wheel with n = 1 give the singleton graph (igraph returns the null graph).
  • Graph::lcf with n = 0 gives the null graph (igraph divides by zero), and LCF shifts and chordal ring offsets are reduced modulo the number of vertices (igraph adds them without overflow checks).
  • Graph::extended_chordal_ring accepts a chord matrix without columns (igraph divides by the number of columns).

Re-exports§

pub use crate::constants::AllowedEdgeTypes;

Enums§

FamousGraph
The named graphs known to Graph::famous.