# Maximize number of nodes which are not part of any edge in a Graph

Given a graph with n nodes and m edges. Find the maximum possible number of nodes which are not part of any edge (m will always be less than or equal to a number of edges in complete graph).

**Examples:**

Input: n = 3, m = 3 Output: Maximum Nodes Left Out: 0 Since it is a complete graph. Input: n = 7, m = 6 Output: Maximum Nodes Left Out: 3 We can construct a complete graph on 4 vertices using 6 edges.

**Approach:** Iterate over all n and see at which a number of nodes if we make a complete graph we obtain a number of edges more than m say it is K. Answer is **n-k**.

- Maximum number of edges which can be used to form a graph on n nodes is
**n * (n – 1) / 2**(A complete Graph). - Then find number of maximum n, which will use m or less than m edges to form a complete graph.
- If still edges are left, then it will cover only one more node, as if it would have covered more than one node than, this is not the maximum value of n.

Below is the implementation of above approach:

## C++

`// C++ program to illustrate above approach ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to return number of nodes left out ` `int` `answer(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `i; ` ` ` `for` `(i = 0; i <= n; i++) { ` ` ` ` ` `// Condition to terminate, when ` ` ` `// m edges are covered ` ` ` `if` `((i * (i - 1)) >= 2 * m) ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `return` `n - i; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 7; ` ` ` `int` `m = 6; ` ` ` `cout << answer(n, m) << endl; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to illustrate above approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to return number of nodes left out ` `static` `int` `answer(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `i; ` ` ` `for` `(i = ` `0` `; i <= n; i++) { ` ` ` ` ` `// Condition to terminate, when ` ` ` `// m edges are covered ` ` ` `if` `((i * (i - ` `1` `)) >= ` `2` `* m) ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `return` `n - i; ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `7` `; ` ` ` `int` `m = ` `6` `; ` ` ` `System.out.print( answer(n, m)); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to illustrate ` `# above approach ` ` ` `# Function to return number of ` `# nodes left out ` `def` `answer(n, m): ` ` ` `for` `i ` `in` `range` `(` `0` `, n ` `+` `1` `, ` `1` `): ` ` ` ` ` `# Condition to terminate, when ` ` ` `# m edges are covered ` ` ` `if` `((i ` `*` `(i ` `-` `1` `)) >` `=` `2` `*` `m): ` ` ` `break` ` ` ` ` `return` `n ` `-` `i ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `7` ` ` `m ` `=` `6` ` ` `print` `(answer(n, m)) ` ` ` `# This code is contributed ` `# by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to illustrate ` `// above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return number ` `// of nodes left out ` `static` `int` `answer(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `i; ` ` ` `for` `(i = 0; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Condition to terminate, when ` ` ` `// m edges are covered ` ` ` `if` `((i * (i - 1)) >= 2 * m) ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `return` `n - i; ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `n = 7; ` ` ` `int` `m = 6; ` ` ` `Console.WriteLine(answer(n, m)); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to illustrate ` `// above approach ` ` ` `// Function to return number ` `// of nodes left out ` `function` `answer(` `$n` `, ` `$m` `) ` `{ ` ` ` `for` `(` `$i` `= 0; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// Condition to terminate, when ` ` ` `// m edges are covered ` ` ` `if` `((` `$i` `* (` `$i` `- 1)) >= 2 * ` `$m` `) ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `return` `$n` `- ` `$i` `; ` `} ` ` ` `// Driver Code ` `$n` `= 7; ` `$m` `= 6; ` `echo` `answer(` `$n` `, ` `$m` `) + ` `"\n"` `; ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Edge Coloring of a Graph
- Number of sink nodes in a graph
- Program to Calculate the Edge Cover of a Graph
- Check if removing a given edge disconnects a graph
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Maximum number of nodes which can be reached from each node in a graph.
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Paths to travel each nodes using each edge (Seven Bridges of Königsberg)
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Detect cycle in the graph using degrees of nodes of graph
- Sum of degrees of all nodes of a undirected graph
- Find maximum number of edge disjoint paths between two vertices
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Number of Triangles in an Undirected Graph

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.