Facebook Pixel

1761. Minimum Degree of a Connected Trio in a Graph

HardGraphEnumeration
Leetcode Link

Problem Description

You have an undirected graph with n nodes (numbered from 1 to n) and a list of edges connecting pairs of nodes. Your task is to find the minimum degree of a "connected trio" in this graph.

A connected trio is a set of exactly three nodes where every pair of nodes in the set is directly connected by an edge. In other words, these three nodes form a triangle in the graph.

The degree of a connected trio is the total number of edges that connect any node in the trio to nodes outside the trio. This counts all edges that have one endpoint inside the trio and the other endpoint outside of it.

For example, if nodes A, B, and C form a connected trio:

  • Count all edges from A to nodes other than B and C
  • Count all edges from B to nodes other than A and C
  • Count all edges from C to nodes other than A and B
  • The sum of these counts is the degree of this trio

Your goal is to:

  1. Find all connected trios in the graph
  2. Calculate the degree of each trio
  3. Return the minimum degree among all trios
  4. If no connected trio exists in the graph, return -1

The input consists of:

  • n: the number of nodes in the graph
  • edges: an array where each element [u, v] represents an undirected edge between nodes u and v
Quick Interview Experience
Help others by sharing your interview experience
Have you seen this problem before?

Intuition

To find connected trios, we need to identify sets of three nodes where each node is connected to the other two. The straightforward way to do this is to check all possible combinations of three nodes and verify if they form a triangle.

For efficient edge checking, we can use an adjacency matrix g[i][j] that tells us in constant time whether nodes i and j are connected. This is faster than repeatedly searching through the edge list.

When calculating the degree of a trio, we need to count edges going outside the trio. If we have nodes i, j, and k forming a trio, we need to count:

  • Edges from i to nodes other than j and k
  • Edges from j to nodes other than i and k
  • Edges from k to nodes other than i and j

A key observation here is that we can use the total degree of each node and subtract the internal connections. Each node in the trio has exactly 2 edges connecting to the other nodes in the trio. So if deg[i] is the total degree of node i, then deg[i] - 2 gives us the edges from i going outside the trio.

Therefore, for a trio (i, j, k), the total degree would be:

  • (deg[i] - 2) + (deg[j] - 2) + (deg[k] - 2)
  • Which simplifies to: deg[i] + deg[j] + deg[k] - 6

This formula allows us to quickly calculate the trio degree without having to iterate through all edges again. We simply enumerate all possible triplets, check if they form a connected trio using our adjacency matrix, and if they do, calculate their degree using this formula and track the minimum.

Learn more about Graph patterns.

Solution Approach

The implementation follows a brute force enumeration strategy with optimized data structures for efficient lookups:

1. Initialize Data Structures:

  • Create an adjacency matrix g of size n Γ— n initialized with False values. This allows O(1) edge existence checks.
  • Create a degree array deg of size n to store the degree of each node.

2. Build the Graph Representation:

  • For each edge [u, v] in the input:
    • Convert to 0-indexed: u = u - 1, v = v - 1 (since nodes are numbered 1 to n)
    • Mark g[u][v] = g[v][u] = True to indicate the undirected edge
    • Increment the degree counters: deg[u] += 1 and deg[v] += 1

3. Find Connected Trios:

  • Initialize answer ans = inf to track the minimum degree
  • Use three nested loops to enumerate all possible triplets (i, j, k) where i < j < k:
    • First loop: i from 0 to n-1
    • Second loop: j from i+1 to n-1, and check if g[i][j] is true
    • Third loop: k from j+1 to n-1
    • For each triplet, check if all three edges exist:
      • If g[i][j] and g[i][k] and g[j][k] are all True, we have a connected trio

4. Calculate Trio Degree:

  • When a connected trio (i, j, k) is found:
    • Calculate its degree as deg[i] + deg[j] + deg[k] - 6
    • The -6 accounts for the 6 internal edge endpoints (each of the 3 edges has 2 endpoints)
    • Update ans = min(ans, deg[i] + deg[j] + deg[k] - 6)

