Prim Algorithm is a legacy greedy algorithm to find the Minimum Spanning Tree (MST) on an undirected connected graph. It has played crucial roles in the design of networks, circuit design and optimization of networks.
- Graph Representation: Weighted graphs both can be handled using an adjacency list or adjacency matrix
- Priority Queue / Min Heap: It is required to identify the next minimum-weight edge efficiently.
- Initialization: Track visited nodes, key values, and parent nodes using arrays.
- Edge Selection Logic: Edge with the lowest weight (or weight) that connects to an unvisited node are chosen at every step.
- Cycle Avoidance: The algorithm inherently escapes this through an edge choice to unvisited vertices only.
- Termination: Keep going until every vertex has been added to the MST.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAX = 1e4;
vector<pair<int, int>> adj[MAX]; // adj[node] = {weight, neighbor}
void primMST(int V) {
vector<int> key(V, INT_MAX), parent(V, -1);
vector<bool> inMST(V, false);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
key[0] = 0;
pq.push({0, 0});
while (!pq.empty()) {
int u = pq.top().second; pq.pop();
inMST[u] = true;
for (auto [weight, v] : adj[u]) {
if (!inMST[v] && weight < key[v]) {
key[v] = weight;
parent[v] = u;
pq.push({key[v], v});
}
}
}
// Output MST
for (int i = 1; i < V; ++i)
cout << parent[i] << " - " << i << "\n";
}
- Fiber-optic or electrical network design
- Maze generation in game development
- Circuit board layout optimization
- Urban traffic planning
- Sensor placement in robotics
In case you are having difficulties with logic, syntax or debugging Prim Algorithm C/C++ code, services such as The Programming Assignment Help will assist you with expert C Programming Assignment Help, step by step explanations or no errors implementations.