Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimum Spanning Tree #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Graph/Minimum Spanning Tree/PrimsMST_PQ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <bits/stdc++.h>
using namespace std;

void primsMST(vector<pair<int, int>> graph[], int V)
{
int weightTotal = 0;
int m = V - 1;
int cnt = 0;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> 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<int, int> 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<pair<int, int>> 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;
}
1 change: 1 addition & 0 deletions Graph/Minimum Spanning Tree/Question.txt
Original file line number Diff line number Diff line change
@@ -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.
97 changes: 97 additions & 0 deletions Graph/Minimum Spanning Tree/kruskalMST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <bits/stdc++.h>
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<edge> 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<edge> 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<edge> 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;
}