TheAlgorithms-C-Plus-Plus/Greedy Algorithms/Kruskals Minimum Spanning Tree.cpp

38 lines
670 B
C++
Raw Normal View History

2017-02-27 19:22:08 +08:00
#include <iostream>
using namespace std;
#define V 6
#define INFINITY 99999
2019-08-21 10:10:08 +08:00
int graph[V][V] = {
2017-02-27 19:22:08 +08:00
{0, 4, 1, 4, INFINITY, INFINITY},
{4, 0, 3, 8, 3, INFINITY},
{1, 3, 0, INFINITY, 1, INFINITY},
{4, 8, INFINITY, 0, 5, 7},
{INFINITY, 3, 1, 5, 0, INFINITY},
2019-08-21 10:10:08 +08:00
{INFINITY, INFINITY, INFINITY, 7, INFINITY, 0}};
2017-02-27 19:22:08 +08:00
void findMinimumEdge()
{
for (int i = 0; i < V; i++)
{
2019-08-21 10:10:08 +08:00
int min = INFINITY;
int minIndex = 0;
2017-02-27 19:22:08 +08:00
for (int j = 0; j < V; j++)
{
2019-08-21 10:10:08 +08:00
if (graph[i][j] != 0 && graph[i][j] < min)
2017-02-27 19:22:08 +08:00
{
2019-08-21 10:10:08 +08:00
min = graph[i][j];
minIndex = j;
2017-02-27 19:22:08 +08:00
}
}
2019-08-21 10:10:08 +08:00
cout << i << " - " << minIndex << "\t" << graph[i][minIndex] << "\n";
2017-02-27 19:22:08 +08:00
}
}
int main()
{
findMinimumEdge();
return 0;
}