Question

I need to write a small program in c++ that executes Kruskal or Prim algorithm whatever...

I need to write a small program in c++ that executes Kruskal or Prim algorithm whatever you want to do. It must ask for a graph and present at the end The minimum cost spanning tree that results from applying the algorithm. It can be presented as if it were a list of Vertices with ordered pairs that solve the edges. Kruskal or Prim will work with non-directed graphs.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//kruskal algorithm

#include<bits/stdc++.h>
using namespace std;
  
// Creating shortcut for an integer pair
typedef pair<int, int> iPair;
  
// Structure to represent a graph
struct Graph
{
int V, E;
vector< pair<int, iPair> > edges;
  
// Constructor
Graph(int V, int E)
{
this->V = V;
this->E = E;
}
  
// Utility function to add an edge
void addEdge(int u, int v, int w)
{
edges.push_back({w, {u, v}});
}
  
// Function to find MST using Kruskal's
// MST algorithm
int kruskalMST();
};
  
// To represent Disjoint Sets
struct DisjointSets
{
int *parent, *rnk;
int n;
  
// Constructor.
DisjointSets(int n)
{
// Allocate memory
this->n = n;
parent = new int[n+1];
rnk = new int[n+1];
  
// Initially, all vertices are in
// different sets and have rank 0.
for (int i = 0; i <= n; i++)
{
rnk[i] = 0;
  
//every element is parent of itself
parent[i] = i;
}
}
  
// Find the parent of a node 'u'
// Path Compression
int find(int u)
{
/* Make the parent of the nodes in the path
from u--> parent[u] point to parent[u] */
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
  
// Union by rank
void merge(int x, int y)
{
x = find(x), y = find(y);
  
/* Make tree with smaller height
a subtree of the other tree */
if (rnk[x] > rnk[y])
parent[y] = x;
else // If rnk[x] <= rnk[y]
parent[x] = y;
  
if (rnk[x] == rnk[y])
rnk[y]++;
}
};
  
/* Functions returns weight of the MST*/
  
int Graph::kruskalMST()
{
int mst_wt = 0; // Initialize result
  
// Sort edges in increasing order on basis of cost
sort(edges.begin(), edges.end());
  
// Create disjoint sets
DisjointSets ds(V);
  
// Iterate through all sorted edges
vector< pair<int, iPair> >::iterator it;
for (it=edges.begin(); it!=edges.end(); it++)
{
int u = it->second.first;
int v = it->second.second;
  
int set_u = ds.find(u);
int set_v = ds.find(v);
  
// Check if the selected edge is creating
// a cycle or not (Cycle is created if u
// and v belong to same set)
if (set_u != set_v)
{
// Current edge will be in the MST
// so print it
cout << u << " - " << v << endl;
  
// Update MST weight
mst_wt += it->first;
  
// Merge two sets
ds.merge(set_u, set_v);
}
}
  
return mst_wt;
}
  
// Driver program
int main()
{
  
int V = 9, E = 14;
Graph kru(V, E);
  
// EDGES FROM 1ST VERTEX
kru.addEdge(0, 1, 1);
kru.addEdge(0, 2, 5);
kru.addEdge(0, 3, 8);
kru.addEdge(0, 4, 6);
     
kru.addEdge(1, 2, 9);
kru.addEdge(1, 3, 10);
kru.addEdge(1, 4, 2);
  
     
kru.addEdge(2, 3, 4);
kru.addEdge(2, 4, 7);
  
kru.addEdge(3, 4, 3);
  
  
  
cout << "Edges of MST are \n";
int mst_wt = kru.kruskalMST();
  
cout << "\nWeight of MST is " << mst_wt;
  
return 0;
}

output

Add a comment
Know the answer?
Add Answer to:
I need to write a small program in c++ that executes Kruskal or Prim algorithm whatever...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT