-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prim.cpp
130 lines (116 loc) · 2.47 KB
/
Prim.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<bits/stdc++.h>
#include<unistd.h>
using namespace std;
typedef vector<int> Arr;
typedef pair<int,int> Pair;
typedef vector<Pair> Adj;
typedef vector<Adj> Graph;
typedef unordered_map<int,int> Map;
#define F first
#define S second
class Cmp{
public:
bool operator()(Pair n1,Pair n2) {
return n1.second>n2.second;
}
};
void printArr(Graph g){
for(int i =0;i<(int)g.size();i++){
cout<<i<<" => ";
for(auto v:g[i])
cout<<"("<<v.first<<","<<v.second<<") ";
cout<<"\n";
}
}
void printEdge(Arr v,int src){
for(int i=0;i<(int)v.size();i++)
if(i != src)
cout<<i<<" => "<<v[i]<<"\n";
}
void decrease_key(Adj &heap,Map &mp,int i){
while(i > 0){
int p = (i-1)/2;
if(heap[i].S < heap[p].S){
mp[heap[i].F] = p;
mp[heap[p].F] = i;
swap(heap[i],heap[p]);
}
i = p;
}
}
Pair extractMin(Adj &heap,Map &mp){
Pair curr = heap.front();
int n = heap.size(),p=0;
mp[heap[n-1].F] = p;
mp.erase(curr.F);
swap(heap[p],heap[n-1]);
heap.pop_back();
n -= 1;
while(p < n/2){
int lc = 2*p+1,rc = 2*p+2,minIndex=p,minVal =heap[p].S;
if(lc < n && minVal>heap[lc].S){
minIndex = lc;
minVal = heap[p].S;
}
if(rc < n && minVal>heap[rc].S){
minIndex = rc;
minVal = heap[p].S;
}
if(p == minIndex)
break;
mp[heap[p].F] = minIndex;
mp[heap[minIndex].F] = p;
swap(heap[p],heap[minIndex]);
p = minIndex;
}
return curr;
}
void dj(Graph &g,Arr &dist,Arr &par,int src){
Map mp;
Adj minHeap;
dist[src] = 0;
for(int i=0;i<(int)dist.size();i++)
minHeap.push_back({i,dist[i]});
make_heap(minHeap.begin(),minHeap.end(),Cmp());
for(int i=0;i<(int)minHeap.size();i++)
mp[minHeap[i].F] = i;
while(!minHeap.empty()){
Pair curr = extractMin(minHeap,mp);
dist[curr.F] = curr.S;
for(auto v:g[curr.F]){
if(mp.find(v.F) != mp.end() && v.S < minHeap[mp[v.F]].S){
minHeap[mp[v.F]].S = v.S;
par[v.F] = curr.F;
decrease_key(minHeap,mp,mp[v.F]);
}
}
}
}
int main(){
Graph g;
int N,A,V,W,src,mstVal = 0;
cout<<"Enter Number of Vertices ...\n";
cin>>N;
Arr dist(N,INT_MAX),par(N,-1);
for(int i=0;i<N;i++){
cout<<"Enter Number of Adj vertex of "<<i<<"th Vertex\n";
cin>>A;
Adj edge;
cout<<"Enter adj Vertex and Wieght (u,w)\n";
for(int j=0;j<A;j++){
cin>>V>>W;
edge.push_back({V,W});
}
g.push_back(edge);
}
cin>>src;
dj(g,dist,par,src);
cout<<"Your Input Graph\n";
printArr(g);
for(int d:dist)
mstVal += d;
cout<<"MSTVal => "<<mstVal<<"\n";
cout<<"Edge of MST\n";
printEdge(par,src);
return 0;
}