14
14
#include < vector> // / for std::vector
15
15
#include < iostream> // / for IO operations
16
16
#include < cassert> // / for assert
17
+ #include < cstdint> // / for fixed-size integer types (e.g., std::uint32_t)
17
18
18
19
/* *
19
20
* @namespace graph
@@ -30,15 +31,19 @@ namespace graph {
30
31
* @param visited a vector to keep track of visited nodes in the current DFS path
31
32
* @returns the number of paths from node `u` to node `v`
32
33
*/
33
- int count_paths_dfs (const std::vector<std::vector<unsigned int >>& A, int u, int v, int n, std::vector<bool >& visited) {
34
+ std::uint32_t count_paths_dfs (const std::vector<std::vector<std::uint32_t >>& A,
35
+ std::uint32_t u,
36
+ std::uint32_t v,
37
+ std::uint32_t n,
38
+ std::vector<bool >& visited) {
34
39
if (u == v) {
35
40
return 1 ; // Base case: Reached the destination node
36
41
}
37
42
38
43
visited[u] = true ; // Mark the current node as visited
39
- int path_count = 0 ; // Count of all paths from `u` to `v`
44
+ std:: uint32_t path_count = 0 ; // Count of all paths from `u` to `v`
40
45
41
- for (int i = 0 ; i < n; i++) {
46
+ for (std:: uint32_t i = 0 ; i < n; i++) {
42
47
if (A[u][i] == 1 && !visited[i]) { // Check if there is an edge and the node is not visited
43
48
path_count += count_paths_dfs (A, i, v, n, visited); // Recursively explore paths from `i` to `v`
44
49
}
@@ -58,7 +63,10 @@ namespace graph {
58
63
* @param n the number of nodes in the graph
59
64
* @returns the number of paths from node `u` to node `v`
60
65
*/
61
- int count_paths (const std::vector<std::vector<unsigned int >>& A, int u, int v, int n) {
66
+ std::uint32_t count_paths (const std::vector<std::vector<std::uint32_t >>& A,
67
+ std::uint32_t u,
68
+ std::uint32_t v,
69
+ std::uint32_t n) {
62
70
std::vector<bool > visited (n, false ); // Initialize a visited vector for tracking nodes
63
71
return count_paths_dfs (A, u, v, n, visited);
64
72
}
@@ -71,43 +79,43 @@ namespace graph {
71
79
*/
72
80
static void test () {
73
81
// Test case 1: Simple directed graph with multiple paths
74
- std::vector<std::vector<unsigned int >> graph1 = {
82
+ std::vector<std::vector<std:: uint32_t >> graph1 = {
75
83
{0 , 1 , 0 , 1 , 0 },
76
84
{0 , 0 , 1 , 0 , 1 },
77
85
{0 , 0 , 0 , 0 , 1 },
78
86
{0 , 0 , 1 , 0 , 0 },
79
87
{0 , 0 , 0 , 0 , 0 }
80
88
};
81
- int n1 = 5 , u1 = 0 , v1 = 4 ;
89
+ std:: uint32_t n1 = 5 , u1 = 0 , v1 = 4 ;
82
90
assert (graph::count_paths (graph1, u1, v1, n1) == 3 ); // There are 3 paths from node 0 to 4
83
91
84
92
// Test case 2: No possible path (disconnected graph)
85
- std::vector<std::vector<unsigned int >> graph2 = {
93
+ std::vector<std::vector<std:: uint32_t >> graph2 = {
86
94
{0 , 1 , 0 , 0 , 0 },
87
95
{0 , 0 , 0 , 0 , 0 },
88
96
{0 , 0 , 0 , 0 , 1 },
89
97
{0 , 0 , 1 , 0 , 0 },
90
98
{0 , 0 , 0 , 0 , 0 }
91
99
};
92
- int n2 = 5 , u2 = 0 , v2 = 4 ;
100
+ std:: uint32_t n2 = 5 , u2 = 0 , v2 = 4 ;
93
101
assert (graph::count_paths (graph2, u2, v2, n2) == 0 ); // No path from node 0 to 4
94
102
95
103
// Test case 3: Cyclic graph with multiple paths
96
- std::vector<std::vector<unsigned int >> graph3 = {
104
+ std::vector<std::vector<std:: uint32_t >> graph3 = {
97
105
{0 , 1 , 0 , 0 , 0 },
98
106
{0 , 0 , 1 , 1 , 0 },
99
107
{1 , 0 , 0 , 0 , 1 },
100
108
{0 , 0 , 1 , 0 , 1 },
101
109
{0 , 0 , 0 , 0 , 0 }
102
110
};
103
- int n3 = 5 , u3 = 0 , v3 = 4 ;
111
+ std:: uint32_t n3 = 5 , u3 = 0 , v3 = 4 ;
104
112
assert (graph::count_paths (graph3, u3, v3, n3) == 3 ); // There are 3 paths from node 0 to 4
105
113
106
114
// Test case 4: Single node graph (self-loop)
107
- std::vector<std::vector<unsigned int >> graph4 = {
115
+ std::vector<std::vector<std:: uint32_t >> graph4 = {
108
116
{0 }
109
117
};
110
- int n4 = 1 , u4 = 0 , v4 = 0 ;
118
+ std:: uint32_t n4 = 1 , u4 = 0 , v4 = 0 ;
111
119
assert (graph::count_paths (graph4, u4, v4, n4) == 1 ); // There is self-loop, so 1 path from node 0 to 0
112
120
113
121
std::cout << " All tests have successfully passed!\n " ;
0 commit comments