diff --git a/Graph/Minimum Spanning Tree/PrimsMST_PQ.cpp b/Graph/Minimum Spanning Tree/PrimsMST_PQ.cpp new file mode 100644 index 0000000..18cbc5a --- /dev/null +++ b/Graph/Minimum Spanning Tree/PrimsMST_PQ.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +void primsMST(vector> graph[], int V) +{ + int weightTotal = 0; + int m = V - 1; + int cnt = 0; + + priority_queue, vector>, greater>> pq; + int visited[V] = {0}, parent[V] = {-1}; + + visited[0] = 1; + for (int j = 0; j < graph[0].size(); j++) + { + int v = graph[0][j].first; + int w = graph[0][j].second; + + if (!visited[v]) + pq.push({w, v}), parent[v] = 0; + } + + while (!pq.empty() && cnt != m) + { + pair node = pq.top(); + pq.pop(); + int u = node.second; + int weight = node.first; + + if (visited[u]) + continue; + + visited[u] = 1; + weightTotal += weight; + cnt++; + + for (int j = 0; j < graph[u].size(); j++) + { + int v = graph[u][j].first; + int w = graph[u][j].second; + + if (!visited[v]) + pq.push({w, v}), parent[v] = u; + } + } + + if (cnt != m) + cout << "MST not possible.\n"; + else + { + cout << weightTotal << endl; + for (int i = 1; i < V; i++) + cout << parent[i] << " " << i << endl; + } +} + +int main() +{ + int V = 9; + vector> graph[V]; + + graph[0].push_back({1, 4}); + graph[0].push_back({7, 8}); + graph[1].push_back({2, 8}); + graph[1].push_back({7, 11}); + graph[2].push_back({3, 7}); + graph[2].push_back({8, 2}); + graph[2].push_back({5, 4}); + graph[3].push_back({4, 9}); + graph[3].push_back({5, 14}); + graph[4].push_back({5, 10}); + graph[5].push_back({6, 2}); + graph[6].push_back({7, 1}); + graph[6].push_back({8, 6}); + graph[7].push_back({8, 7}); + primsMST(graph, V); + return 0; +} diff --git a/Graph/Minimum Spanning Tree/Question.txt b/Graph/Minimum Spanning Tree/Question.txt new file mode 100644 index 0000000..17bd3a7 --- /dev/null +++ b/Graph/Minimum Spanning Tree/Question.txt @@ -0,0 +1 @@ +A minimum spanning tree (MST) or minimum weight spanning tree is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. diff --git a/Graph/Minimum Spanning Tree/kruskalMST.cpp b/Graph/Minimum Spanning Tree/kruskalMST.cpp new file mode 100644 index 0000000..609e831 --- /dev/null +++ b/Graph/Minimum Spanning Tree/kruskalMST.cpp @@ -0,0 +1,97 @@ +#include +using namespace std; + +struct edge +{ + int u; + int v; + int w; +}; + +struct point +{ + int parent; + int rank; +}; + +int comp(edge a, edge b) +{ + if (a.w == b.w) + return a.v < b.v; + return a.w < b.w; +} + +int find(point subset[], int node) +{ + if (subset[node].parent == -1) + return node; + return subset[node].parent = find(subset, subset[node].parent); +} + +void unionFun(point subset[], int xroot, int yroot) +{ + if (subset[yroot].rank >= subset[xroot].rank) + { + subset[xroot].parent = yroot; + subset[yroot].rank += subset[xroot].rank; + } + else + { + subset[yroot].parent = xroot; + subset[xroot].rank += subset[yroot].rank; + } +} + +void kruskalMST(vector graph, int V, int E) +{ + sort(graph.begin(), graph.end(), comp); + point subset[V]; + int cost = 0; + + for (int i = 0; i < V; i++) + { + subset[i].parent = -1; + subset[i].rank = 1; + } + + int cnt = 0; + vector ans; + for (int i = 0; cnt < V - 1, i < E; i++) + { + edge node = graph[i]; + int u = node.u; + int v = node.v; + int w = node.w; + + int xroot = find(subset, u); + int yroot = find(subset, v); + + if (xroot != yroot) + { + cost += w; + ans.push_back(node); + unionFun(subset, xroot, yroot); + } + } + cout << cost << endl; + for (int i = 0; i < ans.size(); i++) + { + cout << ans[i].u << " " << ans[i].v << " " << ans[i].w << endl; + } +} + +int main() +{ + int V = 4; + int E = 5; + + vector graph; + graph.push_back({0, 1, 10}); + graph.push_back({0, 2, 6}); + graph.push_back({0, 3, 5}); + graph.push_back({1, 3, 15}); + graph.push_back({2, 3, 4}); + + kruskalMST(graph, V, E); + return 0; +}