diff --git a/graph/connected_components.cpp b/graph/connected_components.cpp index bfb92c7c8..06bb1ee50 100644 --- a/graph/connected_components.cpp +++ b/graph/connected_components.cpp @@ -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> *adj, int u, int v) { (*adj)[u - 1].push_back(v - 1); @@ -51,14 +52,15 @@ void addEdge(std::vector> *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> *adj, int u, - std::vector &visited) { - visited[u] = true; + std::vector *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> *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> *adj) { int n = adj->size(); @@ -75,7 +81,7 @@ int getConnectedComponents(const std::vector> *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> adj(vertices, std::vector()); + int u = int(), v = int(); while (edges--) { - int u, v; std::cin >> u >> v; graph::addEdge(&adj, u, v); } diff --git a/graph/dijkstra.cpp b/graph/dijkstra.cpp index 620c0215c..90fb0ec4f 100644 --- a/graph/dijkstra.cpp +++ b/graph/dijkstra.cpp @@ -29,8 +29,9 @@ #include #include #include +#include -constexpr long long INF = std::numeric_limits::max(); +constexpr int64_t INF = std::numeric_limits::max(); /** * @namespace graph @@ -67,13 +68,13 @@ int dijkstra(std::vector>> *adj, int s, int t) { int n = adj->size(); /// setting all the distances initially to INF - std::vector dist(n, INF); + std::vector 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::vector>, - std::greater>> + 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>> adj( vertices, std::vector>()); - 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) {