C++|| DFS || DISJOINT SET || EASY TO UNDERSTAND
FIRST approach using DFS,\nwe will make a adjacency list and simply call DFS traversal,\nand check for the components in the graph,\n\nclass Solution {\npublic:
FIRST approach using DFS,\nwe will make a adjacency list and simply call DFS traversal,\nand check for the components in the graph,\n```\nclass Solution {\npublic:\n// DFS traversal of graph\n    void DFS(int s,vector<int>& visited,vector<int> adj[])\n    {\n        visited[s]=1;\n        for(auto i:adj[s])\n        {\n            if(!visited[i])\n                DFS(i,visited,adj);\n        }\n    }\n    int makeConnected(int n, vector<vector<int>>& connections) {\n\t\n\t// if the number of edges are less than minimum numbers of edges to connect the graph(i.e. n-1)\n\t// then just return -1\n        if(connections.size()<n-1)\n            return -1;\n\t\t\t\n        int count=0;\n        vector<int> visited(n,0);\n        // making the adjacency list \n        vector<int> adj[n];\n        for(int i=0;i<connections.size();i++)\n        {\n            adj[connections[i][0]].push_back(connections[i][1]);\n            adj[connections[i][1]].push_back(connections[i][0]);\n        }\n        \n        for(int i=0;i<n;i++)\n        {\n            if(!visited[i])\n               { DFS(i,visited,adj);\n                 count++;\n               }\n        }\n\t\t// returning the minimum number of operations to connect the computers \n        return count-1;\n    }\n};\n```\n\nSECOND approach using disjoint set (union find)\n\n```\nclass Solution {\npublic:\n    \n     // making parent and rank array\n        vector<int> parent;\n        vector<int> rank;\n        \n    // making every node its parent\n    // and assignin rank of every element 0\n        void make_set(int n)\n        {\n            parent=vector<int>(n);\n            for(int i=0;i<n;i++)\n                parent[i]=i;\n            rank=vector<int>(n,0);\n        }\n    // path compression\n    //  this will make a common parent for all the elements that are connected\n        int find_set(int &x)\n        {\n            if(x==parent[x])\n                return x;\n            return parent[x]=find_set(parent[x]);\n        }\n    // to make union of two elements\n        void unionmake(int u,int v)\n        {\n            u=find_set(u);\n            v=find_set(v);\n            if(rank[u]<rank[v])\n                parent[u]=v;\n            else if(rank[u]>rank[v])\n                parent[v]=u;\n            else {\n                parent[v]=u;\n                rank[v]++;\n            }\n        }\n   \n    \n    \n    int makeConnected(int n, vector<vector<int>>& connections) {\n\n         make_set(n);\n        // making union of every edge\n         for(auto connection:connections)\n            unionmake(connection[0],connection[1]);\n        \n        int connected=0;\n        // checking for the element being parent of itself\n        // on incrementing connected it will give the number of components.\n        for(int i=0;i<n;i++)\n       { \n            if(parent[i]==i) \n               connected++;\n       }\n        // if the number of edges are for than the minimum no. of edges required to connect all the computers \n        // then return connected-1\n        if(n-1<=connections.size()) return connected-1;\n        return -1;\n    }\n};\n```
['Depth-First Search', 'C', 'C++']