mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
docs: add documentation in kruskals_minimum_spanning_tree.cpp
(#2482)
* docs: add documentation in `kruskals_minimum_spanning_tree.cpp`
* clang-format and clang-tidy fixes for 4e234390
* chore: remove myself as an author
* chore: `std::endl` -> `\n`
---------
Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
This commit is contained in:
parent
882ba119dc
commit
b480ddb191
@ -1,9 +1,42 @@
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
/**
|
||||
* @file
|
||||
* @brief [Kruskals Minimum Spanning
|
||||
* Tree](https://www.simplilearn.com/tutorials/data-structure-tutorial/kruskal-algorithm)
|
||||
* implementation
|
||||
*
|
||||
* @details
|
||||
* _Quoted from
|
||||
* [Simplilearn](https://www.simplilearn.com/tutorials/data-structure-tutorial/kruskal-algorithm)._
|
||||
*
|
||||
* Kruskal’s algorithm is the concept that is introduced in the graph theory of
|
||||
* discrete mathematics. It is used to discover the shortest path between two
|
||||
* points in a connected weighted graph. This algorithm converts a given graph
|
||||
* into the forest, considering each node as a separate tree. These trees can
|
||||
* only link to each other if the edge connecting them has a low value and
|
||||
* doesn’t generate a cycle in MST structure.
|
||||
*
|
||||
* @author [coleman2246](https://github.com/coleman2246)
|
||||
*/
|
||||
|
||||
void findMinimumEdge(int INFINITY, std::array<std::array<int, 6>, 6> graph) {
|
||||
#include <array> /// for array
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
* @namespace
|
||||
* @brief Greedy Algorithms
|
||||
*/
|
||||
namespace greedy_algorithms {
|
||||
/**
|
||||
* @brief Finds the minimum edge of the given graph.
|
||||
* @param infinity Defines the infinity of the graph
|
||||
* @param graph The graph that will be used to find the edge
|
||||
* @returns void
|
||||
*/
|
||||
template <typename T>
|
||||
void findMinimumEdge(const int &infinity,
|
||||
const std::array<std::array<T, 6>, 6> &graph) {
|
||||
for (int i = 0; i < graph.size(); i++) {
|
||||
int min = INFINITY;
|
||||
int min = infinity;
|
||||
int minIndex = 0;
|
||||
for (int j = 0; j < graph.size(); j++) {
|
||||
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) {
|
||||
}
|
||||
}
|
||||
std::cout << i << " - " << minIndex << "\t" << graph[i][minIndex]
|
||||
<< std::endl;
|
||||
<< "\n";
|
||||
}
|
||||
}
|
||||
} // namespace greedy_algorithms
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
constexpr int INFINITY = 99999;
|
||||
std::array<std::array<int, 6>, 6> graph{
|
||||
@ -26,6 +64,6 @@ int main() {
|
||||
INFINITY, 3, 1, 5, 0, INFINITY,
|
||||
INFINITY, INFINITY, INFINITY, 7, INFINITY, 0};
|
||||
|
||||
findMinimumEdge(INFINITY, graph);
|
||||
greedy_algorithms::findMinimumEdge(INFINITY, graph);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user