-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrim's Algorithm
113 lines (77 loc) · 2.88 KB
/
Prim's Algorithm
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
Refrence : https://practice.geeksforgeeks.org/problems/minimum-spanning-tree/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
--------------------------------------------------------------------------------------
class Solution
{
public:
//Function to find sum of weights of edges of the Minimum Spanning Tree.
int spanningTree(int V, vector<vector<int>> adj[])
{
vector<int> min_wt(V + 1 , INT_MAX);
min_wt[0] = 0;
vector<bool> mst(V + 1 , false); // minimum spanning tree
vector<int> par( V + 1 , -1); // parent
par[0] = -1;// source node has no parent
priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>> pq;
pq.push({0 , 0});// weight , node
while(!pq.empty()){
auto node = pq.top().second;// node
pq.pop();
mst[node] = true;
for(auto X : adj[node]){
if( mst[X[0]] == false and min_wt[X[0]] > X[1]){
min_wt[X[0]] = X[1];
par[X[0]] = node;
pq.push({X[1] , X[0]});
}
}
}
int sum = 0;
for(int i=1 ;i<V ;i++){
sum += min_wt[i];
}
return sum;
}
};
Time Complexity Analysis
If adjacency list is used to represent the graph, then using breadth first search, all the vertices can be traversed in O(V + E) time.
We traverse all the vertices of graph using breadth first search and use a min heap for storing the vertices not yet included in the MST.
To get the minimum weight edge, we use min heap as a priority queue.
Min heap operations like extracting minimum element and decreasing key value takes O(logV) time.
So, overall time complexity
= O(E + V) x O(logV)
= O((E + V)logV)
= O(ElogV)
Time : O((N + E)logN ) ~ O(ElogN)
Space : O(N+ E) + O(N) + O(N) + O(N) ( ==> adj list , min_wt[] , par[] , mst[] )
------------------------------------------------------------------------------
// { Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--) {
int V, E;
cin >> V >> E;
vector<vector<int>> adj[V];
int i=0;
while (i++<E) {
int u, v, w;
cin >> u >> v >> w;
vector<int> t1,t2;
t1.push_back(v);
t1.push_back(w);
adj[u].push_back(t1);
t2.push_back(u);
t2.push_back(w);
adj[v].push_back(t2);
}
Solution obj;
cout << obj.spanningTree(V, adj) << "\n";
}
return 0;
}
// } Driver Code Ends