mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix: linter and spacing for is_graph_bipartite. (#1037)
* fix: linter and spacing for is_graph_bipartite. * updating DIRECTORY.md * clang-tidy fixes fora49ec9b8d7
* clang-format and clang-tidy fixes for40a56d2f
* Address reviewer's comments. * Fix docs wording. Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
f0c218c789
commit
3239fcc19e
@ -1,15 +1,17 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
*
|
*
|
||||||
* @brief Algorithm to check whether a graph is [bipartite](https://en.wikipedia.org/wiki/Bipartite_graph)
|
* @brief Algorithm to check whether a graph is
|
||||||
|
* [bipartite](https://en.wikipedia.org/wiki/Bipartite_graph)
|
||||||
*
|
*
|
||||||
* @details
|
* @details
|
||||||
* A graph is a collection of nodes also called vertices and these vertices
|
* A graph is a collection of nodes also called vertices and these vertices
|
||||||
* are connected by edges.A bipartite graph is a graph whose vertices can be
|
* are connected by edges. A graph is bipartite if its vertices can be
|
||||||
* divided into two disjoint and independent sets U and V such that every edge
|
* divided into two disjoint and independent sets U and V such that every edge
|
||||||
* connects a vertex in U to one in V.
|
* connects a vertex in U to one in V.
|
||||||
*
|
*
|
||||||
* The given Algorithm will determine whether the given graph is bipartite or not
|
* The algorithm implemented in this file determines whether the given graph
|
||||||
|
* is bipartite or not.
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* Example - Here is a graph g1 with 5 vertices and is bipartite
|
* Example - Here is a graph g1 with 5 vertices and is bipartite
|
||||||
@ -30,88 +32,92 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @namespace graph
|
* @namespace graph
|
||||||
* @brief Graph algorithms
|
* @brief Graph algorithms
|
||||||
*/
|
*/
|
||||||
namespace graph{
|
namespace graph {
|
||||||
/**
|
/**
|
||||||
* @namespace is_graph_bipartite
|
* @namespace is_graph_bipartite
|
||||||
* @brief Functions for checking whether a graph is bipartite or not
|
* @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.
|
* @brief Class for representing graph as an adjacency list.
|
||||||
*/
|
*/
|
||||||
class Graph {
|
class Graph {
|
||||||
private:
|
private:
|
||||||
int n; /// size of the graph
|
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;
|
|
||||||
|
|
||||||
|
std::vector<int> side; ///< stores the side of the vertex
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Constructor that initializes the graph on creation
|
* @brief Constructor that initializes the graph on creation
|
||||||
|
* @param size number of vertices of the graph
|
||||||
*/
|
*/
|
||||||
explicit Graph(int size = nax){
|
explicit Graph(int size) {
|
||||||
n = size;
|
n = size;
|
||||||
adj.resize(n);
|
adj.resize(n);
|
||||||
side.resize(n,-1);
|
side.resize(n, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void addEdge(int u, int v); /// function to add edges to our graph
|
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
|
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
|
* @brief Function that add an edge between two nodes or vertices of graph
|
||||||
*
|
*
|
||||||
* @param u is a node or vertex of graph
|
* @param u is a node or vertex of graph
|
||||||
* @param v is a node or vertex of graph
|
* @param v is a node or vertex of graph
|
||||||
*/
|
*/
|
||||||
void Graph::addEdge(int u, int v) {
|
void Graph::addEdge(int u, int v) {
|
||||||
adj[u-1].push_back(v-1);
|
adj[u - 1].push_back(v - 1);
|
||||||
adj[v-1].push_back(u-1);
|
adj[v - 1].push_back(u - 1);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
|
/**
|
||||||
* @brief function that checks whether the graph is bipartite or not
|
* @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 true if the graph is a bipartite graph
|
||||||
* the function returns false if the graph is not a bipartite graph
|
* the function returns false if the graph is not a bipartite graph
|
||||||
*
|
*
|
||||||
* @details
|
* @details
|
||||||
* Here, side refers to the two disjoint subsets of the bipartite graph.
|
* 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.
|
* Initially, the values of side are set to -1 which is an unassigned state. A
|
||||||
* If the current edge has no side assigned to it, then a Breadth First Search operation is performed.
|
* for loop is run for every vertex of the graph. If the current edge has no
|
||||||
* If two neighbours have the same side then the graph will not be bipartite and the value of check becomes false.
|
* side assigned to it, then a Breadth First Search operation is performed. If
|
||||||
* If and only if each pair of neighbours have different sides, the value of check will be true and hence the graph bipartite.
|
* 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.
|
||||||
*
|
*
|
||||||
|
* @returns `true` if th graph is bipartite
|
||||||
|
* @returns `false` otherwise
|
||||||
*/
|
*/
|
||||||
bool Graph::is_bipartite(){
|
bool Graph::is_bipartite() {
|
||||||
bool check = true;
|
bool check = true;
|
||||||
std::queue<int> q;
|
std::queue<int> q;
|
||||||
for (int current_edge = 0; current_edge < n; ++current_edge)
|
for (int current_edge = 0; current_edge < n; ++current_edge) {
|
||||||
{
|
if (side[current_edge] == -1) {
|
||||||
if(side[current_edge] == -1){
|
|
||||||
q.push(current_edge);
|
q.push(current_edge);
|
||||||
side[current_edge] = 0;
|
side[current_edge] = 0;
|
||||||
while(q.size()){
|
while (q.size()) {
|
||||||
int current = q.front();
|
int current = q.front();
|
||||||
q.pop();
|
q.pop();
|
||||||
for(auto neighbour : adj[current]){
|
for (auto neighbour : adj[current]) {
|
||||||
if(side[neighbour] == -1){
|
if (side[neighbour] == -1) {
|
||||||
side[neighbour] = (1 ^ side[current]);
|
side[neighbour] = (1 ^ side[current]);
|
||||||
q.push(neighbour);
|
q.push(neighbour);
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
check &= (side[neighbour] != side[current]);
|
check &= (side[neighbour] != side[current]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,45 +125,47 @@ namespace graph{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return check;
|
return check;
|
||||||
}
|
}
|
||||||
} /// namespace is_graph_bipartite
|
} // namespace is_graph_bipartite
|
||||||
} /// namespace graph
|
} // namespace graph
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to test the above algorithm
|
* Function to test the above algorithm
|
||||||
* @returns none
|
* @returns none
|
||||||
*/
|
*/
|
||||||
static void test(){
|
static void test() {
|
||||||
graph::is_graph_bipartite::Graph G1(5); /// creating graph G1 with 5 vertices
|
graph::is_graph_bipartite::Graph G1(
|
||||||
|
5); /// creating graph G1 with 5 vertices
|
||||||
/// adding edges to the graphs as per the illustrated example
|
/// adding edges to the graphs as per the illustrated example
|
||||||
G1.addEdge(1,2);
|
G1.addEdge(1, 2);
|
||||||
G1.addEdge(1,3);
|
G1.addEdge(1, 3);
|
||||||
G1.addEdge(3,4);
|
G1.addEdge(3, 4);
|
||||||
G1.addEdge(4,5);
|
G1.addEdge(4, 5);
|
||||||
|
|
||||||
graph::is_graph_bipartite::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
|
/// adding edges to the graphs as per the illustrated example
|
||||||
G2.addEdge(1,2);
|
G2.addEdge(1, 2);
|
||||||
G2.addEdge(1,3);
|
G2.addEdge(1, 3);
|
||||||
G2.addEdge(2,3);
|
G2.addEdge(2, 3);
|
||||||
|
|
||||||
/// checking whether the graphs are bipartite or not
|
/// 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";
|
std::cout << "The given graph G1 is a bipartite graph\n";
|
||||||
|
} else {
|
||||||
|
std::cout << "The given graph G1 is not a bipartite graph\n";
|
||||||
}
|
}
|
||||||
else{
|
if (G2.is_bipartite()) {
|
||||||
std::cout<<"The given graph G1 is not a bipartite graph\n";
|
std::cout << "The given graph G2 is a bipartite graph\n";
|
||||||
}
|
} else {
|
||||||
if(G2.is_bipartite()){
|
std::cout << "The given graph G2 is not a bipartite graph\n";
|
||||||
std::cout<<"The given graph G2 is a bipartite graph\n";
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
std::cout<<"The given graph G2 is not a bipartite graph\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function
|
* Main function
|
||||||
*/
|
*/
|
||||||
int main(){
|
int main() {
|
||||||
test(); ///Testing
|
test(); /// Testing
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user