Maximum and minimum isolated vertices in a graph

18 Jun, 2021
Given ‘n’ vertices and ‘m’ edges of a graph. Find the minimum number and maximum number of isolated vertices that are possible in the graph. Examples:  
Input : 4 2
Output : Minimum 0
         Maximum 1

1--2 3--4 <---Minimum -  No isolated vertex
1--2   <--- Maximum - 1 Isolated vertex i.e. 4
   |
   3


Input : 5 2
Output : Minimum 1
         Maximum 2

1--2 3--4 5  <-- Minimum - 1 isolated vertex i.e. 5
1--2   4  5  <-- Maximum - 2 isolated vertex i.e. 4 and 5
   |
   3
 
 
For minimum number of isolated vertices, we connect two vertices by only one edge. Each vertex should be only connected to one other vertex and each vertex should have degree one Thus if the number of edges is ‘m’, and if ‘n’ vertices <=2 * ‘m’ edges, there is no isolated vertex and if this condition is false, there are n-2*m isolated vertices.For maximum number of isolated vertices, we create a polygon such that each vertex is connected to other vertex and each vertex has a diagonal with every other vertex. Thus, number of diagonals from one vertex to other vertex of n sided polygon is n*(n-3)/2 and number of edges connecting adjacent vertices is n. Thus, total number of edges is n*(n-1)/2.
For minimum number of isolated vertices, we connect two vertices by only one edge. Each vertex should be only connected to one other vertex and each vertex should have degree one Thus if the number of edges is ‘m’, and if ‘n’ vertices <=2 * ‘m’ edges, there is no isolated vertex and if this condition is false, there are n-2*m isolated vertices.
For maximum number of isolated vertices, we create a polygon such that each vertex is connected to other vertex and each vertex has a diagonal with every other vertex. Thus, number of diagonals from one vertex to other vertex of n sided polygon is n*(n-3)/2 and number of edges connecting adjacent vertices is n. Thus, total number of edges is n*(n-1)/2.
Below is the implementation of above approach.  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find maximum/minimum number// of isolated vertices.#include <bits/stdc++.h>using namespace std; // Function to find out the minimum and// maximum number of isolated verticesvoid find(int n, int m){    // Condition to find out minimum number    // of isolated vertices    if (n <= 2 * m)        cout << "Minimum " << 0 << endl;    else        cout << "Minimum " << n - 2 * m << endl;     // To find out maximum number of isolated    // vertices    // Loop to find out value of number of    // vertices that are connected    int i;    for (i = 1; i <= n; i++) {        if (i * (i - 1) / 2 >= m)            break;    }    cout << "Maximum " << n - i;} // Driver Functionint main(){    // Number of vertices    int n = 4;     // Number of edges    int m = 2;     // Calling the function to maximum and    // minimum number of isolated vertices    find(n, m);    return 0;}
// Java program to find maximum/minimum number// of isolated vertices. import java.io.*; class GFG {   // Function to find out the minimum and// maximum number of isolated vertices static void find(int n, int m){    // Condition to find out minimum number    // of isolated vertices    if (n <= 2 * m)        System.out.println( "Minimum " + 0);    else        System.out.println( "Minimum " + (n - 2 * m));     // To find out maximum number of isolated    // vertices    // Loop to find out value of number of    // vertices that are connected    int i;    for (i = 1; i <= n; i++) {        if (i * (i - 1) / 2 >= m)            break;    }    System.out.println( "Maximum " + (n - i));} // Driver Function     public static void main (String[] args) {         // Number of vertices    int n = 4;     // Number of edges    int m = 2;     // Calling the function to maximum and    // minimum number of isolated vertices    find(n, m);    }}//This code is contributed by inder_verma.
# Python3 program to find maximum/minimum# number of isolated vertices. # Function to find out the minimum and# maximum number of isolated verticesdef find(n, m) :     # Condition to find out minimum    # number of isolated vertices    if (n <= 2 * m):        print("Minimum ", 0)    else:        print("Minimum ", n - 2 * m )     # To find out maximum number of    # isolated vertices    # Loop to find out value of number    # of vertices that are connected    for i in range(1, n + 1):        if (i * (i - 1) // 2 >= m):            break         print("Maximum ", n - i)     # Driver Codeif __name__ == '__main__':         # Number of vertices    n = 4     # Number of edges    m = 2     # Calling the function to maximum and    # minimum number of isolated vertices    find(n, m) # This code is contributed by# SHUBHAMSINGH10
// C# program to find maximum/// minimum number of isolated vertices.using System; class GFG{ // Function to find out the// minimum and maximum number// of isolated verticesstatic void find(int n, int m){    // Condition to find out minimum    // number of isolated vertices    if (n <= 2 * m)        Console.WriteLine("Minimum " + 0);    else        Console.WriteLine("Minimum " +                         (n - 2 * m));     // To find out maximum number    // of isolated vertices    // Loop to find out value of    // number of vertices that    // are connected    int i;    for (i = 1; i <= n; i++)    {        if (i * (i - 1) / 2 >= m)            break;    }    Console.WriteLine("Maximum " + (n - i));} // Driver Codepublic static void Main (){     // Number of vertices    int n = 4;         // Number of edges    int m = 2;         // Calling the function to     // maximum and minimum number    // of isolated vertices    find(n, m);}} // This code is contributed// by inder_verma.
<?php// PHP program to find maximum/minimum// number of isolated vertices. // Function to find out the// minimum and maximum number// of isolated verticesfunction find($n, $m){    // Condition to find out minimum    // number of isolated vertices    if ($n <= 2 * $m)        echo "Minimum 0\n";    else        echo "Minimum " , ($n - 2 * $m);     // To find out maximum number    // of isolated vertices    // Loop to find out value of number    // of vertices that are connected    for ($i = 1; $i <= $n; $i++)    {        if ($i * ($i - 1) / 2 >= $m)            break;    }    echo "Maximum " , ($n - $i);} // Driver Code // Number of vertices$n = 4; // Number of edges$m = 2; // Calling the function to// maximum and minimum number// of isolated verticesfind($n, $m); // This code is contributed// by inder_verma?>
<script>    // Javascript program to find maximum/    // minimum number of isolated vertices.         // Function to find out the    // minimum and maximum number    // of isolated vertices    function find(n, m)    {             // Condition to find out minimum        // number of isolated vertices        if (n <= 2 * m)            document.write("Minimum " + 0 + "</br>");        else            document.write("Minimum " + (n - 2 * m) + "</br>");         // To find out maximum number        // of isolated vertices        // Loop to find out value of        // number of vertices that        // are connected        let i;        for (i = 1; i <= n; i++)        {            if (i * parseInt((i - 1) / 2, 10) >= m)                break;        }        document.write("Maximum " + (n - i));    }         // Number of vertices    let n = 4;           // Number of edges    let m = 2;           // Calling the function to     // maximum and minimum number    // of isolated vertices    find(n, m); // This code is contributed by divyeshrabadiya07.</script>
Minimum 0
Maximum 1
 
Time Complexity – O(n) 
inderDuMCA
SHUBHAMSINGH10
divyeshrabadiya07
Game Theory
Graph
Game Theory
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Find the player who will win by choosing a number in range [1, K] with sum total N
Classification of Algorithms with Examples
Zero-Sum Game
Maximum cells attacked by Rook or Bishop in given Chessboard
Game Development with Unity | Introduction
Breadth First Search or BFS for a Graph
Depth First Search or DFS for a Graph
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Graph and its representations