All you need to do is check if your graph is bipartite (i.e. if the vertices of your graph can be assigned one of two colors so that no edge connects the vertices of the same color). If you enroll students in a class with integers:
1. Jack 2. Jim 3. Rose
you can easily solve this problem using the Boost Graph library in C ++:
#include <iostream> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/bipartite.hpp> using namespace boost; int main (int argc, char **argv) { typedef adjacency_list <vecS, vecS, undirectedS> vector_graph_t; typedef std::pair <int, int> E; E edges[] = { E (1, 2), E (2, 3), E (3, 1)}; vector_graph_t students (&edges[0],&edges[0] + sizeof(edges) / sizeof(E), 3); if ( is_bipartite(students) ) std::cout << "Bipartite graph" << std::endl; else std::cout << "Non bipartite graph" << std::endl; return 0; }
source share