fix: clang-tidy errors

This commit is contained in:
Ayaan Khan
2020-07-17 22:40:58 +05:30
parent 483e00c67e
commit 0023339182
2 changed files with 23 additions and 16 deletions

View File

@@ -39,8 +39,9 @@ namespace graph {
/**
* @brief Function that add edge between two nodes or vertices of graph
*
* @param u any node or vertex of graph
* @param v any node or vertex of graph
* @param adj adjacency list of graph.
* @param u any node or vertex of graph.
* @param v any node or vertex of graph.
*/
void addEdge(std::vector<std::vector<int>> *adj, int u, int v) {
(*adj)[u - 1].push_back(v - 1);
@@ -51,14 +52,15 @@ void addEdge(std::vector<std::vector<int>> *adj, int u, int v) {
* @brief Utility function for depth first seach algorithm
* this function explores the vertex which is passed into.
*
* @param u vertex or node to be explored
* @param visited already visited vertices
* @param adj adjacency list of graph.
* @param u vertex or node to be explored.
* @param visited already visited vertices.
*/
void explore(const std::vector<std::vector<int>> *adj, int u,
std::vector<bool> &visited) {
visited[u] = true;
std::vector<bool> *visited) {
(*visited)[u] = true;
for (auto v : (*adj)[u]) {
if (!visited[v]) {
if (!(*visited)[v]) {
explore(adj, v, visited);
}
}
@@ -67,6 +69,10 @@ void explore(const std::vector<std::vector<int>> *adj, int u,
/**
* @brief Function that perfoms depth first search algorithm on graph
* and calculated the number of connected components.
*
* @param adj adjacency list of graph.
*
* @return connected_components number of connected components in graph.
*/
int getConnectedComponents(const std::vector<std::vector<int>> *adj) {
int n = adj->size();
@@ -75,7 +81,7 @@ int getConnectedComponents(const std::vector<std::vector<int>> *adj) {
for (int i = 0; i < n; i++) {
if (!visited[i]) {
explore(adj, i, visited);
explore(adj, i, &visited);
connected_components++;
}
}
@@ -122,7 +128,7 @@ int main() {
/// running predefined tests
tests();
int vertices, edges;
int vertices = int(), edges = int();
std::cout << "Enter the number of vertices : ";
std::cin >> vertices;
std::cout << "Enter the number of edges : ";
@@ -130,8 +136,8 @@ int main() {
std::vector<std::vector<int>> adj(vertices, std::vector<int>());
int u = int(), v = int();
while (edges--) {
int u, v;
std::cin >> u >> v;
graph::addEdge(&adj, u, v);
}

View File

@@ -29,8 +29,9 @@
#include <queue>
#include <utility>
#include <vector>
#include <memory>
constexpr long long INF = std::numeric_limits<long long>::max();
constexpr int64_t INF = std::numeric_limits<int64_t>::max();
/**
* @namespace graph
@@ -67,13 +68,13 @@ int dijkstra(std::vector<std::vector<std::pair<int, int>>> *adj, int s, int t) {
int n = adj->size();
/// setting all the distances initially to INF
std::vector<long long> dist(n, INF);
std::vector<int64_t> dist(n, INF);
/// creating a min heap using priority queue
/// first element of pair contains the distance
/// second element of pair contains the vertex
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>,
std::greater<std::pair<int, int>>>
std::greater<>>
pq;
/// pushing the source vertex 's' with 0 distance in min heap
@@ -152,7 +153,7 @@ int main() {
// running predefined tests
tests();
int vertices, edges;
int vertices = int(), edges = int();
std::cout << "Enter the number of vertices : ";
std::cin >> vertices;
std::cout << "Enter the number of edges : ";
@@ -161,13 +162,13 @@ int main() {
std::vector<std::vector<std::pair<int, int>>> adj(
vertices, std::vector<std::pair<int, int>>());
int u, v, w;
int u = int(), v = int(), w = int();
while (edges--) {
std::cin >> u >> v >> w;
graph::addEdge(&adj, u, v, w);
}
int s, t;
int s = int(), t = int();
std::cin >> s >> t;
int dist = graph::dijkstra(&adj, s - 1, t - 1);
if (dist == -1) {