fix: clang-format for graph/ (#1056)

* fix: clang-format for graph/

* remove graph.h
This commit is contained in:
Filip Hlasek 2020-08-27 07:28:31 -07:00 committed by GitHub
parent 3239fcc19e
commit 79fb528dad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 67 additions and 60 deletions

View File

@ -1,8 +1,8 @@
# If necessary, use the RELATIVE flag, otherwise each source file may be listed #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 #with full pathname.RELATIVE may makes it easier to extract an executable name
# automatically. #automatically.
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) #file(GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach( testsourcefile ${APP_SOURCES} ) foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp. # I used a simple string replace, to cut off .cpp.

View File

@ -89,11 +89,12 @@ void add_undirected_edge(std::vector<std::vector<int>> *graph, int u, int v) {
* *
* @param graph Adjacency list representation of graph * @param graph Adjacency list representation of graph
* @param start vertex from where traversing starts * @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<bool> breadth_first_search(const std::vector<std::vector<int>> &graph, std::vector<bool> breadth_first_search(
int start) { const std::vector<std::vector<int>> &graph, int start) {
/// vector to keep track of visited vertices /// vector to keep track of visited vertices
std::vector<bool> visited(graph.size(), false); std::vector<bool> visited(graph.size(), false);
/// a queue that stores vertices that need to be further explored /// a queue that stores vertices that need to be further explored

View File

@ -27,14 +27,14 @@ class Solution {
bridge.push_back({itr, current_node}); 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: public:
std::vector<std::vector<int>> search_bridges( std::vector<std::vector<int>> search_bridges(
int n, int n, const std::vector<std::vector<int>>& connections) {
const std::vector<std::vector<int>>& connections) {
timer = 0; timer = 0;
graph.resize(n); graph.resize(n);
in_time.assign(n, 0); in_time.assign(n, 0);
@ -73,7 +73,8 @@ int main() {
* I assumed that the graph is bi-directional and connected. * I assumed that the graph is bi-directional and connected.
* *
*/ */
std::vector<std::vector<int>> bridges = s1.search_bridges(number_of_node, node); std::vector<std::vector<int>> bridges =
s1.search_bridges(number_of_node, node);
std::cout << bridges.size() << " bridges found!\n"; std::cout << bridges.size() << " bridges found!\n";
for (auto& itr : bridges) { for (auto& itr : bridges) {
std::cout << itr[0] << " --> " << itr[1] << '\n'; std::cout << itr[0] << " --> " << itr[1] << '\n';

View File

@ -1,14 +1,14 @@
#include <iostream> #include <iostream>
#include <list> #include <list>
#include <vector>
#include <stack> #include <stack>
#include <vector>
constexpr int WHITE = 0; constexpr int WHITE = 0;
constexpr int GREY = 1; constexpr int GREY = 1;
constexpr int BLACK = 2; constexpr int BLACK = 2;
constexpr int INF = 99999; constexpr int INF = 99999;
void dfs(const std::vector< std::list<int> > &graph, int start) { void dfs(const std::vector<std::list<int> > &graph, int start) {
std::vector<int> checked(graph.size(), WHITE); std::vector<int> checked(graph.size(), WHITE);
checked[start] = GREY; checked[start] = GREY;
std::stack<int> stack; std::stack<int> stack;
@ -33,7 +33,7 @@ void dfs(const std::vector< std::list<int> > &graph, int start) {
int main() { int main() {
int n = 0; int n = 0;
std::cin >> n; std::cin >> n;
std::vector< std::list<int> > graph(INF); std::vector<std::list<int> > graph(INF);
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
int u = 0, w = 0; int u = 0, w = 0;
std::cin >> u >> w; std::cin >> u >> w;

View File

@ -3,9 +3,8 @@
*/ */
#include <iostream> #include <iostream>
#include <vector>
#include <stack> #include <stack>
#include <vector>
/** /**
* Iterative function/method to print graph: * Iterative function/method to print graph:
@ -13,7 +12,7 @@
* @param V number of vertices * @param V number of vertices
* @return void * @return void
**/ **/
void print(const std::vector< std::vector<int> > &a, int V) { void print(const std::vector<std::vector<int> > &a, int V) {
for (int i = 0; i < V; i++) { for (int i = 0; i < V; i++) {
if (!a[i].empty()) { if (!a[i].empty()) {
std::cout << "i=" << i << "-->"; std::cout << "i=" << i << "-->";
@ -35,7 +34,8 @@ void print(const std::vector< std::vector<int> > &a, int V) {
* @param adj adjacency list representation of the graph * @param adj adjacency list representation of the graph
* @return void * @return void
**/ **/
void push_vertex(int v, std::stack<int> *st, std::vector<bool> *vis, const std::vector< std::vector<int> > &adj) { void push_vertex(int v, std::stack<int> *st, std::vector<bool> *vis,
const std::vector<std::vector<int> > &adj) {
(*vis)[v] = true; (*vis)[v] = true;
for (auto i = adj[v].begin(); i != adj[v].end(); i++) { for (auto i = adj[v].begin(); i != adj[v].end(); i++) {
if ((*vis)[*i] == false) { if ((*vis)[*i] == false) {
@ -52,7 +52,8 @@ void push_vertex(int v, std::stack<int> *st, std::vector<bool> *vis, const std::
* @param grev graph with reversed edges * @param grev graph with reversed edges
* @return void * @return void
**/ **/
void dfs(int v, std::vector<bool> *vis, const std::vector< std::vector<int> > &grev) { void dfs(int v, std::vector<bool> *vis,
const std::vector<std::vector<int> > &grev) {
(*vis)[v] = true; (*vis)[v] = true;
// cout<<v<<" "; // cout<<v<<" ";
for (auto i = grev[v].begin(); i != grev[v].end(); i++) { 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
count of (number of) strongly connected components (SCCs) in the graph. count of (number of) strongly connected components (SCCs) in the graph.
(variable 'count_scc' within function) (variable 'count_scc' within function)
**/ **/
int kosaraju(int V, const std::vector< std::vector<int> > &adj) { int kosaraju(int V, const std::vector<std::vector<int> > &adj) {
std::vector<bool> vis(V, false); std::vector<bool> vis(V, false);
std::stack<int> st; std::stack<int> st;
for (int v = 0; v < V; v++) { for (int v = 0; v < V; v++) {
@ -81,7 +82,7 @@ int kosaraju(int V, const std::vector< std::vector<int> > &adj) {
} }
} }
// making new graph (grev) with reverse edges as in adj[]: // making new graph (grev) with reverse edges as in adj[]:
std::vector< std::vector<int> > grev(V); std::vector<std::vector<int> > grev(V);
for (int i = 0; i < V + 1; i++) { for (int i = 0; i < V + 1; i++) {
for (auto j = adj[i].begin(); j != adj[i].end(); j++) { for (auto j = adj[i].begin(); j != adj[i].end(); j++) {
grev[*j].push_back(i); grev[*j].push_back(i);
@ -114,7 +115,7 @@ int main() {
int a = 0, b = 0; // a->number of nodes, b->directed edges. int a = 0, b = 0; // a->number of nodes, b->directed edges.
std::cin >> a >> b; std::cin >> a >> b;
int m = 0, n = 0; int m = 0, n = 0;
std::vector< std::vector<int> > adj(a + 1); std::vector<std::vector<int> > adj(a + 1);
for (int i = 0; i < b; i++) // take total b inputs of 2 vertices each for (int i = 0; i < b; i++) // take total b inputs of 2 vertices each
// required to form an edge. // required to form an edge.
{ {

View File

@ -1,7 +1,7 @@
#include <iostream>
#include <vector>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <iostream>
#include <vector>
//#include <boost/multiprecision/cpp_int.hpp> //#include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision; // using namespace boost::multiprecision;
const int mx = 1e6 + 5; const int mx = 1e6 + 5;
@ -12,7 +12,7 @@ ll node, edge;
std::vector<std::pair<ll, std::pair<ll, ll>>> edges; std::vector<std::pair<ll, std::pair<ll, ll>>> edges;
void initial() { void initial() {
for (int i = 0; i < node + edge; ++i) { for (int i = 0; i < node + edge; ++i) {
parent[i] = i; parent[i] = i;
} }
} }

View File

@ -9,7 +9,8 @@
* Algorithm: https://cp-algorithms.com/graph/lca_binary_lifting.html * Algorithm: https://cp-algorithms.com/graph/lca_binary_lifting.html
* *
* Complexity: * 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$ * - Query: \f$O(\log N)\f$
* - Space: \f$O(N \log N)\f$ * - Space: \f$O(N \log N)\f$
* *
@ -34,11 +35,11 @@
* lowest_common_ancestor(x, y) = lowest_common_ancestor(y, x) * lowest_common_ancestor(x, y) = lowest_common_ancestor(y, x)
*/ */
#include <cassert>
#include <iostream> #include <iostream>
#include <queue>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <queue>
#include <cassert>
/** /**
* \namespace graph * \namespace graph
@ -50,7 +51,7 @@ namespace graph {
* Its vertices are indexed 0, 1, ..., N - 1. * Its vertices are indexed 0, 1, ..., N - 1.
*/ */
class Graph { class Graph {
public: public:
/** /**
* \brief Populate the adjacency list for each vertex in the graph. * \brief Populate the adjacency list for each vertex in the graph.
* Assumes that evey edge is a pair of valid vertex indices. * 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 N number of vertices in the graph
* @param undirected_edges list of graph's undirected edges * @param undirected_edges list of graph's undirected edges
*/ */
Graph(size_t N, const std::vector< std::pair<int, int> > &undirected_edges) { Graph(size_t N, const std::vector<std::pair<int, int> > &undirected_edges) {
neighbors.resize(N); neighbors.resize(N);
for (auto &edge : undirected_edges) { for (auto &edge : undirected_edges) {
neighbors[edge.first].push_back(edge.second); neighbors[edge.first].push_back(edge.second);
@ -70,19 +71,18 @@ class Graph {
* Function to get the number of vertices in the graph * Function to get the number of vertices in the graph
* @return the number of vertices in the graph. * @return the number of vertices in the graph.
*/ */
int number_of_vertices() const { int number_of_vertices() const { return neighbors.size(); }
return neighbors.size();
}
/** \brief for each vertex it stores a list indicies of its neighbors */ /** \brief for each vertex it stores a list indicies of its neighbors */
std::vector< std::vector<int> > neighbors; std::vector<std::vector<int> > 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 { class RootedTree : public Graph {
public: public:
/** /**
* \brief Constructs the tree by calculating parent for every vertex. * \brief Constructs the tree by calculating parent for every vertex.
* Assumes a valid description of a tree is provided. * 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 undirected_edges list of graph's undirected edges
* @param root_ index of the root vertex * @param root_ index of the root vertex
*/ */
RootedTree(const std::vector< std::pair<int, int> > &undirected_edges, int root_) RootedTree(const std::vector<std::pair<int, int> > &undirected_edges,
int root_)
: Graph(undirected_edges.size() + 1, undirected_edges), root(root_) { : Graph(undirected_edges.size() + 1, undirected_edges), root(root_) {
populate_parents(); populate_parents();
} }
@ -106,7 +107,7 @@ class RootedTree : public Graph {
/** \brief Index of the root vertex. */ /** \brief Index of the root vertex. */
int root; int root;
protected: protected:
/** /**
* \brief Calculate the parents for all the vertices in the tree. * \brief Calculate the parents for all the vertices in the tree.
* Implements the breadth first search algorithm starting from the root * 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. * queries of the lowest common ancestor of two given vertices in the tree.
*/ */
class LowestCommonAncestor { class LowestCommonAncestor {
public: public:
/** /**
* \brief Stores the tree and precomputs "up lifts". * \brief Stores the tree and precomputs "up lifts".
* @param tree_ rooted tree. * @param tree_ rooted tree.
*/ */
explicit LowestCommonAncestor(const RootedTree& tree_) : tree(tree_) { explicit LowestCommonAncestor(const RootedTree &tree_) : tree(tree_) {
populate_up(); populate_up();
} }
/** /**
@ -196,16 +196,16 @@ class LowestCommonAncestor {
} }
/* \brief reference to the rooted tree this structure allows to query */ /* \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 * \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 * 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 index of its parent. The i-th element of the list is an index of
* the (2^i)-th ancestor of the vertex. * the (2^i)-th ancestor of the vertex.
*/ */
std::vector< std::vector<int> > up; std::vector<std::vector<int> > up;
protected: protected:
/** /**
* Populate the "up" structure. See above. * Populate the "up" structure. See above.
*/ */
@ -241,9 +241,8 @@ static void tests() {
* | * |
* 9 * 9
*/ */
std::vector< std::pair<int, int> > edges = { std::vector<std::pair<int, int> > edges = {
{7, 1}, {1, 5}, {1, 3}, {3, 6}, {6, 2}, {2, 9}, {6, 8}, {4, 3}, {0, 4} {7, 1}, {1, 5}, {1, 3}, {3, 6}, {6, 2}, {2, 9}, {6, 8}, {4, 3}, {0, 4}};
};
graph::RootedTree t(edges, 3); graph::RootedTree t(edges, 3);
graph::LowestCommonAncestor lca(t); graph::LowestCommonAncestor lca(t);
assert(lca.lowest_common_ancestor(7, 4) == 3); assert(lca.lowest_common_ancestor(7, 4) == 3);

View File

@ -16,7 +16,7 @@
// std::max capacity of node in graph // std::max capacity of node in graph
const int MAXN = 505; const int MAXN = 505;
class Graph { class Graph {
std::vector< std::vector<int> > residual_capacity, capacity; std::vector<std::vector<int> > residual_capacity, capacity;
int total_nodes = 0; int total_nodes = 0;
int total_edges = 0, source = 0, sink = 0; int total_edges = 0, source = 0, sink = 0;
std::vector<int> parent; std::vector<int> parent;
@ -50,8 +50,10 @@ class Graph {
void set_graph() { void set_graph() {
std::cin >> total_nodes >> total_edges >> source >> sink; std::cin >> total_nodes >> total_edges >> source >> sink;
parent = std::vector<int>(total_nodes, -1); parent = std::vector<int>(total_nodes, -1);
capacity = residual_capacity = std::vector< std::vector<int> >(total_nodes, std::vector<int>(total_nodes)); capacity = residual_capacity = std::vector<std::vector<int> >(
for (int start = 0, destination = 0, capacity_ = 0, i = 0; i < total_edges; ++i) { total_nodes, std::vector<int>(total_nodes));
for (int start = 0, destination = 0, capacity_ = 0, i = 0;
i < total_edges; ++i) {
std::cin >> start >> destination >> capacity_; std::cin >> start >> destination >> capacity_;
residual_capacity[start][destination] = capacity_; residual_capacity[start][destination] = capacity_;
capacity[start][destination] = capacity_; capacity[start][destination] = capacity_;

View File

@ -5,7 +5,7 @@
using PII = std::pair<int, int>; using PII = std::pair<int, int>;
int prim(int x, const std::vector< std::vector<PII> > &graph) { int prim(int x, const std::vector<std::vector<PII> > &graph) {
// priority queue to maintain edges with respect to weights // priority queue to maintain edges with respect to weights
std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Q; std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Q;
std::vector<bool> marked(graph.size(), false); std::vector<bool> marked(graph.size(), false);
@ -40,7 +40,7 @@ int main() {
return 0; return 0;
} }
std::vector< std::vector<PII> > graph(nodes); std::vector<std::vector<PII> > graph(nodes);
// Edges with their nodes & weight // Edges with their nodes & weight
for (int i = 0; i < edges; ++i) { for (int i = 0; i < edges; ++i) {

View File

@ -2,7 +2,8 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
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<std::vector<int>> graph; std::vector<std::vector<int>> graph;
std::vector<bool> visited; std::vector<bool> visited;
std::vector<int> topological_order; std::vector<int> topological_order;
@ -28,7 +29,8 @@ void topological_sort() {
reverse(topological_order.begin(), topological_order.end()); reverse(topological_order.begin(), topological_order.end());
} }
int main() { 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; std::cin >> number_of_vertices >> number_of_edges;
int x = 0, y = 0; int x = 0, y = 0;
graph.resize(number_of_vertices, std::vector<int>()); graph.resize(number_of_vertices, std::vector<int>());
@ -41,7 +43,7 @@ int main() {
std::cout << "Topological Order : \n"; std::cout << "Topological Order : \n";
for (int v : topological_order) { for (int v : topological_order) {
std::cout << v + 1 std::cout << v + 1
<< ' '; // converting zero based indexing back to one based. << ' '; // converting zero based indexing back to one based.
} }
std::cout << '\n'; std::cout << '\n';
return 0; return 0;

View File

@ -4,7 +4,7 @@
#include <queue> #include <queue>
#include <vector> #include <vector>
std::vector<int> topoSortKahn(int N, const std::vector< std::vector<int> > &adj); std::vector<int> topoSortKahn(int N, const std::vector<std::vector<int> > &adj);
int main() { int main() {
int nodes = 0, edges = 0; int nodes = 0, edges = 0;
@ -14,7 +14,7 @@ int main() {
} }
int u = 0, v = 0; int u = 0, v = 0;
std::vector< std::vector<int> > graph(nodes); std::vector<std::vector<int> > graph(nodes);
// create graph // create graph
// example // example
// 6 6 // 6 6
@ -32,7 +32,8 @@ int main() {
} }
} }
std::vector<int> topoSortKahn(int V, const std::vector< std::vector<int> > &adj) { std::vector<int> topoSortKahn(int V,
const std::vector<std::vector<int> > &adj) {
std::vector<bool> vis(V + 1, false); std::vector<bool> vis(V + 1, false);
std::vector<int> deg(V + 1, 0); std::vector<int> deg(V + 1, 0);
for (int i = 0; i < V; i++) { for (int i = 0; i < V; i++) {