You are given an undirected graph. You are given an integer n
which is the number of nodes in the graph and an array edges
, where each edges[i] = [ui, vi]
indicates that there is an undirected edge between ui
and vi
.
A connected trio is a set of three nodes where there is an edge between every pair of them.
The degree of a connected trio is the number of edges where one endpoint is in the trio, and the other is not.
Return the minimum degree of a connected trio in the graph, or -1
if the graph has no connected trios.
Example 1:
Input: n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]] Output: 3 Explanation: There is exactly one trio, which is [1,2,3]. The edges that form its degree are bolded in the figure above.
Example 2:
Input: n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]] Output: 0 Explanation: There are exactly three trios: 1) [1,4,3] with degree 0. 2) [2,5,6] with degree 2. 3) [5,6,7] with degree 2.
Constraints:
2 <= n <= 400
edges[i].length == 2
1 <= edges.length <= n * (n-1) / 2
1 <= ui, vi <= n
ui != vi
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves checking every possible group of three connected nodes in the graph. For each group, we calculate its 'degree' (number of connections) and keep track of the smallest degree we find. In the end, we return this minimum degree, or a special value if no connected trio exists.
Here's how the algorithm would work step-by-step:
def minimum_degree_connected_trio(number_of_nodes: int, edges: list[list[int]]) -> int:
adjacency_matrix = [[False] * (number_of_nodes + 1) for _ in range(number_of_nodes + 1)]
degrees = [0] * (number_of_nodes + 1)
for edge in edges:
node_a, node_b = edge
adjacency_matrix[node_a][node_b] = True
adjacency_matrix[node_b][node_a] = True
degrees[node_a] += 1
degrees[node_b] += 1
minimum_trio_degree = float('inf')
found_trio = False
# Iterate through all possible combinations of three nodes
for node_i in range(1, number_of_nodes + 1):
for node_j in range(node_i + 1, number_of_nodes + 1):
for node_k in range(node_j + 1, number_of_nodes + 1):
# Check if all three nodes are connected to each other
if adjacency_matrix[node_i][node_j] and \
adjacency_matrix[node_i][node_k] and \
adjacency_matrix[node_j][node_k]:
# Calculate the degree of the trio
trio_degree = degrees[node_i] + degrees[node_j] + degrees[node_k] - 6
# Update the minimum degree if necessary
minimum_trio_degree = min(minimum_trio_degree, trio_degree)
found_trio = True
# If no connected trio was found, return -1
if not found_trio:
return -1
return minimum_trio_degree
The problem asks us to find a group of three connected nodes in a graph with the smallest combined degree. Instead of checking every possible group of three, we can be smarter by focusing on existing connections and then checking for the third connection to form a trio. This significantly reduces the work we have to do.
Here's how the algorithm would work step-by-step:
def minimum_degree_of_connected_trio(number_of_nodes, edges):
adjacency_matrix = [[False] * (number_of_nodes + 1) for _ in range(number_of_nodes + 1)]
degrees = [0] * (number_of_nodes + 1)
for edge_start, edge_end in edges:
adjacency_matrix[edge_start][edge_end] = True
adjacency_matrix[edge_end][edge_start] = True
degrees[edge_start] += 1
degrees[edge_end] += 1
minimum_trio_degree = float('inf')
# Iterate through all possible edge combinations.
for edge_start, edge_end in edges:
# Find a third node connected to both ends of the edge.
for potential_third_node in range(1, number_of_nodes + 1):
if adjacency_matrix[edge_start][potential_third_node] and adjacency_matrix[edge_end][potential_third_node]:
# Calculate trio degree, subtracting 2 for connections.
trio_degree = degrees[edge_start] + degrees[edge_end] + degrees[potential_third_node] - 6
minimum_trio_degree = min(minimum_trio_degree, trio_degree)
# No trio found return -1, otherwise return minimum degree
if minimum_trio_degree == float('inf'):
return -1
else:
return minimum_trio_degree
Case | How to Handle |
---|---|
Empty graph (n=0) | Return -1 immediately since no trio can exist in an empty graph. |
Graph with fewer than 3 nodes (n < 3) | Return -1 immediately since a trio requires at least 3 nodes. |
No edges present in the graph | Return -1 as no connected trio can exist without any edges. |
Complete graph where every node is connected to every other node | The minimum degree will be easily calculable as each trio will have degree 3. |
Graph where no trios exist at all | Return -1 after iterating through all possible trios and not finding any. |
Large graph with many nodes and edges: potential integer overflow when calculating degree sum. | Use a data type with a larger range (e.g., long) to store the degree sum to prevent integer overflow. |
Disconnected graph: some nodes might not belong to any trio | The adjacency matrix or list approach will naturally handle this by not considering nodes in different components together. |
Graph represented by adjacency list with unsorted neighbors for each node. | Ensure the algorithm used to find connected trios correctly identifies neighbors regardless of their order in the adjacency list. |