diff --git a/graph/bidirectional_dijkstra.cpp b/graph/bidirectional_dijkstra.cpp index 5f6d36928..b524e9371 100644 --- a/graph/bidirectional_dijkstra.cpp +++ b/graph/bidirectional_dijkstra.cpp @@ -13,13 +13,14 @@ * https://www.youtube.com/watch?v=DINCL5cd_w0&t=24s */ -#include /// for assert -#include /// for integral typedefs -#include /// for io operations -#include /// for variable INF -#include /// for the priority_queue of distances -#include /// for make_pair function -#include /// for store the graph, the distances, and the path +#include // for assert +#include // for uint64_t, int64_t +#include // for basic_ostream, operator<<, char_traits, cout +#include // for numeric_limits +#include // for priority_queue +#include // for pair, make_pair +#include // for vector +#include // for greater constexpr int64_t INF = std::numeric_limits::max(); diff --git a/graph/breadth_first_search.cpp b/graph/breadth_first_search.cpp index 4bc58f342..d9f25f5b2 100644 --- a/graph/breadth_first_search.cpp +++ b/graph/breadth_first_search.cpp @@ -45,14 +45,14 @@ * push that element into the queue and mark this as visited * */ -#include -#include -#include -#include -#include -#include -#include -#include +#include // for assert +#include // for size_t +#include // for less +#include // for basic_ostream, operator<<, cout, basic_istream... +#include // for list +#include // for map, operator== +#include // for queue +#include // for basic_string, operator<, char_traits, allocator /** * \namespace graph diff --git a/graph/connected_components.cpp b/graph/connected_components.cpp index 06bb1ee50..0a80a95b9 100644 --- a/graph/connected_components.cpp +++ b/graph/connected_components.cpp @@ -25,10 +25,9 @@ * */ -#include -#include -#include -#include +#include // for assert +#include // for basic_ostream, char_traits, operator<<, basic_is... +#include // for vector /** * @namespace graph diff --git a/graph/connected_components_with_dsu.cpp b/graph/connected_components_with_dsu.cpp index 72c4f34fe..38452f1c5 100644 --- a/graph/connected_components_with_dsu.cpp +++ b/graph/connected_components_with_dsu.cpp @@ -17,10 +17,11 @@ * @author Unknown author * @author [Sagar Pandya](https://github.com/sagarpandyansit) */ -#include /// for integer typedefs -#include /// for IO operations -#include /// for std::set -#include /// for std::vector +#include // for int64_t, uint32_t +#include // for char_traits, basic_istream, cin, basic_ostream +#include // for set +#include // for vector +#include // for swap /** * @namespace graph diff --git a/graph/cycle_check_directed_graph.cpp b/graph/cycle_check_directed_graph.cpp index fbe6063ca..161e76843 100644 --- a/graph/cycle_check_directed_graph.cpp +++ b/graph/cycle_check_directed_graph.cpp @@ -7,14 +7,15 @@ * */ -#include /// for integral typedefs -#include // for std::cout -#include // for std::map -#include // for std::queue -#include // for throwing errors -#include // for std::remove_reference -#include // for std::move -#include // for std::vector +#include // for uint8_t +#include // for char_traits, basic_ostream, operator<< +#include // for map, operator!=, _Rb_tree_iterator +#include // for queue +#include // for range_error +#include // for remove_reference +#include // for move, pair +#include // for vector +#include // for initializer_list /** * Implementation of non-weighted directed edge of a graph. diff --git a/graph/depth_first_search.cpp b/graph/depth_first_search.cpp index e99d44fc9..aa6630493 100644 --- a/graph/depth_first_search.cpp +++ b/graph/depth_first_search.cpp @@ -32,9 +32,9 @@ * */ -#include -#include -#include +#include // for size_t +#include // for char_traits, basic_ostream, operator<<, cout +#include // for vector /** * diff --git a/graph/depth_first_search_with_stack.cpp b/graph/depth_first_search_with_stack.cpp index 5376ea4c4..e1c9d805f 100644 --- a/graph/depth_first_search_with_stack.cpp +++ b/graph/depth_first_search_with_stack.cpp @@ -2,7 +2,8 @@ * * @file * @brief [Depth First Search Algorithm using Stack - * (Depth First Search Algorithm)](https://en.wikipedia.org/wiki/Depth-first_search) + * (Depth First Search + * Algorithm)](https://en.wikipedia.org/wiki/Depth-first_search) * * @author [Ayaan Khan](http://github.com/ayaankhan98) * @author [Saurav Uppoor](https://github.com/sauravUppoor) @@ -24,23 +25,26 @@ *

