-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathPrim's algorithm.cpp
130 lines (99 loc) · 3.57 KB
/
Prim's algorithm.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
/*
Prim's Algorithm
Given an undirected, connected and weighted graph G(V, E) with V number of vertices (which are numbered from 0 to V-1) and E number of edges.
Find and print the Minimum Spanning Tree (MST) using Prim's algorithm.
For printing MST follow the steps -
1. In one line, print an edge which is part of MST in the format -
v1 v2 w
where, v1 and v2 are the vertices of the edge which is included in MST and whose weight is w. And v1 <= v2 i.e. print the smaller vertex first
while printing an edge.
2. Print V-1 edges in above format in different lines.
Note : Order of different edges doesn't matter.
Input Format :
Line 1: Two Integers V and E (separated by space)
Next E lines : Three integers ei, ej and wi, denoting that there exists an edge between vertex ei and vertex ej with weight wi (separated by space)
Output Format :
Print the MST, as described in the task.
Constraints :
2 <= V, E <= 10^5
1 <= Wi <= 10^5
Time Limit: 1 sec
Sample Input 1 :
4 4
0 1 3
0 3 5
1 2 1
2 3 8
Sample Output 1 :
0 1 3
1 2 1
0 3 5
*/
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
int getMinimumVertex(vector<bool>& visited, vector<int>& weight) {
int v = visited.size();
int minVertex = -1;
for(int i = 0; i < v; i++) {
if(!visited[i] and (minVertex == -1 or weight[i] < weight[minVertex])) {
minVertex = i;
}
}
return minVertex;
}
vector<pair<pair<int,int>,int>> primsMST(vector<vector<int>>& graph, vector<bool>& visited, vector<int>& parent, vector<int>& weight) {
// First we find the vertex with minimum weight and then explore the remaining vertex
int v = visited.size();
// The loop runs to v - 1 since when we reach the last vertex
// al of it's neightbour would already have been analysed
for(int i = 0; i < v - 1; i++) {
// minimum weighted vertex
int minVertex = getMinimumVertex(visited, weight);
visited[minVertex] = true;
// Exploring the neighbours of minVertex
for(int j = 0; j < v; j++) {
if(graph[minVertex][j] and !visited[j] and graph[minVertex][j] < weight[j]) {
weight[j] = graph[minVertex][j];
parent[j] = minVertex;
}
}
}
vector<pair<pair<int,int>,int>> answer;
// Here the loop starts with 1 since parent[0] = -1 and as such there is no edge that ends at zero.
for(int i = 1; i < v; i++) {
if(parent[i] < i) {
answer.push_back({{parent[i], i}, weight[i]});
} else {
answer.push_back({{i, parent[i]}, weight[i]});
}
}
return answer;
}
int main() {
// Write your code here
int v, e;
cin >> v >> e;
vector<vector<int>> graph(v, vector<int>(v));
for(int i = 0; i < e; i++) {
int source, destination, weight;
cin >> source >> destination >> weight;
graph[source][destination] = weight;
graph[destination][source] = weight;
}
vector<bool> visited(v, false);
vector<int> parent(v);
vector<int> weight(v, INT_MAX); // Weight of vertices initialized with infinity
// Taking the 0 vertex as source
parent[0] = -1;
weight[0] = 0;
vector<pair<pair<int,int>,int>> MST = primsMST(graph, visited, parent, weight);
for(auto it : MST) {
cout << it.first.first << " " << it.first.second << " " << it.second << endl;
}
return 0;
}
// Time Complexity : O(V^2)
// Auxillary Space : O(V^2)
// Note : We can use priority queue and adjacency list to reduce the complexity to O((E + v)logV)