5. Return Result:

  • After checking all possible triplets:
    • If ans is still inf, no connected trio was found, return -1
    • Otherwise, return ans as the minimum trio degree

The time complexity is O(nΒ³) for checking all triplets, and space complexity is O(nΒ²) for the adjacency matrix.

Ready to land your dream job?

Unlock your dream job with a 5-minute evaluator for a personalized learning plan!

Start Evaluator

Example Walkthrough

Let's walk through a small example to illustrate the solution approach.

Example Graph:

  • n = 5 nodes (numbered 1 to 5)
  • edges = [[1,2], [1,3], [2,3], [2,4], [3,4], [3,5], [4,5]]

Step 1: Build Graph Representation

First, we create our adjacency matrix g (5Γ—5) and degree array deg:

  • Process edge [1,2]: Set g[0][1] = g[1][0] = True, deg[0]++ = 2, deg[1]++ = 2
  • Process edge [1,3]: Set g[0][2] = g[2][0] = True, deg[0]++ = 2, deg[2]++ = 2
  • Process edge [2,3]: Set g[1][2] = g[2][1] = True, deg[1]++ = 3, deg[2]++ = 3
  • Process edge [2,4]: Set g[1][3] = g[3][1] = True, deg[1]++ = 3, deg[3]++ = 2
  • Process edge [3,4]: Set g[2][3] = g[3][2] = True, deg[2]++ = 4, deg[3]++ = 3
  • Process edge [3,5]: Set g[2][4] = g[4][2] = True, deg[2]++ = 4, deg[4]++ = 2
  • Process edge [4,5]: Set g[3][4] = g[4][3] = True, deg[3]++ = 3, deg[4]++ = 2

Final degrees: deg = [2, 3, 4, 3, 2] (for nodes 1-5 respectively)

Step 2: Find Connected Trios

We enumerate all triplets (i, j, k) where i < j < k:

  • Triplet (0, 1, 2) β†’ Nodes (1, 2, 3):

    • Check: g[0][1] βœ“, g[0][2] βœ“, g[1][2] βœ“ β†’ This is a connected trio!
    • Calculate degree: deg[0] + deg[1] + deg[2] - 6 = 2 + 3 + 4 - 6 = 3
    • Update ans = 3
  • Triplet (0, 1, 3) β†’ Nodes (1, 2, 4):

    • Check: g[0][1] βœ“, g[0][3] βœ— β†’ Not a connected trio
  • Triplet (0, 1, 4) β†’ Nodes (1, 2, 5):

    • Check: g[0][1] βœ“, g[0][4] βœ— β†’ Not a connected trio
  • Triplet (1, 2, 3) β†’ Nodes (2, 3, 4):

    • Check: g[1][2] βœ“, g[1][3] βœ“, g[2][3] βœ“ β†’ This is a connected trio!
    • Calculate degree: deg[1] + deg[2] + deg[3] - 6 = 3 + 4 + 3 - 6 = 4
    • ans remains 3 (since 3 < 4)
  • Triplet (2, 3, 4) β†’ Nodes (3, 4, 5):

    • Check: g[2][3] βœ“, g[2][4] βœ“, g[3][4] βœ“ β†’ This is a connected trio!
    • Calculate degree: deg[2] + deg[3] + deg[4] - 6 = 4 + 3 + 2 - 6 = 3
    • ans remains 3 (since 3 = 3)

Step 3: Return Result

We found three connected trios:

  • Trio (1,2,3) with degree 3
  • Trio (2,3,4) with degree 4
  • Trio (3,4,5) with degree 3

The minimum degree is 3, so we return 3.

Verification of Trio (1,2,3):

  • Node 1 has edges to: 2 (in trio), 3 (in trio) β†’ 0 external edges
  • Node 2 has edges to: 1 (in trio), 3 (in trio), 4 (external) β†’ 1 external edge
  • Node 3 has edges to: 1 (in trio), 2 (in trio), 4 (external), 5 (external) β†’ 2 external edges
  • Total external edges = 0 + 1 + 2 = 3 βœ“

Solution Implementation

1from typing import List
2from math import inf
3
4
5class Solution:
6    def minTrioDegree(self, n: int, edges: List[List[int]]) -> int:
7        # Create adjacency matrix to track connections between nodes
8        # graph[i][j] = True if there's an edge between node i and j
9        graph = [[False] * n for _ in range(n)]
10      
11        # Track the degree (number of connections) for each node
12        degree = [0] * n
13      
14        # Build the graph from edges (convert to 0-indexed)
15        for u, v in edges:
16            u, v = u - 1, v - 1  # Convert from 1-indexed to 0-indexed
17            graph[u][v] = graph[v][u] = True  # Undirected graph
18            degree[u] += 1
19            degree[v] += 1
20      
21        # Initialize minimum trio degree to infinity
22        min_degree = inf
23      
24        # Check all possible trios (i, j, k) where i < j < k
25        for i in range(n):
26            for j in range(i + 1, n):
27                if graph[i][j]:  # If i and j are connected
28                    for k in range(j + 1, n):
29                        # Check if k forms a trio with i and j
30                        if graph[i][k] and graph[j][k]:
31                            # Calculate trio degree: sum of degrees minus internal edges
32                            # Each trio has 3 internal edges, counted twice in degree sum
33                            trio_degree = degree[i] + degree[j] + degree[k] - 6
34                            min_degree = min(min_degree, trio_degree)
35      
36        # Return -1 if no trio found, otherwise return minimum trio degree
37        return -1 if min_degree == inf else min_degree
38
1class Solution {
2    public int minTrioDegree(int n, int[][] edges) {
3        // Create adjacency matrix to track connections between nodes
4        boolean[][] adjacencyMatrix = new boolean[n][n];
5      
6        // Array to store the degree (number of connections) for each node
7        int[] degree = new int[n];
8      
9        // Build the graph: populate adjacency matrix and calculate degrees
10        for (int[] edge : edges) {
11            // Convert to 0-indexed (nodes are 1-indexed in input)
12            int nodeU = edge[0] - 1;
13            int nodeV = edge[1] - 1;
14          
15            // Mark bidirectional connection in adjacency matrix
16            adjacencyMatrix[nodeU][nodeV] = true;
17            adjacencyMatrix[nodeV][nodeU] = true;
18          
19            // Increment degree count for both nodes
20            degree[nodeU]++;
21            degree[nodeV]++;
22        }
23      
24        // Initialize minimum trio degree to a large value (1 << 30 = 2^30)
25        int minTrioDegree = 1 << 30;
26      
27        // Try all possible combinations of three nodes to find trios
28        for (int i = 0; i < n; i++) {
29            for (int j = i + 1; j < n; j++) {
30                // Check if nodes i and j are connected
31                if (adjacencyMatrix[i][j]) {
32                    for (int k = j + 1; k < n; k++) {
33                        // Check if nodes i-k and j-k are also connected (forming a trio)
34                        if (adjacencyMatrix[i][k] && adjacencyMatrix[j][k]) {
35                            // Calculate trio degree: sum of all three nodes' degrees
36                            // minus 6 (the 3 internal edges counted twice)
37                            int currentTrioDegree = degree[i] + degree[j] + degree[k] - 6;
38                            minTrioDegree = Math.min(minTrioDegree, currentTrioDegree);
39                        }
40                    }
41                }
42            }
43        }
44      
45        // Return -1 if no trio found, otherwise return the minimum trio degree
46        return minTrioDegree == 1 << 30 ? -1 : minTrioDegree;
47    }
48}
49
1class Solution {
2public:
3    int minTrioDegree(int n, vector<vector<int>>& edges) {
4        // Create adjacency matrix to store graph connections
5        // Using vector instead of VLA for standard C++ compliance
6        vector<vector<bool>> adjacencyMatrix(n, vector<bool>(n, false));
7      
8        // Array to store the degree of each vertex
9        vector<int> vertexDegree(n, 0);
10      
11        // Build the graph from edges (converting 1-indexed to 0-indexed)
12        for (const auto& edge : edges) {
13            int vertex1 = edge[0] - 1;
14            int vertex2 = edge[1] - 1;
15          
16            // Mark bidirectional connection in adjacency matrix
17            adjacencyMatrix[vertex1][vertex2] = true;
18            adjacencyMatrix[vertex2][vertex1] = true;
19          
20            // Increment degree for both vertices
21            vertexDegree[vertex1]++;
22            vertexDegree[vertex2]++;
23        }
24      
25        int minimumTrioDegree = INT_MAX;
26      
27        // Iterate through all possible trios of vertices
28        for (int firstVertex = 0; firstVertex < n; ++firstVertex) {
29            for (int secondVertex = firstVertex + 1; secondVertex < n; ++secondVertex) {
30                // Check if first and second vertices are connected
31                if (adjacencyMatrix[firstVertex][secondVertex]) {
32                    for (int thirdVertex = secondVertex + 1; thirdVertex < n; ++thirdVertex) {
33                        // Check if all three vertices form a connected trio
34                        if (adjacencyMatrix[secondVertex][thirdVertex] && 
35                            adjacencyMatrix[firstVertex][thirdVertex]) {
36                            // Calculate trio degree: sum of degrees minus internal edges (6 = 3 edges * 2)
37                            // Each edge in the trio is counted twice in the degree sum
38                            int currentTrioDegree = vertexDegree[firstVertex] + 
39                                                   vertexDegree[secondVertex] + 
40                                                   vertexDegree[thirdVertex] - 6;
41                          
42                            minimumTrioDegree = min(minimumTrioDegree, currentTrioDegree);
43                        }
44                    }
45                }
46            }
47        }
48      
49        // Return -1 if no trio found, otherwise return minimum trio degree
50        return minimumTrioDegree == INT_MAX ? -1 : minimumTrioDegree;
51    }
52};
53
1/**
2 * Finds the minimum degree of a connected trio in an undirected graph.
3 * A trio is a set of three nodes where each node is directly connected to the other two.
4 * The degree of a trio is the number of edges connected to the trio (excluding the three edges within the trio).
5 * 
6 * @param n - The number of nodes in the graph (nodes are numbered from 1 to n)
7 * @param edges - Array of edges where each edge is represented as [u, v]
8 * @returns The minimum degree among all trios, or -1 if no trio exists
9 */
10function minTrioDegree(n: number, edges: number[][]): number {
11    // Create adjacency matrix to represent the graph
12    // graph[i][j] = true if there's an edge between node i and node j
13    const graph: boolean[][] = Array.from({ length: n }, () => Array(n).fill(false));
14  
15    // Store the degree (number of connections) for each node
16    const degrees: number[] = Array(n).fill(0);
17  
18    // Build the graph and calculate degrees
19    // Convert 1-indexed nodes to 0-indexed for internal representation
20    for (const [u, v] of edges) {
21        const nodeU: number = u - 1;
22        const nodeV: number = v - 1;
23      
24        // Mark bidirectional edge in adjacency matrix
25        graph[nodeU][nodeV] = true;
26        graph[nodeV][nodeU] = true;
27      
28        // Increment degree count for both nodes
29        degrees[nodeU]++;
30        degrees[nodeV]++;
31    }
32  
33    let minimumTrioDegree: number = Infinity;
34  
35    // Iterate through all possible trios (i, j, k) where i < j < k
36    for (let i = 0; i < n; i++) {
37        for (let j = i + 1; j < n; j++) {
38            // Check if nodes i and j are connected
39            if (graph[i][j]) {
40                for (let k = j + 1; k < n; k++) {
41                    // Check if all three nodes form a trio (all pairs are connected)
42                    if (graph[i][k] && graph[j][k]) {
43                        // Calculate trio degree: sum of all three nodes' degrees minus 6
44                        // (subtract 6 because each of the 3 internal edges is counted twice)
45                        const trioDegree: number = degrees[i] + degrees[j] + degrees[k] - 6;
46                        minimumTrioDegree = Math.min(minimumTrioDegree, trioDegree);
47                    }
48                }
49            }
50        }
51    }
52  
53    // Return -1 if no trio was found, otherwise return the minimum degree
54    return minimumTrioDegree === Infinity ? -1 : minimumTrioDegree;
55}
56

