The "Minimum Degree of a Connected Trio in a Graph" problem asks you to analyze an undirected graph with n
nodes, labeled from 1
to n
, and a list of undirected edges. Your goal is to find the minimum degree of any connected trio of nodes in the graph.
You need to return the minimum degree among all connected trios in the graph. If there is no such trio, return -1
.
Constraints:
At first glance, the problem seems to require checking all possible trios of nodes for being connected and then calculating their degree. This brute-force approach would involve iterating over all possible combinations of three nodes, checking if all three edges exist, and then counting the external connections.
However, with n
up to 400, the number of possible trios is roughly 10 million (nC3
), and checking each trio naively can be inefficient. Thus, we need to optimize:
The key insight is to use data structures that allow constant-time checks and precompute as much as possible to avoid repeated work.
Let's break down the algorithm step by step:
adj
where adj[i][j]
is True
if there is an edge between nodes i
and j
.
deg
.
(i, j, k)
where i < j < k
:
adj[i][j]
, adj[j][k]
, adj[i][k]
).
deg[i] + deg[j] + deg[k] - 6
.
-1
; otherwise, return the minimum degree.
Design Choices:
i < j < k
avoids duplicate trio checks.
Sample Input:
n = 6
, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]
deg[1] = 3
(edges to 2, 3, 4)
deg[2] = 3
(edges to 1, 3, 5)
deg[3] = 3
(edges to 1, 2, 6)
deg[1] + deg[2] + deg[3] = 3 + 3 + 3 = 9
9 - 6 = 3
3
.
class Solution:
def minTrioDegree(self, n: int, edges: List[List[int]]) -> int:
adj = [[False] * (n + 1) for _ in range(n + 1)]
deg = [0] * (n + 1)
for u, v in edges:
adj[u][v] = adj[v][u] = True
deg[u] += 1
deg[v] += 1
ans = float('inf')
for i in range(1, n + 1):
for j in range(i + 1, n + 1):
if not adj[i][j]:
continue
for k in range(j + 1, n + 1):
if adj[i][k] and adj[j][k]:
trio_deg = deg[i] + deg[j] + deg[k] - 6
ans = min(ans, trio_deg)
return ans if ans != float('inf') else -1
class Solution {
public:
int minTrioDegree(int n, vector<vector<int>>& edges) {
vector<vector<bool>> adj(n + 1, vector<bool>(n + 1, false));
vector<int> deg(n + 1, 0);
for (const auto& e : edges) {
int u = e[0], v = e[1];
adj[u][v] = adj[v][u] = true;
deg[u]++;
deg[v]++;
}
int ans = INT_MAX;
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j) {
if (!adj[i][j]) continue;
for (int k = j + 1; k <= n; ++k) {
if (adj[i][k] && adj[j][k]) {
int trio_deg = deg[i] + deg[j] + deg[k] - 6;
ans = min(ans, trio_deg);
}
}
}
}
return ans == INT_MAX ? -1 : ans;
}
};
class Solution {
public int minTrioDegree(int n, int[][] edges) {
boolean[][] adj = new boolean[n + 1][n + 1];
int[] deg = new int[n + 1];
for (int[] e : edges) {
int u = e[0], v = e[1];
adj[u][v] = adj[v][u] = true;
deg[u]++;
deg[v]++;
}
int ans = Integer.MAX_VALUE;
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j) {
if (!adj[i][j]) continue;
for (int k = j + 1; k <= n; ++k) {
if (adj[i][k] && adj[j][k]) {
int trioDeg = deg[i] + deg[j] + deg[k] - 6;
ans = Math.min(ans, trioDeg);
}
}
}
}
return ans == Integer.MAX_VALUE ? -1 : ans;
}
}
var minTrioDegree = function(n, edges) {
const adj = Array.from({length: n+1}, () => Array(n+1).fill(false));
const deg = Array(n+1).fill(0);
for (let [u, v] of edges) {
adj[u][v] = adj[v][u] = true;
deg[u]++;
deg[v]++;
}
let ans = Infinity;
for (let i = 1; i <= n; ++i) {
for (let j = i+1; j <= n; ++j) {
if (!adj[i][j]) continue;
for (let k = j+1; k <= n; ++k) {
if (adj[i][k] && adj[j][k]) {
let trioDeg = deg[i] + deg[j] + deg[k] - 6;
ans = Math.min(ans, trioDeg);
}
}
}
}
return ans === Infinity ? -1 : ans;
};
To solve the "Minimum Degree of a Connected Trio in a Graph" problem, we: