Skip to content

Commit dd0cf9b

Browse files
committed
Added test cases in greedy_algorithms\kruskals_minimum_spanning_tree.cpp
1 parent 15e3fed commit dd0cf9b

File tree

1 file changed

+168
-15
lines changed

1 file changed

+168
-15
lines changed

greedy_algorithms/kruskals_minimum_spanning_tree.cpp

Lines changed: 168 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <array> /// for array
2222
#include <iostream> /// for IO operations
23+
#include <limits> /// for numeric limits
2324

2425
/**
2526
* @namespace
@@ -32,14 +33,19 @@ namespace greedy_algorithms {
3233
* @param graph The graph that will be used to find the edge
3334
* @returns void
3435
*/
35-
template <typename T>
36-
void findMinimumEdge(const int &infinity,
37-
const std::array<std::array<T, 6>, 6> &graph) {
36+
template <typename T, std::size_t N, std::size_t M>
37+
void findMinimumEdge(const T &infinity,
38+
const std::array<std::array<T, N>, M> &graph) {
39+
if (N != M) {
40+
std::cout << "\nWrong input passed. Provided array has dimensions " << N
41+
<< "x" << M << ". Please provide a square matrix.\n";
42+
return;
43+
}
3844
for (int i = 0; i < graph.size(); i++) {
3945
int min = infinity;
4046
int minIndex = 0;
4147
for (int j = 0; j < graph.size(); j++) {
42-
if (graph[i][j] != 0 && graph[i][j] < min) {
48+
if (i != j && graph[i][j] != 0 && graph[i][j] < min) {
4349
min = graph[i][j];
4450
minIndex = j;
4551
}
@@ -50,20 +56,167 @@ void findMinimumEdge(const int &infinity,
5056
}
5157
} // namespace greedy_algorithms
5258

59+
/**
60+
* define a large constant value for int
61+
* define a large constant value for float
62+
* define a large constant value for double
63+
*/
64+
constexpr int INFINITY_INT = std::numeric_limits<int>::max();
65+
constexpr float INFINITY_FLOAT = std::numeric_limits<float>::max();
66+
constexpr double INFINITY_DOUBLE = std::numeric_limits<double>::max();
67+
68+
/**
69+
* @brief Self-test implementations
70+
* @returns void
71+
*/
72+
static void test() {
73+
// Test case with interger values
74+
std::cout << "\nTest Case 1 :\n";
75+
std::array<std::array<int, 6>, 6> graph1{0,
76+
4,
77+
1,
78+
4,
79+
INFINITY_INT,
80+
INFINITY_INT,
81+
4,
82+
0,
83+
3,
84+
8,
85+
3,
86+
INFINITY_INT,
87+
1,
88+
3,
89+
0,
90+
INFINITY_INT,
91+
1,
92+
INFINITY_INT,
93+
4,
94+
8,
95+
INFINITY_INT,
96+
0,
97+
5,
98+
7,
99+
INFINITY_INT,
100+
3,
101+
1,
102+
5,
103+
0,
104+
INFINITY_INT,
105+
INFINITY_INT,
106+
INFINITY_INT,
107+
INFINITY_INT,
108+
7,
109+
INFINITY_INT,
110+
0};
111+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph1);
112+
113+
// Test case with floating values
114+
std::cout << "\nTest Case 2 :\n";
115+
std::array<std::array<float, 3>, 3> graph2{0.0f, 2.5f, INFINITY_FLOAT, 2.5f,
116+
0.0f, 3.2f, INFINITY_FLOAT, 3.2f,
117+
0.0f};
118+
greedy_algorithms::findMinimumEdge(INFINITY_FLOAT, graph2);
119+
120+
// Test case with double values
121+
std::cout << "\nTest Case 3 :\n";
122+
std::array<std::array<double, 5>, 5> graph3{0.0,
123+
10.5,
124+
INFINITY_DOUBLE,
125+
6.7,
126+
3.3,
127+
10.5,
128+
0.0,
129+
8.1,
130+
15.4,
131+
INFINITY_DOUBLE,
132+
INFINITY_DOUBLE,
133+
8.1,
134+
0.0,
135+
INFINITY_DOUBLE,
136+
7.8,
137+
6.7,
138+
15.4,
139+
INFINITY_DOUBLE,
140+
0.0,
141+
9.9,
142+
3.3,
143+
INFINITY_DOUBLE,
144+
7.8,
145+
9.9,
146+
0.0};
147+
greedy_algorithms::findMinimumEdge(INFINITY_DOUBLE, graph3);
148+
149+
// Test Case with negative weights
150+
std::cout << "\nTest Case 4 :\n";
151+
std::array<std::array<int, 3>, 3> graph_neg{0, -2, 4, -2, 0, 3, 4, 3, 0};
152+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph_neg);
153+
154+
// Test Case with Self-Loops
155+
std::cout << "\nTest Case 5 :\n";
156+
std::array<std::array<int, 3>, 3> graph_self_loop{
157+
2, 1, INFINITY_INT, INFINITY_INT, 0, 4, INFINITY_INT, 4, 0};
158+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph_self_loop);
159+
160+
// Test Case with no edges
161+
std::cout << "\nTest Case 6 :\n";
162+
std::array<std::array<int, 4>, 4> no_edges{
163+
0, INFINITY_INT, INFINITY_INT, INFINITY_INT, INFINITY_INT,
164+
0, INFINITY_INT, INFINITY_INT, INFINITY_INT, INFINITY_INT,
165+
0, INFINITY_INT, INFINITY_INT, INFINITY_INT, INFINITY_INT,
166+
0};
167+
greedy_algorithms::findMinimumEdge(INFINITY_INT, no_edges);
168+
169+
// Test Case with a non-connected graph
170+
std::cout << "\nTest Case 7:\n";
171+
std::array<std::array<int, 4>, 4> partial_graph{
172+
0, 2, INFINITY_INT, 6, 2, 0, 3, INFINITY_INT, INFINITY_INT, 3, 0,
173+
4, 6, INFINITY_INT, 4, 0};
174+
greedy_algorithms::findMinimumEdge(INFINITY_INT, partial_graph);
175+
176+
// Test Case with Directed weighted graph. Krushkal algorithm does not give
177+
// optimal answer
178+
std::cout << "\nTest Case 8:\n";
179+
std::array<std::array<int, 4>, 4> directed_graph{
180+
0,
181+
3,
182+
7,
183+
INFINITY_INT, // Vertex 0 has edges to Vertex 1 and Vertex 2
184+
INFINITY_INT,
185+
0,
186+
2,
187+
5, // Vertex 1 has edges to Vertex 2 and Vertex 3
188+
INFINITY_INT,
189+
INFINITY_INT,
190+
0,
191+
1, // Vertex 2 has an edge to Vertex 3
192+
INFINITY_INT,
193+
INFINITY_INT,
194+
INFINITY_INT,
195+
0}; // Vertex 3 has no outgoing edges
196+
greedy_algorithms::findMinimumEdge(INFINITY_INT, directed_graph);
197+
198+
// Test case with wrong input passed
199+
std::cout << "\nTest Case 9:\n";
200+
std::array<std::array<int, 4>, 3> graph9{0, 5, 5, 5, 5, 0,
201+
5, 5, 5, 5, 5, 5};
202+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph9);
203+
204+
// Test case with all same values between every edges
205+
std::cout << "\nTest Case 10:\n";
206+
std::array<std::array<int, 5>, 5> graph10{0, 5, 5, 5, 5, 5, 0, 5, 5,
207+
5, 5, 5, 0, 5, 5, 5, 5, 5,
208+
0, 5, 5, 5, 5, 5, 0};
209+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph10);
210+
211+
std::cout << "\nAll tests have successfully passed!\n";
212+
}
213+
53214
/**
54215
* @brief Main function
55216
* @returns 0 on exit
56217
*/
218+
57219
int main() {
58-
constexpr int INFINITY = 99999;
59-
std::array<std::array<int, 6>, 6> graph{
60-
0, 4, 1, 4, INFINITY, INFINITY,
61-
4, 0, 3, 8, 3, INFINITY,
62-
1, 3, 0, INFINITY, 1, INFINITY,
63-
4, 8, INFINITY, 0, 5, 7,
64-
INFINITY, 3, 1, 5, 0, INFINITY,
65-
INFINITY, INFINITY, INFINITY, 7, INFINITY, 0};
66-
67-
greedy_algorithms::findMinimumEdge(INFINITY, graph);
220+
test(); // run Self-test implementation
68221
return 0;
69-
}
222+
}

0 commit comments

Comments
 (0)