"kruskal algorithm time complexity" 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 algorithm time complexity" answered properly. Developers are finding an appropriate answer about kruskal algorithm time complexity related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like kruskal algorithm time complexity. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on kruskal algorithm time complexity.
kruskal's algorithm
#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);
}
kruskal algorithm time complexity
Time complexity:- O(ElogV)
All those coders who are working on the C++ based application and are stuck on kruskal algorithm time complexity can get a collection of related answers to their query. Programmers need to enter their query on kruskal algorithm time complexity related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about kruskal algorithm time complexity for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of kruskal algorithm time complexity while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "kruskal algorithm time complexity". Visit this developer's friendly online web community, CodeProZone, and get your queries like kruskal algorithm time complexity resolved professionally and stay updated to the latest C++ updates.