From e2f344c498e27aba17194032d4da5e77b82acdc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Hl=C3=A1sek?= Date: Sat, 1 Aug 2020 22:55:50 -0700 Subject: [PATCH] Fix linter warnings in fixgraph branch. --- graph/bfs.cpp | 8 +- .../bridge_finding_with_tarjan_algorithm.cpp | 4 +- graph/cycle_check_directed_graph.cpp | 35 +++--- graph/dfs.cpp | 6 +- graph/kosaraju.cpp | 53 +++++---- graph/kruskal.cpp | 105 +++++------------- graph/lca.cpp | 4 +- graph/topological_sort.cpp | 8 +- 8 files changed, 92 insertions(+), 131 deletions(-) diff --git a/graph/bfs.cpp b/graph/bfs.cpp index 31f9c0770..9e66f3ff2 100644 --- a/graph/bfs.cpp +++ b/graph/bfs.cpp @@ -91,7 +91,7 @@ std::vector beadth_first_search(const std::vector> &adj, std::vector result; /// vector to keep track of visited vertices - std::vector visited(vertices, 0); + std::vector visited(vertices, false); std::queue tracker; /// marking the start vertex as visited @@ -172,7 +172,7 @@ int main() { /// running predefined test cases tests(); - size_t vertices, edges; + size_t vertices = 0, edges = 0; std::cout << "Enter the number of vertices : "; std::cin >> vertices; std::cout << "Enter the number of edges : "; @@ -185,7 +185,7 @@ int main() { std::cout << "Enter vertices in pair which have edges between them : " << std::endl; while (edges--) { - int u, v; + int u = 0, v = 0; std::cin >> u >> v; graph::addEdge(&adj, u, v); } @@ -193,4 +193,4 @@ int main() { /// running Breadth First Search Algorithm on the graph graph::beadth_first_search(adj, 0); return 0; -} \ No newline at end of file +} diff --git a/graph/bridge_finding_with_tarjan_algorithm.cpp b/graph/bridge_finding_with_tarjan_algorithm.cpp index 2b54e1b96..5f1b5796a 100644 --- a/graph/bridge_finding_with_tarjan_algorithm.cpp +++ b/graph/bridge_finding_with_tarjan_algorithm.cpp @@ -15,7 +15,7 @@ using std::vector; class Solution { vector> graph; vector in_time, out_time; - int timer; + int timer = 0; vector> bridge; vector visited; void dfs(int current_node, int parent) { @@ -51,7 +51,7 @@ class Solution { return bridge; } }; -int main(void) { +int main() { Solution s1; int number_of_node = 5; vector> node; diff --git a/graph/cycle_check_directed_graph.cpp b/graph/cycle_check_directed_graph.cpp index c991730c0..cb36a13c8 100644 --- a/graph/cycle_check_directed_graph.cpp +++ b/graph/cycle_check_directed_graph.cpp @@ -1,39 +1,44 @@ #include #include -#include +#include using std::vector; using std::pair; -void explore(int i, vector> &adj, int *state) +void explore(int i, const vector> &adj, std::vector *state) { - state[i] = 1; - for(auto it2 : adj[i]) + (*state)[i] = 1; + for (const auto it2 : adj[i]) { - if (state[it2] == 0) + if ((*state)[it2] == 0) { - explore(it2, adj,state); + explore(it2, adj, state); } - if (state[it2] == 1) + if ((*state)[it2] == 1) { std::cout<<"1"; exit(0); } } - state[i] = 2; + (*state)[i] = 2; }; -int acyclic(vector > &adj,size_t n) { +int acyclic(const vector > &adj, size_t n) { //write your code here - int state[n]; // permitted states are 0 1 and 2 + // permitted states are 0 1 and 2 + std::vector state(n, 0); // mark the states of all vertices initially to 0 - for(int i=0;i > &adj,size_t n) { } int main() { - size_t n, m; + size_t n = 0, m = 0; std::cin >> n >> m; vector > adj(n, vector()); for (size_t i = 0; i < m; i++) { - int x, y; + int x = 0, y = 0; std::cin >> x >> y; adj[x - 1].push_back(y - 1); } diff --git a/graph/dfs.cpp b/graph/dfs.cpp index f79179045..e3f4e7ece 100644 --- a/graph/dfs.cpp +++ b/graph/dfs.cpp @@ -107,7 +107,7 @@ void depth_first_search(const std::vector> &adj, /** Main function */ int main() { - size_t vertices, edges; + size_t vertices = 0, edges = 0; std::cout << "Enter the Vertices : "; std::cin >> vertices; std::cout << "Enter the Edges : "; @@ -120,7 +120,7 @@ int main() { std::cout << "Enter the vertices which have edges between them : " << std::endl; while (edges--) { - size_t u, v; + size_t u = 0, v = 0; std::cin >> u >> v; graph::addEdge(&adj, u, v); } @@ -130,4 +130,4 @@ int main() { std::cout << std::endl; return 0; -} \ No newline at end of file +} diff --git a/graph/kosaraju.cpp b/graph/kosaraju.cpp index a9ef121aa..751ace56f 100644 --- a/graph/kosaraju.cpp +++ b/graph/kosaraju.cpp @@ -13,13 +13,17 @@ * @param V : vertices * @return void **/ -void print(std::vector a[], int V) { +void print(const std::vector< std::vector > &a, int V) { for (int i = 0; i < V; i++) { - if (!a[i].empty()) + if (!a[i].empty()) { std::cout << "i=" << i << "-->"; - for (int j = 0; j < a[i].size(); j++) std::cout << a[i][j] << " "; - if (!a[i].empty()) + } + for (int j : a[i]) { + std::cout << j << " "; + } + if (!a[i].empty()) { std::cout << std::endl; + } } } @@ -27,32 +31,34 @@ void print(std::vector a[], int V) { * //Recursive function/method to push vertices into stack passed as parameter: * @param v : vertices * @param &st : stack passed by reference - * @param vis[] : array to keep track of visited nodes (boolean type) + * @param &vis : array to keep track of visited nodes (boolean type) * @param adj[] : array of vectors to represent graph * @return void **/ -void push_vertex(int v, std::stack &st, bool vis[], std::vector adj[]) { - vis[v] = true; +void push_vertex(int v, std::stack *st, std::vector *vis, const std::vector< std::vector > &adj) { + (*vis)[v] = true; for (auto i = adj[v].begin(); i != adj[v].end(); i++) { - if (vis[*i] == false) + if ((*vis)[*i] == false) { push_vertex(*i, st, vis, adj); + } } - st.push(v); + st->push(v); } /** * //Recursive function/method to implement depth first traversal(dfs): * @param v : vertices - * @param vis[] : array to keep track of visited nodes (boolean type) + * @param vis : array to keep track of visited nodes (boolean type) * @param grev[] : graph with reversed edges * @return void **/ -void dfs(int v, bool vis[], std::vector grev[]) { - vis[v] = true; +void dfs(int v, std::vector *vis, const std::vector< 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, std::vector adj[]) { - bool vis[V] = {}; +int kosaraju(int V, const std::vector< std::vector > &adj) { + std::vector vis(V, false); std::stack st; for (int v = 0; v < V; v++) { - if (vis[v] == false) - push_vertex(v, st, vis, adj); + if (vis[v] == false) { + push_vertex(v, &st, &vis, adj); + } } // making new graph (grev) with reverse edges as in adj[]: - std::vector grev[V]; + std::vector< 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); @@ -89,7 +96,7 @@ int kosaraju(int V, std::vector adj[]) { int t = st.top(); st.pop(); if (vis[t] == false) { - dfs(t, vis, grev); + dfs(t, &vis, grev); count_scc++; } } @@ -101,13 +108,13 @@ int kosaraju(int V, std::vector adj[]) { // All critical/corner cases have been taken care of. // Input your required values: (not hardcoded) int main() { - int t; + int t = 0; std::cin >> t; while (t--) { - int a, b; // a->number of nodes, b->directed edges. + int a = 0, b = 0; // a->number of nodes, b->directed edges. std::cin >> a >> b; - int m, n; - std::vector adj[a + 1]; + int m = 0, n = 0; + std::vector< 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 861b81ae1..deec717c0 100644 --- a/graph/kruskal.cpp +++ b/graph/kruskal.cpp @@ -1,75 +1,21 @@ #include #include #include +#include //#include // using namespace boost::multiprecision; const int mx = 1e6 + 5; -const long int inf = 2e9; -typedef long long ll; -#define rep(i, n) for (i = 0; i < n; i++) -#define repp(i, a, b) for (i = a; i <= b; i++) -#define pii pair -#define vpii vector -#define vi vector -#define vll vector -#define r(x) scanf("%d", &x) -#define rs(s) scanf("%s", s) -#define gc getchar_unlocked -#define pc putchar_unlocked -#define mp make_pair -#define pb push_back -#define lb lower_bound -#define ub upper_bound -#define endl "\n" -#define fast \ - ios_base::sync_with_stdio(false); \ - cin.tie(NULL); \ - cout.tie(NULL); -using namespace std; -void in(int &x) { - register int c = gc(); - x = 0; - int neg = 0; - for (; ((c < 48 || c > 57) && c != '-'); c = gc()) - ; - if (c == '-') { - neg = 1; - c = gc(); - } - for (; c > 47 && c < 58; c = gc()) { - x = (x << 1) + (x << 3) + c - 48; - } - if (neg) - x = -x; -} -void out(int n) { - int N = n, rev, count = 0; - rev = N; - if (N == 0) { - pc('0'); - return; - } - while ((rev % 10) == 0) { - count++; - rev /= 10; - } - rev = 0; - while (N != 0) { - rev = (rev << 3) + (rev << 1) + N % 10; - N /= 10; - } - while (rev != 0) { - pc(rev % 10 + '0'); - rev /= 10; - } - while (count--) pc('0'); -} -ll parent[mx], arr[mx], node, edge; -vector>> v; +typedef int64_t ll; + +std::array parent, arr; +ll node, edge; +std::vector>> v; void initial() { - int i; - rep(i, node + edge) parent[i] = i; + for (int i = 0; i < node + edge; ++i) { + parent[i] = i; + } } + int root(int i) { while (parent[i] != i) { parent[i] = parent[parent[i]]; @@ -77,16 +23,18 @@ int root(int i) { } return i; } + void join(int x, int y) { int root_x = root(x); // Disjoint set union by rank int root_y = root(y); parent[root_x] = root_y; } + ll kruskal() { - ll mincost = 0, i, x, y; - rep(i, edge) { - x = v[i].second.first; - y = v[i].second.second; + ll mincost = 0; + for (int i = 0; i < edge; ++i) { + ll x = v[i].second.first; + ll y = v[i].second.second; if (root(x) != root(y)) { mincost += v[i].first; join(x, y); @@ -94,23 +42,22 @@ ll kruskal() { } return mincost; } + int main() { - fast; - while (1) { - int i, j, from, to, cost, totalcost = 0; - cin >> node >> edge; // Enter the nodes and edges - if (node == 0 && edge == 0) + while (true) { + int from = 0, to = 0, cost = 0, totalcost = 0; + std::cin >> node >> edge; // Enter the nodes and edges + if (node == 0 && edge == 0) { break; // Enter 0 0 to break out + } initial(); // Initialise the parent array - rep(i, edge) { - cin >> from >> to >> cost; - v.pb(mp(cost, mp(from, to))); + for (int i = 0; i < edge; ++i) { + std::cin >> from >> to >> cost; + v.emplace_back(make_pair(cost, std::make_pair(from, to))); totalcost += cost; } sort(v.begin(), v.end()); - // rep(i,v.size()) - // cout< adj[N]; // Graph diff --git a/graph/topological_sort.cpp b/graph/topological_sort.cpp index e7dd7ab63..f77780cdc 100644 --- a/graph/topological_sort.cpp +++ b/graph/topological_sort.cpp @@ -10,8 +10,9 @@ std::vector ans; void dfs(int v) { visited[v] = true; for (int u : G[v]) { - if (!visited[u]) + if (!visited[u]) { dfs(u); + } } ans.push_back(v); } @@ -20,15 +21,16 @@ void topological_sort() { visited.assign(n, false); ans.clear(); for (int i = 0; i < n; ++i) { - if (!visited[i]) + if (!visited[i]) { dfs(i); + } } reverse(ans.begin(), ans.end()); } int main() { std::cout << "Enter the number of vertices and the number of directed edges\n"; std::cin >> n >> m; - int x, y; + int x = 0, y = 0; G.resize(n, std::vector()); for (int i = 0; i < n; ++i) { std::cin >> x >> y;