Problem Description
Given an undirected graph with n nodes and a list of edges, we need to find the minimum degree of any connected trio. A connected trio is a set of three nodes where every pair of nodes is directly connected by an edge. The degree of a connected trio is defined as the total number of edges that have one endpoint in the trio and the other outside the trio. If no trio exists, return -1.
Key Insights
- Represent the graph using an adjacency list and an edge lookup structure.
- For each connected pair (u, v), compute the intersection of their neighbor sets to quickly find a third node w that forms a trio with u and v.
- To avoid duplicate detections of the same trio, enforce an ordering on the nodes (for example, u < v < w).
- Calculate the trio degree by summing the degrees of the three nodes then subtracting 6 (to remove the internal trio connections, as each internal edge is counted twice in the sum).
Space and Time Complexity
Time Complexity: O(E * d) where E is the number of edges and d is the average degree, because for each edge we compute the intersection of neighbor sets. In the worst-case, it could approach O(n^3) if the graph is very dense. Space Complexity: O(n + E) for storing the adjacency list and other auxiliary data structures.
Solution
We begin by building an adjacency list for each node as well as a set to allow constant-time lookup of whether an edge exists. We also precompute the degree of each node. Then, for every pair of connected nodes (u, v) with u < v, we find all common neighbors w (with w > v) such that (u, v, w) forms a trio. For each trio, its degree is computed as (degree[u] + degree[v] + degree[w] - 6). We keep track of the minimum degree found over all trios. If no trio is identified, we return -1.