mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix: clang-format for graph/ (#1056)
* fix: clang-format for graph/ * remove graph.h
This commit is contained in:
parent
3239fcc19e
commit
79fb528dad
@ -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.
|
||||
|
@ -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 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,
|
||||
int start) {
|
||||
std::vector<bool> breadth_first_search(
|
||||
const std::vector<std::vector<int>> &graph, int start) {
|
||||
/// vector to keep track of visited vertices
|
||||
std::vector<bool> visited(graph.size(), false);
|
||||
/// a queue that stores vertices that need to be further explored
|
||||
|
@ -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<std::vector<int>> search_bridges(
|
||||
int n,
|
||||
const std::vector<std::vector<int>>& connections) {
|
||||
int n, const std::vector<std::vector<int>>& 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<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";
|
||||
for (auto& itr : bridges) {
|
||||
std::cout << itr[0] << " --> " << itr[1] << '\n';
|
||||
|
@ -1,14 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
|
||||
constexpr int WHITE = 0;
|
||||
constexpr int GREY = 1;
|
||||
constexpr int BLACK = 2;
|
||||
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);
|
||||
checked[start] = GREY;
|
||||
std::stack<int> stack;
|
||||
@ -33,7 +33,7 @@ void dfs(const std::vector< std::list<int> > &graph, int start) {
|
||||
int main() {
|
||||
int n = 0;
|
||||
std::cin >> n;
|
||||
std::vector< std::list<int> > graph(INF);
|
||||
std::vector<std::list<int> > graph(INF);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int u = 0, w = 0;
|
||||
std::cin >> u >> w;
|
||||
|
@ -3,9 +3,8 @@
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* Iterative function/method to print graph:
|
||||
@ -13,7 +12,7 @@
|
||||
* @param V number of vertices
|
||||
* @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++) {
|
||||
if (!a[i].empty()) {
|
||||
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
|
||||
* @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;
|
||||
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<int> *st, std::vector<bool> *vis, const std::
|
||||
* @param grev graph with reversed edges
|
||||
* @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;
|
||||
// cout<<v<<" ";
|
||||
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.
|
||||
(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::stack<int> st;
|
||||
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[]:
|
||||
std::vector< std::vector<int> > grev(V);
|
||||
std::vector<std::vector<int> > 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<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
|
||||
// required to form an edge.
|
||||
{
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
//#include <boost/multiprecision/cpp_int.hpp>
|
||||
// using namespace boost::multiprecision;
|
||||
const int mx = 1e6 + 5;
|
||||
|
@ -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 <cassert>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <cassert>
|
||||
|
||||
/**
|
||||
* \namespace graph
|
||||
@ -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<int, int> > &undirected_edges) {
|
||||
Graph(size_t N, const std::vector<std::pair<int, int> > &undirected_edges) {
|
||||
neighbors.resize(N);
|
||||
for (auto &edge : undirected_edges) {
|
||||
neighbors[edge.first].push_back(edge.second);
|
||||
@ -70,16 +71,15 @@ 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<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 {
|
||||
public:
|
||||
@ -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<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_) {
|
||||
populate_parents();
|
||||
}
|
||||
@ -135,7 +136,6 @@ class RootedTree : public Graph {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
@ -148,7 +148,7 @@ class LowestCommonAncestor {
|
||||
* \brief Stores the tree and precomputs "up lifts".
|
||||
* @param tree_ rooted tree.
|
||||
*/
|
||||
explicit LowestCommonAncestor(const RootedTree& tree_) : tree(tree_) {
|
||||
explicit LowestCommonAncestor(const RootedTree &tree_) : tree(tree_) {
|
||||
populate_up();
|
||||
}
|
||||
|
||||
@ -196,14 +196,14 @@ 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<int> > up;
|
||||
std::vector<std::vector<int> > up;
|
||||
|
||||
protected:
|
||||
/**
|
||||
@ -241,9 +241,8 @@ static void tests() {
|
||||
* |
|
||||
* 9
|
||||
*/
|
||||
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}
|
||||
};
|
||||
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}};
|
||||
graph::RootedTree t(edges, 3);
|
||||
graph::LowestCommonAncestor lca(t);
|
||||
assert(lca.lowest_common_ancestor(7, 4) == 3);
|
||||
|
@ -16,7 +16,7 @@
|
||||
// std::max capacity of node in graph
|
||||
const int MAXN = 505;
|
||||
class Graph {
|
||||
std::vector< std::vector<int> > residual_capacity, capacity;
|
||||
std::vector<std::vector<int> > residual_capacity, capacity;
|
||||
int total_nodes = 0;
|
||||
int total_edges = 0, source = 0, sink = 0;
|
||||
std::vector<int> parent;
|
||||
@ -50,8 +50,10 @@ class Graph {
|
||||
void set_graph() {
|
||||
std::cin >> total_nodes >> total_edges >> source >> sink;
|
||||
parent = std::vector<int>(total_nodes, -1);
|
||||
capacity = residual_capacity = std::vector< std::vector<int> >(total_nodes, std::vector<int>(total_nodes));
|
||||
for (int start = 0, destination = 0, capacity_ = 0, i = 0; i < total_edges; ++i) {
|
||||
capacity = residual_capacity = std::vector<std::vector<int> >(
|
||||
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_;
|
||||
residual_capacity[start][destination] = capacity_;
|
||||
capacity[start][destination] = capacity_;
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
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
|
||||
std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Q;
|
||||
std::vector<bool> marked(graph.size(), false);
|
||||
@ -40,7 +40,7 @@ int main() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector< std::vector<PII> > graph(nodes);
|
||||
std::vector<std::vector<PII> > graph(nodes);
|
||||
|
||||
// Edges with their nodes & weight
|
||||
for (int i = 0; i < edges; ++i) {
|
||||
|
@ -2,7 +2,8 @@
|
||||
#include <iostream>
|
||||
#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<bool> visited;
|
||||
std::vector<int> 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<int>());
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <queue>
|
||||
#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 nodes = 0, edges = 0;
|
||||
@ -14,7 +14,7 @@ int main() {
|
||||
}
|
||||
int u = 0, v = 0;
|
||||
|
||||
std::vector< std::vector<int> > graph(nodes);
|
||||
std::vector<std::vector<int> > graph(nodes);
|
||||
// create graph
|
||||
// example
|
||||
// 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<int> deg(V + 1, 0);
|
||||
for (int i = 0; i < V; i++) {
|
||||
|
Loading…
Reference in New Issue
Block a user