prims c++

#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <utility>

using namespace std;
const int MAX = 1e4 + 5;
typedef pair<long long, int> PII;
bool marked[MAX];
vector <PII> adj[MAX];

long long prim(int x)
{
    priority_queue<PII, vector<PII>, greater<PII> > Q;
    int y;
    long long minimumCost = 0;
    PII p;
    Q.push(make_pair(0, x));
    while(!Q.empty())
    {
        // Select the edge with minimum weight
        p = Q.top();
        Q.pop();
        x = p.second;
        // Checking for cycle
        if(marked[x] == true)
            continue;
        minimumCost += p.first;
        marked[x] = true;
        for(int i = 0;i < adj[x].size();++i)
        {
            y = adj[x][i].second;
            if(marked[y] == false)
                Q.push(adj[x][i]);
        }
    }
    return minimumCost;
}

int main()
{
    int nodes, edges, x, y;
    long long weight, minimumCost;
    cin >> nodes >> edges;
    for(int i = 0;i < edges;++i)
    {
        cin >> x >> y >> weight;
        adj[x].push_back(make_pair(weight, y));
        adj[y].push_back(make_pair(weight, x));
    }
    // Selecting 1 as the starting node
    minimumCost = prim(1);
    cout << minimumCost << endl;
    return 0;
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
prims algorithm c++ program implementing prims algorithm c++ Prims algorithm using c++ best way to implement prims algorithm in c++ prims code in c++ prims algor for minum spanning tree prim algorithm cpp prim's algorithm minimum spanning tree prim's algorithm in python Apply Prim&rsquo;s algorithm on the following graph and show all the steps what are needed to be done, starting vertex is A {solution requird in table format] prim algorithm python minimum spanning tree prim prism algorithm prims algorithm in c++ gfg prims mst algorithm with example in c++ prims algo in c Prims Spannig Tree prims algorithm in cpp prims algorithm competitive program in cpp what is prims algorithm prin's algorithm mst prim algorithm c++ prim's algorithm spanning tree prim's algorithm analysis mst using prim's algorithm find mst using prims algorithm Create a minimal spanning tree using Prims, then find the minimum total cost. adding an edge to mst java prim dfs primes algorithm prim algorithm c++ for powergrid prims cpp Find the MST using prim&rsquo;s algorithm. ) Given the following graph, use Prim&rsquo;s algorithm to compute the Minimum Spanning Tree (MST) of the graph. Write down the edges of the MST in sequence based on the Prim&rsquo;s algorithm Write a C program to accept undirected weighted graph from user and represent it with Adjacency List and find a minimum spanning tree using Prims algorithm. prim's algorithm c++ code minimum cost spanning tree prim's algorithm c++ what is prim's algorithm Write a C program to accept undirected weighted graph from user and represent it with Adjacency List and find a minimum spanning tree using Prims algorithm prim algorithm in c prims mst algorithm prims kruskal algorithm mst prim algorithm c++ program to implement minimum spanning tree using prim's algorithm mst primm algorithm prim's mst algorithm prim's algorithm using three array prims algorithm code c++ prims algorithm c++ linear search Prim&rsquo;s algorithm prince algorithm in c print minimum spanning tree prim's algorithm minimum spanning tree using prim's algorithm in c++ prims algorithm geeks prims algorithm implementation java prims algorithm implementation algorithm of spanning tree python prims algorithm code in cpp PRIMES algorithim in greeks for greeks minimum cost spanning tree using prim's algorithm prim's algorithm implementation prims and kruskal algorithm gfg run the prim's algorithm starting at E mst prim's algorithm Implement Prim's algorithm prime algorithm prim algorithm java prims algorithm c+ prim's algorithm pseudocode geeks for geeks find the correct statement data structure Prim's Minimal Spanning Tree Algorithm Dijkstra's Minimum Spanning Tree Algorithm Floyd-Warshall's All pairs shortest path Algorithm mst algorithm prim Prim&rsquo;s algorithm for the computation of minimum cost spanning tree for an undirected graph Prims&rsquo;s algorithm for the computation of minimum cost spanning tree for an undirected graph prims algorithm program in cpp prims algorithm python geeksforgeeks prims algorithm values minimum spanning tree prim's algorithm code prim mst is a dynamic programming prim mst uses write prim's algorithm for adjacenecy list prim's algorithm using greedy method example prims algorithm using dynamic programming graph prim algorithm prim's algorithm program in java prims algorithm program injava What is Prim&rsquo;s Algorithm. prim's algorithm geeksforgeeks minimum cost spanning tree using greedy method program for prims algorithm in c code of minimum spanning tree of given undirected graph using prims algorithm What is the time complexity for Prims algorithm for finding MST for graph with N number of nodes? prim implementation c while finding the minimum spanning distance using prim&quot;s algorithm, what are the required conditions While finding the Minimum Spanning Distance using Prim&quot;s Algorithm, which is true Prim's Minimal Spanning Tree Algorithm prim minimum spanning tree sopannig tree prim prim's algorithm gfg Implementation of Prim&rsquo;s Algorithm solve mst prime graph prims algorithm gfg prim's algorithm mst 2)Find the MST and cost of given graph using prims algorithm in c python program to implement prim's algorithm to find mst prims algorithm program in python minimum spanning tree prim's algorithm prim's spanning tree algorithm prim's algo in java prims algorithm is a prim's algorithm in c++ with output prim's algorithm in c++ prim's algorithm in c Prim&rsquo;s algorithm is a print minimum spanning tree how to find mst from adjacency matrix prims questionawith solution prims algorithm implementation c++ prim algo in c prim's algorithm for minimum spanning tree algorithm constructs a spanning tree by considering one vertex at a time.2 pointskruskal's algorithmprim's algorithmshortest path algorithm' code for prims algorithm prims and kruskal algorithm prim's algorithm minimum spanning tree java code Find edges in the Minimum Spanning Tree for the given graph using Prim&rsquo;s Algorithm. (initialization from node A) prim algorithm c++ how to optimize prims algorithm in &quot;complete&quot; graph prim's spanning tree Prim's Algorithm for finding Minimum Spanning Tree (Coding approach using Adjacency Matrix) mst using prim&rsquo;s algo prims algorithm in java Write a program to find Minimum Cost Spanning Tree of a given undirected graph using Prims algorithm. prim algorithm code python implementation of prims algorithm prim's algorithm practice minimum spanning tree using prims algorithm minimum spanning tree algorithm geeks for geeks prims minimum spanning tree prim and kruskal algorithm prims and kruskal algorithm in python prim algorithm prims java prim java prims algorithm prim's algorithm prims algo cpp prim mst python prims algorithm in c++ python prims algorithm python prim's algorithm undirected graph prim c++ prims graph in python prims program in java prims kruskal algorithm example cpp prims code for prim's algorithm in c minimum spanning tree prim's algorithm c++ prims algoritm in python prim's mst algorithm c++ prim's algorithm python prims algorithm cpp prims algorithm c++ prim's algorithm c++ Prim's python java prim time Prims O(E log V) prims algorithm implementation in c++ prim implementation c++ prim jarnik algorithm java mst code prims program in cpp prims algorithm code prims algorithm example prims algorithm code in java prims algorithm python prim's algorithm java prims algorithm java prims c prim kruskal dijkstra python prims prims c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source