Category: connected-components

Construct a Graph from size of components for each node

#include using namespace std;  int constructConnectedComponent(int A[], int N){              bool flag = false;                  map mp;    for (int i = 0; i < N; i++) {        mp[A[i]].push_back(i);        if (A[i] != 1)            flag = true;    }      if (!flag) {        cout

Continue Reading

Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum

#include using namespace std;    int maximumAND(int arr[], int n, int m) {               int tot = 1 > i) & 1) {                                  if (count == 0) {                                          andans = arr[i];                 }                 else {                                                               andans = andans & arr[i];                 }                                                      count++;             }         }                              if […]

Continue Reading

Check for transitive property in a given Undirected Graph

#include using namespace std;    class Graph {             int V;                  list* adj;             void DFSUtil(int v, bool visited[], int id[],                  int id_number, int& c);    public:     Graph(int V);     ~Graph();             void addEdge(int v, int w);                  bool connectedComponents(); };    Graph::Graph(int V) {     this->V = V; […]

Continue Reading

Minimize count of connections required to be rearranged to make all the computers connected

   #include using namespace std;    void DFS(unordered_map& adj,          int node, vector& visited) {          if (visited[node])         return;             visited[node] = true;             for (auto x : adj[node]) {                     if (visited[x] == false)             DFS(adj, x, visited);     } }    int makeConnectedUtil(int N,                       int connections[][2],                       int M) {               vector visited(N, […]

Continue Reading