Skip to main content

Module cliques

Module cliques 

Source
Expand description

Cliques, independent vertex sets and vertex/edge colorings (igraph_cliques.h, igraph_coloring.h).

A clique is a set of vertices that are pairwise adjacent; an independent vertex set is a set of vertices no two of which are adjacent (i.e. a clique of the complementer graph). A proper vertex coloring assigns colors to vertices so that adjacent vertices always differ. The clique and independent-set functions ignore edge directions (igraph emits a warning for directed graphs), self-loops and multi-edges. The coloring checks ignore self-loops too, but is_bipartite_coloring reports edge directions and is_edge_coloring treats parallel edges as adjacent.

Three families of sets are distinguished, both for cliques and for independent sets:

  • all sets (every clique, including every subset of a clique);
  • maximal sets, which cannot be extended by adding one more vertex;
  • largest (maximum) sets, those of the largest possible size. The size of the largest clique is the clique number ω(G), the size of the largest independent set is the independence number α(G).

§Size ranges

The enumeration functions take the admissible sizes as a Rust range of usize (RangeBounds): .. means “any size”, 3.. “at least 3”, 2..=4 “between 2 and 4”, ..5 “fewer than 5”. An empty range (e.g. 4..=2 or ..1) is rejected with an ErrorKind::InvalidValue error, since no set could ever satisfy it. A lower bound larger than the number of vertices is not an error: the result is simply empty. An optional max_results stops the search after that many sets were found (None means no limit, Some(0) returns nothing).

§Nesting searches in callbacks

The functions based on the Cliquer library (cliques, cliques_callback, clique_size_hist and the weighted clique functions) share per-thread state inside igraph: in igraph 1.0.0 and 1.0.1 the Cliquer wrapper keeps the options of the running search in a single thread-local global that every search overwrites. Calling one of them from inside a cliques_callback closure is therefore refused with an ErrorKind::Failure error instead of corrupting the running search. All other functions (for instance maximal_cliques or clique_number) may be used freely there, and maximal_cliques_callback closures may call anything. The restriction is per thread: searches running on other threads are independent. A nested call that fails only returns its Err to the closure: the running search is not affected and can go on.

§Example

use igraph::{cliques::ColoringGreedy, prelude::*};

// Two triangles {0, 1, 2} and {2, 3, 4} sharing vertex 2, plus the edge 4-5.
let g = Graph::from_edges(&[(0, 1), (1, 2), (0, 2), (2, 3), (3, 4), (2, 4), (4, 5)], 6, false)
    .unwrap();

assert_eq!(g.clique_number().unwrap(), 3);
let mut largest = g.largest_cliques().unwrap();
largest.iter_mut().for_each(|c| c.sort());
largest.sort();
assert_eq!(largest, vec![vec![0, 1, 2], vec![2, 3, 4]]);

// Maximal cliques by size: none of size 1, one of size 2 (4-5), two triangles.
assert_eq!(g.maximal_cliques_hist(..).unwrap(), vec![0, 1, 2]);

// Vertices 0, 3 and 5 are pairwise non-adjacent.
assert_eq!(g.independence_number().unwrap(), 3);

// A proper coloring needs at least ω(G) = 3 colors.
let colors = g.vertex_coloring_greedy(ColoringGreedy::DSatur).unwrap();
assert!(g.is_vertex_coloring(&colors).unwrap());
assert_eq!(colors.iter().max(), Some(&2));

// Zachary's karate club: its 45 triangles are its cliques of size 3, and
// its two largest cliques have 5 members.
let karate = Graph::famous("Zachary").unwrap();
assert_eq!(karate.clique_size_hist(3..=3).unwrap(), vec![0, 0, 45]);
assert_eq!(karate.count_triangles().unwrap(), 45.0);
assert_eq!(karate.clique_number().unwrap(), 5);

§Provided functionality

Rust methodC functionComputes
cliquesigraph_cliquesall cliques in a size range
cliques_callbackigraph_cliques_callbackstreams all cliques to a closure
clique_size_histigraph_clique_size_histnumber of cliques of each size
largest_cliquesigraph_largest_cliquesthe maximum cliques
clique_numberigraph_clique_numberω(G)
maximal_cliquesigraph_maximal_cliquesmaximal cliques (Bron–Kerbosch)
maximal_cliques_callbackigraph_maximal_cliques_callbackstreams maximal cliques to a closure
maximal_cliques_countigraph_maximal_cliques_countnumber of maximal cliques
maximal_cliques_histigraph_maximal_cliques_histmaximal cliques by size
maximal_cliques_subsetigraph_maximal_cliques_subsetmaximal cliques started from some vertices
write_maximal_cliquesigraph_maximal_cliques_filewrites maximal cliques to an io::Write
weighted_cliquesigraph_weighted_cliques(maximal) cliques in a weight range
largest_weighted_cliquesigraph_largest_weighted_cliquesheaviest cliques
weighted_clique_numberigraph_weighted_clique_numberweight of the heaviest clique
independent_vertex_setsigraph_independent_vertex_setsall independent sets in a size range
maximal_independent_vertex_setsigraph_maximal_independent_vertex_setsmaximal independent sets
largest_independent_vertex_setsigraph_largest_independent_vertex_setsmaximum independent sets
independence_numberigraph_independence_numberα(G)
vertex_coloring_greedyigraph_vertex_coloring_greedya proper vertex coloring
is_vertex_coloringigraph_is_vertex_coloringchecks a vertex coloring
is_bipartite_coloringigraph_is_bipartite_coloringchecks a 2-coloring, and edge orientation
is_edge_coloringigraph_is_edge_coloringchecks an edge coloring

Cliques and independent sets are returned as Vec<Vec<VertexId>>; the order of the sets, and of the vertices inside each set, is unspecified (sort them if you need a canonical form).

§See also

TaskElsewhere in the crate
test whether a given vertex set is a clique / independent setGraph::is_clique, Graph::is_independent_vertex_set (structural)
triangles (the 3-cliques) onlyGraph::count_triangles, Graph::list_triangles (isomorphism), Graph::transitivity_undirected (mixing)
switch between cliques and independent setsGraph::complementer (operators): α(G) = ω(complement of G)
degeneracy, which bounds the cost of maximal_cliquesGraph::coreness: the degeneracy is the largest coreness
2-coloringsGraph::is_bipartite, Graph::bipartite_types (bipartite)
graph classes where χ(G) = ω(G)Graph::is_perfect, Graph::is_chordal (structural)
graphs with known ω, α and χGraph::full, Graph::turan, Graph::wheel, Graph::famous (constructors), Graph::mycielskian (operators: triangle-free graphs of growing chromatic number)

Structs§

WeightedCliqueOptions
Options of Graph::weighted_cliques.

Enums§

ColoringGreedy
Vertex ordering heuristic of Graph::vertex_coloring_greedy (igraph_coloring_greedy_t).