-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartite.cpp
More file actions
37 lines (34 loc) · 1001 Bytes
/
Bipartite.cpp
File metadata and controls
37 lines (34 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "Bipartite.h"
Bipartite::Bipartite(const Graph& graph) :
_is_bipartite{true},
_color(graph.num_vertices(), false),
_marked(graph.num_vertices(), false),
_edge_to(graph.num_vertices()),
_cycle{}
{
for (auto v = 0; v < graph.num_vertices(); ++v) {
if (!_marked[v]) { _dfs(graph, v); }
}
}
bool Bipartite::color(int v) const
{
if (!_is_bipartite) { throw std::runtime_error{"not bipartite"}; }
return _color[v];
}
void Bipartite::_dfs(const Graph& graph, int v)
{
_marked[v] = true;
for (int w : graph.adjacent(v)) {
if (!_cycle.empty()) { return; }
if (!_marked[w]) {
_edge_to[w] = v;
_color[w] = !_color[v];
_dfs(graph, w);
} else if (_color[w] == _color[v]) {
_is_bipartite = false;
_cycle.push_back(w);
for (auto x = v; x != w; x = _edge_to[x]) { _cycle.push_back(x); }
_cycle.push_back(w);
}
}
}