Skip to content

Commit 79fb528

Browse files
authored
fix: clang-format for graph/ (#1056)
* fix: clang-format for graph/ * remove graph.h
1 parent 3239fcc commit 79fb528

11 files changed

+67
-60
lines changed

graph/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2-
# with full pathname. RELATIVE may makes it easier to extract an executable name
3-
# automatically.
4-
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
5-
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
1+
#If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
#with full pathname.RELATIVE may makes it easier to extract an executable name
3+
#automatically.
4+
file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
5+
#file(GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
66
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
77
foreach( testsourcefile ${APP_SOURCES} )
88
# I used a simple string replace, to cut off .cpp.

graph/breadth_first_search.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ void add_undirected_edge(std::vector<std::vector<int>> *graph, int u, int v) {
8989
*
9090
* @param graph Adjacency list representation of graph
9191
* @param start vertex from where traversing starts
92-
* @returns a binary vector indicating which vertices were visited during the search.
92+
* @returns a binary vector indicating which vertices were visited during the
93+
* search.
9394
*
9495
*/
95-
std::vector<bool> breadth_first_search(const std::vector<std::vector<int>> &graph,
96-
int start) {
96+
std::vector<bool> breadth_first_search(
97+
const std::vector<std::vector<int>> &graph, int start) {
9798
/// vector to keep track of visited vertices
9899
std::vector<bool> visited(graph.size(), false);
99100
/// a queue that stores vertices that need to be further explored

graph/bridge_finding_with_tarjan_algorithm.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class Solution {
2727
bridge.push_back({itr, current_node});
2828
}
2929
}
30-
out_time[current_node] = std::min(out_time[current_node], out_time[itr]);
30+
out_time[current_node] =
31+
std::min(out_time[current_node], out_time[itr]);
3132
}
3233
}
3334

3435
public:
3536
std::vector<std::vector<int>> search_bridges(
36-
int n,
37-
const std::vector<std::vector<int>>& connections) {
37+
int n, const std::vector<std::vector<int>>& connections) {
3838
timer = 0;
3939
graph.resize(n);
4040
in_time.assign(n, 0);
@@ -73,7 +73,8 @@ int main() {
7373
* I assumed that the graph is bi-directional and connected.
7474
*
7575
*/
76-
std::vector<std::vector<int>> bridges = s1.search_bridges(number_of_node, node);
76+
std::vector<std::vector<int>> bridges =
77+
s1.search_bridges(number_of_node, node);
7778
std::cout << bridges.size() << " bridges found!\n";
7879
for (auto& itr : bridges) {
7980
std::cout << itr[0] << " --> " << itr[1] << '\n';

graph/depth_first_search_with_stack.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include <iostream>
22
#include <list>
3-
#include <vector>
43
#include <stack>
4+
#include <vector>
55

66
constexpr int WHITE = 0;
77
constexpr int GREY = 1;
88
constexpr int BLACK = 2;
99
constexpr int INF = 99999;
1010

11-
void dfs(const std::vector< std::list<int> > &graph, int start) {
11+
void dfs(const std::vector<std::list<int> > &graph, int start) {
1212
std::vector<int> checked(graph.size(), WHITE);
1313
checked[start] = GREY;
1414
std::stack<int> stack;
@@ -33,7 +33,7 @@ void dfs(const std::vector< std::list<int> > &graph, int start) {
3333
int main() {
3434
int n = 0;
3535
std::cin >> n;
36-
std::vector< std::list<int> > graph(INF);
36+
std::vector<std::list<int> > graph(INF);
3737
for (int i = 0; i < n; ++i) {
3838
int u = 0, w = 0;
3939
std::cin >> u >> w;

graph/kosaraju.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
*/
44

55
#include <iostream>
6-
#include <vector>
76
#include <stack>
8-
7+
#include <vector>
98

109
/**
1110
* Iterative function/method to print graph:
1211
* @param a adjacency list representation of the graph
1312
* @param V number of vertices
1413
* @return void
1514
**/
16-
void print(const std::vector< std::vector<int> > &a, int V) {
15+
void print(const std::vector<std::vector<int> > &a, int V) {
1716
for (int i = 0; i < V; i++) {
1817
if (!a[i].empty()) {
1918
std::cout << "i=" << i << "-->";
@@ -35,7 +34,8 @@ void print(const std::vector< std::vector<int> > &a, int V) {
3534
* @param adj adjacency list representation of the graph
3635
* @return void
3736
**/
38-
void push_vertex(int v, std::stack<int> *st, std::vector<bool> *vis, const std::vector< std::vector<int> > &adj) {
37+
void push_vertex(int v, std::stack<int> *st, std::vector<bool> *vis,
38+
const std::vector<std::vector<int> > &adj) {
3939
(*vis)[v] = true;
4040
for (auto i = adj[v].begin(); i != adj[v].end(); i++) {
4141
if ((*vis)[*i] == false) {
@@ -52,7 +52,8 @@ void push_vertex(int v, std::stack<int> *st, std::vector<bool> *vis, const std::
5252
* @param grev graph with reversed edges
5353
* @return void
5454
**/
55-
void dfs(int v, std::vector<bool> *vis, const std::vector< std::vector<int> > &grev) {
55+
void dfs(int v, std::vector<bool> *vis,
56+
const std::vector<std::vector<int> > &grev) {
5657
(*vis)[v] = true;
5758
// cout<<v<<" ";
5859
for (auto i = grev[v].begin(); i != grev[v].end(); i++) {
@@ -72,7 +73,7 @@ no SCCs i.e. none(0) or there will be x no. of SCCs (x>0)) i.e. it returns the
7273
count of (number of) strongly connected components (SCCs) in the graph.
7374
(variable 'count_scc' within function)
7475
**/
75-
int kosaraju(int V, const std::vector< std::vector<int> > &adj) {
76+
int kosaraju(int V, const std::vector<std::vector<int> > &adj) {
7677
std::vector<bool> vis(V, false);
7778
std::stack<int> st;
7879
for (int v = 0; v < V; v++) {
@@ -81,7 +82,7 @@ int kosaraju(int V, const std::vector< std::vector<int> > &adj) {
8182
}
8283
}
8384
// making new graph (grev) with reverse edges as in adj[]:
84-
std::vector< std::vector<int> > grev(V);
85+
std::vector<std::vector<int> > grev(V);
8586
for (int i = 0; i < V + 1; i++) {
8687
for (auto j = adj[i].begin(); j != adj[i].end(); j++) {
8788
grev[*j].push_back(i);
@@ -114,7 +115,7 @@ int main() {
114115
int a = 0, b = 0; // a->number of nodes, b->directed edges.
115116
std::cin >> a >> b;
116117
int m = 0, n = 0;
117-
std::vector< std::vector<int> > adj(a + 1);
118+
std::vector<std::vector<int> > adj(a + 1);
118119
for (int i = 0; i < b; i++) // take total b inputs of 2 vertices each
119120
// required to form an edge.
120121
{

graph/kruskal.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#include <iostream>
2-
#include <vector>
31
#include <algorithm>
42
#include <array>
3+
#include <iostream>
4+
#include <vector>
55
//#include <boost/multiprecision/cpp_int.hpp>
66
// using namespace boost::multiprecision;
77
const int mx = 1e6 + 5;
@@ -12,7 +12,7 @@ ll node, edge;
1212
std::vector<std::pair<ll, std::pair<ll, ll>>> edges;
1313
void initial() {
1414
for (int i = 0; i < node + edge; ++i) {
15-
parent[i] = i;
15+
parent[i] = i;
1616
}
1717
}
1818

graph/lowest_common_ancestor.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* Algorithm: https://cp-algorithms.com/graph/lca_binary_lifting.html
1010
*
1111
* Complexity:
12-
* - Precomputation: \f$O(N \log N)\f$ where \f$N\f$ is the number of vertices in the tree
12+
* - Precomputation: \f$O(N \log N)\f$ where \f$N\f$ is the number of vertices
13+
* in the tree
1314
* - Query: \f$O(\log N)\f$
1415
* - Space: \f$O(N \log N)\f$
1516
*
@@ -34,11 +35,11 @@
3435
* lowest_common_ancestor(x, y) = lowest_common_ancestor(y, x)
3536
*/
3637

38+
#include <cassert>
3739
#include <iostream>
40+
#include <queue>
3841
#include <utility>
3942
#include <vector>
40-
#include <queue>
41-
#include <cassert>
4243

4344
/**
4445
* \namespace graph
@@ -50,15 +51,15 @@ namespace graph {
5051
* Its vertices are indexed 0, 1, ..., N - 1.
5152
*/
5253
class Graph {
53-
public:
54+
public:
5455
/**
5556
* \brief Populate the adjacency list for each vertex in the graph.
5657
* Assumes that evey edge is a pair of valid vertex indices.
5758
*
5859
* @param N number of vertices in the graph
5960
* @param undirected_edges list of graph's undirected edges
6061
*/
61-
Graph(size_t N, const std::vector< std::pair<int, int> > &undirected_edges) {
62+
Graph(size_t N, const std::vector<std::pair<int, int> > &undirected_edges) {
6263
neighbors.resize(N);
6364
for (auto &edge : undirected_edges) {
6465
neighbors[edge.first].push_back(edge.second);
@@ -70,27 +71,27 @@ class Graph {
7071
* Function to get the number of vertices in the graph
7172
* @return the number of vertices in the graph.
7273
*/
73-
int number_of_vertices() const {
74-
return neighbors.size();
75-
}
74+
int number_of_vertices() const { return neighbors.size(); }
7675

7776
/** \brief for each vertex it stores a list indicies of its neighbors */
78-
std::vector< std::vector<int> > neighbors;
77+
std::vector<std::vector<int> > neighbors;
7978
};
8079

8180
/**
82-
* Representation of a rooted tree. For every vertex its parent is precalculated.
81+
* Representation of a rooted tree. For every vertex its parent is
82+
* precalculated.
8383
*/
8484
class RootedTree : public Graph {
85-
public:
85+
public:
8686
/**
8787
* \brief Constructs the tree by calculating parent for every vertex.
8888
* Assumes a valid description of a tree is provided.
8989
*
9090
* @param undirected_edges list of graph's undirected edges
9191
* @param root_ index of the root vertex
9292
*/
93-
RootedTree(const std::vector< std::pair<int, int> > &undirected_edges, int root_)
93+
RootedTree(const std::vector<std::pair<int, int> > &undirected_edges,
94+
int root_)
9495
: Graph(undirected_edges.size() + 1, undirected_edges), root(root_) {
9596
populate_parents();
9697
}
@@ -106,7 +107,7 @@ class RootedTree : public Graph {
106107
/** \brief Index of the root vertex. */
107108
int root;
108109

109-
protected:
110+
protected:
110111
/**
111112
* \brief Calculate the parents for all the vertices in the tree.
112113
* Implements the breadth first search algorithm starting from the root
@@ -135,21 +136,20 @@ class RootedTree : public Graph {
135136
}
136137
}
137138
}
138-
139139
};
140140

141141
/**
142142
* A structure that holds a rooted tree and allow for effecient
143143
* queries of the lowest common ancestor of two given vertices in the tree.
144144
*/
145145
class LowestCommonAncestor {
146-
public:
146+
public:
147147
/**
148148
* \brief Stores the tree and precomputs "up lifts".
149149
* @param tree_ rooted tree.
150150
*/
151-
explicit LowestCommonAncestor(const RootedTree& tree_) : tree(tree_) {
152-
populate_up();
151+
explicit LowestCommonAncestor(const RootedTree &tree_) : tree(tree_) {
152+
populate_up();
153153
}
154154

155155
/**
@@ -196,16 +196,16 @@ class LowestCommonAncestor {
196196
}
197197

198198
/* \brief reference to the rooted tree this structure allows to query */
199-
const RootedTree& tree;
199+
const RootedTree &tree;
200200
/**
201201
* \brief for every vertex stores a list of its ancestors by powers of two
202202
* For each vertex, the first element of the corresponding list contains
203203
* the index of its parent. The i-th element of the list is an index of
204204
* the (2^i)-th ancestor of the vertex.
205205
*/
206-
std::vector< std::vector<int> > up;
206+
std::vector<std::vector<int> > up;
207207

208-
protected:
208+
protected:
209209
/**
210210
* Populate the "up" structure. See above.
211211
*/
@@ -241,9 +241,8 @@ static void tests() {
241241
* |
242242
* 9
243243
*/
244-
std::vector< std::pair<int, int> > edges = {
245-
{7, 1}, {1, 5}, {1, 3}, {3, 6}, {6, 2}, {2, 9}, {6, 8}, {4, 3}, {0, 4}
246-
};
244+
std::vector<std::pair<int, int> > edges = {
245+
{7, 1}, {1, 5}, {1, 3}, {3, 6}, {6, 2}, {2, 9}, {6, 8}, {4, 3}, {0, 4}};
247246
graph::RootedTree t(edges, 3);
248247
graph::LowestCommonAncestor lca(t);
249248
assert(lca.lowest_common_ancestor(7, 4) == 3);

graph/max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// std::max capacity of node in graph
1717
const int MAXN = 505;
1818
class Graph {
19-
std::vector< std::vector<int> > residual_capacity, capacity;
19+
std::vector<std::vector<int> > residual_capacity, capacity;
2020
int total_nodes = 0;
2121
int total_edges = 0, source = 0, sink = 0;
2222
std::vector<int> parent;
@@ -50,8 +50,10 @@ class Graph {
5050
void set_graph() {
5151
std::cin >> total_nodes >> total_edges >> source >> sink;
5252
parent = std::vector<int>(total_nodes, -1);
53-
capacity = residual_capacity = std::vector< std::vector<int> >(total_nodes, std::vector<int>(total_nodes));
54-
for (int start = 0, destination = 0, capacity_ = 0, i = 0; i < total_edges; ++i) {
53+
capacity = residual_capacity = std::vector<std::vector<int> >(
54+
total_nodes, std::vector<int>(total_nodes));
55+
for (int start = 0, destination = 0, capacity_ = 0, i = 0;
56+
i < total_edges; ++i) {
5557
std::cin >> start >> destination >> capacity_;
5658
residual_capacity[start][destination] = capacity_;
5759
capacity[start][destination] = capacity_;

graph/prim.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
using PII = std::pair<int, int>;
77

8-
int prim(int x, const std::vector< std::vector<PII> > &graph) {
8+
int prim(int x, const std::vector<std::vector<PII> > &graph) {
99
// priority queue to maintain edges with respect to weights
1010
std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Q;
1111
std::vector<bool> marked(graph.size(), false);
@@ -40,7 +40,7 @@ int main() {
4040
return 0;
4141
}
4242

43-
std::vector< std::vector<PII> > graph(nodes);
43+
std::vector<std::vector<PII> > graph(nodes);
4444

4545
// Edges with their nodes & weight
4646
for (int i = 0; i < edges; ++i) {

graph/topological_sort.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#include <iostream>
33
#include <vector>
44

5-
int number_of_vertices, number_of_edges; // For number of Vertices (V) and number of edges (E)
5+
int number_of_vertices,
6+
number_of_edges; // For number of Vertices (V) and number of edges (E)
67
std::vector<std::vector<int>> graph;
78
std::vector<bool> visited;
89
std::vector<int> topological_order;
@@ -28,7 +29,8 @@ void topological_sort() {
2829
reverse(topological_order.begin(), topological_order.end());
2930
}
3031
int main() {
31-
std::cout << "Enter the number of vertices and the number of directed edges\n";
32+
std::cout
33+
<< "Enter the number of vertices and the number of directed edges\n";
3234
std::cin >> number_of_vertices >> number_of_edges;
3335
int x = 0, y = 0;
3436
graph.resize(number_of_vertices, std::vector<int>());
@@ -41,7 +43,7 @@ int main() {
4143
std::cout << "Topological Order : \n";
4244
for (int v : topological_order) {
4345
std::cout << v + 1
44-
<< ' '; // converting zero based indexing back to one based.
46+
<< ' '; // converting zero based indexing back to one based.
4547
}
4648
std::cout << '\n';
4749
return 0;

0 commit comments

Comments
 (0)