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.
Below is the implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void find( int n, int m)
{
if (n <= 2 * m)
cout << "Minimum " << 0 << endl;
else
cout << "Minimum " << n - 2 * m << endl;
int i;
for (i = 0; i <= n; i++) {
if (i * (i - 1) / 2 >= m)
break ;
}
cout << "Maximum " << n - i;
}
int main()
{
int n = 4;
int m = 2;
find(n, m);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void find( int n, int m)
{
if (n <= 2 * m)
System.out.println( "Minimum " + 0 );
else
System.out.println( "Minimum " + (n - 2 * m));
int i;
for (i = 0 ; i <= n; i++) {
if (i * (i - 1 ) / 2 >= m)
break ;
}
System.out.println( "Maximum " + (n - i));
}
public static void main (String[] args) {
int n = 4 ;
int m = 2 ;
find(n, m);
}
}
|
Python3
def find(n, m) :
if (n < = 2 * m):
print ( "Minimum " , 0 )
else :
print ( "Minimum " , n - 2 * m )
for i in range ( 0 , n + 1 ):
if (i * (i - 1 ) / / 2 > = m):
break
print ( "Maximum " , n - i)
if __name__ = = '__main__' :
n = 4
m = 2
find(n, m)
|
C#
using System;
class GFG
{
static void find( int n, int m)
{
if (n <= 2 * m)
Console.WriteLine( "Minimum " + 0);
else
Console.WriteLine( "Minimum " +
(n - 2 * m));
int i;
for (i = 0; i <= n; i++)
{
if (i * (i - 1) / 2 >= m)
break ;
}
Console.WriteLine( "Maximum " + (n - i));
}
public static void Main ()
{
int n = 4;
int m = 2;
find(n, m);
}
}
|
PHP
<?php
function find( $n , $m )
{
if ( $n <= 2 * $m )
echo "Minimum 0\n" ;
else
echo "Minimum " , ( $n - 2 * $m );
for ( $i = 0; $i <= $n ; $i ++)
{
if ( $i * ( $i - 1) / 2 >= $m )
break ;
}
echo "Maximum " , ( $n - $i );
}
$n = 4;
$m = 2;
find( $n , $m );
?>
|
Javascript
<script>
function find(n, m)
{
if (n <= 2 * m)
document.write( "Minimum " + 0 + "</br>" );
else
document.write( "Minimum " + (n - 2 * m) + "</br>" );
let i;
for (i = 0; i <= n; i++)
{
if (i * parseInt((i - 1) / 2, 10) >= m)
break ;
}
document.write( "Maximum " + (n - i));
}
let n = 4;
let m = 2;
find(n, m);
</script>
|
Output
Minimum 0
Maximum 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Oct, 2022
Like Article
Save Article