Leetcode 2685: Count the Number of Complete Components

grid47
grid47
Exploring patterns and algorithms
Feb 12, 2024 7 min read

You are given a graph with n vertices, numbered from 0 to n-1. The graph contains undirected edges described in a 2D array edges, where each element edges[i] = [ai, bi] indicates that there is an undirected edge between vertices ai and bi. A connected component is a subgraph in which there is a path between any two vertices, and no vertex is connected to vertices outside of the subgraph. A connected component is said to be complete if there is an edge between every pair of vertices in that component. Your task is to return the number of complete connected components in the graph.
Problem
Approach
Steps
Complexity
Input: You are given an integer `n`, representing the number of vertices in the graph, and a list `edges` where each element is an edge connecting two vertices.
Example: Input: n = 6, edges = [[0,1], [0,2], [1,2], [3,4]]
Constraints:
• 1 <= n <= 50
• 0 <= edges.length <= n * (n - 1) / 2
• edges[i].length == 2
• 0 <= ai, bi <= n - 1
• ai != bi
• There are no repeated edges.
Output: Return the number of complete connected components in the graph.
Example: Output: 3
Constraints:
• The output should be a single integer representing the number of complete connected components.
Goal: The goal is to count the number of complete connected components in the graph, where each component must have an edge between every pair of its vertices.
Steps:
• Step 1: Initialize a Union-Find (disjoint-set) data structure to track the connected components in the graph.
• Step 2: Process each edge and union the vertices that are connected by that edge.
• Step 3: For each connected component, check if it is complete by comparing the number of edges in the component with the expected number of edges for a complete graph (n*(n-1)/2).
• Step 4: Count how many of the connected components are complete and return that count.
Goal: The problem has the following constraints on the graph size and edges.
Steps:
• The number of vertices `n` is between 1 and 50.
• The number of edges is between 0 and n*(n-1)/2, meaning the graph can have up to a fully connected structure.
• Each edge is represented as a pair of integers, where 0 <= ai, bi <= n-1 and ai != bi.
Assumptions:
• The graph may have disconnected components.
• The graph may have some incomplete components where not every pair of vertices is connected by an edge.
Input: Input: n = 6, edges = [[0,1], [0,2], [1,2], [3,4]]
Explanation: In this graph, there are 3 connected components. The first component, consisting of vertices 0, 1, and 2, is complete because there are edges between every pair. The second component, consisting of vertex 3 and 4, is also complete. The third component is just a single vertex, which is trivially complete. Thus, the output is 3.

Input: Input: n = 6, edges = [[0,1], [0,2], [1,2], [3,4], [3,5]]
Explanation: The first component, consisting of vertices 0, 1, and 2, is complete. However, the second component, consisting of vertices 3, 4, and 5, is not complete because there is no edge between vertices 4 and 5. Thus, the output is 1.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus