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.
Input: N = 3
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(n2/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, 4m2 / n ≤ mn, which implies m ≤ n2 / 4
Below is the implementation of above approach:
Time Complexity: O(1)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum number of edges in Bipartite graph
- Maximum number of edges among all connected components of an undirected graph
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Number of Simple Graph with N Vertices and M Edges
- Minimum number of edges between two vertices of a graph using DFS
- Minimum number of edges between two vertices of a Graph
- Count number of edges in an undirected graph
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Program to find total number of edges in a Complete Graph
- All vertex pairs connected with exactly k edges in a graph
- Path with minimum XOR sum of edges in a directed graph
- Barabasi Albert Graph (for Scale Free Models)
- Shortest path with exactly k edges in a directed and weighted graph
- Largest subset of Graph vertices with edges of 2 or more colors
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Program to find the diameter, cycles and edges of a Wheel Graph
- Count ways to change direction of edges such that graph becomes acyclic
- Assign directions to edges so that the directed graph remains acyclic
- Maximum number of bridges in a path of a given graph