"kruskal's algorithm" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "kruskal's algorithm" answered properly. Developers are finding an appropriate answer about kruskal's algorithm related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like kruskal's algorithm. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on kruskal's algorithm. 

kruskal's algorithm

By Bloody BuzzardBloody Buzzard on Aug 30, 2020
#include<bits/stdc++.h>

using namespace std;

int  main()
{
	int n = 9;
	
	int mat[9][9] = {
	{100,4,100,100,100,100,100,8,100},
	{4,100,8,100,100,100,100,100,100},
	{100,8,100,7,100,4,100,100,2},
	{100,100,7,100,9,14,100,100,100},
	{100,100,100,9,100,10,100,100,100},
	{100,100,4,14,10,100,2,100,100},
	{100,100,100,100,100,2,100,1,6},
	{8,100,100,100,100,100,1,100,7},
	{100,100,2,100,100,100,6,7,100}};
	
	int parent[n];
	
	int edges[100][3];
	int count = 0;
	
	for(int i=0;i<n;i++)
		for(int j=i;j<n;j++)
		{
			if(mat[i][j] != 100)
			{
				edges[count][0] = i;
				edges[count][1] = j;
				edges[count++][2] = mat[i][j];	
			}		
		}

	for(int i=0;i<count-1;i++)
		for(int j=0;j<count-i-1;j++)
			if(edges[j][2] > edges[j+1][2])
				{
					int t1=edges[j][0], t2=edges[j][1], t3=edges[j][2];
					
					edges[j][0] = edges[j+1][0];
					edges[j][1] = edges[j+1][1];
					edges[j][2] = edges[j+1][2];
					
					edges[j+1][0] = t1;
					edges[j+1][1] = t2;
					edges[j+1][2] = t3;
				}
				
	int mst[n-1][2];
	int mstVal = 0;
	int l = 0;
	
	cout<<endl;
	
	for(int i=0;i<n;i++)
		parent[i] = -1;
	cout<<endl;
				
	for(int i=0;i<count;i++)
	{
		if((parent[edges[i][0]] == -1 && parent[edges[i][1]] == -1))
		{
			parent[edges[i][0]] = edges[i][0];
			parent[edges[i][1]] = edges[i][0];
			
			mst[l][0] = edges[i][0];
			mst[l++][1] = edges[i][1];
			
			mstVal += edges[i][2];
		}
		
		else if((parent[edges[i][0]] == -1 && parent[edges[i][1]] != -1))
		{
			parent[edges[i][0]] = parent[edges[i][1]];
			
			mst[l][0] = edges[i][1];
			mst[l++][1] = edges[i][0];
			
			mstVal += edges[i][2];
		}
		
		else if((parent[edges[i][0]] != -1 && parent[edges[i][1]] == -1))
		{
			parent[edges[i][1]] = parent[edges[i][0]];
			
			mst[l][0] = edges[i][0];
			mst[l++][1] = edges[i][1];
			
			mstVal += edges[i][2];
		}
		
		else if(parent[edges[i][0]] != -1 && parent[edges[i][1]] != -1 && parent[edges[i][0]] != parent[edges[i][1]])
		{
			int p = parent[edges[i][1]];
			for(int j=0;j<n;j++)
				if(parent[j] == p)
					parent[j] = parent[edges[i][0]];
			
			mst[l][0] = edges[i][0];
			mst[l++][1] = edges[i][1];
			
			mstVal += edges[i][2];
		}
	}
	
	for(int i=0;i<l;i++)
		cout<<mst[i][0]<<" -> "<<mst[i][1]<<endl;
	
	cout<<endl;
	cout<<mstVal<<endl;
		
	return(0);
}

Add Comment

4

kruskal algorithm time complexity

By AlgoristyAlgoristy on Apr 26, 2021
Time complexity:- O(ElogV)

Add Comment

0

Kruskals in c++

By VeNOMVeNOM on Dec 08, 2020
#include<bits/stdc++.h> 
using namespace std; 
  

typedef  pair<int, int> iPair; 
  

struct Graph 
{ 
    int V, E; 
    vector< pair<int, iPair> > edges; 
  
   
    Graph(int V, int E) 
    { 
        this->V = V; 
        this->E = E; 
    } 
  
    void addEdge(int u, int v, int w) 
    { 
        edges.push_back({w, {u, v}}); 
    } 
 
  
    int kruskalMST(); 
}; 
  

struct DisjointSets 
{ 
    int *parent, *rnk; 
    int n; 
  

    DisjointSets(int n) 
    { 
    
        this->n = n; 
        parent = new int[n+1]; 
        rnk = new int[n+1]; 
  
    
        for (int i = 0; i <= n; i++) 
        { 
            rnk[i] = 0; 
  
      
            parent[i] = i; 
        } 
    } 
 
    int find(int u) 
    { 
     
        if (u != parent[u]) 
            parent[u] = find(parent[u]); 
        return parent[u]; 
    } 
  
 
    void merge(int x, int y) 
    { 
        x = find(x), y = find(y); 
  
       
        if (rnk[x] > rnk[y]) 
            parent[y] = x; 
        else 
            parent[x] = y; 
  
        if (rnk[x] == rnk[y]) 
            rnk[y]++; 
    } 
}; 
  

  
int Graph::kruskalMST() 
{ 
    int mst_wt = 0; 
  
    sort(edges.begin(), edges.end()); 
  

    DisjointSets ds(V); 
  

    vector< pair<int, iPair> >::iterator it; 
    for (it=edges.begin(); it!=edges.end(); it++) 
    { 
        int u = it->second.first; 
        int v = it->second.second; 
  
        int set_u = ds.find(u); 
        int set_v = ds.find(v); 
  
      
        if (set_u != set_v) 
        { 
          
            cout << u << " - " << v << endl; 
  
      
            mst_wt += it->first; 
  
        
            ds.merge(set_u, set_v); 
        } 
    } 
  
    return mst_wt; 
} 
  

int main() 
{ 
   
    int V = 9, E = 14; 
    Graph g(V, E); 
  
   
    g.addEdge(0, 1, 4); 
    g.addEdge(0, 7, 8); 
    g.addEdge(1, 2, 8); 
    g.addEdge(1, 7, 11); 
    g.addEdge(2, 3, 7); 
    g.addEdge(2, 8, 2); 
    g.addEdge(2, 5, 4); 
    g.addEdge(3, 4, 9); 
    g.addEdge(3, 5, 14); 
    g.addEdge(4, 5, 10); 
    g.addEdge(5, 6, 2); 
    g.addEdge(6, 7, 1); 
    g.addEdge(6, 8, 6); 
    g.addEdge(7, 8, 7); 
  
    cout << "Edges of MST are \n"; 
    int mst_wt = g.kruskalMST(); 
  
    cout << "\nWeight of MST is " << mst_wt; 
  
    return 0; 
} 

Add Comment

-1

All those coders who are working on the C++ based application and are stuck on kruskal's algorithm can get a collection of related answers to their query. Programmers need to enter their query on kruskal's algorithm related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about kruskal's algorithm for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of kruskal's algorithm while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "kruskal's algorithm". Visit this developer's friendly online web community, CodeProZone, and get your queries like kruskal's algorithm resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "kruskal's algorithm"

View All C++ queries

C++ queries related to "kruskal's algorithm"

Browse Other Code Languages

CodeProZone