Skip to main content

Module conversion

Module conversion 

Source
Expand description

Conversion of graphs to matrices, edge lists, Prüfer sequences, and between directed and undirected graphs (igraph_conversion.h).

This module binds the whole of igraph’s conversion chapter. Every function is a method of Graph:

MethodC functionResult
Graph::get_adjacencyigraph_get_adjacencydense adjacency Matrix (optionally weighted)
Graph::get_adjacency_sparseigraph_get_adjacency_sparsesparse adjacency matrix as a CooMatrix
Graph::get_stochasticigraph_get_stochasticrow- or column-stochastic transition Matrix
Graph::get_stochastic_sparseigraph_get_stochastic_sparsethe same, as a CooMatrix
Graph::get_edgelistigraph_get_edgelistflat edge list, row- or column-wise
Graph::to_directed / Graph::into_directedigraph_to_directedundirected → directed, in place / by value
Graph::to_undirected / Graph::into_undirectedigraph_to_undirecteddirected → undirected, in place / by value
Graph::to_undirected_with_combigraph_to_undirectedthe same, combining the edge attributes of merged edges
Graph::to_pruferigraph_to_pruferPrüfer sequence of a labelled tree

§Sparse results

igraph returns sparse matrices as igraph_sparsemat_t (a CXSparse matrix, exposed by this crate as SparseMat). The sparse wrappers of this module read that matrix out into a plain-Rust CooMatrix (coordinate / triplet format), with duplicate entries summed up and the entries sorted in row-major order. It is easy to inspect, compare and iterate over; convert it back with CooMatrix::to_sparsemat when you need the sparse linear algebra of crate::linalg (solvers, factorizations, ARPACK eigensolvers).

§See also

§Example

use igraph::prelude::*;

// A directed 3-cycle 0 → 1 → 2 → 0.
let mut g = Graph::from_edges(&[(0, 1), (1, 2), (2, 0)], 3, true).unwrap();
let a = g.get_adjacency(GetAdjacency::Both, None, Loops::Twice).unwrap();
assert_eq!(a.to_rows(), vec![vec![0.0, 1.0, 0.0], vec![0.0, 0.0, 1.0], vec![1.0, 0.0, 0.0]]);

// Forget the directions: the adjacency matrix becomes symmetric.
g.to_undirected(ToUndirected::Collapse).unwrap();
let a = g.get_adjacency(GetAdjacency::Both, None, Loops::Twice).unwrap();
assert_eq!(a, a.transposed());
// ... and it is enough to rebuild the graph.
let h = Graph::adjacency(&a, Adjacency::Undirected, Loops::Twice).unwrap();
assert_eq!(h.ecount(), 3);

// Random walk transition probabilities: each row sums to one.
let p = g.get_stochastic(false, None).unwrap();
assert!(p.rows().all(|r| (r.iter().sum::<f64>() - 1.0).abs() < 1e-12));

// A path is a tree: its Prüfer sequence lists the inner vertices.
let path = Graph::from_edges(&[(0, 1), (1, 2), (2, 3)], 4, false).unwrap();
assert_eq!(path.to_prufer().unwrap(), vec![1, 2]);
assert!(Graph::from_prufer(&[1, 2]).unwrap().is_same_graph(&path).unwrap());

On a larger scale, with Zachary’s karate club network: the sparse adjacency matrix stores 2|E| entries, and its row sums are the degrees.

use igraph::prelude::*;

let karate = Graph::famous("Zachary").unwrap();
let a = karate.get_adjacency_sparse(GetAdjacency::Both, None, Loops::Twice).unwrap();
assert_eq!(a.nnz(), 2 * karate.ecount());
let degrees = karate.degree(.., NeighborMode::All, Loops::Twice).unwrap();
let row_sums: Vec<i64> = a.row_sums().iter().map(|&s| s as i64).collect();
assert_eq!(row_sums, degrees);

Structs§

CooMatrix
A sparse real matrix in coordinate (triplet) format.