From 79fb528dad8b8ae9a13ad31d17c9018a13d5d49a Mon Sep 17 00:00:00 2001 From: Filip Hlasek Date: Thu, 27 Aug 2020 07:28:31 -0700 Subject: [PATCH] fix: clang-format for graph/ (#1056) * fix: clang-format for graph/ * remove graph.h --- graph/CMakeLists.txt | 10 ++--- graph/breadth_first_search.cpp | 7 +-- .../bridge_finding_with_tarjan_algorithm.cpp | 9 ++-- graph/depth_first_search_with_stack.cpp | 6 +-- graph/kosaraju.cpp | 17 +++---- graph/kruskal.cpp | 6 +-- graph/lowest_common_ancestor.cpp | 45 +++++++++---------- ...th_ford_fulkerson_and_edmond_karp_algo.cpp | 8 ++-- graph/prim.cpp | 4 +- graph/topological_sort.cpp | 8 ++-- graph/topological_sort_by_kahns_algo.cpp | 7 +-- 11 files changed, 67 insertions(+), 60 deletions(-) diff --git a/graph/CMakeLists.txt b/graph/CMakeLists.txt index 02b8f4840..7f6ea0e69 100644 --- a/graph/CMakeLists.txt +++ b/graph/CMakeLists.txt @@ -1,8 +1,8 @@ -# If necessary, use the RELATIVE flag, otherwise each source file may be listed -# with full pathname. RELATIVE may makes it easier to extract an executable name -# automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) -# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) +#If necessary, use the RELATIVE flag, otherwise each source file may be listed +#with full pathname.RELATIVE may makes it easier to extract an executable name +#automatically. +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) +#file(GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) foreach( testsourcefile ${APP_SOURCES} ) # I used a simple string replace, to cut off .cpp. diff --git a/graph/breadth_first_search.cpp b/graph/breadth_first_search.cpp index 0f4f46160..a03a13bcc 100644 --- a/graph/breadth_first_search.cpp +++ b/graph/breadth_first_search.cpp @@ -89,11 +89,12 @@ void add_undirected_edge(std::vector> *graph, int u, int v) { * * @param graph Adjacency list representation of graph * @param start vertex from where traversing starts - * @returns a binary vector indicating which vertices were visited during the search. + * @returns a binary vector indicating which vertices were visited during the + * search. * */ -std::vector breadth_first_search(const std::vector> &graph, - int start) { +std::vector breadth_first_search( + const std::vector> &graph, int start) { /// vector to keep track of visited vertices std::vector visited(graph.size(), false); /// a queue that stores vertices that need to be further explored diff --git a/graph/bridge_finding_with_tarjan_algorithm.cpp b/graph/bridge_finding_with_tarjan_algorithm.cpp index a9f76c033..c4443dd5d 100644 --- a/graph/bridge_finding_with_tarjan_algorithm.cpp +++ b/graph/bridge_finding_with_tarjan_algorithm.cpp @@ -27,14 +27,14 @@ class Solution { bridge.push_back({itr, current_node}); } } - out_time[current_node] = std::min(out_time[current_node], out_time[itr]); + out_time[current_node] = + std::min(out_time[current_node], out_time[itr]); } } public: std::vector> search_bridges( - int n, - const std::vector>& connections) { + int n, const std::vector>& connections) { timer = 0; graph.resize(n); in_time.assign(n, 0); @@ -73,7 +73,8 @@ int main() { * I assumed that the graph is bi-directional and connected. * */ - std::vector> bridges = s1.search_bridges(number_of_node, node); + std::vector> bridges = + s1.search_bridges(number_of_node, node); std::cout << bridges.size() << " bridges found!\n"; for (auto& itr : bridges) { std::cout << itr[0] << " --> " << itr[1] << '\n'; diff --git a/graph/depth_first_search_with_stack.cpp b/graph/depth_first_search_with_stack.cpp index 2d5752322..9810edeb1 100644 --- a/graph/depth_first_search_with_stack.cpp +++ b/graph/depth_first_search_with_stack.cpp @@ -1,14 +1,14 @@ #include #include -#include #include +#include constexpr int WHITE = 0; constexpr int GREY = 1; constexpr int BLACK = 2; constexpr int INF = 99999; -void dfs(const std::vector< std::list > &graph, int start) { +void dfs(const std::vector > &graph, int start) { std::vector checked(graph.size(), WHITE); checked[start] = GREY; std::stack stack; @@ -33,7 +33,7 @@ void dfs(const std::vector< std::list > &graph, int start) { int main() { int n = 0; std::cin >> n; - std::vector< std::list > graph(INF); + std::vector > graph(INF); for (int i = 0; i < n; ++i) { int u = 0, w = 0; std::cin >> u >> w; diff --git a/graph/kosaraju.cpp b/graph/kosaraju.cpp index 5949c0bb8..3b7fdacbb 100644 --- a/graph/kosaraju.cpp +++ b/graph/kosaraju.cpp @@ -3,9 +3,8 @@ */ #include -#include #include - +#include /** * Iterative function/method to print graph: @@ -13,7 +12,7 @@ * @param V number of vertices * @return void **/ -void print(const std::vector< std::vector > &a, int V) { +void print(const std::vector > &a, int V) { for (int i = 0; i < V; i++) { if (!a[i].empty()) { std::cout << "i=" << i << "-->"; @@ -35,7 +34,8 @@ void print(const std::vector< std::vector > &a, int V) { * @param adj adjacency list representation of the graph * @return void **/ -void push_vertex(int v, std::stack *st, std::vector *vis, const std::vector< std::vector > &adj) { +void push_vertex(int v, std::stack *st, std::vector *vis, + const std::vector > &adj) { (*vis)[v] = true; for (auto i = adj[v].begin(); i != adj[v].end(); i++) { if ((*vis)[*i] == false) { @@ -52,7 +52,8 @@ void push_vertex(int v, std::stack *st, std::vector *vis, const std:: * @param grev graph with reversed edges * @return void **/ -void dfs(int v, std::vector *vis, const std::vector< std::vector > &grev) { +void dfs(int v, std::vector *vis, + const std::vector > &grev) { (*vis)[v] = true; // cout<0)) i.e. it returns the count of (number of) strongly connected components (SCCs) in the graph. (variable 'count_scc' within function) **/ -int kosaraju(int V, const std::vector< std::vector > &adj) { +int kosaraju(int V, const std::vector > &adj) { std::vector vis(V, false); std::stack st; for (int v = 0; v < V; v++) { @@ -81,7 +82,7 @@ int kosaraju(int V, const std::vector< std::vector > &adj) { } } // making new graph (grev) with reverse edges as in adj[]: - std::vector< std::vector > grev(V); + std::vector > grev(V); for (int i = 0; i < V + 1; i++) { for (auto j = adj[i].begin(); j != adj[i].end(); j++) { grev[*j].push_back(i); @@ -114,7 +115,7 @@ int main() { int a = 0, b = 0; // a->number of nodes, b->directed edges. std::cin >> a >> b; int m = 0, n = 0; - std::vector< std::vector > adj(a + 1); + std::vector > adj(a + 1); for (int i = 0; i < b; i++) // take total b inputs of 2 vertices each // required to form an edge. { diff --git a/graph/kruskal.cpp b/graph/kruskal.cpp index e179131a1..39f695cb0 100644 --- a/graph/kruskal.cpp +++ b/graph/kruskal.cpp @@ -1,7 +1,7 @@ -#include -#include #include #include +#include +#include //#include // using namespace boost::multiprecision; const int mx = 1e6 + 5; @@ -12,7 +12,7 @@ ll node, edge; std::vector>> edges; void initial() { for (int i = 0; i < node + edge; ++i) { - parent[i] = i; + parent[i] = i; } } diff --git a/graph/lowest_common_ancestor.cpp b/graph/lowest_common_ancestor.cpp index 11029a5b8..7d5ab42b4 100644 --- a/graph/lowest_common_ancestor.cpp +++ b/graph/lowest_common_ancestor.cpp @@ -9,7 +9,8 @@ * Algorithm: https://cp-algorithms.com/graph/lca_binary_lifting.html * * Complexity: - * - Precomputation: \f$O(N \log N)\f$ where \f$N\f$ is the number of vertices in the tree + * - Precomputation: \f$O(N \log N)\f$ where \f$N\f$ is the number of vertices + * in the tree * - Query: \f$O(\log N)\f$ * - Space: \f$O(N \log N)\f$ * @@ -34,11 +35,11 @@ * lowest_common_ancestor(x, y) = lowest_common_ancestor(y, x) */ +#include #include +#include #include #include -#include -#include /** * \namespace graph @@ -50,7 +51,7 @@ namespace graph { * Its vertices are indexed 0, 1, ..., N - 1. */ class Graph { - public: + public: /** * \brief Populate the adjacency list for each vertex in the graph. * Assumes that evey edge is a pair of valid vertex indices. @@ -58,7 +59,7 @@ class Graph { * @param N number of vertices in the graph * @param undirected_edges list of graph's undirected edges */ - Graph(size_t N, const std::vector< std::pair > &undirected_edges) { + Graph(size_t N, const std::vector > &undirected_edges) { neighbors.resize(N); for (auto &edge : undirected_edges) { neighbors[edge.first].push_back(edge.second); @@ -70,19 +71,18 @@ class Graph { * Function to get the number of vertices in the graph * @return the number of vertices in the graph. */ - int number_of_vertices() const { - return neighbors.size(); - } + int number_of_vertices() const { return neighbors.size(); } /** \brief for each vertex it stores a list indicies of its neighbors */ - std::vector< std::vector > neighbors; + std::vector > neighbors; }; /** - * Representation of a rooted tree. For every vertex its parent is precalculated. + * Representation of a rooted tree. For every vertex its parent is + * precalculated. */ class RootedTree : public Graph { - public: + public: /** * \brief Constructs the tree by calculating parent for every vertex. * Assumes a valid description of a tree is provided. @@ -90,7 +90,8 @@ class RootedTree : public Graph { * @param undirected_edges list of graph's undirected edges * @param root_ index of the root vertex */ - RootedTree(const std::vector< std::pair > &undirected_edges, int root_) + RootedTree(const std::vector > &undirected_edges, + int root_) : Graph(undirected_edges.size() + 1, undirected_edges), root(root_) { populate_parents(); } @@ -106,7 +107,7 @@ class RootedTree : public Graph { /** \brief Index of the root vertex. */ int root; - protected: + protected: /** * \brief Calculate the parents for all the vertices in the tree. * Implements the breadth first search algorithm starting from the root @@ -135,7 +136,6 @@ class RootedTree : public Graph { } } } - }; /** @@ -143,13 +143,13 @@ class RootedTree : public Graph { * queries of the lowest common ancestor of two given vertices in the tree. */ class LowestCommonAncestor { - public: + public: /** * \brief Stores the tree and precomputs "up lifts". * @param tree_ rooted tree. */ - explicit LowestCommonAncestor(const RootedTree& tree_) : tree(tree_) { - populate_up(); + explicit LowestCommonAncestor(const RootedTree &tree_) : tree(tree_) { + populate_up(); } /** @@ -196,16 +196,16 @@ class LowestCommonAncestor { } /* \brief reference to the rooted tree this structure allows to query */ - const RootedTree& tree; + const RootedTree &tree; /** * \brief for every vertex stores a list of its ancestors by powers of two * For each vertex, the first element of the corresponding list contains * the index of its parent. The i-th element of the list is an index of * the (2^i)-th ancestor of the vertex. */ - std::vector< std::vector > up; + std::vector > up; - protected: + protected: /** * Populate the "up" structure. See above. */ @@ -241,9 +241,8 @@ static void tests() { * | * 9 */ - std::vector< std::pair > edges = { - {7, 1}, {1, 5}, {1, 3}, {3, 6}, {6, 2}, {2, 9}, {6, 8}, {4, 3}, {0, 4} - }; + std::vector > edges = { + {7, 1}, {1, 5}, {1, 3}, {3, 6}, {6, 2}, {2, 9}, {6, 8}, {4, 3}, {0, 4}}; graph::RootedTree t(edges, 3); graph::LowestCommonAncestor lca(t); assert(lca.lowest_common_ancestor(7, 4) == 3); 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 87b2fa101..20571cdc5 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 @@ -16,7 +16,7 @@ // std::max capacity of node in graph const int MAXN = 505; class Graph { - std::vector< std::vector > residual_capacity, capacity; + std::vector > residual_capacity, capacity; int total_nodes = 0; int total_edges = 0, source = 0, sink = 0; std::vector parent; @@ -50,8 +50,10 @@ class Graph { void set_graph() { std::cin >> total_nodes >> total_edges >> source >> sink; parent = std::vector(total_nodes, -1); - capacity = residual_capacity = std::vector< std::vector >(total_nodes, std::vector(total_nodes)); - for (int start = 0, destination = 0, capacity_ = 0, i = 0; i < total_edges; ++i) { + capacity = residual_capacity = std::vector >( + total_nodes, std::vector(total_nodes)); + for (int start = 0, destination = 0, capacity_ = 0, i = 0; + i < total_edges; ++i) { std::cin >> start >> destination >> capacity_; residual_capacity[start][destination] = capacity_; capacity[start][destination] = capacity_; diff --git a/graph/prim.cpp b/graph/prim.cpp index 0432926ef..f1def6161 100644 --- a/graph/prim.cpp +++ b/graph/prim.cpp @@ -5,7 +5,7 @@ using PII = std::pair; -int prim(int x, const std::vector< std::vector > &graph) { +int prim(int x, const std::vector > &graph) { // priority queue to maintain edges with respect to weights std::priority_queue, std::greater > Q; std::vector marked(graph.size(), false); @@ -40,7 +40,7 @@ int main() { return 0; } - std::vector< std::vector > graph(nodes); + std::vector > graph(nodes); // Edges with their nodes & weight for (int i = 0; i < edges; ++i) { diff --git a/graph/topological_sort.cpp b/graph/topological_sort.cpp index 3827b9bcf..5de8ed69e 100644 --- a/graph/topological_sort.cpp +++ b/graph/topological_sort.cpp @@ -2,7 +2,8 @@ #include #include -int number_of_vertices, number_of_edges; // For number of Vertices (V) and number of edges (E) +int number_of_vertices, + number_of_edges; // For number of Vertices (V) and number of edges (E) std::vector> graph; std::vector visited; std::vector topological_order; @@ -28,7 +29,8 @@ void topological_sort() { reverse(topological_order.begin(), topological_order.end()); } int main() { - std::cout << "Enter the number of vertices and the number of directed edges\n"; + std::cout + << "Enter the number of vertices and the number of directed edges\n"; std::cin >> number_of_vertices >> number_of_edges; int x = 0, y = 0; graph.resize(number_of_vertices, std::vector()); @@ -41,7 +43,7 @@ int main() { std::cout << "Topological Order : \n"; for (int v : topological_order) { std::cout << v + 1 - << ' '; // converting zero based indexing back to one based. + << ' '; // converting zero based indexing back to one based. } std::cout << '\n'; return 0; diff --git a/graph/topological_sort_by_kahns_algo.cpp b/graph/topological_sort_by_kahns_algo.cpp index c29c0fc2d..d491fa80f 100644 --- a/graph/topological_sort_by_kahns_algo.cpp +++ b/graph/topological_sort_by_kahns_algo.cpp @@ -4,7 +4,7 @@ #include #include -std::vector topoSortKahn(int N, const std::vector< std::vector > &adj); +std::vector topoSortKahn(int N, const std::vector > &adj); int main() { int nodes = 0, edges = 0; @@ -14,7 +14,7 @@ int main() { } int u = 0, v = 0; - std::vector< std::vector > graph(nodes); + std::vector > graph(nodes); // create graph // example // 6 6 @@ -32,7 +32,8 @@ int main() { } } -std::vector topoSortKahn(int V, const std::vector< std::vector > &adj) { +std::vector topoSortKahn(int V, + const std::vector > &adj) { std::vector vis(V + 1, false); std::vector deg(V + 1, 0); for (int i = 0; i < V; i++) {