Given a number **N** which is the number of nodes in a graph, the task is to find the maximum number of edges that N-vertex graph can have such that graph is triangle-free (which means there should not be any three edges A, B, C in the graph such that A is connected to B, B is connected to C and C is connected to A). The graph cannot contain a self-loop or multi edges.

**Examples:**

Input:N = 4

Output:4

Explanation:

Input:N = 3

Output:2

Explanation:

If there are three edges in 3-vertex graph then it will have a triangle.

**Approach:** This Problem can be solved using Mantel’s Theorem which states that the maximum number of edges in a graph without containing any triangle is floor(n^{2}/4). In other words, one must delete nearly half of the edges to obtain a triangle-free graph.

**How Mantel’s Theorem Works ?**

For any Graph, such that the graph is Triangle free then for any vertex Z can only be connected to any of one vertex from x and y, i.e. For any edge connected between x and y, d(x) + d(y) ≤ N, where d(x) and d(y) is the degree of the vertex x and y.

- Then, the Degree of all vertex –

- By Cauchy-Schwarz inequality –

- Therefore, 4m
^{2}/ n ≤ mn, which implies m ≤ n^{2}/ 4

Below is the implementation of above approach:

## C++

`// C++ implementation to find the maximum ` `// number of edges for triangle free graph ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum number of ` `// edges in a N-vertex graph. ` `int` `solve(` `int` `n) ` `{ ` ` ` `// According to the Mantel's theorem ` ` ` `// the maximum number of edges will be ` ` ` `// floor of [(n^2)/4] ` ` ` `int` `ans = (n * n / 4); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Function ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `cout << solve(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the maximum ` `// number of edges for triangle free graph ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the maximum number of ` ` ` `// edges in a N-vertex graph. ` ` ` `public` `static` `int` `solve(` `int` `n) ` ` ` `{ ` ` ` ` ` `// According to the Mantel's theorem ` ` ` `// the maximum number of edges will be ` ` ` `// floor of [(n^2)/4] ` ` ` `int` `ans = (n * n / ` `4` `); ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` `System.out.println(solve(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by divyamohan123 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the maximum ` `// number of edges for triangle free graph ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the maximum number of ` ` ` `// edges in a N-vertex graph. ` ` ` `public` `static` `int` `solve(` `int` `n) ` ` ` `{ ` ` ` ` ` `// According to the Mantel's theorem ` ` ` `// the maximum number of edges will be ` ` ` `// floor of [(n^2)/4] ` ` ` `int` `ans = (n * n / 4); ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` `Console.WriteLine(solve(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the maximum ` `# number of edges for triangle free graph ` ` ` `# Function to find the maximum number of ` `# edges in a N-vertex graph. ` `def` `solve(n): ` ` ` ` ` `# According to the Mantel's theorem ` ` ` `# the maximum number of edges will be ` ` ` `# floor of [(n^2)/4] ` ` ` `ans ` `=` `(n ` `*` `n ` `/` `/` `4` `) ` ` ` ` ` `return` `ans ` ` ` `# Driver Function ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `10` ` ` `print` `(solve(n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

25

**Time Complexity:** 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.

## Recommended Posts:

- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Minimum colors required such that edges forming cycle do not have same color
- Minimum number of edges that need to be added to form a triangle
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Maximum number of edges among all connected components of an undirected graph
- Maximum number of edges in Bipartite graph
- Maximum number of edges to be removed to contain exactly K connected components in the Graph
- Count ways to change direction of edges such that graph becomes acyclic
- Maximum edges that can be added to DAG so that is remains DAG
- Maximum number of prime factors a number can have with exactly x factors
- Count number of edges in an undirected graph
- Program to find total number of edges in a Complete Graph
- Number of Simple Graph with N Vertices and M Edges
- Minimum number of Edges to be added to a Graph to satisfy the given condition
- Minimum number of edges between two vertices of a graph using DFS
- Minimum number of edges between two vertices of a Graph
- Shortest path with exactly k edges in a directed and weighted graph
- Assign directions to edges so that the directed graph remains acyclic
- Largest subset of Graph vertices with edges of 2 or more colors

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.