0% found this document useful (0 votes)
5 views8 pages

Daa 56

The document presents two programs for finding the minimum cost spanning tree of an undirected graph using Prim's and Kruskal's algorithms. It includes implementations for both algorithms and compares their execution times on graphs of increasing sizes. The results are outputted to a file and displayed in a table format.

Uploaded by

sheoranpooja726
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Daa 56

The document presents two programs for finding the minimum cost spanning tree of an undirected graph using Prim's and Kruskal's algorithms. It includes implementations for both algorithms and compares their execution times on graphs of increasing sizes. The results are outputted to a file and displayed in a table format.

Uploaded by

sheoranpooja726
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAM 5

Find minimum cost spanning tree of a given undirected


graph using prim's algorithm

#include<iostream>
#include<vector>

using namespace std;


class graph{
private:
int size;
vector<vector <pair<int,int > > >adjlist;

public:
graph(int size){
this->size=size;
adjlist.resize(size);
}

void addEdge(int u,int v,int weight){


adjlist[u].push_back(make_pair(v,weight));
adjlist[v].push_back(make_pair(u,weight));
}
vector<vector <pair<int,int > > >&getadjlist(){
return adjlist;
}

void printgraph(graph g){


//vector<vector<pair<int,int> > >adjlist1=g.getadjlist();
for(int i=0;i<size;i++){
cout<<i<<" ";
for( auto p: g.adjlist[i]){
cout<<"("<<p. rst<<" "<<p.second<<")";

}
cout<<endl;
}
}
fi
void prims(int start){

vector<int>child(size,INT_MAX);
vector<int>parent(size,-1);
vector<bool>inmst(size,false);
child[start]=0;

priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int,


int> > > pq;
pq.push(make_pair(0,start));

while(!pq.empty()){
int u=pq.top().second;
pq.pop();
if(inmst[u])
continue;

inmst[u]=true;

for(auto [v,weight]:adjlist[u]){
if(!inmst[v]&&child[v]>weight){
child[v]=weight;
pq.push(make_pair(child[v],v));
parent[v]=u;
}
}}

for(int i=0;i<size;i++){
cout<<parent[i]<<"-"<<i<<" "<<child[i]<<endl;
}
}

void kruskal(){

};
};

int main(){
int V = 5;
graph g(V);

g.addEdge(0, 1, 2);
g.addEdge(0, 3, 6);
g.addEdge(1, 2, 3);
g.addEdge(1, 3, 8);
g.addEdge(1, 4, 5);
g.addEdge(2, 4, 7);
g.addEdge(3, 4, 9);

cout << "Graph:\n";


g.printgraph(g);

cout << "\nMinimum Spanning Tree (Prim's Algorithm):\n";


g.prims(0);

return 0;

OUTPUT:
PROGRAM 7

compare the execution time of prim's and kruskal's


algorithms for computing minimum spanning tree on
graph of increasing sizes.

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <chrono>
#include <cstdlib>

using namespace std;


using namespace chrono;

typedef pair<int, int> pii;

void primMST(int V, vector<vector<pii> > &adj) {


priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<int> key(V, INT_MAX), parent(V, -1);
vector<bool> inMST(V, false);
key[0] = 0;
pq.push(make_pair(0, 0));

while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (inMST[u]) continue;
inMST[u] = true;
for (size_t i = 0; i < adj[u].size(); i++) {
int weight = adj[u][i]. rst, v = adj[u][i].second;
if (!inMST[v] && weight < key[v]) {
key[v] = weight;
pq.push(make_pair(weight, v));
parent[v] = u;
}
fi
}
}
}

struct Edge {
int u, v, weight;
};

class DSU {
vector<int> parent, rank;
public:
DSU(int n) {
parent.resize(n);
rank.resize(n, 0);
for (int i = 0; i < n; i++) parent[i] = i;
}
int nd(int x) { return parent[x] == x ? x : (parent[x] = nd(parent[x])); }
void unite(int x, int y) {
int rx = nd(x), ry = nd(y);
if (rx != ry) {
if (rank[rx] < rank[ry]) parent[rx] = ry;
else if (rank[rx] > rank[ry]) parent[ry] = rx;
else { parent[ry] = rx; rank[rx]++; }
}
}
};

bool cmp(Edge a, Edge b) { return a.weight < b.weight; }

void kruskalMST(int V, vector<Edge> &edges) {


sort(edges.begin(), edges.end(), cmp);
DSU dsu(V);
for (size_t i = 0; i < edges.size(); i++) {
if (dsu. nd(edges[i].u) != dsu. nd(edges[i].v)) {
dsu.unite(edges[i].u, edges[i].v);
}
}
}

void generateGraph(int V, int E, vector<vector<pii> > &adj, vector<Edge>


&edges) {
adj.assign(V, vector<pii>());
edges.clear();
srand(time(0));
fi
fi
fi
fi
fi
fi
for (int i = 0; i < E; i++) {
int u = rand() % V;
int v = rand() % V;
int weight = 1 + rand() % 100;
if (u != v) {
adj[u].push_back(make_pair(weight, v));
adj[v].push_back(make_pair(weight, u));
edges.push_back((Edge){u, v, weight});
}
}
}

int main() {

int n=5;
vector<pair<int,int> >adj[n];

vector<int> sizes;
sizes.push_back(10);
sizes.push_back(100);
sizes.push_back(1000);
sizes.push_back(10000);
sizes.push_back(50000);
sizes.push_back(100000);
sizes.push_back(200000);

vector<long long> primTimes, kruskalTimes;

for (size_t i = 0; i < sizes.size(); i++) {


int V = sizes[i];
int E = V * 3;
vector<vector<pii> > adj;
vector<Edge> edges;
generateGraph(V, E, adj, edges);

auto start = high_resolution_clock::now();


primMST(V, adj);
auto end = high_resolution_clock::now();
primTimes.push_back(duration_cast<milliseconds>(end - start).count());

start = high_resolution_clock::now();
kruskalMST(V, edges);
end = high_resolution_clock::now();
kruskalTimes.push_back(duration_cast<milliseconds>(end -
start).count());

cout << "V=" << V << " | Prim: " << primTimes.back() << "ms | Kruskal:
" << kruskalTimes.back() << "ms\n";
}

FILE *f = fopen("mst.txt", "w");


for (size_t i = 0; i < sizes.size(); i++)
fprintf(f, "%d %lld %lld\n", sizes[i], primTimes[i], kruskalTimes[i]);
fclose(f);

return 0;
}

OUTPUT:

prim's kruskal's

700

525

350

175

0
10 100 1000 10000 50000 100000 200000
V PRIM'S KRUSKAL'S

10 0 0

100 0 0

1000 3 0

10000 25 5

50000 131 29

100000 314 58

200000 659 117

You might also like