Problem Statement

There are A islands and there are M bridges connecting them. Each bridge has some cost attached to it.

We need to find bridges with minimal cost such that all islands are connected.

It is guaranteed that input data will contain at least one possible scenario in which all islands are connected with each other.

Input Format:

The first argument contains an integer, A, representing the number of islands. The second argument contains an 2-d integer matrix, B, of size M x 3: => Island B[i][0] and B[i][1] are connected using a bridge of cost B[i][2].

Output Format:

Return an integer representing the minimal cost required.

Constraints:

1 <= A, M <= 6e4 1 <= B[i][0], B[i][1] <= A 1 <= B[i][2] <= 1e3

Examples:

`Input 1: A = 4 B = [ [1, 2, 1] [2, 3, 4] [1, 4, 3] [4, 3, 2] [1, 3, 10] ]

Output 1: 6

Explanation 1: We can choose bridges (1, 2, 1), (1, 4, 3) and (4, 3, 2), where the total cost incurred will be (1 + 3 + 2) = 6.

Input 2: A = 4 B = [ [1, 2, 1] [2, 3, 2] [3, 4, 4] [1, 4, 3] ]

Output 2: 6

Explanation 2: We can choose bridges (1, 2, 1), (2, 3, 2) and (1, 4, 3), where the total cost incurred wi`

Problem Link

Commutable Islands - InterviewBit

Reference Videos

https://www.youtube.com/watch?v=eTaWFhPXPz4&list=PLEJXowNB4kPzByLnnFYNSCoqtFz0VKLk5&index=16&t=1226s&ab_channel=TECHDOSE

https://www.youtube.com/watch?v=_Iz-QLBGKpM&list=PLEJXowNB4kPzByLnnFYNSCoqtFz0VKLk5&index=19

Code (Kruskal's Algorithm using Disjoint Sets)

int findParent (vector<int> &parent,int i) // finds the absolute parent of node i
{
    if(parent[i]==-1)
    return i;
    
    return findParent(parent,parent[i]);
}
void Union(int i,int j,vector<int> &parent)
{
    int ip = findParent(parent,i);
    int jp = findParent(parent,j);
    parent[ip]=jp;
}
static bool compare(vector<int> &b1,vector<int> &b2)
{
    return b1[2]<b2[2];
}
int Solution::solve(int A, vector<vector<int> > &B) {
    
    sort(B.begin(),B.end(),compare);// sorting the edges wrt to their weights
    // so that at each iteration we pick up the minimum weight                           
    vector<int> parent(A+1,-1);// initializing absolute parent of all nodes as -1
                               // here a node can be considered its own parent
    int cost = 0;// initial cost of the MST
    int edges = 0;// initial number no. edges in the MST
    
    for(auto edge:B)// iterating over the edges
    {
        if(findParent(parent,edge[0])==findParent(parent,edge[1]))// if the absolute 
// parent of two vertices is same then there exists a cycle
        continue;
        
        Union(edge[0],edge[1],parent);// else add the edge from u to v making the 
//absolute parent of v as the absolute parent of u (can be done vice versa) 
        cost+=edge[2];
        edges++;
        
        if(edges==A-1)// max number of edges in a MST in no. of vertices - 1, 
//not writing this statement also works since edges added afterwards will cause a cycle
        break;
    }
    return cost;
    
    
 
    
    
}
        

Time Complexity