Skip to content

Commit d2cede0

Browse files
committed
Add Prim's algorithm for minimum spanning tree
1 parent e96835e commit d2cede0

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ A brief explanation to each of the following topics will be added soon, hopefull
6262
- [Floyd Warshal's Algorithm](https://github.com/OmarBazaraa/Competitive-Programming/blob/master/graphs/shortest_path/floyd_warshal.cpp)
6363
- [Minimum Spanning Tree](https://github.com/OmarBazaraa/Competitive-Programming/tree/master/graphs/minimum_spanning_tree)
6464
- [Kruskal's Algorithm](https://github.com/OmarBazaraa/Competitive-Programming/blob/master/graphs/minimum_spanning_tree/kruskal.cpp)
65-
- Prim's Algorithm **<i>(comming soon)</i>**
65+
- [Prim's Algorithm](https://github.com/OmarBazaraa/Competitive-Programming/blob/master/graphs/minimum_spanning_tree/prim.cpp)
6666
- [Strongly Connected Components (SCC)](https://github.com/OmarBazaraa/Competitive-Programming/tree/master/graphs/strongly_connected_components)
6767
- [Kosaraju's Algorithm](https://github.com/OmarBazaraa/Competitive-Programming/blob/master/graphs/strongly_connected_components/kosaraju.cpp)
6868
- [Lowest Common Ancestor (LCA)](https://github.com/OmarBazaraa/Competitive-Programming/tree/master/graphs/lowest_common_ancestor)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
const int N = 100100;
6+
7+
8+
// Struct holds all needed information about the edges
9+
// with comparison operator defined for the priority queue sorting.
10+
struct edge {
11+
int to, weight, weightSum;
12+
13+
edge() {}
14+
edge(int t, int w, int s = 0) : to(t), weight(w), weightSum(s) {}
15+
16+
// Note that we are actually implementing the greater than operator (not the less than operator)
17+
// as the priority queue is implemented as max heap.
18+
bool operator<(const edge& rhs) const {
19+
return weightSum > rhs.weightSum;
20+
}
21+
};
22+
23+
int n, m;
24+
bool vis[N];
25+
vector<edge> edges[N];
26+
27+
// Returns the total weight of the minimum spanning tree of the given weighted graph.
28+
// O(n.log(n))
29+
void primMST() {
30+
priority_queue<edge> q;
31+
q.push(edge(src, 0));
32+
33+
int MST = 0;
34+
35+
while (!q.empty()) {
36+
int u = q.top().to;
37+
int w = q.top().weight;
38+
int s = q.top().weightSum;
39+
q.pop();
40+
41+
if (vis[u]++) {
42+
continue;
43+
}
44+
45+
MST += w;
46+
47+
for (edge& e : edges[u]) {
48+
if (!vis[e.to]) {
49+
q.push(edge(e.to, e.weight, s + e.weight));
50+
}
51+
}
52+
}
53+
54+
return MST;
55+
}
56+
57+
// Reads a weighted undirected graph.
58+
void read() {
59+
cin >> n >> m;
60+
61+
while (m--) {
62+
int u, v, w;
63+
scanf("%d %d %d", &u, &v, &w);
64+
edges[u].push_back(edge(v, w));
65+
edges[v].push_back(edge(u, w));
66+
}
67+
}

0 commit comments

Comments
 (0)