forked from manishbisht/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.cpp
More file actions
72 lines (62 loc) · 1.51 KB
/
prim.cpp
File metadata and controls
72 lines (62 loc) · 1.51 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
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
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef pair<int,int> iPair;
class Graph{
int V;
list <pair<int,int> > *adj;
public:
Graph(int V);
void addEdge(int u,int v,int w);
void primMST();
};
Graph::Graph(int V){
this->V = V;
adj = new list<iPair>[this->V];
}
void Graph::addEdge(int u,int v,int w){
adj[u].push_back(make_pair(v,w));
adj[v].push_back(make_pair(u,w));
}
void Graph::primMST(){
//Priority Queue (PQ)
//Implement MIN HEAP Pair
priority_queue<iPair,vector<iPair>,greater<iPair> > pq;
int src = 0;
vector<int> key(V,INF);
vector<int> parent(V,-1);
vector<bool> inMST(V,false);
pq.push(make_pair(0,src));
key[src] = 0;
while(!pq.empty()){
int u = pq.top().second;
pq.pop();
inMST[u] = true;
list<pair<int,int> >::iterator it;
for(it = adj[u].begin();it!=adj[u].end();it++){
int v = (*it).first;
int weight = (*it).second;
if(!inMST[v] && key[v] > weight){
key[v] = weight;
pq.push(make_pair(key[v],v));
parent[v] = u;
}
}
}
// Printing the minimum spanning tree
for(int i=1;i<V;i++){
cout << parent[i] << " - " << i << endl;
}
}
int main(){
int V = 5;
Graph g(V);
g.addEdge(0,1,3);
g.addEdge(0,2,2);
g.addEdge(1,2,1);
g.addEdge(1,3,4);
g.addEdge(2,4,1);
g.addEdge(3,4,2);
g.primMST();
return 0;
}