isBipartite<V> function

bool isBipartite<V>({
  1. required Iterable<V> vertices,
  2. Iterable<V> neighboursOf(
    1. V vertex
    )?,
  3. Iterable<V> successorsOf(
    1. V vertex
    )?,
  4. StorageStrategy<V>? vertexStrategy,
})

Checks if a graph is bipartite.

A graph is bipartite if the vertices can be divided into two disjoint and independent sets, U and V, such that every edge connects a vertex in U to one in V. This is equivalent to checking if the graph is 2-colorable or has no odd-length cycles.

The algorithm traverses the graph using a breadth-first search approach. It handles disconnected graphs by iterating through all vertices.

See https://en.wikipedia.org/wiki/Bipartite_graph.

Implementation

bool isBipartite<V>({
  required Iterable<V> vertices,
  Iterable<V> Function(V vertex)? neighboursOf,
  Iterable<V> Function(V vertex)? successorsOf,
  StorageStrategy<V>? vertexStrategy,
}) =>
    bipartiteColoring<V>(
      vertices: vertices,
      neighboursOf: neighboursOf,
      successorsOf: successorsOf,
      vertexStrategy: vertexStrategy,
    ) !=
    null;