Skip to content

Commit dabd6d2

Browse files
chore: use iwyu on graph/**.cpp
1 parent 740bd65 commit dabd6d2

16 files changed

+176
-163
lines changed

graph/bidirectional_dijkstra.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
* https://www.youtube.com/watch?v=DINCL5cd_w0&t=24s
1414
*/
1515

16-
#include <cassert> /// for assert
17-
#include <cstdint> /// for integral typedefs
18-
#include <iostream> /// for io operations
19-
#include <limits> /// for variable INF
20-
#include <queue> /// for the priority_queue of distances
21-
#include <utility> /// for make_pair function
22-
#include <vector> /// for store the graph, the distances, and the path
16+
#include <cassert> // for assert
17+
#include <cstdint> // for uint64_t, int64_t
18+
#include <iostream> // for basic_ostream, operator<<, char_traits, cout
19+
#include <limits> // for numeric_limits
20+
#include <queue> // for priority_queue
21+
#include <utility> // for pair, make_pair
22+
#include <vector> // for vector
23+
#include <functional> // for greater
2324

2425
constexpr int64_t INF = std::numeric_limits<int64_t>::max();
2526

graph/breadth_first_search.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
* push that element into the queue and mark this as visited
4646
*
4747
*/
48-
#include <algorithm>
49-
#include <cassert>
50-
#include <iostream>
51-
#include <list>
52-
#include <map>
53-
#include <queue>
54-
#include <string>
55-
#include <vector>
48+
#include <cassert> // for assert
49+
#include <cstddef> // for size_t
50+
#include <functional> // for less
51+
#include <iostream> // for basic_ostream, operator<<, cout, basic_istream...
52+
#include <list> // for list
53+
#include <map> // for map, operator==
54+
#include <queue> // for queue
55+
#include <string> // for basic_string, operator<, char_traits, allocator
5656

5757
/**
5858
* \namespace graph

graph/connected_components.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@
2525
*
2626
*/
2727

28-
#include <algorithm>
29-
#include <cassert>
30-
#include <iostream>
31-
#include <vector>
28+
#include <cassert> // for assert
29+
#include <iostream> // for basic_ostream, char_traits, operator<<, basic_is...
30+
#include <vector> // for vector
3231

3332
/**
3433
* @namespace graph

graph/connected_components_with_dsu.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
* @author Unknown author
1818
* @author [Sagar Pandya](https://github.com/sagarpandyansit)
1919
*/
20-
#include <cstdint> /// for integer typedefs
21-
#include <iostream> /// for IO operations
22-
#include <set> /// for std::set
23-
#include <vector> /// for std::vector
20+
#include <cstdint> // for int64_t, uint32_t
21+
#include <iostream> // for char_traits, basic_istream, cin, basic_ostream
22+
#include <set> // for set
23+
#include <vector> // for vector
24+
#include <utility> // for swap
2425

2526
/**
2627
* @namespace graph

graph/cycle_check_directed_graph.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
*
88
*/
99

10-
#include <cstdint> /// for integral typedefs
11-
#include <iostream> // for std::cout
12-
#include <map> // for std::map
13-
#include <queue> // for std::queue
14-
#include <stdexcept> // for throwing errors
15-
#include <type_traits> // for std::remove_reference
16-
#include <utility> // for std::move
17-
#include <vector> // for std::vector
10+
#include <cstdint> // for uint8_t
11+
#include <iostream> // for char_traits, basic_ostream, operator<<
12+
#include <map> // for map, operator!=, _Rb_tree_iterator
13+
#include <queue> // for queue
14+
#include <stdexcept> // for range_error
15+
#include <type_traits> // for remove_reference
16+
#include <utility> // for move, pair
17+
#include <vector> // for vector
18+
#include <initializer_list> // for initializer_list
1819

1920
/**
2021
* Implementation of non-weighted directed edge of a graph.

graph/depth_first_search.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
*
3333
*/
3434

35-
#include <algorithm>
36-
#include <iostream>
37-
#include <vector>
35+
#include <cstddef> // for size_t
36+
#include <iostream> // for char_traits, basic_ostream, operator<<, cout
37+
#include <vector> // for vector
3838

3939
/**
4040
*

graph/depth_first_search_with_stack.cpp

Lines changed: 89 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
*
33
* @file
44
* @brief [Depth First Search Algorithm using Stack
5-
* (Depth First Search Algorithm)](https://en.wikipedia.org/wiki/Depth-first_search)
5+
* (Depth First Search
6+
* Algorithm)](https://en.wikipedia.org/wiki/Depth-first_search)
67
*
78
* @author [Ayaan Khan](http://github.com/ayaankhan98)
89
* @author [Saurav Uppoor](https://github.com/sauravUppoor)
@@ -24,22 +25,25 @@
2425
* <h4>Working</h4>
2526
* 1. Mark all vertices as unvisited (colour it WHITE).
2627
* 2. Push starting vertex into the stack and colour it GREY.
27-
* 3. Once a node is popped out of the stack and is coloured GREY, we colour it BLACK.
28+
* 3. Once a node is popped out of the stack and is coloured GREY, we colour it
29+
* BLACK.
2830
* 4. Push all its neighbours which are not coloured BLACK.
2931
* 5. Repeat steps 4 and 5 until the stack is empty.
3032
*/
3133

32-
#include <iostream> /// for IO operations
33-
#include <stack> /// header for std::stack
34-
#include <vector> /// header for std::vector
35-
#include <cassert> /// header for preprocessor macro assert()
36-
#include <limits> /// header for limits of integral types
34+
#include <stdint.h> // for int16_t, int64_t
3735

38-
constexpr int WHITE = 0; /// indicates the node hasn't been explored
39-
constexpr int GREY = 1; /// indicates node is in stack waiting to be explored
40-
constexpr int BLACK = 2; /// indicates node has already been explored
41-
constexpr int64_t INF = std::numeric_limits<int16_t>::max();
36+
#include <cassert> // for assert
37+
#include <cstddef> // for size_t
38+
#include <iostream> // for basic_ostream, operator<<, char_traits, cout, endl
39+
#include <limits> // for numeric_limits
40+
#include <stack> // for stack
41+
#include <vector> // for vector, operator==
4242

43+
constexpr int WHITE = 0; /// indicates the node hasn't been explored
44+
constexpr int GREY = 1; /// indicates node is in stack waiting to be explored
45+
constexpr int BLACK = 2; /// indicates node has already been explored
46+
constexpr int64_t INF = std::numeric_limits<int16_t>::max();
4347

4448
/**
4549
* @namespace graph
@@ -48,7 +52,8 @@ constexpr int64_t INF = std::numeric_limits<int16_t>::max();
4852
namespace graph {
4953
/**
5054
* @namespace depth_first_search
51-
* @brief Functions for [Depth First Search](https://en.wikipedia.org/wiki/Depth-first_search) algorithm
55+
* @brief Functions for [Depth First
56+
* Search](https://en.wikipedia.org/wiki/Depth-first_search) algorithm
5257
*/
5358
namespace depth_first_search {
5459
/**
@@ -62,14 +67,14 @@ namespace depth_first_search {
6267
*
6368
*/
6469
void addEdge(std::vector<std::vector<size_t>> *adj, size_t u, size_t v) {
65-
/*
66-
*
67-
* Here we are considering undirected graph that's the
68-
* reason we are adding v to the adjacency list representation of u
69-
* and also adding u to the adjacency list representation of v
70-
*
71-
*/
72-
(*adj)[u - 1].push_back(v - 1);
70+
/*
71+
*
72+
* Here we are considering undirected graph that's the
73+
* reason we are adding v to the adjacency list representation of u
74+
* and also adding u to the adjacency list representation of v
75+
*
76+
*/
77+
(*adj)[u - 1].push_back(v - 1);
7378
}
7479

7580
/**
@@ -84,7 +89,8 @@ void addEdge(std::vector<std::vector<size_t>> *adj, size_t u, size_t v) {
8489
* @return vector with nodes stored in the order of DFS traversal
8590
*
8691
*/
87-
std::vector<size_t> dfs(const std::vector<std::vector<size_t> > &graph, size_t start) {
92+
std::vector<size_t> dfs(const std::vector<std::vector<size_t>> &graph,
93+
size_t start) {
8894
/// checked[i] stores the status of each node
8995
std::vector<size_t> checked(graph.size(), WHITE), traversed_path;
9096

@@ -121,49 +127,51 @@ std::vector<size_t> dfs(const std::vector<std::vector<size_t> > &graph, size_t s
121127
* @returns none
122128
*/
123129
static void tests() {
124-
size_t start_pos;
125-
126-
/// Test 1
127-
std::cout << "Case 1: " << std::endl;
128-
start_pos = 1;
129-
std::vector<std::vector<size_t> > g1(3, std::vector<size_t>());
130-
131-
graph::depth_first_search::addEdge(&g1, 1, 2);
132-
graph::depth_first_search::addEdge(&g1, 2, 3);
133-
graph::depth_first_search::addEdge(&g1, 3, 1);
134-
135-
std::vector<size_t> expected1 {1, 2, 3}; /// for the above sample data, this is the expected output
136-
assert(graph::depth_first_search::dfs(g1, start_pos - 1) == expected1);
137-
std::cout << "Passed" << std::endl;
138-
139-
/// Test 2
140-
std::cout << "Case 2: " << std::endl;
141-
start_pos = 1;
142-
std::vector<std::vector<size_t> > g2(4, std::vector<size_t>());
143-
144-
graph::depth_first_search::addEdge(&g2, 1, 2);
145-
graph::depth_first_search::addEdge(&g2, 1, 3);
146-
graph::depth_first_search::addEdge(&g2, 2, 4);
147-
graph::depth_first_search::addEdge(&g2, 4, 1);
148-
149-
std::vector<size_t> expected2 {1, 3, 2, 4}; /// for the above sample data, this is the expected output
150-
assert(graph::depth_first_search::dfs(g2, start_pos - 1) == expected2);
151-
std::cout << "Passed" << std::endl;
152-
153-
/// Test 3
154-
std::cout << "Case 3: " << std::endl;
155-
start_pos = 2;
156-
std::vector<std::vector<size_t> > g3(4, std::vector<size_t>());
157-
158-
graph::depth_first_search::addEdge(&g3, 1, 2);
159-
graph::depth_first_search::addEdge(&g3, 1, 3);
160-
graph::depth_first_search::addEdge(&g3, 2, 4);
161-
graph::depth_first_search::addEdge(&g3, 4, 1);
162-
163-
std::vector<size_t> expected3 {2, 4, 1, 3}; /// for the above sample data, this is the expected output
164-
assert(graph::depth_first_search::dfs(g3, start_pos - 1) == expected3);
165-
std::cout << "Passed" << std::endl;
166-
130+
size_t start_pos;
131+
132+
/// Test 1
133+
std::cout << "Case 1: " << std::endl;
134+
start_pos = 1;
135+
std::vector<std::vector<size_t>> g1(3, std::vector<size_t>());
136+
137+
graph::depth_first_search::addEdge(&g1, 1, 2);
138+
graph::depth_first_search::addEdge(&g1, 2, 3);
139+
graph::depth_first_search::addEdge(&g1, 3, 1);
140+
141+
std::vector<size_t> expected1{
142+
1, 2, 3}; /// for the above sample data, this is the expected output
143+
assert(graph::depth_first_search::dfs(g1, start_pos - 1) == expected1);
144+
std::cout << "Passed" << std::endl;
145+
146+
/// Test 2
147+
std::cout << "Case 2: " << std::endl;
148+
start_pos = 1;
149+
std::vector<std::vector<size_t>> g2(4, std::vector<size_t>());
150+
151+
graph::depth_first_search::addEdge(&g2, 1, 2);
152+
graph::depth_first_search::addEdge(&g2, 1, 3);
153+
graph::depth_first_search::addEdge(&g2, 2, 4);
154+
graph::depth_first_search::addEdge(&g2, 4, 1);
155+
156+
std::vector<size_t> expected2{
157+
1, 3, 2, 4}; /// for the above sample data, this is the expected output
158+
assert(graph::depth_first_search::dfs(g2, start_pos - 1) == expected2);
159+
std::cout << "Passed" << std::endl;
160+
161+
/// Test 3
162+
std::cout << "Case 3: " << std::endl;
163+
start_pos = 2;
164+
std::vector<std::vector<size_t>> g3(4, std::vector<size_t>());
165+
166+
graph::depth_first_search::addEdge(&g3, 1, 2);
167+
graph::depth_first_search::addEdge(&g3, 1, 3);
168+
graph::depth_first_search::addEdge(&g3, 2, 4);
169+
graph::depth_first_search::addEdge(&g3, 4, 1);
170+
171+
std::vector<size_t> expected3{
172+
2, 4, 1, 3}; /// for the above sample data, this is the expected output
173+
assert(graph::depth_first_search::dfs(g3, start_pos - 1) == expected3);
174+
std::cout << "Passed" << std::endl;
167175
}
168176

169177
/**
@@ -174,34 +182,35 @@ int main() {
174182
tests(); // execute the tests
175183

176184
size_t vertices = 0, edges = 0, start_pos = 1;
177-
std::vector<size_t> traversal;
185+
std::vector<size_t> traversal;
178186

179187
std::cout << "Enter the Vertices : ";
180-
std::cin >> vertices;
181-
std::cout << "Enter the Edges : ";
182-
std::cin >> edges;
188+
std::cin >> vertices;
189+
std::cout << "Enter the Edges : ";
190+
std::cin >> edges;
183191

184192
/// creating a graph
185-
std::vector<std::vector<size_t> > adj(vertices, std::vector<size_t>());
193+
std::vector<std::vector<size_t>> adj(vertices, std::vector<size_t>());
186194

187195
/// taking input for the edges
188-
std::cout << "Enter the vertices which have edges between them : " << std::endl;
189-
while (edges--) {
190-
size_t u = 0, v = 0;
191-
std::cin >> u >> v;
192-
graph::depth_first_search::addEdge(&adj, u, v);
193-
}
196+
std::cout << "Enter the vertices which have edges between them : "
197+
<< std::endl;
198+
while (edges--) {
199+
size_t u = 0, v = 0;
200+
std::cin >> u >> v;
201+
graph::depth_first_search::addEdge(&adj, u, v);
202+
}
194203

195204
/// taking input for the starting position
196205
std::cout << "Enter the starting vertex [1,n]: " << std::endl;
197-
std::cin >> start_pos;
198-
start_pos -= 1;
199-
traversal = graph::depth_first_search::dfs(adj, start_pos);
206+
std::cin >> start_pos;
207+
start_pos -= 1;
208+
traversal = graph::depth_first_search::dfs(adj, start_pos);
200209

201210
/// Printing the order of traversal
202211
for (auto x : traversal) {
203-
std::cout << x << ' ';
204-
}
212+
std::cout << x << ' ';
213+
}
205214

206215
return 0;
207216
}

graph/dijkstra.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
* at the code below to understand in better way.
2424
*
2525
*/
26-
#include <cassert>
27-
#include <iostream>
28-
#include <limits>
29-
#include <memory>
30-
#include <queue>
31-
#include <utility>
32-
#include <vector>
26+
#include <stdint.h> // for int64_t
27+
#include <cassert> // for assert
28+
#include <iostream> // for basic_ostream, char_traits, operator<<, basic_...
29+
#include <limits> // for numeric_limits
30+
#include <queue> // for priority_queue
31+
#include <utility> // for pair, make_pair
32+
#include <vector> // for vector
33+
#include <functional> // for greater
3334

3435
constexpr int64_t INF = std::numeric_limits<int64_t>::max();
3536

graph/hamiltons_cycle.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
* @author [vakhokoto](https://github.com/vakhokoto)
1616
* @author [Krishna Vedala](https://github.com/kvedala)
1717
*/
18-
#include <cassert>
19-
#include <iostream>
20-
#include <vector>
18+
#include <cassert> // for assert
19+
#include <cstddef> // for size_t
20+
#include <iostream> // for operator<<, basic_ostream, cout
21+
#include <vector> // for vector
2122

2223
/**
2324
* The function determines if there is a hamilton's cycle in the graph

0 commit comments

Comments
 (0)