Densest Graph Without Triangles: Count and Build
Trees & GraphsAuto-graded · 25 test casesLast updated
Find the largest edge count of a triangle-free simple graph on n vertices and output a canonical extremal graph, testing Mantel's theorem and the complete bipartite construction.
Densest Graph Without Triangles
You have n labelled points, numbered 0 to n - 1. You want to join some pairs of them with undirected edges so that the result is a simple graph (no self-loops, no repeated edges) that contains no triangle. A triangle is any three distinct points that are all pairwise joined.
Among all such graphs, find the largest possible number of edges, and also output one specific graph that reaches that number.
Task
Implement densest_triangle_free_graph(n). It must return a two-element list [max_edges, edges]:
max_edgesis an integer, the maximum number of edges a triangle-free simple graph onnvertices can have.edgesis a list of edges, each written as a two-element list[u, v]withu < v.
Required output graph
To make the output unique, edges must describe exactly this graph:
- Let
h = n // 2(integer division). - The left group is the vertices
0, 1, ..., h - 1. The right group is the verticesh, h + 1, ..., n - 1. - Include one edge
[u, v]for every left vertexuand every right vertexv, and no other edges. - List the edges in increasing order of
u, and for equaluin increasing order ofv.
max_edges must equal the length of edges, which is h * (n - h).
Constraints
1 <= n <= 1000- The graph is simple: no self-loops and no duplicate edges.
- Building the edge list should take
O(n^2)time, andmax_edgesshould be computed inO(1). - The output uses
O(n^2)space. - For
n = 1the result is[0, []].
Examples
Example 1
Input: densest_triangle_free_graph(4)
Output: [4, [[0, 2], [0, 3], [1, 2], [1, 3]]]
Example 2
Input: densest_triangle_free_graph(5)
Output: [6, [[0, 2], [0, 3], [0, 4], [1, 2], [1, 3], [1, 4]]]
Example 3
Input: densest_triangle_free_graph(1)
Output: [0, []]
Example 4
Input: densest_triangle_free_graph(2)
Output: [1, [[0, 1]]]
Example 5
Input: densest_triangle_free_graph(3)
Output: [2, [[0, 1], [0, 2]]]
Hints
Hint 1
Think about which graphs obviously contain no triangle: a graph whose vertices split into two groups with edges only between the groups has no odd cycles at all.
Hint 2
If the groups have sizes a and n - a, the number of edges is a * (n - a). Which a maximizes this over integers?
Hint 3
For any edge (u, v) in a triangle-free graph, the neighbourhoods of u and v are disjoint, so deg(u) + deg(v) <= n. Summing over edges and applying Cauchy-Schwarz shows m <= n^2 / 4, which matches the construction.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is Mantel's theorem: a triangle-free graph on n vertices has at most floor(n^2 / 4) edges, and a balanced complete bipartite graph reaches that bound.
Split the vertices into a left group {0, ..., n//2 - 1} and a right group {n//2, ..., n - 1}. Join every left vertex to every right vertex and add nothing else. The edge count is (n // 2) * (n - n // 2), which equals floor(n^2 / 4).
Why it is correct
Achievability. A triangle needs three mutually adjacent vertices. Among any three vertices of this graph, two lie in the same group, and there is no edge inside a group. So no triangle exists. With group sizes a and n - a, the edge count a(n - a) is a downward parabola in a, and the best integer choice is a = floor(n / 2).
Optimality. Let G be triangle-free with m edges and degrees d(v).
- For an edge
(u, v), the neighbourhoods ofuandvcannot share a vertex (it would complete a triangle), sod(u) + d(v) <= n. - Summing over all edges gives
sum over v of d(v)^2 <= m * n, because each vertexvappears ind(v)edges. - By Cauchy-Schwarz,
sum d(v)^2 >= (2m)^2 / n. - Combining these gives
4m^2 / n <= m * n, som <= n^2 / 4. Sincemis an integer,m <= floor(n^2 / 4).
Since the construction meets this bound, it is optimal.
Edge cases
n = 1: the left group is empty, so the result is[0, []].n = 2: the result is[1, [[0, 1]]].n = 3: the left group is{0}and the right group is{1, 2}, giving 2 edges.- Odd
n: the extra vertex goes to the right group.
Complexity
- Edge count:
O(1). - Building the edge list:
O(n^2)time, which is optimal since the output has aboutn^2 / 4entries. - Space:
O(n^2)for the output; auxiliary space isO(1).
Reference solution
from typing import List
def densest_triangle_free_graph(n: int) -> List:
left_size = n // 2
right_start = left_size
right_size = n - left_size
edges = []
for u in range(left_size):
for v in range(right_start, n):
edges.append([u, v])
return [left_size * right_size, edges]