0% found this document useful (0 votes)
14 views10 pages

Dsa Exp 8 9 10

Uploaded by

rajatvernekar40
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)
14 views10 pages

Dsa Exp 8 9 10

Uploaded by

rajatvernekar40
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
You are on page 1/ 10

CODE

#include <iostream>
#include <vector>
using namespace std;

void createGraph(vector<vector<int> >& adjMatrix, int edges) {


int u, v;
cout << "Enter edges (u v) as pairs (0-indexed):\n";
for (int i = 0; i < edges; i++) {
cin >> u >> v;
adjMatrix[u][v] = 1;
adjMatrix[v][u] = 1;
}
}

void displayGraph(const vector<vector<int> >& adjMatrix) {


int V = adjMatrix.size();
cout << "Adjacency Matrix:\n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
cout << adjMatrix[i][j] << " ";
}
cout << endl;
}
}

void calculateDegrees(const vector<vector<int> >& adjMatrix) {


int V = adjMatrix.size();
cout << "\nDegree of each vertex:\n";
for (int i = 0; i < V; i++) {
int degree = 0;
for (int j = 0; j < V; j++) {
if (adjMatrix[i][j] == 1) {
degree++;
}
}
cout << "Vertex " << i << ": " << degree << endl;
}
}

int main() {
int V, E;
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;

vector<vector<int> > adjMatrix(V, vector<int>(V, 0));

createGraph(adjMatrix, E);
displayGraph(adjMatrix);
calculateDegrees(adjMatrix);
return 0;
}

OUTPUT
CODE
#include <iostream>

#include <vector>

#include <queue>

using namespace std;

void BFS(const vector<vector<int> >& tree, int root) {

cout << "BFS Traversal: ";

queue<int> q;

vector<bool> visited(tree.size(), false);

q.push(root);

visited[root] = true;

while (!q.empty()) {

int node = q.front();

q.pop();

cout << node << " ";

for (size_t i = 0; i < tree[node].size(); i++) {

int neighbor = tree[node][i];

if (!visited[neighbor]) {

q.push(neighbor);

visited[neighbor] = true;

cout << endl;

void DFS(const vector<vector<int> >& tree, int node, vector<bool>& visited) {

cout << node << " ";

visited[node] = true;
for (size_t i = 0; i < tree[node].size(); i++) {

int neighbor = tree[node][i];

if (!visited[neighbor]) {

DFS(tree, neighbor, visited);

void DFS_Start(const vector<vector<int> >& tree, int root) {

vector<bool> visited(tree.size(), false);

cout << "DFS Traversal: ";

DFS(tree, root, visited);

cout << endl;

int main() {

int V, E;

cout << "Enter number of vertices: ";

cin >> V;

cout << "Enter number of edges: ";

cin >> E;

vector<vector<int> > tree(V);

cout << "Enter edges (u v) as pairs (0-indexed):\n";

for (int i = 0; i < E; i++) {

int u, v;

cin >> u >> v;

tree[u].push_back(v);

tree[v].push_back(u);

int root;

cout << "Enter root node for traversal: ";


cin >> root;

BFS(tree, root);

DFS_Start(tree, root);

return 0;

OUTPUT
CODE

#include <iostream>

#include <vector>

#include <algorithm>

#include <queue>

using namespace std;

class Edge {

public:

int u, v, weight;

Edge(int u, int v, int weight) : u(u), v(v), weight(weight) {}

};

// Comparison function for sorting edges in Kruskal's algorithm

bool compareEdges(const Edge& a, const Edge& b) {

return a.weight < b.weight;

// Disjoint Set (Union-Find) for Kruskal's algorithm

class DisjointSet {

private:

vector<int> parent, rank;

public:

DisjointSet(int n) {

parent.resize(n);

rank.resize(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[rootV] = rootU;

} else if (rank[rootU] < rank[rootV]) {

parent[rootU] = rootV;

} else {

parent[rootV] = rootU;

rank[rootU]++;

};

// Kruskal's Algorithm

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

sort(edges.begin(), edges.end(), compareEdges);

DisjointSet ds(V);

vector<Edge> mst;

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

const Edge& edge = edges[i];

if (ds.find(edge.u) != ds.find(edge.v)) {

mst.push_back(edge);

ds.unionSets(edge.u, edge.v);

cout << "Kruskal's MST Edges:\n";

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


cout << mst[i].u << " -- " << mst[i].v << " == " << mst[i].weight << endl;

// Prim's Algorithm

void prim(int V, const vector<vector<pair<int, int> > >& adjList) {

vector<bool> inMST(V, false);

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

int start = 0;

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

int mstCost = 0;

cout << "Prim's MST Edges:\n";

while (!pq.empty()) {

int weight = pq.top().first;

int u = pq.top().second;

pq.pop();

if (inMST[u]) continue;

inMST[u] = true;

mstCost += weight;

cout << "Vertex included: " << u << " with weight " << weight << endl;

for (size_t i = 0; i < adjList[u].size(); i++) {

int v = adjList[u][i].first;

int w = adjList[u][i].second;

if (!inMST[v]) {

pq.push(make_pair(w, v));

cout << "Total cost of MST: " << mstCost << endl;
}

int main() {

int V, E;

cout << "Enter number of vertices: ";

cin >> V;

cout << "Enter number of edges: ";

cin >> E;

vector<Edge> edges;

vector<vector<pair<int, int> > > adjList(V);

cout << "Enter edges (u v weight):\n";

for (int i = 0; i < E; i++) {

int u, v, weight;

cin >> u >> v >> weight;

edges.push_back(Edge(u, v, weight));

adjList[u].push_back(make_pair(v, weight));

adjList[v].push_back(make_pair(u, weight));

cout << "\nKruskal's Algorithm:\n";

kruskal(V, edges);

cout << "\nPrim's Algorithm:\n";

prim(V, adjList);

return 0;

}
OUTPUT

You might also like