Time and Space Complexity

Time Complexity: O(nΒ³)

The algorithm uses three nested loops to find all possible trios (groups of three connected nodes):

  • The outermost loop iterates through all nodes i from 0 to n-1: O(n) iterations
  • The second loop iterates through nodes j from i+1 to n-1: up to O(n) iterations
  • The innermost loop iterates through nodes k from j+1 to n-1: up to O(n) iterations

For each trio (i, j, k), the algorithm performs constant time operations:

  • Checking if edges exist: g[i][j], g[i][k], g[j][k] - each is O(1)
  • Computing the degree sum and minimum - O(1)

Additionally, the preprocessing step builds the adjacency matrix and degree array by iterating through all edges once, which takes O(E) time where E is the number of edges. Since E ≀ nΒ², this doesn't affect the overall complexity.

Therefore, the total time complexity is O(n) Γ— O(n) Γ— O(n) = O(nΒ³).

Space Complexity: O(nΒ²)

The algorithm uses:

  • A 2D adjacency matrix g of size n Γ— n to store edge information: O(nΒ²) space
  • A degree array deg of size n: O(n) space
  • A few scalar variables (ans, loop indices): O(1) space

The dominant term is the adjacency matrix, giving us a total space complexity of O(nΒ²).

Learn more about how to find time and space complexity quickly.

