From a9312b3901fd3237dc2d0dfe6230f026f04b9dc2 Mon Sep 17 00:00:00 2001 From: Tushar Mohan Date: Thu, 28 Oct 2021 07:18:58 +0530 Subject: [PATCH] feat: a different implementation of checking bipartite-ness of a graph (#1769) * feat: a different implementation of checking bipartite-ness of a graph * updating DIRECTORY.md * fix: code formatter error * fix: requested changes * fix: request changes * fix : requested changed * pass parameters by reference * pass parameters by reference * fix : visited to pointer * fix : line length below 80 chars Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal Co-authored-by: Ayaan Khan --- DIRECTORY.md | 1 + graph/is_graph_bipartite2.cpp | 134 ++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 graph/is_graph_bipartite2.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index ae84537c9..618142c55 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -124,6 +124,7 @@ * [Hamiltons Cycle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/hamiltons_cycle.cpp) * [Hopcroft Karp](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/hopcroft_karp.cpp) * [Is Graph Bipartite](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/is_graph_bipartite.cpp) + * [Is Graph Bipartite2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/is_graph_bipartite2.cpp) * [Kosaraju](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/kosaraju.cpp) * [Kruskal](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/kruskal.cpp) * [Lowest Common Ancestor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/lowest_common_ancestor.cpp) diff --git a/graph/is_graph_bipartite2.cpp b/graph/is_graph_bipartite2.cpp new file mode 100644 index 000000000..07374da9a --- /dev/null +++ b/graph/is_graph_bipartite2.cpp @@ -0,0 +1,134 @@ +/** + * @brief Check whether a given graph is bipartite or not + * @details + * 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 + * in the other set. + * In this implementation, using a graph in the form of adjacency + * list, check whether the given graph is a bipartite or not. + * + * References used: [GeeksForGeeks](https://www.geeksforgeeks.org/bipartite-graph/) + * @author [tushar2407](https://github.com/tushar2407) + */ +#include /// for IO operations +#include /// for queue data structure +#include /// for vector data structure +#include /// for assert + +/** + * @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 + * @param visited is the vector which stores whether a given node has been + * traversed or not yet + * @returns boolean + */ +bool checkBipartite( + const std::vector> &graph, + int64_t index, + std::vector *visited +) +{ + std::queue 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()) + { + int64_t u = q.front(); + q.pop(); + for(uint64_t i=0;i> &graph) +{ + std::vector visited(graph.size()); ///< stores boolean values + /// which signify whether that node had been visited or not + + for(uint64_t i=0;i> graph = { + {1,3}, + {0,2}, + {1,3}, + {0,2} + }; + + assert(graph::isBipartite(graph) == true); /// check whether the above + /// defined graph is indeed bipartite + + std::vector> graph_not_bipartite = { + {1,2,3}, + {0,2}, + {0,1,3}, + {0,2} + }; + + assert(graph::isBipartite(graph_not_bipartite) == false); /// check whether + /// the above defined graph is indeed bipartite + std::cout << "All tests have successfully passed!\n"; +} +/** + * @brief Main function + * Instantitates a dummy graph of a small size with + * a few edges between random nodes. + * On applying the algorithm, it checks if the instantiated + * graph is bipartite or not. + * @returns 0 on exit + */ +int main() +{ + test(); // run self-test implementations + return 0; +}