Working

* 1. Mark all vertices as unvisited (colour it WHITE). * 2. Push starting vertex into the stack and colour it GREY. - * 3. Once a node is popped out of the stack and is coloured GREY, we colour it BLACK. + * 3. Once a node is popped out of the stack and is coloured GREY, we colour it + * BLACK. * 4. Push all its neighbours which are not coloured BLACK. * 5. Repeat steps 4 and 5 until the stack is empty. */ -#include /// for IO operations -#include /// header for std::stack -#include /// header for std::vector -#include /// header for preprocessor macro assert() -#include /// header for limits of integral types +#include // for int16_t, int64_t -constexpr int WHITE = 0; /// indicates the node hasn't been explored -constexpr int GREY = 1; /// indicates node is in stack waiting to be explored -constexpr int BLACK = 2; /// indicates node has already been explored +#include // for assert +#include // for size_t +#include // for basic_ostream, operator<<, char_traits, cout, endl +#include // for numeric_limits +#include // for stack +#include // for vector, operator== + +constexpr int WHITE = 0; /// indicates the node hasn't been explored +constexpr int GREY = 1; /// indicates node is in stack waiting to be explored +constexpr int BLACK = 2; /// indicates node has already been explored constexpr int64_t INF = std::numeric_limits::max(); - /** * @namespace graph * @brief Graph algorithms @@ -48,7 +52,8 @@ constexpr int64_t INF = std::numeric_limits::max(); namespace graph { /** * @namespace depth_first_search - * @brief Functions for [Depth First Search](https://en.wikipedia.org/wiki/Depth-first_search) algorithm + * @brief Functions for [Depth First + * Search](https://en.wikipedia.org/wiki/Depth-first_search) algorithm */ namespace depth_first_search { /** @@ -62,14 +67,14 @@ namespace depth_first_search { * */ void addEdge(std::vector> *adj, size_t u, size_t v) { - /* - * - * Here we are considering undirected graph that's the - * reason we are adding v to the adjacency list representation of u - * and also adding u to the adjacency list representation of v - * - */ - (*adj)[u - 1].push_back(v - 1); + /* + * + * Here we are considering undirected graph that's the + * reason we are adding v to the adjacency list representation of u + * and also adding u to the adjacency list representation of v + * + */ + (*adj)[u - 1].push_back(v - 1); } /** @@ -84,7 +89,8 @@ void addEdge(std::vector> *adj, size_t u, size_t v) { * @return vector with nodes stored in the order of DFS traversal * */ -std::vector dfs(const std::vector > &graph, size_t start) { +std::vector dfs(const std::vector> &graph, + size_t start) { /// checked[i] stores the status of each node std::vector checked(graph.size(), WHITE), traversed_path; @@ -121,49 +127,51 @@ std::vector dfs(const std::vector > &graph, size_t s * @returns none */ static void tests() { - size_t start_pos; + size_t start_pos; - /// Test 1 - std::cout << "Case 1: " << std::endl; - start_pos = 1; - std::vector > g1(3, std::vector()); + /// Test 1 + std::cout << "Case 1: " << std::endl; + start_pos = 1; + std::vector> g1(3, std::vector()); - graph::depth_first_search::addEdge(&g1, 1, 2); - graph::depth_first_search::addEdge(&g1, 2, 3); - graph::depth_first_search::addEdge(&g1, 3, 1); + graph::depth_first_search::addEdge(&g1, 1, 2); + graph::depth_first_search::addEdge(&g1, 2, 3); + graph::depth_first_search::addEdge(&g1, 3, 1); - std::vector expected1 {1, 2, 3}; /// for the above sample data, this is the expected output - assert(graph::depth_first_search::dfs(g1, start_pos - 1) == expected1); - std::cout << "Passed" << std::endl; + std::vector expected1{ + 1, 2, 3}; /// for the above sample data, this is the expected output + assert(graph::depth_first_search::dfs(g1, start_pos - 1) == expected1); + std::cout << "Passed" << std::endl; - /// Test 2 - std::cout << "Case 2: " << std::endl; - start_pos = 1; - std::vector > g2(4, std::vector()); + /// Test 2 + std::cout << "Case 2: " << std::endl; + start_pos = 1; + std::vector> g2(4, std::vector()); - graph::depth_first_search::addEdge(&g2, 1, 2); - graph::depth_first_search::addEdge(&g2, 1, 3); - graph::depth_first_search::addEdge(&g2, 2, 4); - graph::depth_first_search::addEdge(&g2, 4, 1); + graph::depth_first_search::addEdge(&g2, 1, 2); + graph::depth_first_search::addEdge(&g2, 1, 3); + graph::depth_first_search::addEdge(&g2, 2, 4); + graph::depth_first_search::addEdge(&g2, 4, 1); - std::vector expected2 {1, 3, 2, 4}; /// for the above sample data, this is the expected output - assert(graph::depth_first_search::dfs(g2, start_pos - 1) == expected2); - std::cout << "Passed" << std::endl; + std::vector expected2{ + 1, 3, 2, 4}; /// for the above sample data, this is the expected output + assert(graph::depth_first_search::dfs(g2, start_pos - 1) == expected2); + std::cout << "Passed" << std::endl; - /// Test 3 - std::cout << "Case 3: " << std::endl; - start_pos = 2; - std::vector > g3(4, std::vector()); + /// Test 3 + std::cout << "Case 3: " << std::endl; + start_pos = 2; + std::vector> g3(4, std::vector()); - graph::depth_first_search::addEdge(&g3, 1, 2); - graph::depth_first_search::addEdge(&g3, 1, 3); - graph::depth_first_search::addEdge(&g3, 2, 4); - graph::depth_first_search::addEdge(&g3, 4, 1); - - std::vector expected3 {2, 4, 1, 3}; /// for the above sample data, this is the expected output - assert(graph::depth_first_search::dfs(g3, start_pos - 1) == expected3); - std::cout << "Passed" << std::endl; + graph::depth_first_search::addEdge(&g3, 1, 2); + graph::depth_first_search::addEdge(&g3, 1, 3); + graph::depth_first_search::addEdge(&g3, 2, 4); + graph::depth_first_search::addEdge(&g3, 4, 1); + std::vector expected3{ + 2, 4, 1, 3}; /// for the above sample data, this is the expected output + assert(graph::depth_first_search::dfs(g3, start_pos - 1) == expected3); + std::cout << "Passed" << std::endl; } /** @@ -174,34 +182,35 @@ int main() { tests(); // execute the tests size_t vertices = 0, edges = 0, start_pos = 1; - std::vector traversal; + std::vector traversal; std::cout << "Enter the Vertices : "; - std::cin >> vertices; - std::cout << "Enter the Edges : "; - std::cin >> edges; + std::cin >> vertices; + std::cout << "Enter the Edges : "; + std::cin >> edges; /// creating a graph - std::vector > adj(vertices, std::vector()); + std::vector> adj(vertices, std::vector()); /// taking input for the edges - std::cout << "Enter the vertices which have edges between them : " << std::endl; - while (edges--) { - size_t u = 0, v = 0; - std::cin >> u >> v; - graph::depth_first_search::addEdge(&adj, u, v); - } + std::cout << "Enter the vertices which have edges between them : " + << std::endl; + while (edges--) { + size_t u = 0, v = 0; + std::cin >> u >> v; + graph::depth_first_search::addEdge(&adj, u, v); + } /// taking input for the starting position std::cout << "Enter the starting vertex [1,n]: " << std::endl; - std::cin >> start_pos; - start_pos -= 1; - traversal = graph::depth_first_search::dfs(adj, start_pos); + std::cin >> start_pos; + start_pos -= 1; + traversal = graph::depth_first_search::dfs(adj, start_pos); /// Printing the order of traversal for (auto x : traversal) { - std::cout << x << ' '; - } + std::cout << x << ' '; + } return 0; } diff --git a/graph/dijkstra.cpp b/graph/dijkstra.cpp index a76214560..a4b7c705a 100644 --- a/graph/dijkstra.cpp +++ b/graph/dijkstra.cpp @@ -23,13 +23,14 @@ * at the code below to understand in better way. * */ -#include -#include -#include -#include -#include -#include -#include +#include // for int64_t +#include // for assert +#include // for basic_ostream, char_traits, operator<<, basic_... +#include // for numeric_limits +#include // for priority_queue +#include // for pair, make_pair +#include // for vector +#include // for greater constexpr int64_t INF = std::numeric_limits::max(); diff --git a/graph/hamiltons_cycle.cpp b/graph/hamiltons_cycle.cpp index 1506b78d2..a646321a6 100644 --- a/graph/hamiltons_cycle.cpp +++ b/graph/hamiltons_cycle.cpp @@ -15,9 +15,10 @@ * @author [vakhokoto](https://github.com/vakhokoto) * @author [Krishna Vedala](https://github.com/kvedala) */ -#include -#include -#include +#include // for assert +#include // for size_t +#include // for operator<<, basic_ostream, cout +#include // for vector /** * The function determines if there is a hamilton's cycle in the graph diff --git a/graph/hopcroft_karp.cpp b/graph/hopcroft_karp.cpp index d4e002948..c5ba5e1b8 100644 --- a/graph/hopcroft_karp.cpp +++ b/graph/hopcroft_karp.cpp @@ -45,13 +45,12 @@ */ -#include -#include -#include -#include -#include -#include -#include +#include // for char_traits, basic_istream::operator>>, basic_is... +#include // for queue +#include // for list, operator!=, _List_iterator +#include // for INT_MAX +#include // for assert +#include // for vector /** * @namespace graph diff --git a/graph/kruskal.cpp b/graph/kruskal.cpp index 39f695cb0..9cf83d52d 100644 --- a/graph/kruskal.cpp +++ b/graph/kruskal.cpp @@ -1,7 +1,10 @@ -#include -#include -#include -#include +#include // for int64_t +#include // for sort +#include // for array +#include // for char_traits, basic_istream::operator>>, basic_i... +#include // for vector +#include // for pair, make_pair + //#include // using namespace boost::multiprecision; const int mx = 1e6 + 5; diff --git a/graph/lowest_common_ancestor.cpp b/graph/lowest_common_ancestor.cpp index 7d5ab42b4..cfb8efbdf 100644 --- a/graph/lowest_common_ancestor.cpp +++ b/graph/lowest_common_ancestor.cpp @@ -35,11 +35,11 @@ * lowest_common_ancestor(x, y) = lowest_common_ancestor(y, x) */ -#include -#include -#include -#include -#include +#include // for assert +#include // for size_t +#include // for queue +#include // for pair, swap +#include // for vector /** * \namespace graph diff --git a/graph/max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp b/graph/max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp index 20571cdc5..6708c303c 100644 --- a/graph/max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp +++ b/graph/max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp @@ -4,15 +4,14 @@ * Copyright: 2020, Open-Source * Last Modified: May 25, 2020 */ -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include // for min +#include // for bitset +#include // for char_traits, basic_ostream, operator<<, basic_i... +#include // for numeric_limits +#include // for queue +#include // for tuple, make_tuple, tie +#include // for vector + // std::max capacity of node in graph const int MAXN = 505; class Graph { diff --git a/graph/prim.cpp b/graph/prim.cpp index f1def6161..9e56dba65 100644 --- a/graph/prim.cpp +++ b/graph/prim.cpp @@ -1,7 +1,9 @@ // C++ program to implement Prim's Algorithm -#include -#include -#include +#include // for char_traits, basic_istream::operator>>, basic_... +#include // for priority_queue +#include // for vector +#include // for greater +#include // for make_pair, pair using PII = std::pair; diff --git a/graph/topological_sort_by_kahns_algo.cpp b/graph/topological_sort_by_kahns_algo.cpp index d491fa80f..612186d8f 100644 --- a/graph/topological_sort_by_kahns_algo.cpp +++ b/graph/topological_sort_by_kahns_algo.cpp @@ -1,8 +1,6 @@ -#include -#include -#include -#include -#include +#include // for char_traits, basic_istream::operator>>, basic_is... +#include // for queue +#include // for vector std::vector topoSortKahn(int N, const std::vector > &adj); diff --git a/graph/travelling_salesman_problem.cpp b/graph/travelling_salesman_problem.cpp index 8b4608009..72a02b82c 100644 --- a/graph/travelling_salesman_problem.cpp +++ b/graph/travelling_salesman_problem.cpp @@ -17,12 +17,11 @@ * This is the naive implementation of the problem. */ -#include /// for std::min -#include /// for assert -#include /// for integral typedefs -#include /// for IO operations -#include /// for limits of integral types -#include /// for std::vector +#include // for min, next_permutation +#include // for assert +#include // for uint32_t, int32_t +#include // for basic_ostream, operator<<, char_traits, cout, endl +#include // for vector /** * @namespace graph