0% found this document useful (0 votes)
6 views

notes_20241023105527

Uploaded by

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

notes_20241023105527

Uploaded by

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

10.

23 10:54 AM
#include <bits/stdc++.h>
using namespace std;

const int MAX = 4;


vector<pair<int, int> > adj[MAX];

bool mstSet[MAX];
int key[MAX], parent[MAX];

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


adj[u].push_back(make_pair(v, wt));
adj[v].push_back(make_pair(u, wt));
}

int minKey() {
int min = INT_MAX, min_index;
for (int v = 0; v < MAX; v++) {
if (!mstSet[v] && key[v] < min) {
min = key[v];
min_index = v;
}
}
return min_index;
}

void primMST() {
for (int i = 0; i < MAX; i++) {
key[i] = INT_MAX;
mstSet[i] = false;
parent[i] = -1;
}

key[0] = 0;

for (int count = 0; count < MAX - 1; count++) {


int u = minKey();
mstSet[u] = true;

for (int i = 0; i < adj[u].size(); i++) {


int v = adj[u][i].first;
int weight = adj[u][i].second;
if (!mstSet[v] && weight < key[v]) {
parent[v] = u;
key[v] = weight;
}
}
}

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


string departments[MAX] = {"IT", "CS", "ENTC", "Robotics"};
for (int i = 1; i < MAX; i++) {
cout << departments[parent[i]] << " - " << departments[i] << " : " <<
key[i] << endl;
}
}

void displayInput() {
cout << "Input Edges:\n";
cout << "IT to CS: 12\n";
cout << "CS to ENTC: 8\n";
cout << "IT to Robotics: 9\n";
cout << "IT to ENTC: 4\n";
cout << "\n";
}

int main() {
displayInput();

addEdge(0, 1, 12);
addEdge(1, 2, 8);
addEdge(0, 3, 9);
addEdge(0, 2, 4);

primMST();

return 0;
}

You might also like