Minimum Degree of a Connected Trio in a Graph - Problem

You're given an undirected graph with n nodes and a list of edges. Your task is to find a special formation called a "connected trio" and determine which one has the minimum degree.

What is a Connected Trio?
A connected trio is a group of three nodes where every pair is directly connected by an edge. Think of it as a triangle in the graph - nodes A, B, and C form a trio if there are edges A-B, B-C, and A-C.

What is the Degree of a Trio?
The degree of a connected trio is the count of external edges - edges that have one endpoint inside the trio and the other endpoint outside the trio. Internal edges within the trio don't count!

Goal: Return the minimum degree among all connected trios in the graph, or -1 if no connected trio exists.

Example: If trio {1,2,3} has external connections to nodes 4,5,6,7, and the total external edges are 5, then this trio's degree is 5.

Input & Output

example_1.py — Basic Triangle
$ Input: n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]
Output: 3
💡 Note: There is exactly one trio: [1,2,3]. The degree is 3+4+2-6=3 (node 1 connects to 4, node 2 connects to 5, node 3 connects to 6, minus 6 internal connections)
example_2.py — Multiple Triangles
$ Input: n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]
Output: 0
💡 Note: Triangle [5,6,7] has degree 2+3+2-6=1, and triangle [2,5,6] has degree 2+3+3-6=2. But there's actually a triangle with degree 0 possible
example_3.py — No Triangle
$ Input: n = 4, edges = [[1,2],[2,3],[3,4]]
Output: -1
💡 Note: This is a simple path graph with no triangles, so no connected trio exists

Visualization

Tap to expand
AliceBobCarolDanEveFrankTRIOConnected Trio: {Alice, Bob, Carol}External Connections: Alice→Dan, Bob→Eve, Carol→FrankTrio Degree = 3 external edgesAlgorithm Steps:1. Find all triangular friendships2. Count external connections3. Return minimum degree trioTime: O(m√m)Space: O(n+m)
Understanding the Visualization
1
Identify Triangles
Find groups of 3 people who are all mutual friends
2
Count External Friends
For each trio, count their connections to people outside the group
3
Find Minimum
The trio with the least external connections is most tight-knit
Key Takeaway
🎯 Key Insight: Use edge-based triangle detection with pre-calculated degrees to efficiently find the minimum degree connected trio without checking all possible triplets

Time & Space Complexity

Time Complexity
⏱️
O(m√m)

For each edge, check intersection of two adjacency lists. In dense graphs, this approaches O(m√m)

n
2n
Linear Growth
Space Complexity
O(n + m)

Space for adjacency lists and degree arrays

n
2n
Linearithmic Space

Constraints

  • 2 ≤ n ≤ 400
  • edges[i].length == 2
  • 1 ≤ edges.length ≤ n * (n-1) / 2
  • 1 ≤ ui, vi ≤ n
  • ui ≠ vi
  • No repeated edges in the input
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 25
48.0K Views
Medium-High Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen