Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views3 pages

Program 9

The document presents a C++ implementation of Prim's Algorithm to find the Minimum Cost Spanning Tree (MST) of a connected undirected graph using a priority queue (min-heap). It includes code that initializes the graph, processes edges, and outputs the MST along with its total cost. The program prompts the user for the number of vertices and edges, and then for the edges themselves to construct the graph.

Uploaded by

rorariw821
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)
4 views3 pages

Program 9

The document presents a C++ implementation of Prim's Algorithm to find the Minimum Cost Spanning Tree (MST) of a connected undirected graph using a priority queue (min-heap). It includes code that initializes the graph, processes edges, and outputs the MST along with its total cost. The program prompts the user for the number of vertices and edges, and then for the edges themselves to construct the graph.

Uploaded by

rorariw821
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/ 3

PROGRAM 9

Aim:

To implement Prim’s Algorithm to find the Minimum Cost Spanning Tree (MST) of a given
connected undirected graph using a priority queue (min-heap).

CODE:

#include <iostream>

#include <vector>
#include <queue>

#include <climits>

using namespace std;

// Structure to represent an edge

struct Edge {
int dest, weight;

};

// Custom comparator for priority queue (min-heap)

struct Compare {

bool operator()(pair<int, int> a, pair<int, int> b) {

return a.second > b.second; // Min-heap based on weight


}

};

// Function to implement Prim's Algorithm

void primMST(vector<vector<Edge>>& graph, int V) {

vector<int> key(V, INT_MAX); // Store minimum edge weight to reach each vertex

vector<int> parent(V, -1); // Store MST edges


vector<bool> inMST(V, false); // Track visited nodes
priority_queue<pair<int, int>, vector<pair<int, int>>, Compare> pq;

// Start from vertex 0


key[0] = 0;

pq.push({0, 0}); // (vertex, weight)

while (!pq.empty()) {

int u = pq.top().first;

pq.pop();

inMST[u] = true; // Include vertex in MST

// Explore all adjacent edges

for (auto& edge : graph[u]) {

int v = edge.dest, weight = edge.weight;

if (!inMST[v] && weight < key[v]) { // If new edge has smaller weight

key[v] = weight;

pq.push({v, key[v]});

parent[v] = u;
}

// Print the MST

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

int totalCost = 0;
for (int i = 1; i < V; i++) {

cout << parent[i] << " - " << i << " : " << key[i] << endl;
totalCost += key[i];
}

cout << "Total Cost: " << totalCost << endl;

// Main function

int main() {

int V, E;

cout << "Enter number of vertices and edges: ";

cin >> V >> E;

vector<vector<Edge>> graph(V);

cout << "Enter edges (src dest weight):\n";

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

int src, dest, weight;

cin >> src >> dest >> weight;

graph[src].push_back({dest, weight});

graph[dest].push_back({src, weight}); // Undirected graph

primMST(graph, V);

return 0;

You might also like