-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims.cpp
More file actions
43 lines (43 loc) · 1.45 KB
/
prims.cpp
File metadata and controls
43 lines (43 loc) · 1.45 KB
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
#include <bits/stdc++.h>
using namespace std;
#define vertices 5
bool check(int u, int v, vector<bool> visited){
if(u == v)return false;
if(visited[u] == false && visited[v] == false)return false;
else if(visited[u] == true && visited[v] == true)return false;
return true;
}
void prims(int cost[][vertices]){
vector<bool> visited(vertices, false);
visited[0] = true;
int edgeNo = 0, totalCost = 0;
while (edgeNo < vertices - 1) {
int mincost = INT_MAX, a = -1, b = -1;
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
if (cost[i][j] < mincost && check(i, j, visited)) {
mincost = cost[i][j];
a = i;
b = j;
}
}
}
if (a != -1 && b != -1) {
cout<<"Edge "<<edgeNo++<<" : ("<<a<<" , "<<b<<" ) : cost = "<<mincost<<endl;
totalCost += mincost;
visited[b] = visited[a] = true;
}
}
cout<<"Cost of Minimum spanning tree ="<<totalCost;
}
int main() {
int cost[][vertices] =
{ { INT_MAX, 12, INT_MAX, 25, INT_MAX },//adj[i][j]=is the cost between i and j vertices
{ 12, INT_MAX, 11, 8, 12 },
{ INT_MAX, 11, INT_MAX, INT_MAX, 17 },
{ 25, 8, INT_MAX, INT_MAX, 15 },
{ INT_MAX, 12, 17, 15, INT_MAX }, };
cout<<"The Minimum spanning tree for the given tree is :\n";
prims(cost);
return 0;
}