forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primsMST.cpp
132 lines (113 loc) · 2.87 KB
/
primsMST.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Minimum spanning tree using Prim's algorithm.
* Approach greedy
* Calculate the weight of minimum spanning tree(sum of weight of edges of MST)
* and print the edges which makes MST.
* Input format:
* ROW 1 :: V - number of vertices, E - number of Edges.
* Next E lines give details of each edge in format x y w.
* where x is start of edge, y is end of edge and w is weight of xy edge
* After that, S - Index of the starting point of finding the MST.
* all the input is not 0 indexed.
*/
#include <iostream>
#include <limits>
const int MIN = std::numeric_limits<int>::min();
const int MAX = std::numeric_limits<int>::max();
/**
* Print the Minimum spanning tree
*/
void printMST( int ** graph, int * MST, int V ) {
std::cout << " Edge Weight \n";
for ( int v = 0; v < V; ++v ) {
if (MST[v] != -1 && graph[v][MST[v]] != -1 ) {
std::cout << v+1 << "<-->" << MST[v]+1 << " " << graph[v][MST[v]] << std::endl;
}
}
}
/**
* minKey
* returns the index of the vertex which is not yet part of MST
* and has minimum key.
*/
int minKey( int * keys, bool * inMST, int V ) {
int min = MAX, minIndex;
for ( int v = 0; v < V; ++v ) {
if ( inMST[v] == false && min > keys[v] ) {
minIndex = v;
min = keys[v];
}
}
return minIndex;
}
/**
* primsMSTWeight
* returns the weight of the minimum spanning tree of the graph
* Args:
* graph: adjacency matrix of the graph
* V : Total number of vertices in the graph
* S : Starting index of MST
*/
int primsMSTWeight( int ** graph, int V, int S, int * MST)
{
int * key = new int[V];
bool * inMST = new bool[V];
for ( int v = 0; v < V; ++v ) {
inMST[v] = false;
key[v] = MAX;
}
key[S] = 0;
for ( int count = 0; count < V-1; ++count ) {
int u = minKey(key, inMST, V);
inMST[u] = true;
for ( int v = 0; v < V; ++v ) {
if ( (graph[u][v] != -1) && (inMST[v] == false) && (key[v] > graph[u][v]) ) {
key[v] = graph[u][v];
MST[v] = u;
}
}
}
int sum = 0;
for ( int v = 0 ;v < V; ++v ) {
if ( MST[V] != -1 && graph[v][MST[v]] != -1 ) {
sum += graph[v][MST[v]];
}
}
delete[] key;
delete[] inMST;
return sum;
}
int main()
{
int V, E, x, y, w, S;
/**
* V -- number of vertices
* E -- number of edges
* xy-- edge with endpoints x and y
* w -- weight of edge
* S -- Starting index
*/
std::cin >> V >> E;
int ** graph = new int*[V];
for ( int v = 0; v < V; ++v ) {
graph[v] = new int[V];
for ( int w = 0; w < V; ++w ) {
graph[v][w] = -1;
}
}
for ( int i = 0; i < E; ++i ) {
std::cin >> x >> y >> w;
if ( graph[x-1][y-1] == -1 ||
( (graph[x-1][y-1] > 0) && (graph[x-1][y-1] > w ))) {
graph[x-1][y-1] = w;
graph[y-1][x-1] = w;
}
}
std::cin >> S;
int * MST = new int[V];
MST[S-1] = -1;
std::cout << "Min Spanning tree weight " << primsMSTWeight(graph, V, S-1, MST) << std::endl;
std::cout << "Min Spanning tree:\n";
printMST(graph, MST, V);
return 0;
}