-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrims.cpp
130 lines (120 loc) · 2.77 KB
/
Prims.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
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
//Prims is applied to find MST to a graph with property of graph as follows:-
//1) Undirected
//2) Connected
//3) Weighted
/*
1 --- 3
/ | /|
Graph is 0 | / |
\ |/ |
2 --- 4
weights of edges are as follows:-
0----1 : 4
0----2 : 8
1----3 : 6
1----2 : 2
2----3 : 3
2----4 : 9
3----4 : 5
*/
//compile it using g++ Prims.cpp -o prims -std=c++11
//run it as ./prims and paste this below test case
/*run this test case
5 7
0 1 4
0 2 8
1 2 2
1 3 6
2 3 3
2 4 9
3 4 5
*/
void printMatrix(vector<vector<int>> adjMatrix)
{
int n = adjMatrix.size();
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << adjMatrix[i][j] << " ";
}
cout << endl;
}
}
int findMinVertex(bool *visited, int *weight, int nodes)
{
int minVertex = -1;
for(int i=0; i<nodes; i++)
{
if(!visited[i] && (minVertex==-1 || weight[i] < weight[minVertex]))
{
minVertex = i;
}
}
return minVertex;
}
void primsAlgo(vector<vector<int>> adjMatrix, int nodes)
{
int *parent = new int[nodes];
bool *visited = new bool[nodes];
int *weight = new int[nodes];
for(int i=0; i<nodes; i++)
{
visited[i] = false;
weight[i] = INT_MAX;
}
parent[0] = -1;
weight[0] = 0;
for(int i=0;i<nodes;i++)
{
//find min vertex
int minVertex = findMinVertex(visited,weight,nodes);
visited[minVertex] = true;
//explore the unvisited neighbours of min vertex
for(int j=0; j<nodes; j++)
{
if(adjMatrix[minVertex][j]!=0 && !visited[j])
{
if(adjMatrix[minVertex][j] < weight[j])
{
weight[j] = adjMatrix[minVertex][j];
parent[j] = minVertex;
}
}
}
}
int total_min_distance = 0;
for(int i=1; i<nodes; i++)
{
if(parent[i] < i)
{
total_min_distance += weight[i];
cout << parent[i] << " " << i << " " << weight[i] << endl;
}
else
{
total_min_distance += weight[i];
cout << i << " " << parent[i] << " " << weight[i] << endl;
}
}
cout << "Total Minimum Distance taken is " << total_min_distance << endl;
}
int main() {
int nodes, edges;
cin >> nodes >> edges;
vector<vector<int>> adjMatrix(nodes, vector<int>(nodes, 0));
for(int i=0; i<edges; i++)
{
int u, v, w;
cin >> u >> v >> w;
adjMatrix[u][v] = w;
adjMatrix[v][u] = w;
}
printMatrix(adjMatrix);
primsAlgo(adjMatrix,nodes);
return 0;
}