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:
| Method | C function | Result |
|---|---|---|
Graph::get_adjacency | igraph_get_adjacency | dense adjacency Matrix (optionally weighted) |
Graph::get_adjacency_sparse | igraph_get_adjacency_sparse | sparse adjacency matrix as a CooMatrix |
Graph::get_stochastic | igraph_get_stochastic | row- or column-stochastic transition Matrix |
Graph::get_stochastic_sparse | igraph_get_stochastic_sparse | the same, as a CooMatrix |
Graph::get_edgelist | igraph_get_edgelist | flat edge list, row- or column-wise |
Graph::to_directed / Graph::into_directed | igraph_to_directed | undirected → directed, in place / by value |
Graph::to_undirected / Graph::into_undirected | igraph_to_undirected | directed → undirected, in place / by value |
Graph::to_undirected_with_comb | igraph_to_undirected | the same, combining the edge attributes of merged edges |
Graph::to_prufer | igraph_to_prufer | Prü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
- The inverse conversions are constructors in
crate::constructors:Graph::adjacency,Graph::weighted_adjacency,Graph::sparse_adjacencyandGraph::sparse_weighted_adjacencybuild a graph from an adjacency matrix,Graph::from_flat_edgesfrom the output ofGraph::get_edgelist, andGraph::from_pruferfrom a Prüfer sequence. - Other matrices of a graph: the Laplacian (
Graph::get_laplacian,Graph::get_laplacian_sparse) incrate::structural, and the spectra of adjacency matrices (Graph::eigen_adjacency) incrate::linalg. - Random walks:
Graph::random_walksimulates the walk whose transition matrix isGraph::get_stochastic, andGraph::pagerankcomputes its (damped) stationary distribution. - Directedness:
Graph::is_mutual,Graph::has_mutualandGraph::reciprocitytell how muchToUndirected::MutualandToUndirected::Collapsewill differ;Graph::is_dagchecks the result ofToDirected::Acyclic.
§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.