Maximize Count of Nodes Disconnected from all Other Nodes in a Graph


We must locate and isolate the nodes with the fewest connections in order to maximise the number of nodes in a graph that are disconnected from all other nodes. The tactic entails eliminating nodes with the lowest degree (fewest connections) repeatedly until no more of these nodes are found. The most extreme number of nodes that are separated from each other is delivered as a result of doing this, which continuously isolates different components within the graph. This strategy expands the number of nodes within the graph that have no associations with any other nodes by guaranteeing that the remaining nodes are, as it were, negligibly associated.

Methods Used

  • Greedy Approach

  • Maximum Independent Set (MIS) Approach

Greedy Approach

The Greedy Approach involves repeatedly removing nodes with the lowest degree (fewest connections) in order to maximise the number of nodes in a graph that are not connected to any other node. Up until there are no more such nodes, the process keeps going. The number of isolated nodes is increased by repeatedly eliminating nodes with the fewest connections in order to create disconnected components within the graph.It's vital to keep in mind that the Greedy Approach might not continuously guarantee the precise greatest number of disconnected nodes, as the result may shift depending on the order in which nodes are eliminated. Be that as it may, it offers a generally straightforward and successful strategy to raise the number of nodes within the graph that are unconnected.

Algorithm

  • Beginning with the initial graph G.

  • Create an empty list at the beginning to store the disconnected nodes.

  • Up until no more nodes are able to be removed, repeat the following steps:

  • a. In the current graph G, identify the node with the lowest degree (fewest connections).

    b. Pick any node if there are multiple nodes with the same lowest degree.

    c.Adding the chosen node to the list of disconnected nodes after removing it from the graph G.

  • Continue searching until no nodes of the lowest degree are left.

  • The number of nodes within the chart that are segregated from one another is spoken to by the list of withdrawn nodes that's gotten at the conclusion of the strategy.

Example

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Graph {
   int V;
   vector<vector<int>> adj;

   Graph(int vertices) : V(vertices) {
      adj.resize(V, vector<int>());
   }

   void addEdge(int u, int v) {
      adj[u].push_back(v);
      adj[v].push_back(u);
   }

   int findLowestDegreeNode() {
      int minDegree = V + 1;
      int node = -1;

      for (int i = 0; i < V; ++i) {
         if (adj[i].size() < minDegree) {
            minDegree = adj[i].size();
            node = i;
         }
      }

      return node;
   }

   vector<int> getDisconnectedNodes() {
      vector<int> disconnected;
      while (true) {
         int node = findLowestDegreeNode();
         if (node == -1)
            break;

         disconnected.push_back(node);
         for (int neighbor : adj[node]) {
            adj[neighbor].erase(remove(adj[neighbor].begin(), adj[neighbor].end
(), node), adj[neighbor].end());
         }
         adj[node].clear();
      }
      return disconnected;
   }
};

void printGraph(Graph& G) {
   for (int i = 0; i < G.V; ++i) {
      cout << "Node " << i << " : ";
      for (int neighbor : G.adj[i]) {
         cout << neighbor << " ";
      }
      cout << endl;
   }
}

int main() {
   Graph G(5);
   G.addEdge(0, 1);
   G.addEdge(0, 2);
   G.addEdge(1, 2);
   G.addEdge(3, 4);

   cout << "Initial Graph:" << endl;
   printGraph(G);

   G.getDisconnectedNodes();

   cout << "Graph after removing nodes:" << endl;
   printGraph(G);

   return 0;
}

Output

Initial Graph:
Node 0 : 1 2 
Node 1 : 0 2 
Node 2 : 0 1 
Node 3 : 4 
Node 4 : 3 

Maximum Independent Set (MIS) Approach

The Maximum Independent Set (MIS) Approach seeks to identify the largest feasible subset of nodes in which no two nodes are adjacent (connected), with the goal of increasing the number of nodes in a graph that are disconnected from all other nodes. Finding nodes in the graph with no shared edges and adding them to the independent set is the process. By maximising the number of disconnected nodes, this makes sure that the chosen nodes are not connected to one another. The selected nodes and their neighbours are iteratively removed from the graph as the algorithm continues. We obtain the maximum count of nodes that are disconnected from every other node in the graph by repeating this procedure until no more nodes can be added to the independent set.

Algorithm

  • Beginning with the initial graph G.

  • To store the Maximum Independent Set (MIS), initialise an empty set.

  • Up until no more nodes can be added to the MIS, repeat the following steps:

  • a. Locate a node in the current graph G that is unconnected to any other nodes by any common edges, or neighbours.

    b. Include the chosen node in the MIS set.

    c. Subtract the chosen node and its neighbours from graph G.

  • Continue doing this until the MIS set can contain no more nodes.

Example

#include <iostream>
#include <vector>
#include <set>
using namespace std;

class Graph {
private:
   vector<set<int>> adjacencyList;
public:
   Graph(int numNodes) : adjacencyList(numNodes) {}
   void addEdge(int u, int v);
   set<int> findIndependentSet();
};

void Graph::addEdge(int u, int v) {
   adjacencyList[u].insert(v);
   adjacencyList[v].insert(u);
}

set<int> Graph::findIndependentSet() {
   set<int> independentSet;
   vector<bool> included(adjacencyList.size(), false);
   
   while (true) {
      int nodeToAdd = -1;
      for (int i = 0; i < adjacencyList.size(); ++i) {
         if (!included[i]) {
            bool canAdd = true;
            for (const int& neighbor : adjacencyList[i]) {
               if (included[neighbor]) {
                  canAdd = false;
                  break;
               }
            }
            if (canAdd) {
               nodeToAdd = i;
               break;
            }
         }
      }
      if (nodeToAdd == -1) {
         break;
      }
      independentSet.insert(nodeToAdd);
      included[nodeToAdd] = true;
      for (const int& neighbor : adjacencyList[nodeToAdd]) {
         included[neighbor] = true;
      }
   }
   return independentSet;
}

int main() {
   // Example graph with 7 nodes and 6 edges
   Graph graph(7);
   graph.addEdge(0, 1);
   graph.addEdge(0, 2);
   graph.addEdge(1, 3);
   graph.addEdge(1, 4);
   graph.addEdge(2, 5);
   graph.addEdge(2, 6);

   set<int> maxIndependentSet = graph.findIndependentSet();
   for (const int& node : maxIndependentSet) {
      cout << node << " ";
   }
   cout << endl;

   return 0;
}

Output

0

Conclusion

We use two active methods, the Greedy Approach and the Maximum Independent Set (MIS) Approach, to maximise the number of nodes in a graph that are disconnected from each other. The Greedy Approach involves creating disconnected components, repeatedly removing nodes with the lowest degree, and increasing the number of isolated nodes. Although simple, it might not always ensure the precise maximum count. The MIS Approach, on the other hand, seeks to isolate nodes from one another by locating the largest feasible subset of nodes without any adjacent connections. The MIS Approach offers a more reliable method by methodically reaching the maximum number of disconnected nodes by iteratively adding nodes to the independent set and removing them from the graph along with their neighbours.

Updated on: 04-Aug-2023

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements