mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 18:46:50 +08:00
chore: use iwyu on graph/**.cpp
This commit is contained in:
@@ -13,13 +13,14 @@
|
||||
* https://www.youtube.com/watch?v=DINCL5cd_w0&t=24s
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for io operations
|
||||
#include <limits> /// for variable INF
|
||||
#include <queue> /// for the priority_queue of distances
|
||||
#include <utility> /// for make_pair function
|
||||
#include <vector> /// for store the graph, the distances, and the path
|
||||
#include <cassert> // for assert
|
||||
#include <cstdint> // for uint64_t, int64_t
|
||||
#include <iostream> // for basic_ostream, operator<<, char_traits, cout
|
||||
#include <limits> // for numeric_limits
|
||||
#include <queue> // for priority_queue
|
||||
#include <utility> // for pair, make_pair
|
||||
#include <vector> // for vector
|
||||
#include <functional> // for greater
|
||||
|
||||
constexpr int64_t INF = std::numeric_limits<int64_t>::max();
|
||||
|
||||
|
||||
@@ -45,14 +45,14 @@
|
||||
* push that element into the queue and mark this as visited
|
||||
*
|
||||
*/
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cassert> // for assert
|
||||
#include <cstddef> // for size_t
|
||||
#include <functional> // for less
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, basic_istream...
|
||||
#include <list> // for list
|
||||
#include <map> // for map, operator==
|
||||
#include <queue> // for queue
|
||||
#include <string> // for basic_string, operator<, char_traits, allocator
|
||||
|
||||
/**
|
||||
* \namespace graph
|
||||
|
||||
@@ -25,10 +25,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert> // for assert
|
||||
#include <iostream> // for basic_ostream, char_traits, operator<<, basic_is...
|
||||
#include <vector> // for vector
|
||||
|
||||
/**
|
||||
* @namespace graph
|
||||
|
||||
@@ -17,10 +17,11 @@
|
||||
* @author Unknown author
|
||||
* @author [Sagar Pandya](https://github.com/sagarpandyansit)
|
||||
*/
|
||||
#include <cstdint> /// for integer typedefs
|
||||
#include <iostream> /// for IO operations
|
||||
#include <set> /// for std::set
|
||||
#include <vector> /// for std::vector
|
||||
#include <cstdint> // for int64_t, uint32_t
|
||||
#include <iostream> // for char_traits, basic_istream, cin, basic_ostream
|
||||
#include <set> // for set
|
||||
#include <vector> // for vector
|
||||
#include <utility> // for swap
|
||||
|
||||
/**
|
||||
* @namespace graph
|
||||
|
||||
@@ -7,14 +7,15 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> // for std::cout
|
||||
#include <map> // for std::map
|
||||
#include <queue> // for std::queue
|
||||
#include <stdexcept> // for throwing errors
|
||||
#include <type_traits> // for std::remove_reference
|
||||
#include <utility> // for std::move
|
||||
#include <vector> // for std::vector
|
||||
#include <cstdint> // for uint8_t
|
||||
#include <iostream> // for char_traits, basic_ostream, operator<<
|
||||
#include <map> // for map, operator!=, _Rb_tree_iterator
|
||||
#include <queue> // for queue
|
||||
#include <stdexcept> // for range_error
|
||||
#include <type_traits> // for remove_reference
|
||||
#include <utility> // for move, pair
|
||||
#include <vector> // for vector
|
||||
#include <initializer_list> // for initializer_list
|
||||
|
||||
/**
|
||||
* Implementation of non-weighted directed edge of a graph.
|
||||
|
||||
@@ -32,9 +32,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstddef> // for size_t
|
||||
#include <iostream> // for char_traits, basic_ostream, operator<<, cout
|
||||
#include <vector> // for vector
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -23,13 +23,14 @@
|
||||
* at the code below to understand in better way.
|
||||
*
|
||||
*/
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <stdint.h> // for int64_t
|
||||
#include <cassert> // for assert
|
||||
#include <iostream> // for basic_ostream, char_traits, operator<<, basic_...
|
||||
#include <limits> // for numeric_limits
|
||||
#include <queue> // for priority_queue
|
||||
#include <utility> // for pair, make_pair
|
||||
#include <vector> // for vector
|
||||
#include <functional> // for greater
|
||||
|
||||
constexpr int64_t INF = std::numeric_limits<int64_t>::max();
|
||||
|
||||
|
||||
@@ -15,9 +15,10 @@
|
||||
* @author [vakhokoto](https://github.com/vakhokoto)
|
||||
* @author [Krishna Vedala](https://github.com/kvedala)
|
||||
*/
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert> // for assert
|
||||
#include <cstddef> // for size_t
|
||||
#include <iostream> // for operator<<, basic_ostream, cout
|
||||
#include <vector> // for vector
|
||||
|
||||
/**
|
||||
* The function determines if there is a hamilton's cycle in the graph
|
||||
|
||||
@@ -45,13 +45,12 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <queue>
|
||||
#include <list>
|
||||
#include <climits>
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
#include <iostream> // for char_traits, basic_istream::operator>>, basic_is...
|
||||
#include <queue> // for queue
|
||||
#include <list> // for list, operator!=, _List_iterator
|
||||
#include <climits> // for INT_MAX
|
||||
#include <cassert> // for assert
|
||||
#include <vector> // for vector
|
||||
|
||||
/**
|
||||
* @namespace graph
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stdint.h> // for int64_t
|
||||
#include <algorithm> // for sort
|
||||
#include <array> // for array
|
||||
#include <iostream> // for char_traits, basic_istream::operator>>, basic_i...
|
||||
#include <vector> // for vector
|
||||
#include <utility> // for pair, make_pair
|
||||
|
||||
//#include <boost/multiprecision/cpp_int.hpp>
|
||||
// using namespace boost::multiprecision;
|
||||
const int mx = 1e6 + 5;
|
||||
|
||||
@@ -35,11 +35,11 @@
|
||||
* lowest_common_ancestor(x, y) = lowest_common_ancestor(y, x)
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <cassert> // for assert
|
||||
#include <cstddef> // for size_t
|
||||
#include <queue> // for queue
|
||||
#include <utility> // for pair, swap
|
||||
#include <vector> // for vector
|
||||
|
||||
/**
|
||||
* \namespace graph
|
||||
|
||||
@@ -4,15 +4,14 @@
|
||||
* Copyright: 2020, Open-Source
|
||||
* Last Modified: May 25, 2020
|
||||
*/
|
||||
#include <algorithm>
|
||||
#include <bitset>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <queue>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <algorithm> // for min
|
||||
#include <bitset> // for bitset
|
||||
#include <iostream> // for char_traits, basic_ostream, operator<<, basic_i...
|
||||
#include <limits> // for numeric_limits
|
||||
#include <queue> // for queue
|
||||
#include <tuple> // for tuple, make_tuple, tie
|
||||
#include <vector> // for vector
|
||||
|
||||
// std::max capacity of node in graph
|
||||
const int MAXN = 505;
|
||||
class Graph {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
// C++ program to implement Prim's Algorithm
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <iostream> // for char_traits, basic_istream::operator>>, basic_...
|
||||
#include <queue> // for priority_queue
|
||||
#include <vector> // for vector
|
||||
#include <functional> // for greater
|
||||
#include <utility> // for make_pair, pair
|
||||
|
||||
using PII = std::pair<int, int>;
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <iostream> // for char_traits, basic_istream::operator>>, basic_is...
|
||||
#include <queue> // for queue
|
||||
#include <vector> // for vector
|
||||
|
||||
std::vector<int> topoSortKahn(int N, const std::vector<std::vector<int> > &adj);
|
||||
|
||||
|
||||
@@ -17,12 +17,11 @@
|
||||
* This is the naive implementation of the problem.
|
||||
*/
|
||||
|
||||
#include <algorithm> /// for std::min
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for IO operations
|
||||
#include <limits> /// for limits of integral types
|
||||
#include <vector> /// for std::vector
|
||||
#include <algorithm> // for min, next_permutation
|
||||
#include <cassert> // for assert
|
||||
#include <cstdint> // for uint32_t, int32_t
|
||||
#include <iostream> // for basic_ostream, operator<<, char_traits, cout, endl
|
||||
#include <vector> // for vector
|
||||
|
||||
/**
|
||||
* @namespace graph
|
||||
|
||||
Reference in New Issue
Block a user