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.
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.
Minimum 0 Maximum 1
Time Complexity – O(n)
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live