From 3e355d0f7ad5bc8afdcfa1f8c53afc967ac16fd5 Mon Sep 17 00:00:00 2001 From: AkVaya Date: Sat, 15 Aug 2020 00:53:31 +0530 Subject: [PATCH] Performed the requested changes --- graph/is_graph_bipartite.cpp | 156 +++++++++++++++++++---------------- 1 file changed, 86 insertions(+), 70 deletions(-) diff --git a/graph/is_graph_bipartite.cpp b/graph/is_graph_bipartite.cpp index 86b82b781..426d25560 100644 --- a/graph/is_graph_bipartite.cpp +++ b/graph/is_graph_bipartite.cpp @@ -31,93 +31,109 @@ #include #include -const int nax = 5e5 + 1; /** * Class for representing graph as an adjacency list. */ -class graph { - private: - int n; /// size of the graph - - std::vector > adj; /// adj stores the graph as an adjacency list - - std::vector side; ///stores the side of the vertex - - public: - /** - * @brief Constructor that initializes the graph on creation - */ - graph(int size = nax){ - n = size; - adj.resize(n); - 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 - * - * @param u is a node or vertex of graph - * @param v is a node or vertex of graph + * @namespace graph + * @brief Graph algorithms */ -void graph::addEdge(int u, int v) { - 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 - * the function returns true if the graph is a bipartite graph - * the function returns false if the graph is not a bipartite graph - * - * @details - * Here, side refers to the two disjoint subsets of the bipartite graph. - * Initially, the values of side are set to -1 which is an unassigned state. A for loop is run for every vertex of the graph. - * If the current edge has no side assigned to it, then a Breadth First Search operation is performed. - * If two neighbours have the same side then the graph will not be bipartite and the value of check becomes false. - * If and only if each pair of neighbours have different sides, the value of check will be true and hence the graph bipartite. - * - */ -bool graph::is_bipartite(){ - bool check = true; - std::queue q; - 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()){ - int current = q.front(); - q.pop(); - for(auto neighbour : adj[current]){ - if(side[neighbour] == -1){ - side[neighbour] = (1 ^ side[current]); - q.push(neighbour); - } - else{ - check &= (side[neighbour] != side[current]); +namespace graph{ + /** + * @namespace is_graph_bipartite + * @brief Functions for checking whether a graph is bipartite or not + */ + namespace is_graph_bipartite{ + + class Graph { + private: + int n; /// size of the graph + + std::vector > adj; /// adj stores the graph as an adjacency list + + std::vector 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){ + n = size; + adj.resize(n); + 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 + * + * @param u is a node or vertex of 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); + } + /** + * @brief function that checks whether the graph is bipartite or not + * the function returns true if the graph is a bipartite graph + * the function returns false if the graph is not a bipartite graph + * + * @details + * Here, side refers to the two disjoint subsets of the bipartite graph. + * Initially, the values of side are set to -1 which is an unassigned state. A for loop is run for every vertex of the graph. + * If the current edge has no side assigned to it, then a Breadth First Search operation is performed. + * If two neighbours have the same side then the graph will not be bipartite and the value of check becomes false. + * If and only if each pair of neighbours have different sides, the value of check will be true and hence the graph bipartite. + * + */ + bool Graph::is_bipartite(){ + bool check = true; + std::queue q; + 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()){ + int current = q.front(); + q.pop(); + for(auto neighbour : adj[current]){ + if(side[neighbour] == -1){ + side[neighbour] = (1 ^ side[current]); + q.push(neighbour); + } + else{ + check &= (side[neighbour] != side[current]); + } + } } } } + return check; } - } - return check; -} + } /// namespace is_graph_bipartite +} /// namespace graph /** - * Function to test the above algorithm + * Function to test the above algorithm + * @returns none */ -void test(){ - graph G1(5); /// creating graph G1 with 5 vertices +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); - graph G2(3); /// creating graph G2 with 3 vertices + 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);