From a49ec9b8d79e62be49587043cf482a9f705e3ed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Hl=C3=A1sek?= Date: Sun, 16 Aug 2020 12:27:51 -0700 Subject: [PATCH 1/3] fix: linter and spacing for is_graph_bipartite. --- graph/is_graph_bipartite.cpp | 265 +++++++++++++++++------------------ 1 file changed, 131 insertions(+), 134 deletions(-) diff --git a/graph/is_graph_bipartite.cpp b/graph/is_graph_bipartite.cpp index baadf71fa..0c4993ff6 100644 --- a/graph/is_graph_bipartite.cpp +++ b/graph/is_graph_bipartite.cpp @@ -1,33 +1,33 @@ /** * @file * - * @brief Algorithm to check whether a graph is [bipartite](https://en.wikipedia.org/wiki/Bipartite_graph) - * - * @details - * 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 - * divided into two disjoint and independent sets U and V such that every edge - * connects a vertex in U to one in V. - * - * The given Algorithm will determine whether the given graph is bipartite or not - * - *
- * 	Example - Here is a graph g1 with 5 vertices and is bipartite
- *	
- *		1   4
- *	   / \ / \
- *	  2   3   5
- *	
- *	Example - Here is a graph G2 with 3 vertices and is not bipartite
- *	
- *		1 --- 2
- *		 \   /
- *		   3
- *	
- *	
+ * @brief Algorithm to check whether a graph is [bipartite](https://en.wikipedia.org/wiki/Bipartite_graph) + * + * @details + * 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 + * divided into two disjoint and independent sets U and V such that every edge + * connects a vertex in U to one in V. + * + * The given Algorithm will determine whether the given graph is bipartite or not + * + *
+ *   Example - Here is a graph g1 with 5 vertices and is bipartite
+ *
+ *      1   4
+ *     / \ / \
+ *    2   3   5
+ *
+ *  Example - Here is a graph G2 with 3 vertices and is not bipartite
+ *
+ *    1 --- 2
+ *     \   /
+ *       3
+ *
+ *  
+ * + * @author [Akshat Vaya](https://github.com/AkVaya) * - * @author [Akshat Vaya](https://github.com/AkVaya) - * */ #include #include @@ -37,127 +37,124 @@ * @namespace graph * @brief Graph algorithms */ -namespace graph{ - /** - * @namespace is_graph_bipartite - * @brief Functions for checking whether a graph is bipartite or not - */ - namespace is_graph_bipartite{ - /** - * @brief Class for representing graph as an adjacency list. - */ - class Graph { - private: - int n; /// size of the graph +namespace graph { + /** + * @namespace is_graph_bipartite + * @brief Functions for checking whether a graph is bipartite or not + */ + namespace is_graph_bipartite { + /** + * @brief 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< std::vector > adj; /// adj stores the graph as an adjacency list - std::vector side; ///stores the side of the vertex + std::vector side; ///stores the side of the vertex - static const int nax = 5e5 + 1; + 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); + } - 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 - 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; - } - } /// namespace is_graph_bipartite -} /// namespace 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; + } + } // namespace is_graph_bipartite +} // namespace graph /** - * Function to test the above algorithm - * @returns none + * Function to test the above algorithm + * @returns none */ 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::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::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); + 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); - /// checking whether the graphs are bipartite or not - if(G1.is_bipartite()){ - std::cout<<"The given graph G1 is a bipartite graph\n"; - } - else{ - std::cout<<"The given graph G1 is not a bipartite graph\n"; - } - if(G2.is_bipartite()){ - std::cout<<"The given graph G2 is a bipartite graph\n"; - } - else{ - std::cout<<"The given graph G2 is not a bipartite graph\n"; - } + /// checking whether the graphs are bipartite or not + if (G1.is_bipartite()) { + std::cout<<"The given graph G1 is a bipartite graph\n"; + } + else { + std::cout<<"The given graph G1 is not a bipartite graph\n"; + } + if (G2.is_bipartite()) { + 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 */ -int main(){ - test(); ///Testing - return 0; +int main() { + test(); ///Testing + return 0; } From 629299b9344d41dae16281c3decc9da320839b7c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 16 Aug 2020 19:28:52 +0000 Subject: [PATCH 2/3] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 76fa1154d..60f4717a4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -82,6 +82,7 @@ * [Dfs With Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/dfs_with_stack.cpp) * [Dijkstra](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/dijkstra.cpp) * [Hamiltons Cycle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/hamiltons_cycle.cpp) + * [Is Graph Bipartite](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/graph/is_graph_bipartite.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) From 518ff584838bde25f4135b9eaf761bc7daa4c6f9 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 16 Aug 2020 19:29:11 +0000 Subject: [PATCH 3/3] clang-tidy fixes for a49ec9b8d79e62be49587043cf482a9f705e3ed1 --- math/fibonacci.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 493523b61..79de528ea 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -17,8 +17,9 @@ unsigned int fibonacci(unsigned int n) { /* If the input is 0 or 1 just return the same This will set the first 2 values of the sequence */ - if (n <= 1) + if (n <= 1) { return n; + } /* Add the last 2 values of the sequence to get next */ return fibonacci(n - 1) + fibonacci(n - 2); @@ -58,7 +59,7 @@ static void test() { /// Main function int main() { test(); - int n; + int n = 0; std::cin >> n; assert(n >= 0); std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl;