Skip to main content

is_bipartite

Function is_bipartite 

Source
pub fn is_bipartite(graph: &Graph) -> IgraphResult<BipartiteResult>
Expand description

Check whether a graph is bipartite and optionally return the partition.

A graph is bipartite if its vertices can be divided into two disjoint sets such that every edge connects a vertex in one set to a vertex in the other. Equivalently, a graph is bipartite iff it contains no odd-length cycles.

Edge directions are ignored — the test treats the graph as undirected.

§Examples

use rust_igraph::{Graph, is_bipartite};

// Path 0-1-2-3 is bipartite
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
let result = is_bipartite(&g).unwrap();
assert!(result.is_bipartite);
assert_eq!(result.types.len(), 4);

// Triangle is not bipartite
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
let result = is_bipartite(&g).unwrap();
assert!(!result.is_bipartite);