Expand description
Graph operators: unions, intersections, complements, subgraphs,
simplification, rewiring and graph products (igraph_operators.h).
This module turns the operators of igraph’s
Graph Operators
chapter into methods of Graph. Operators that build a new graph
borrow their operands (&self) and return a fresh Graph; operators
that igraph performs in place take &mut self
(simplify, contract_vertices,
connect_neighborhood,
reverse_edges, rewire). Clone the
graph first if you need to keep the original.
Operators working on many graphs at once (e.g.
Graph::union_many) are associated functions accepting any iterator of
graph references, such as [&g1, &g2, &g3] or graphs.iter().
As in igraph, vertex ids are never “matched by name”: two graphs are combined by identifying vertices with the same id. Unless stated otherwise, the operands must have the same directedness, and graph, vertex and edge attributes are not handled by these wrappers.
§Example
use igraph::prelude::*;
// A 4-cycle and a "diagonal" graph on the same vertex set.
let square = Graph::ring(4, false, false, true).unwrap();
let diagonals = Graph::from_edges(&[(0, 2), (1, 3)], 4, false).unwrap();
// Their union is K4, the complement of the square is the diagonals.
let k4 = square.union(&diagonals).unwrap();
assert_eq!(k4, Graph::full(4, false, false).unwrap());
assert_eq!(square.complementer(false).unwrap(), diagonals);
assert_eq!(k4.difference(&square).unwrap(), diagonals);
// Two disjoint copies of the square, then the subgraph induced by one of them.
let two = square.disjoint_union(&square).unwrap();
assert_eq!((two.vcount(), two.ecount()), (8, 8));
let back = two.induced_subgraph(4..8, SubgraphImplementation::Auto).unwrap();
assert_eq!(back, square);
// The Cartesian product of two edges is a square (up to relabeling).
let edge = Graph::full(2, false, false).unwrap();
let prod = edge.product(&edge, Product::Cartesian).unwrap();
assert!(prod.isomorphic(&square).unwrap());§Provided functionality
| Rust | C function | What it does |
|---|---|---|
Graph::disjoint_union | igraph_disjoint_union | side-by-side copy of two graphs |
Graph::disjoint_union_many | igraph_disjoint_union_many | side-by-side copy of many graphs |
Graph::union, Graph::union_map | igraph_union | edges in either graph (+ edge maps) |
Graph::union_many, Graph::union_many_map | igraph_union_many | edges in any graph (+ edge maps) |
Graph::intersection, Graph::intersection_map | igraph_intersection | edges in both graphs (+ edge maps) |
Graph::intersection_many, Graph::intersection_many_map | igraph_intersection_many | edges in all graphs (+ edge maps) |
Graph::difference | igraph_difference | edges of the first graph missing from the second |
Graph::join | igraph_join | disjoint union plus all edges between the two parts |
Graph::complementer | igraph_complementer | complement graph |
Graph::compose, Graph::compose_map | igraph_compose | composition of relations |
Graph::contract_vertices | igraph_contract_vertices | merge groups of vertices (in place) |
Graph::permute_vertices | igraph_permute_vertices | relabel vertices |
Graph::connect_neighborhood | igraph_connect_neighborhood | connect vertices within k steps (in place) |
Graph::graph_power | igraph_graph_power | the k-th power of a graph |
Graph::rewire | igraph_rewire | degree-preserving random edge switches (in place) |
Graph::simplify | igraph_simplify | remove multi-edges and/or loops (in place) |
Graph::induced_subgraph | igraph_induced_subgraph | subgraph induced by a vertex set |
Graph::induced_subgraph_map | igraph_induced_subgraph_map | same, with the vertex id maps |
Graph::induced_subgraph_edges | igraph_induced_subgraph_edges | ids of the edges inside a vertex set |
Graph::subgraph_from_edges | igraph_subgraph_from_edges | subgraph spanned by an edge set |
Graph::reverse_edges | igraph_reverse_edges | flip the direction of some edges (in place) |
Graph::product | igraph_product | Cartesian, lexicographic, strong, tensor, modular products |
Graph::rooted_product | igraph_rooted_product | rooted product |
Graph::mycielskian | igraph_mycielskian | iterated Mycielski construction |
igraph_add_edge, also declared in this header, is wrapped by the core
method Graph::add_edge.
§See also
- Ready-made graphs to feed these operators:
Graph::famous,Graph::full,Graph::ring,Graph::square_lattice,Graph::hypercube,Graph::wheel,Graph::mycielski_graph(constructors) andGraph::full_bipartite(bipartite). - Comparing results:
==compares labelled graphs (Graph::is_same_graph);Graph::isomorphicandGraph::canonical_permutation(isomorphism) compare structure up to relabeling. - Other ways to cut a graph apart:
Graph::delete_vertices(core),Graph::decomposeandGraph::neighborhood_graphs(components). - Other randomizations:
Graph::rewire_edgesandGraph::degree_sequence_game(games). - Changing directedness rather than edges:
Graph::to_directedandGraph::to_undirected(conversion).
Structs§
- Edge
Mapped - A graph produced by a binary operator, together with the edge maps that relate its edges to the edges of the two operands.
- Edge
Mapped Many - A graph produced by an operator on many graphs, together with one edge
map per operand (see
Graph::union_many_mapandGraph::intersection_many_map). - Induced
Subgraph - An induced subgraph together with the correspondence between its
vertices and the vertices of the original graph (see
Graph::induced_subgraph_map). - Rewiring
Stats - Statistics of a
Graph::rewirerun (igraph_rewiring_stats_t).