mdgaziur001

Prim's Algorithm

Sep 6th, 2026
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. #define INF 1e9
  6.  
  7. using namespace std;
  8.  
  9. pair<int, vector<int>> prim(vector<vector<pair<int, int>>> &graph, int start) {
  10.     // Min heap storing { weight, node }
  11.     priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> queue;
  12.     // We start with the start node. It has an edge weight of 0, obviously!
  13.     queue.push({ 0, start });
  14.  
  15.     // We keep track of the nodes we've included in the MST
  16.     vector<int> visited(graph.size());
  17.  
  18.     // We keep track of the minimum edge we've found for each node so far
  19.     vector<int> min_edge(graph.size(), INF);
  20.  
  21.     // We keep track of the node connected using minimum edge for each node
  22.     vector<int> parents(graph.size(), -1);
  23.  
  24.     // When we add a node, we include it's edge weight
  25.     int total_mst_cost = 0;
  26.  
  27.     // We keep going until we exhaust all the nodes.
  28.     while (!queue.empty()) {
  29.         // destructuring pair<weight, node>
  30.         auto [ weight, node ] = queue.top();
  31.         queue.pop();
  32.        
  33.         // We already added the node into MST, ignore it
  34.         if (visited[node]) continue;
  35.  
  36.         // We include this node into the tree, add it's edge weight
  37.         visited[node] = true;
  38.         total_mst_cost += weight;
  39.  
  40.         // Iterate through each of the edges and check if any of the edges have smaller weight than what we already discovered.
  41.         for (auto &edge: graph[node]) {
  42.             auto [ neighbor, edge_weight ] = edge;
  43.  
  44.             // If we already included the node into the MST, we ignore this
  45.             if (!visited[neighbor]) {
  46.                 if (edge_weight < min_edge[neighbor]) {
  47.                     // We do not include the node into the MST right here,
  48.                     // because we can potentially find a better edge weight. For example,
  49.                     // if we find an edge weight { 10, B }, we push it into the queue. After that, we may find { 2, B }. The priority queue will put this before the { 10, B } edge. So, this will cause the better edge to be calculated, turning visited[B] = true, and dropping { 10, B } edge.
  50.                     parents[neighbor] = node;
  51.                     min_edge[neighbor] = edge_weight;
  52.                     queue.push({ edge_weight, neighbor });
  53.                 }
  54.             }
  55.         }
  56.     }
  57.  
  58.     return { total_mst_cost, parents };
  59. }
  60.  
  61. int main() {
  62.     int nodes, edges, start;
  63.     cin >> nodes >> edges >> start;
  64.  
  65.     vector<vector<pair<int, int>>> graph(nodes + 1);
  66.  
  67.     for (int i = 0; i < edges; i++) {
  68.         int from, to, weight;
  69.         cin >> from >> to >> weight;
  70.         graph[from].push_back({ to, weight });
  71.         graph[to].push_back({ from, weight });
  72.     }
  73.  
  74.     auto [ total_cost, parents ] = prim(graph, start);
  75.    
  76.     cout << "Total Cost of Minimum Spanning Tree: " << total_cost << "\n";
  77.    
  78.     // Print the edges that make up the tree
  79.     cout << "Edges used in the MST:\n";
  80.     for (int i = 1; i <= nodes; i++) { // Start at 2 because 1 is the root
  81.         if (i != start && parents[i] != -1) {
  82.             cout << parents[i] << " - " << i << "\n";
  83.         }
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment