...one of the most highly
regarded and expertly designed C++ library projects in the
world. — Herb Sutter and Andrei
// Version with a colormap to retrieve the bipartition 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) template <typename Graph, typename IndexMap, typename OutputIterator> OutputIterator find_odd_cycle (const Graph& graph, const IndexMap index_map, OutputIterator result) // Version which uses the internal index map template <typename Graph, typename OutputIterator> OutputIterator find_odd_cycle (const Graph& graph, OutputIterator result)
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.
IN: const Graph& graph
An undirected graph. The graph type must be a model of Vertex List Graph and Incidence Graph.
IN: const IndexMap index_map
This maps each vertex to an integer in the range [0, num_vertices(graph)). The type VertexIndexMap must be a model of Readable Property Map. The value type of the map must be an integer type. The vertex descriptor type of the graph needs to be usable as the key type of the map.
OUT: PartitionMap partition_map
The algorithm tests whether the graph is bipartite and assigns each vertex either a white or a black color, according to the partition. The PartitionMap type must be a model of Readable Property Map and Writable Property Map. The value type must model ColorValue.
OUT: OutputIterator result
The find_odd_cycle function finds an odd-length cycle if the graph is not bipartite. The sequence of vertices producing such a cycle is written into this iterator. The OutputIterator type must be a model of OutputIterator. The graph's vertex descriptor type must be in the set of value types of the iterator. The final value is returned by the function. If the graph is bipartite (i.e. no odd-length cycle exists), nothing is written, thus the given iterator matches the return value.
The time complexity for the algorithm is O(V + E).
The file example/bipartite_example.cpp
contains an example of testing an undirected graph for bipartiteness.
Copyright © 2010 Matthias Walter (firstname.lastname@example.org)