forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code14.cpp
161 lines (134 loc) · 4.34 KB
/
code14.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Minimum Spanning tree
// Basically it is forming a tree kind of structure where there are v vertices and v-1 edges and every node is connected to other node
// directly or indirectly
// In prims algorithm, we make two sets of (included in MST) & (Not included in MST)
// then we take first node include in MST and then take weight of all edges connected to it and not included in MST and then find minimum
// of them and making this move again and again till we include (v-1) edges
// also graph should be connected weighted undirected
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 1e9 + 7;
void add_edge(vector<pair<int, int>> vec[], int u, int v, int weight)
{
vec[u].push_back({v, weight});
vec[v].push_back({u, weight});
}
// this algo is without using priority queue
void prims_algo1(vector<pair<int, int>> vec[], int v)
{
// one array for checking whether included in mst or not
// second array for distance computation
vector<bool> mst(v, false);
vector<int> dist(v, INT_MAX);
// int ans = 0;
int source = 0;
dist[source] = 0;
for (int i = 0; i <= v - 1; i++)
{
// finding minimum edge which includes one non added node
int pos = -1; // index of node which is not yet included in MST and is minimum
// cout << "dist: array: " << endl;
// for (int j = 0; j < dist.size(); j++)
// {
// cout << dist[j] << " ";
// }
// cout<<"mist array: "<<endl;
// for(int j = 0;j<mst.size();j++) {
// cout<<mst[j]<<" ";
// }
// cout<<endl;
// cout << endl;
for (int j = 0; j < dist.size(); j++)
{
if ((dist[j] < dist[pos] && mst[j] == false) || pos == -1 && mst[j] == false)
{
pos = j;
// cout<<"pos: "<<pos<<" ";
}
}
mst[pos] = true;
// cout<<endl;
// ans += dist[pos];
for (int j = 0; j < vec[pos].size(); j++)
{
int adjacent = vec[pos][j].first;
if (dist[adjacent] > vec[pos][j].second && mst[adjacent] == false)
{
dist[adjacent] = vec[pos][j].second;
}
}
}
int ans = 0;
for (int i = 0; i < dist.size(); i++)
{
ans += dist[i];
}
cout << "final answer from algo 1 is : " << ans << endl;
}
struct mycmp
{
bool operator()(const pair<int, int> &a, const pair<int, int> &b)
{
return a.second > b.second;
}
};
void prims_algo2(vector<pair<int, int>> vec[], int v)
{
vector<bool> mst(v, false);
vector<int> dist(v, INT_MAX);
dist[0] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, mycmp> pq;
pq.push({0, 0});
int ans = 0;
int count = 0;
for (int i = 0; i < v; i++)
{
while (pq.empty() == false && mst[pq.top().first] == true)
{
pq.pop();
}
pair<int, int> curr = pq.top();
pq.pop();
// cout << "curr.first() " << curr.first << " curr.second() " << curr.second << endl;
mst[curr.first] = true;
ans += curr.second;
for (int j = 0; j < vec[curr.first].size(); j++)
{
int adjacent = vec[curr.first][j].first;
if (mst[adjacent] == false && dist[adjacent] > vec[curr.first][j].second)
{
pq.push({adjacent, vec[curr.first][j].second});
}
}
}
cout << "Final answer from algo 2 is : " << ans << endl;
}
int main()
{
int v = 7;
vector<pair<int, int>> adj[v];
// adding all edges now
add_edge(adj, 0, 1, 7);
add_edge(adj, 0, 3, 5);
add_edge(adj, 1, 3, 9);
add_edge(adj, 1, 2, 8);
add_edge(adj, 1, 4, 7);
add_edge(adj, 2, 4, 5);
add_edge(adj, 3, 4, 15);
add_edge(adj, 3, 5, 6);
add_edge(adj, 4, 5, 8);
add_edge(adj, 4, 6, 9);
add_edge(adj, 5, 6, 11);
// int v = 5;
// vector<pair<int, int>> adj[v];
// add_edge(adj, 0, 1, 5);
// add_edge(adj, 0, 2, 2);
// add_edge(adj, 1, 2, 4);
// add_edge(adj, 1, 4, 1);
// add_edge(adj, 2, 3, 6);
// add_edge(adj, 4, 3, 3);
prims_algo1(adj, v);
prims_algo2(adj, v);
return 0;
}