Common Pitfalls

1. Incorrect Index Conversion

One of the most common mistakes is forgetting to convert between 1-indexed nodes (in the problem) and 0-indexed arrays (in the implementation). This can lead to:

  • Array index out of bounds errors
  • Incorrect graph construction
  • Missing or extra edges

Example of the pitfall:

# WRONG: Forgetting to convert indices
for u, v in edges:
    graph[u][v] = graph[v][u] = True  # This will cause index errors!
    degree[u] += 1
    degree[v] += 1

Solution: Always convert from 1-indexed to 0-indexed immediately when processing edges:

# CORRECT: Convert to 0-indexed
for u, v in edges:
    u, v = u - 1, v - 1  # Convert first
    graph[u][v] = graph[v][u] = True
    degree[u] += 1
    degree[v] += 1

2. Incorrect Trio Degree Calculation

Another frequent error is miscalculating the trio degree by not properly accounting for internal edges. The trio degree should only count edges going outside the trio.

Example of the pitfall:

# WRONG: Not subtracting internal edges
trio_degree = degree[i] + degree[j] + degree[k]  # This counts internal edges too!

Why -6 is needed:

  • Each node in the trio connects to the other 2 nodes (internal edges)
  • Total internal connections: 3 nodes Γ— 2 connections each = 6
  • These 6 connections should not count toward the trio degree

Solution:

# CORRECT: Subtract the 6 internal edge endpoints
trio_degree = degree[i] + degree[j] + degree[k] - 6

3. Inefficient Edge Checking

Using an adjacency list instead of an adjacency matrix can make trio detection inefficient.

Example of the pitfall:

# INEFFICIENT: Using adjacency list requires O(degree) lookup
adj_list = {i: set() for i in range(n)}
# Checking if edge exists: if k in adj_list[j] - O(1) but with higher constant

Solution: Use an adjacency matrix for O(1) edge existence checks:

# EFFICIENT: Direct O(1) lookup
if graph[i][k] and graph[j][k]:  # Instant check

4. Duplicate Trio Counting

While the nested loops with i < j < k prevent this, a common mistake when modifying the algorithm is to accidentally count the same trio multiple times.

Example of the pitfall:

# WRONG: Could count same trio multiple times
for i in range(n):
    for j in range(n):  # Should be range(i+1, n)
        for k in range(n):  # Should be range(j+1, n)

Solution: Maintain strict ordering in the loops:

# CORRECT: Each trio counted exactly once
for i in range(n):
    for j in range(i + 1, n):
        for k in range(j + 1, n):
Discover Your Strengths and Weaknesses: Take Our 5-Minute Quiz to Tailor Your Study Plan:

Which of the tree traversal order can be used to obtain elements in a binary search tree in sorted order?


Recommended Readings

Want a Structured Path to Master System Design Too? Don’t Miss This!

Load More