find_odd_cycle
Tests a graph for bipartiteness and, if not bipartite, finds an odd-length cycle.
Complexity: O(V + E)
Defined in: <boost/graph/bipartite.hpp>
Example
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/bipartite.hpp>
int main() {
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;
Graph g(5);
// Triangle 0-1-2 makes this non-bipartite
boost::add_edge(0, 1, g); boost::add_edge(1, 2, g);
boost::add_edge(2, 0, g); boost::add_edge(2, 3, g);
boost::add_edge(3, 4, g);
using Partition = boost::one_bit_color_map<
boost::property_map<Graph, boost::vertex_index_t>::type>;
Partition partition(num_vertices(g), get(boost::vertex_index, g));
std::vector<int> cycle;
boost::find_odd_cycle(g, get(boost::vertex_index, g), partition,
std::back_inserter(cycle));
if (cycle.empty()) {
std::cout << "Graph is bipartite\n";
} else {
std::cout << "Odd cycle found:";
for (auto v : cycle) std::cout << " " << v;
std::cout << "\n";
}
}
Odd cycle found: 2 1 0
(1) With partition map and index map
template <typename Graph, typename IndexMap,
typename PartitionMap, typename OutputIterator>
OutputIterator find_odd_cycle(
const Graph& graph,
const IndexMap index_map,
PartitionMap partition_map,
OutputIterator result);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Vertex List Graph and Incidence Graph. |
IN |
|
This maps each vertex to an integer in the range
|
OUT |
|
The algorithm tests whether the graph is bipartite and assigns each
vertex either a white or a black color, according to the partition. The
|
OUT |
|
The |
(2) With index map (no partition map)
template <typename Graph, typename IndexMap,
typename OutputIterator>
OutputIterator find_odd_cycle(
const Graph& graph,
const IndexMap index_map,
OutputIterator result);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Vertex List Graph and Incidence Graph. |
IN |
|
This maps each vertex to an integer in the range
|
OUT |
|
The |
(3) Using the internal index map
template <typename Graph, typename OutputIterator>
OutputIterator find_odd_cycle(
const Graph& graph,
OutputIterator result);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Vertex List Graph and Incidence Graph. |
OUT |
|
The |
Description
The find_odd_cycle function tests a given graph for bipartiteness
using a DFS-based coloring approach.
An undirected graph is bipartite if one can partition its set of
vertices into two sets "left" and "right", such that each edge goes from
either side to the other. Obviously, a two-coloring of the graph is
exactly the same as a two-partition. is_bipartite() tests whether
such a two-coloring is possible and can return it in a given property
map.
Another equivalent characterization is the non-existance of odd-length
cycles, meaning that a graph is bipartite if and only if it does not
contain a cycle with an odd number of vertices as a subgraph.
find_odd_cycle() does nearly the same as
is_bipartite(), but additionally
constructs an odd-length cycle if the graph is found to be not
bipartite.
The bipartition is recorded in the color map partition_map, which
will contain a two-coloring of the graph, i.e. an assignment of black
and white to the vertices such that no edge is monochromatic. The
odd-length cycle is written into the Output Iterator result if one
exists. The final final iterator is returned by the function.