chore: use iwyu on graph/**.cpp

This commit is contained in:
realstealthninja
2024-09-13 15:38:23 +05:30
parent 740bd65932
commit dabd6d2591
16 changed files with 167 additions and 154 deletions

View File

@@ -2,7 +2,8 @@
*
* @file
* @brief [Depth First Search Algorithm using Stack
* (Depth First Search Algorithm)](https://en.wikipedia.org/wiki/Depth-first_search)
* (Depth First Search
* Algorithm)](https://en.wikipedia.org/wiki/Depth-first_search)
*
* @author [Ayaan Khan](http://github.com/ayaankhan98)
* @author [Saurav Uppoor](https://github.com/sauravUppoor)
@@ -24,23 +25,26 @@
* <h4>Working</h4>
* 1. Mark all vertices as unvisited (colour it WHITE).
* 2. Push starting vertex into the stack and colour it GREY.
* 3. Once a node is popped out of the stack and is coloured GREY, we colour it BLACK.
* 3. Once a node is popped out of the stack and is coloured GREY, we colour it
* BLACK.
* 4. Push all its neighbours which are not coloured BLACK.
* 5. Repeat steps 4 and 5 until the stack is empty.
*/
#include <iostream> /// for IO operations
#include <stack> /// header for std::stack
#include <vector> /// header for std::vector
#include <cassert> /// header for preprocessor macro assert()
#include <limits> /// header for limits of integral types
#include <stdint.h> // for int16_t, int64_t
constexpr int WHITE = 0; /// indicates the node hasn't been explored
constexpr int GREY = 1; /// indicates node is in stack waiting to be explored
constexpr int BLACK = 2; /// indicates node has already been explored
#include <cassert> // for assert
#include <cstddef> // for size_t
#include <iostream> // for basic_ostream, operator<<, char_traits, cout, endl
#include <limits> // for numeric_limits
#include <stack> // for stack
#include <vector> // for vector, operator==
constexpr int WHITE = 0; /// indicates the node hasn't been explored
constexpr int GREY = 1; /// indicates node is in stack waiting to be explored
constexpr int BLACK = 2; /// indicates node has already been explored
constexpr int64_t INF = std::numeric_limits<int16_t>::max();
/**
* @namespace graph
* @brief Graph algorithms
@@ -48,7 +52,8 @@ constexpr int64_t INF = std::numeric_limits<int16_t>::max();
namespace graph {
/**
* @namespace depth_first_search
* @brief Functions for [Depth First Search](https://en.wikipedia.org/wiki/Depth-first_search) algorithm
* @brief Functions for [Depth First
* Search](https://en.wikipedia.org/wiki/Depth-first_search) algorithm
*/
namespace depth_first_search {
/**
@@ -62,14 +67,14 @@ namespace depth_first_search {
*
*/
void addEdge(std::vector<std::vector<size_t>> *adj, size_t u, size_t v) {
/*
*
* Here we are considering undirected graph that's the
* reason we are adding v to the adjacency list representation of u
* and also adding u to the adjacency list representation of v
*
*/
(*adj)[u - 1].push_back(v - 1);
/*
*
* Here we are considering undirected graph that's the
* reason we are adding v to the adjacency list representation of u
* and also adding u to the adjacency list representation of v
*
*/
(*adj)[u - 1].push_back(v - 1);
}
/**
@@ -84,7 +89,8 @@ void addEdge(std::vector<std::vector<size_t>> *adj, size_t u, size_t v) {
* @return vector with nodes stored in the order of DFS traversal
*
*/
std::vector<size_t> dfs(const std::vector<std::vector<size_t> > &graph, size_t start) {
std::vector<size_t> dfs(const std::vector<std::vector<size_t>> &graph,
size_t start) {
/// checked[i] stores the status of each node
std::vector<size_t> checked(graph.size(), WHITE), traversed_path;
@@ -121,49 +127,51 @@ std::vector<size_t> dfs(const std::vector<std::vector<size_t> > &graph, size_t s
* @returns none
*/
static void tests() {
size_t start_pos;
size_t start_pos;
/// Test 1
std::cout << "Case 1: " << std::endl;
start_pos = 1;
std::vector<std::vector<size_t> > g1(3, std::vector<size_t>());
/// Test 1
std::cout << "Case 1: " << std::endl;
start_pos = 1;
std::vector<std::vector<size_t>> g1(3, std::vector<size_t>());
graph::depth_first_search::addEdge(&g1, 1, 2);
graph::depth_first_search::addEdge(&g1, 2, 3);
graph::depth_first_search::addEdge(&g1, 3, 1);
graph::depth_first_search::addEdge(&g1, 1, 2);
graph::depth_first_search::addEdge(&g1, 2, 3);
graph::depth_first_search::addEdge(&g1, 3, 1);
std::vector<size_t> expected1 {1, 2, 3}; /// for the above sample data, this is the expected output
assert(graph::depth_first_search::dfs(g1, start_pos - 1) == expected1);
std::cout << "Passed" << std::endl;
std::vector<size_t> expected1{
1, 2, 3}; /// for the above sample data, this is the expected output
assert(graph::depth_first_search::dfs(g1, start_pos - 1) == expected1);
std::cout << "Passed" << std::endl;
/// Test 2
std::cout << "Case 2: " << std::endl;
start_pos = 1;
std::vector<std::vector<size_t> > g2(4, std::vector<size_t>());
/// Test 2
std::cout << "Case 2: " << std::endl;
start_pos = 1;
std::vector<std::vector<size_t>> g2(4, std::vector<size_t>());
graph::depth_first_search::addEdge(&g2, 1, 2);
graph::depth_first_search::addEdge(&g2, 1, 3);
graph::depth_first_search::addEdge(&g2, 2, 4);
graph::depth_first_search::addEdge(&g2, 4, 1);
graph::depth_first_search::addEdge(&g2, 1, 2);
graph::depth_first_search::addEdge(&g2, 1, 3);
graph::depth_first_search::addEdge(&g2, 2, 4);
graph::depth_first_search::addEdge(&g2, 4, 1);
std::vector<size_t> expected2 {1, 3, 2, 4}; /// for the above sample data, this is the expected output
assert(graph::depth_first_search::dfs(g2, start_pos - 1) == expected2);
std::cout << "Passed" << std::endl;
std::vector<size_t> expected2{
1, 3, 2, 4}; /// for the above sample data, this is the expected output
assert(graph::depth_first_search::dfs(g2, start_pos - 1) == expected2);
std::cout << "Passed" << std::endl;
/// Test 3
std::cout << "Case 3: " << std::endl;
start_pos = 2;
std::vector<std::vector<size_t> > g3(4, std::vector<size_t>());
/// Test 3
std::cout << "Case 3: " << std::endl;
start_pos = 2;
std::vector<std::vector<size_t>> g3(4, std::vector<size_t>());
graph::depth_first_search::addEdge(&g3, 1, 2);
graph::depth_first_search::addEdge(&g3, 1, 3);
graph::depth_first_search::addEdge(&g3, 2, 4);
graph::depth_first_search::addEdge(&g3, 4, 1);
std::vector<size_t> expected3 {2, 4, 1, 3}; /// for the above sample data, this is the expected output
assert(graph::depth_first_search::dfs(g3, start_pos - 1) == expected3);
std::cout << "Passed" << std::endl;
graph::depth_first_search::addEdge(&g3, 1, 2);
graph::depth_first_search::addEdge(&g3, 1, 3);
graph::depth_first_search::addEdge(&g3, 2, 4);
graph::depth_first_search::addEdge(&g3, 4, 1);
std::vector<size_t> expected3{
2, 4, 1, 3}; /// for the above sample data, this is the expected output
assert(graph::depth_first_search::dfs(g3, start_pos - 1) == expected3);
std::cout << "Passed" << std::endl;
}
/**
@@ -174,34 +182,35 @@ int main() {
tests(); // execute the tests
size_t vertices = 0, edges = 0, start_pos = 1;
std::vector<size_t> traversal;
std::vector<size_t> traversal;
std::cout << "Enter the Vertices : ";
std::cin >> vertices;
std::cout << "Enter the Edges : ";
std::cin >> edges;
std::cin >> vertices;
std::cout << "Enter the Edges : ";
std::cin >> edges;
/// creating a graph
std::vector<std::vector<size_t> > adj(vertices, std::vector<size_t>());
std::vector<std::vector<size_t>> adj(vertices, std::vector<size_t>());
/// taking input for the edges
std::cout << "Enter the vertices which have edges between them : " << std::endl;
while (edges--) {
size_t u = 0, v = 0;
std::cin >> u >> v;
graph::depth_first_search::addEdge(&adj, u, v);
}
std::cout << "Enter the vertices which have edges between them : "
<< std::endl;
while (edges--) {
size_t u = 0, v = 0;
std::cin >> u >> v;
graph::depth_first_search::addEdge(&adj, u, v);
}
/// taking input for the starting position
std::cout << "Enter the starting vertex [1,n]: " << std::endl;
std::cin >> start_pos;
start_pos -= 1;
traversal = graph::depth_first_search::dfs(adj, start_pos);
std::cin >> start_pos;
start_pos -= 1;
traversal = graph::depth_first_search::dfs(adj, start_pos);
/// Printing the order of traversal
for (auto x : traversal) {
std::cout << x << ' ';
}
std::cout << x << ' ';
}
return 0;
}