0% found this document useful (0 votes)
10 views4 pages

Daa Assignment2

Uploaded by

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

Daa Assignment2

Uploaded by

Kinza Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

22SP-032-CS

UIT University

Department of Computer Science

Assignment -2 (CSC 301) Design and Analysis of Algorithms

CLO-2

Max Marks: 10

Name : Kinza Fatima


Roll no: 22SP-032-CS
22SP-032-CS

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Edge {
int src, dest, weight;

// Comparison operator for sorting edges by weight


bool operator<(const Edge& other) const {
return weight < [Link];
}
};

class DisjointSet {
private:
vector<int> parent, rank;

public:
// Constructor to initialize DSU
DisjointSet(int n) {
[Link](n);
[Link](n, 0);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}

int find(int u) {
if (u != parent[u]) {
parent[u] = find(parent[u]);
}
return parent[u];
}

void unionSets(int u, int v) {


int rootU = find(u);
int rootV = find(v);

if (rootU != rootV) {
if (rank[rootU] < rank[rootV]) {
parent[rootU] = rootV;
} else if (rank[rootU] > rank[rootV]) {
parent[rootV] = rootU;
} else {
parent[rootV] = rootU;
rank[rootU]++;
}
22SP-032-CS

}
}
};
class Graph {
private:
int V; // Number of vertices
vector<Edge> edges; // List of edges

public:
// Constructor
Graph(int V) : V(V) {}

// Add an edge to the graph


void addEdge(int src, int dest, int weight) {
edges.push_back({src, dest, weight});
}

void kruskalMST() {
sort([Link](), [Link]());

DisjointSet dsu(V);
vector<Edge> mst;

for (const auto& edge : edges) {


if ([Link]([Link]) != [Link]([Link])) {
mst.push_back(edge);
[Link]([Link], [Link]);
}
}

cout << "Edges in the Minimum Spanning Tree (MST):" << endl;
for (const auto& edge : mst) {
cout << [Link] << " -- " << [Link] << " == " << [Link] << endl;
}
}
};

int main() {
int V = 8; // Number of vertices
Graph g(V);

[Link](0, 1, 8);
[Link](0, 7, 5);
[Link](0, 5, 10);
[Link](7, 6, 3);
[Link](6, 3, 2);
[Link](7, 1, 4);
[Link](6, 4, 3);
[Link](4, 1, 4);
22SP-032-CS

[Link](4, 3, 1);
[Link](3, 2, 3);
[Link](3, 5, 6);
[Link](2, 1, 4);
[Link](2, 5, 3);
[Link](5, 1, 4);

[Link]();

return 0;
}

You might also like