Merge branch 'is_graph_bipartite' into fix_clang

This commit is contained in:
Krishna Vedala 2020-08-26 10:04:37 -04:00
commit d4d4060175
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -37,12 +37,12 @@
* @namespace graph
* @brief Graph algorithms
*/
namespace graph{
namespace graph {
/**
* @namespace is_graph_bipartite
* @brief Functions for checking whether a graph is bipartite or not
*/
namespace is_graph_bipartite{
namespace is_graph_bipartite {
/**
* @brief Class for representing graph as an adjacency list.
*/
@ -50,27 +50,25 @@ namespace graph{
private:
int n; /// size of the graph
std::vector<std::vector <int> > adj; /// adj stores the graph as an adjacency list
std::vector< std::vector<int> > adj; /// adj stores the graph as an adjacency list
std::vector<int> side; ///stores the side of the vertex
static const int nax = 5e5 + 1;
public:
/**
* @brief Constructor that initializes the graph on creation
*/
explicit Graph(int size = nax){
explicit Graph(int size=nax){
n = size;
adj.resize(n);
side.resize(n,-1);
side.resize(n, -1);
}
void addEdge(int u, int v); /// function to add edges to our graph
bool is_bipartite(); /// function to check whether the graph is bipartite or not
};
/**
* @brief Function that add an edge between two nodes or vertices of graph
@ -79,8 +77,8 @@ namespace graph{
* @param v is a node or vertex of graph
*/
void Graph::addEdge(int u, int v) {
adj[u-1].push_back(v-1);
adj[v-1].push_back(u-1);
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
/**
* @brief function that checks whether the graph is bipartite or not
@ -98,20 +96,19 @@ namespace graph{
bool Graph::is_bipartite(){
bool check = true;
std::queue<int> q;
for (int current_edge = 0; current_edge < n; ++current_edge)
{
if(side[current_edge] == -1){
for (int current_edge = 0; current_edge < n; ++current_edge) {
if (side[current_edge] == -1){
q.push(current_edge);
side[current_edge] = 0;
while(q.size()){
while (q.size()) {
int current = q.front();
q.pop();
for(auto neighbour : adj[current]){
if(side[neighbour] == -1){
for (auto neighbour : adj[current]) {
if (side[neighbour] == -1) {
side[neighbour] = (1 ^ side[current]);
q.push(neighbour);
}
else{
else {
check &= (side[neighbour] != side[current]);
}
}
@ -120,8 +117,8 @@ namespace graph{
}
return check;
}
} /// namespace is_graph_bipartite
} /// namespace graph
} // namespace is_graph_bipartite
} // namespace graph
/**
* Function to test the above algorithm
* @returns none
@ -129,35 +126,35 @@ namespace graph{
static void test(){
graph::is_graph_bipartite::Graph G1(5); /// creating graph G1 with 5 vertices
/// adding edges to the graphs as per the illustrated example
G1.addEdge(1,2);
G1.addEdge(1,3);
G1.addEdge(3,4);
G1.addEdge(4,5);
G1.addEdge(1, 2);
G1.addEdge(1, 3);
G1.addEdge(3, 4);
G1.addEdge(4, 5);
graph::is_graph_bipartite::Graph G2(3); /// creating graph G2 with 3 vertices
/// adding edges to the graphs as per the illustrated example
G2.addEdge(1,2);
G2.addEdge(1,3);
G2.addEdge(2,3);
G2.addEdge(1, 2);
G2.addEdge(1, 3);
G2.addEdge(2, 3);
/// checking whether the graphs are bipartite or not
if(G1.is_bipartite()){
if (G1.is_bipartite()) {
std::cout<<"The given graph G1 is a bipartite graph\n";
}
else{
else {
std::cout<<"The given graph G1 is not a bipartite graph\n";
}
if(G2.is_bipartite()){
if (G2.is_bipartite()) {
std::cout<<"The given graph G2 is a bipartite graph\n";
}
else{
else {
std::cout<<"The given graph G2 is not a bipartite graph\n";
}
}
/**
* Main function
*/
int main(){
int main() {
test(); ///Testing
return 0;
}