2021-10-28 09:48:58 +08:00
|
|
|
/**
|
|
|
|
* @brief Check whether a given graph is bipartite or not
|
|
|
|
* @details
|
2021-11-01 21:56:40 +08:00
|
|
|
* A bipartite graph is the one whose nodes can be divided into two
|
|
|
|
* disjoint sets in such a way that the nodes in a set are not
|
|
|
|
* connected to each other at all, i.e. no intra-set connections.
|
|
|
|
* The only connections that exist are that of inter-set,
|
|
|
|
* i.e. the nodes from one set are connected to a subset of nodes
|
2021-10-28 09:48:58 +08:00
|
|
|
* in the other set.
|
2021-11-01 21:56:40 +08:00
|
|
|
* In this implementation, using a graph in the form of adjacency
|
2021-10-28 09:48:58 +08:00
|
|
|
* list, check whether the given graph is a bipartite or not.
|
2021-11-01 21:56:40 +08:00
|
|
|
*
|
|
|
|
* References used:
|
|
|
|
* [GeeksForGeeks](https://www.geeksforgeeks.org/bipartite-graph/)
|
2021-10-28 09:48:58 +08:00
|
|
|
* @author [tushar2407](https://github.com/tushar2407)
|
|
|
|
*/
|
2021-11-01 21:56:40 +08:00
|
|
|
#include <cassert> /// for assert
|
|
|
|
#include <iostream> /// for IO operations
|
|
|
|
#include <queue> /// for queue data structure
|
|
|
|
#include <vector> /// for vector data structure
|
2021-10-28 09:48:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @namespace graph
|
|
|
|
* @brief Graphical algorithms
|
|
|
|
*/
|
|
|
|
namespace graph {
|
|
|
|
/**
|
|
|
|
* @brief function to check whether the passed graph is bipartite or not
|
|
|
|
* @param graph is a 2D matrix whose rows or the first index signify the node
|
|
|
|
* and values in that row signify the nodes it is connected to
|
|
|
|
* @param index is the valus of the node currently under observation
|
2021-11-01 21:56:40 +08:00
|
|
|
* @param visited is the vector which stores whether a given node has been
|
2021-10-28 09:48:58 +08:00
|
|
|
* traversed or not yet
|
|
|
|
* @returns boolean
|
|
|
|
*/
|
2021-11-01 21:56:40 +08:00
|
|
|
bool checkBipartite(const std::vector<std::vector<int64_t>> &graph,
|
|
|
|
int64_t index, std::vector<int64_t> *visited) {
|
|
|
|
std::queue<int64_t> q; ///< stores the neighbouring node indexes in squence
|
|
|
|
/// of being reached
|
|
|
|
q.push(index); /// insert the current node into the queue
|
|
|
|
(*visited)[index] = 1; /// mark the current node as travelled
|
|
|
|
while (q.size()) {
|
2021-10-28 09:48:58 +08:00
|
|
|
int64_t u = q.front();
|
|
|
|
q.pop();
|
2021-11-01 21:56:40 +08:00
|
|
|
for (uint64_t i = 0; i < graph[u].size(); i++) {
|
|
|
|
int64_t v =
|
|
|
|
graph[u][i]; ///< stores the neighbour of the current node
|
|
|
|
if (!(*visited)[v]) /// check whether the neighbour node is
|
|
|
|
/// travelled already or not
|
2021-10-28 09:48:58 +08:00
|
|
|
{
|
2021-11-01 21:56:40 +08:00
|
|
|
(*visited)[v] =
|
|
|
|
((*visited)[u] == 1)
|
|
|
|
? -1
|
|
|
|
: 1; /// colour the neighbouring node with
|
|
|
|
/// different colour than the current node
|
|
|
|
q.push(v); /// insert the neighbouring node into the queue
|
|
|
|
} else if ((*visited)[v] ==
|
|
|
|
(*visited)[u]) /// if both the current node and its
|
|
|
|
/// neighbour has the same state then it
|
|
|
|
/// is not a bipartite graph
|
2021-10-28 09:48:58 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:56:40 +08:00
|
|
|
return true; /// return true when all the connected nodes of the current
|
|
|
|
/// nodes are travelled and satisify all the above conditions
|
2021-10-28 09:48:58 +08:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @brief returns true if the given graph is bipartite else returns false
|
2021-11-01 21:56:40 +08:00
|
|
|
* @param graph is a 2D matrix whose rows or the first index signify the node
|
2021-10-28 09:48:58 +08:00
|
|
|
* and values in that row signify the nodes it is connected to
|
|
|
|
* @returns booleans
|
|
|
|
*/
|
2021-11-01 21:56:40 +08:00
|
|
|
bool isBipartite(const std::vector<std::vector<int64_t>> &graph) {
|
|
|
|
std::vector<int64_t> visited(
|
|
|
|
graph.size()); ///< stores boolean values
|
|
|
|
/// which signify whether that node had been visited or
|
|
|
|
/// not
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < graph.size(); i++) {
|
|
|
|
if (!visited[i]) /// if the current node is not visited then check
|
|
|
|
/// whether the sub-graph of that node is a bipartite
|
|
|
|
/// or not
|
2021-10-28 09:48:58 +08:00
|
|
|
{
|
2021-11-01 21:56:40 +08:00
|
|
|
if (!checkBipartite(graph, i, &visited)) {
|
2021-10-28 09:48:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace graph
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Self-test implementations
|
|
|
|
* @returns void
|
|
|
|
*/
|
2021-11-01 21:56:40 +08:00
|
|
|
static void test() {
|
|
|
|
std::vector<std::vector<int64_t>> graph = {{1, 3}, {0, 2}, {1, 3}, {0, 2}};
|
2021-10-28 09:48:58 +08:00
|
|
|
|
2021-11-01 21:56:40 +08:00
|
|
|
assert(graph::isBipartite(graph) ==
|
|
|
|
true); /// check whether the above
|
|
|
|
/// defined graph is indeed bipartite
|
2021-10-28 09:48:58 +08:00
|
|
|
|
|
|
|
std::vector<std::vector<int64_t>> graph_not_bipartite = {
|
2021-11-01 21:56:40 +08:00
|
|
|
{1, 2, 3}, {0, 2}, {0, 1, 3}, {0, 2}};
|
2021-10-28 09:48:58 +08:00
|
|
|
|
2021-11-01 21:56:40 +08:00
|
|
|
assert(graph::isBipartite(graph_not_bipartite) ==
|
|
|
|
false); /// check whether
|
|
|
|
/// the above defined graph is indeed bipartite
|
2021-10-28 09:48:58 +08:00
|
|
|
std::cout << "All tests have successfully passed!\n";
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @brief Main function
|
2021-11-01 21:56:40 +08:00
|
|
|
* Instantitates a dummy graph of a small size with
|
2021-10-28 09:48:58 +08:00
|
|
|
* a few edges between random nodes.
|
2021-11-01 21:56:40 +08:00
|
|
|
* On applying the algorithm, it checks if the instantiated
|
2021-10-28 09:48:58 +08:00
|
|
|
* graph is bipartite or not.
|
|
|
|
* @returns 0 on exit
|
|
|
|
*/
|
2021-11-01 21:56:40 +08:00
|
|
|
int main() {
|
2021-10-28 09:48:58 +08:00
|
|
|
test(); // run self-test implementations
|
|
|
|
return 0;
|
|
|
|
}
|