1
- /* * Given an undirected, connected and weighted graph, find MST of the graphs
2
- * using Prim’s algorithm.
1
+ /* *
2
+ * Prim’s MST algorithm for adjacency list representation of undirected,
3
+ * connected and weighted graphs.
3
4
*/
4
5
#include < bits/stdc++.h>
5
6
using namespace std ;
6
7
7
- typedef pair<int , int > int_pair;
8
-
9
8
class graph {
10
9
int nv;
11
10
// A weighted graph, so need to store a vertex and
12
11
// weight pair for every single vertex.
13
- vector<vector<int_pair>> adj;
14
-
12
+ vector<vector<pair<int , int >>> adj;
15
13
public:
16
14
graph (int num) : nv(num), adj(num) {}
17
15
void addEdge (int u, int v, int wt) {
@@ -24,42 +22,44 @@ class graph {
24
22
25
23
void graph::prims (int s) {
26
24
// Create a min-heap using priority_queue container. priority_queue
27
- // container class provides a constructor that requires two extra
28
- // arguments to make it a min heap i.e. vector<...> & greater<...>.
29
- priority_queue<int_pair, vector<int_pair>, greater<int_pair>> heap;
30
-
31
- // A vector for weight and initialise as INT_MAX.
32
- vector<int > weight (nv, INT_MAX);
25
+ // container constructor requires two extra arguments to make it a
26
+ // min heap i.e. vector<...> & greater<...>.
27
+ priority_queue<pair<int , int >, vector<pair<int , int >>,
28
+ greater<pair<int , int >> > heap;
33
29
30
+ // A vector to store weights of vertices with all weights
31
+ // intialised as INT_MAX.
32
+ vector<int > weight (nv, INT_MAX);
34
33
// Another vector to store the parent of vertices.
35
34
vector<int > parent (nv, -1 );
36
-
37
35
// A vector to keep track of vertices included in MST.
38
36
vector<bool > mst (nv, false );
39
-
37
+
40
38
// Set weight of source vertex as zero.
41
39
weight[s] = 0 ;
40
+
42
41
// Insert source vertex in the heap.
43
42
heap.push (make_pair (weight[s], s));
44
-
43
+
45
44
while (!heap.empty ()) {
46
45
// Extract the min vertex based on their associated weights.
47
46
int u = heap.top ().second ;
48
47
heap.pop ();
49
-
48
+
50
49
// Include extracted vertex in mst.
51
50
mst[u] = true ;
52
- vector<pair< int , int >>::iterator i;
53
- for (i = adj[u].begin (); i != adj[u].end (); ++i) {
54
- // Get the vertex label and the weight
51
+
52
+ for (auto i = adj[u].begin (); i != adj[u].end (); ++i) {
53
+ // Get the vertex label and the weights
55
54
int v = (*i).first ;
56
55
int v_wt = (*i).second ;
57
-
56
+
58
57
// If v is not in mst and edge (u, v) weight < weight of v
59
58
if (mst[v] == false && v_wt < weight[v]) {
60
59
// Update weight of v
61
60
weight[v] = v_wt;
62
61
heap.push (make_pair (weight[v], v));
62
+ // Update parent of v
63
63
parent[v] = u;
64
64
}
65
65
}
@@ -72,14 +72,15 @@ void graph::prims(int s) {
72
72
73
73
int main () {
74
74
graph g (5 );
75
-
75
+
76
76
g.addEdge (0 , 1 , 1 );
77
77
g.addEdge (0 , 2 , 3 );
78
78
g.addEdge (1 , 2 , 7 );
79
79
g.addEdge (2 , 3 , 4 );
80
80
g.addEdge (3 , 4 , 6 );
81
81
82
82
g.prims (0 );
83
-
83
+
84
84
return 0 ;
85
85
}
86
+ // Time complexity : O(E*Log V))
0 commit comments