Skip to main content

Module operators

Module operators 

Source
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

RustC functionWhat it does
Graph::disjoint_unionigraph_disjoint_unionside-by-side copy of two graphs
Graph::disjoint_union_manyigraph_disjoint_union_manyside-by-side copy of many graphs
Graph::union, Graph::union_mapigraph_unionedges in either graph (+ edge maps)
Graph::union_many, Graph::union_many_mapigraph_union_manyedges in any graph (+ edge maps)
Graph::intersection, Graph::intersection_mapigraph_intersectionedges in both graphs (+ edge maps)
Graph::intersection_many, Graph::intersection_many_mapigraph_intersection_manyedges in all graphs (+ edge maps)
Graph::differenceigraph_differenceedges of the first graph missing from the second
Graph::joinigraph_joindisjoint union plus all edges between the two parts
Graph::complementerigraph_complementercomplement graph
Graph::compose, Graph::compose_mapigraph_composecomposition of relations
Graph::contract_verticesigraph_contract_verticesmerge groups of vertices (in place)
Graph::permute_verticesigraph_permute_verticesrelabel vertices
Graph::connect_neighborhoodigraph_connect_neighborhoodconnect vertices within k steps (in place)
Graph::graph_powerigraph_graph_powerthe k-th power of a graph
Graph::rewireigraph_rewiredegree-preserving random edge switches (in place)
Graph::simplifyigraph_simplifyremove multi-edges and/or loops (in place)
Graph::induced_subgraphigraph_induced_subgraphsubgraph induced by a vertex set
Graph::induced_subgraph_mapigraph_induced_subgraph_mapsame, with the vertex id maps
Graph::induced_subgraph_edgesigraph_induced_subgraph_edgesids of the edges inside a vertex set
Graph::subgraph_from_edgesigraph_subgraph_from_edgessubgraph spanned by an edge set
Graph::reverse_edgesigraph_reverse_edgesflip the direction of some edges (in place)
Graph::productigraph_productCartesian, lexicographic, strong, tensor, modular products
Graph::rooted_productigraph_rooted_productrooted product
Graph::mycielskianigraph_mycielskianiterated Mycielski construction

igraph_add_edge, also declared in this header, is wrapped by the core method Graph::add_edge.

§See also

Structs§

EdgeMapped
A graph produced by a binary operator, together with the edge maps that relate its edges to the edges of the two operands.
EdgeMappedMany
A graph produced by an operator on many graphs, together with one edge map per operand (see Graph::union_many_map and Graph::intersection_many_map).
InducedSubgraph
An induced subgraph together with the correspondence between its vertices and the vertices of the original graph (see Graph::induced_subgraph_map).
RewiringStats
Statistics of a Graph::rewire run (igraph_rewiring_stats_t).