Maximize count of nodes disconnected from all other nodes in a Graph

Given two integers N and E which denotes the number of nodes and the number of edges of an undirected graph, the task is to maximize the number of nodes which is not connected to any other node in the graph, without using any self-loops.
Examples:

Input: N = 5, E = 1 
Output:
Explanation: 
Since there is only 1 edge in the graph which can be used to connect two nodes. 
Therefore, three node remains disconnected.

Input: N = 5, E = 2 
Output: 2

Approach: The approach is based on the idea that to maximize the number of disconnected nodes, the new nodes will not be added to the graph until every two distinct nodes become connected. Below are the steps to solve this problem:

  1. Initialize two variables curr and rem to store the nodes connected and the edges remaining unassigned respectively.
  2. If rem becomes 0, then the required answer will be N – curr.
  3. Otherwise, increment the value of curr by 1.
  4. So, the maximum edges needed in the current step to keep every two distinct nodes connected is min(rem, curr). Subtract it from rem and increment curr.
  5. Repeat this process until rem reduces to zero.
  6. Finally, print N – curr.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function which returns
// the maximum number of
// isolated nodes
int maxDisconnected(int N, int E)
{
    // Used nodes
    int curr = 1;
 
    // Remaining edges
    int rem = E;
 
    // Count nodes used
    while (rem > 0) {
        rem = rem
              - min(
                    curr, rem);
        curr++;
    }
 
    // If given edges are non-zero
    if (curr > 1) {
        return N - curr;
    }
    else {
        return N;
    }
}
 
// Driver Code
int main()
{
    // Given N and E
    int N = 5, E = 1;
 
    // Function Call
    cout << maxDisconnected(N, E);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of
// the above approach
import java.util.*;
 
class GFG{
 
// Function which returns
// the maximum number of
// isolated nodes
static int maxDisconnected(int N, int E)
{
     
    // Used nodes
    int curr = 1;
 
    // Remaining edges
    int rem = E;
 
    // Count nodes used
    while (rem > 0)
    {
        rem = rem - Math.min(
                    curr, rem);
        curr++;
    }
 
    // If given edges are non-zero
    if (curr > 1)
    {
        return N - curr;
    }
    else
    {
        return N;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N and E
    int N = 5, E = 1;
 
    // Function call
    System.out.print(maxDisconnected(N, E));
}
}
 
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of
# the above approach
 
# Function which returns
# the maximum number of
# isolated nodes
def maxDisconnected(N, E):
 
    # Used nodes
    curr = 1
 
    # Remaining edges
    rem = E
 
    # Count nodes used
    while (rem > 0):
        rem = rem - min(curr, rem)
        curr += 1
 
    # If given edges are non-zero
    if (curr > 1):
        return N - curr
    else:
        return N
 
# Driver Code
if __name__ == '__main__':
 
    # Given N and E
    N = 5
    E = 1
 
    # Function call
    print(maxDisconnected(N, E))
 
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of
// the above approach
using System;
class GFG{
 
// Function which returns
// the maximum number of
// isolated nodes
static int maxDisconnected(int N,
                           int E)
{   
  // Used nodes
  int curr = 1;
 
  // Remaining edges
  int rem = E;
 
  // Count nodes used
  while (rem > 0)
  {
    rem = rem - Math.Min(curr, rem);
    curr++;
  }
 
  // If given edges are non-zero
  if (curr > 1)
  {
    return N - curr;
  }
  else
  {
    return N;
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given N and E
  int N = 5, E = 1;
 
  // Function call
  Console.Write(maxDisconnected(N, E));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output: 

3


Time Complexity: O(E) 
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Code and Let Code

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.