Skip to content

Commit d06be43

Browse files
authored
Create main.cpp
1 parent 21a7554 commit d06be43

File tree

1 file changed

+109
-0
lines changed
  • 23 - Graph Data Structure Problems/14 - Minimum Spanning Tree | Prim's Algorithm

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<list>
4+
#include<climits>
5+
6+
using namespace std;
7+
8+
class Graph {
9+
public:
10+
vector<list<pair<int, int>>> adjacencyList; // Adjacency list to store the graph (each list element is a pair: destination vertex and edge weight)
11+
12+
// Constructor to initialize the adjacency list with n vertices
13+
Graph(int n) {
14+
adjacencyList.resize(n); // Resize the adjacency list to have n vertices
15+
}
16+
17+
// Method to add an undirected edge between source and destination with a given weight
18+
void addEdge(int source, int destination, int weight) {
19+
adjacencyList[source].push_back({destination, weight});
20+
adjacencyList[destination].push_back({source, weight});
21+
}
22+
23+
// Method to print the adjacency list of the graph
24+
void printAdjacencyList() {
25+
for (int i = 0; i < adjacencyList.size(); ++i) {
26+
cout << i << " -> "; // Print the vertex number
27+
for (auto neighbour : adjacencyList[i]) { // Traverse through all neighbors of the vertex
28+
cout << "(" << neighbour.first << ", " << neighbour.second << "), "; // Print destination vertex and weight
29+
}
30+
cout << endl;
31+
}
32+
}
33+
34+
// Implementation of Prim's Algorithm to find the Minimum Spanning Tree (MST)
35+
void PrimsAlgorithm(int n, vector<int>& key, vector<bool>& MST, vector<int>& parent, int& totalWeight) {
36+
37+
key[0] = 0; // Start from vertex 0, minimum key for starting point
38+
parent[0] = -1; // No parent for the start node
39+
40+
for (int count = 0; count < n; ++count) {
41+
// Step 1: Find the node with the minimum key that is not yet in MST
42+
int minKeyValue = INT_MAX; // Initialize minimum key value to infinity
43+
int selectedNode = -1; // Variable to store the selected node with minimum key
44+
45+
// Iterate through all nodes to find the node with the minimum key that is not in the MST
46+
for (int i = 0; i < n; ++i) {
47+
if (!MST[i] && key[i] < minKeyValue) { // If the node is not in MST and has a smaller key
48+
minKeyValue = key[i]; // Update the minimum key value
49+
selectedNode = i; // Update the selected node
50+
}
51+
}
52+
53+
// Step 2: Include this node in MST
54+
MST[selectedNode] = true; // Mark the node as included in MST
55+
56+
// Step 3: Update the total weight of the MST by adding the key value of the selected node
57+
if (parent[selectedNode] != -1) { // If the selected node has a parent
58+
totalWeight += key[selectedNode]; // Add its key value to the total weight
59+
}
60+
61+
// Step 4: Visit all adjacent nodes and update their key values
62+
for (auto neighbor : adjacencyList[selectedNode]) { // Traverse through all neighbors of the selected node
63+
int adjacentNode = neighbor.first; // Get the adjacent node
64+
int edgeWeight = neighbor.second; // Get the weight of the edge
65+
if (!MST[adjacentNode] && edgeWeight < key[adjacentNode]) { // If the adjacent node is not in MST and the edge weight is smaller than its current key
66+
key[adjacentNode] = edgeWeight; // Update the key value of the adjacent node
67+
parent[adjacentNode] = selectedNode; // Set the parent of the adjacent node
68+
}
69+
}
70+
}
71+
}
72+
};
73+
74+
int main() {
75+
Graph g(5); // Create a graph object with 5 vertices
76+
77+
// Add edges to the graph (source, destination, weight)
78+
g.addEdge(0, 1, 2); // Edge between vertex 0 and vertex 1 with weight 2
79+
g.addEdge(0, 3, 6); // Edge between vertex 0 and vertex 3 with weight 6
80+
g.addEdge(1, 2, 3); // Edge between vertex 1 and vertex 2 with weight 3
81+
g.addEdge(1, 4, 5); // Edge between vertex 1 and vertex 4 with weight 5
82+
g.addEdge(1, 3, 8); // Edge between vertex 1 and vertex 3 with weight 8
83+
g.addEdge(2, 4, 7); // Edge between vertex 2 and vertex 4 with weight 7
84+
85+
// Print the adjacency list of the graph
86+
cout << "Adjacency List of the Graph:" << endl;
87+
g.printAdjacencyList();
88+
89+
int n = 5; // Number of vertices
90+
91+
vector<int> key(n, INT_MAX); // Key values for each node, initialized to infinity
92+
vector<bool> MST(n, false); // To track nodes included in MST (initialize all as false)
93+
vector<int> parent(n, -1); // To store the parent of each node in MST (initialize all as -1)
94+
int totalWeight = 0; // To store the total weight of the MST
95+
96+
// Apply Prim's algorithm to find the Minimum Spanning Tree
97+
g.PrimsAlgorithm(n, key, MST, parent, totalWeight);
98+
99+
// Print the total weight of the Minimum Spanning Tree
100+
cout << "\nMinimum Spanning Tree Weight is: " << totalWeight << endl;
101+
102+
// Optional: Print the MST edges (parent-child relationship with their respective edge weights)
103+
cout << "\nEdges in the Minimum Spanning Tree:" << endl;
104+
for (int i = 1; i < n; ++i) {
105+
cout << parent[i] << " - " << i << " with weight " << key[i] << endl;
106+
}
107+
108+
return 0; // Return 0 to indicate successful execution
109+
}

0 commit comments

Comments
 (0)