1
- #include < array>
2
- #include < iostream>
1
+ /* *
2
+ * @file
3
+ * @brief [Kruskals Minimum Spanning
4
+ * Tree](https://www.simplilearn.com/tutorials/data-structure-tutorial/kruskal-algorithm)
5
+ * implementation
6
+ *
7
+ * @details
8
+ * _Quoted from
9
+ * [Simplilearn](https://www.simplilearn.com/tutorials/data-structure-tutorial/kruskal-algorithm)._
10
+ *
11
+ * Kruskal’s algorithm is the concept that is introduced in the graph theory of
12
+ * discrete mathematics. It is used to discover the shortest path between two
13
+ * points in a connected weighted graph. This algorithm converts a given graph
14
+ * into the forest, considering each node as a separate tree. These trees can
15
+ * only link to each other if the edge connecting them has a low value and
16
+ * doesn’t generate a cycle in MST structure.
17
+ *
18
+ * @author [coleman2246](https://github.com/coleman2246)
19
+ */
3
20
4
- void findMinimumEdge (int INFINITY, std::array<std::array<int , 6 >, 6 > graph) {
21
+ #include < array> // / for array
22
+ #include < iostream> // / for IO operations
23
+
24
+ /* *
25
+ * @namespace
26
+ * @brief Greedy Algorithms
27
+ */
28
+ namespace greedy_algorithms {
29
+ /* *
30
+ * @brief Finds the minimum edge of the given graph.
31
+ * @param infinity Defines the infinity of the graph
32
+ * @param graph The graph that will be used to find the edge
33
+ * @returns void
34
+ */
35
+ template <typename T>
36
+ void findMinimumEdge (const int &infinity,
37
+ const std::array<std::array<T, 6 >, 6 > &graph) {
5
38
for (int i = 0 ; i < graph.size (); i++) {
6
- int min = INFINITY ;
39
+ int min = infinity ;
7
40
int minIndex = 0 ;
8
41
for (int j = 0 ; j < graph.size (); j++) {
9
42
if (graph[i][j] != 0 && graph[i][j] < min) {
@@ -12,10 +45,15 @@ void findMinimumEdge(int INFINITY, std::array<std::array<int, 6>, 6> graph) {
12
45
}
13
46
}
14
47
std::cout << i << " - " << minIndex << " \t " << graph[i][minIndex]
15
- << std::endl ;
48
+ << " \n " ;
16
49
}
17
50
}
51
+ } // namespace greedy_algorithms
18
52
53
+ /* *
54
+ * @brief Main function
55
+ * @returns 0 on exit
56
+ */
19
57
int main () {
20
58
constexpr int INFINITY = 99999 ;
21
59
std::array<std::array<int , 6 >, 6 > graph{
@@ -26,6 +64,6 @@ int main() {
26
64
INFINITY, 3 , 1 , 5 , 0 , INFINITY,
27
65
INFINITY, INFINITY, INFINITY, 7 , INFINITY, 0 };
28
66
29
- findMinimumEdge (INFINITY, graph);
67
+ greedy_algorithms:: findMinimumEdge (INFINITY, graph);
30
68
return 0 ;
31
69
}
0 commit comments