forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimAlgo.cpp
54 lines (46 loc) · 1.15 KB
/
PrimAlgo.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
#include <bits/stdc++.h>
using namespace std;
struct MST
{
int vertex; // the point with parent
bool visit;
};
int matrix[n][n]; // store initial graph
MST point[n]; // answer
int dis[n]; // store the minimum side
// initial(with the start vertex 3)
void set(int v){
for(int i=0; i<n; i++)
fill_n(matrix[i], n, inf);
matrix[1][3] = matrix[3][1] = 4;
/*...etc...*/
// start from vertex 3
for(int i=0; i<n; i++)
point[i].vertex = v, point[i].visit = true;
// initial other array to infinite
fill(dis, dis+n, inf);
}
void solve(int now){
// if all the point are finished
int check = n;
while(!point[--check].visit);
if(!check)
return;
// else recursive
dis[now] = inf;
point[now].visit = false;
int next = 0;
for(int i=1; i<n; i++)
if(point[i].visit){
if(dis[i] > matrix[now][i])
dis[i] = matrix[now][i], point[i].vertex = now;
next = dis[next] > dis[i] ? i : next;
}
solve(next);
}
int main(){
int start = 3; // start vertex
set(start); // initial
solve(start); // prim's algorithm
return 0;
}