LeetCode 323: Number of Connected Components in an Undirected Graph Solution
Master LeetCode problem 323 (Number of Connected Components in an Undirected Graph), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
323. Number of Connected Components in an Undirected Graph
Problem Explanation
Explanation:
To solve this problem, we can use a graph traversal technique such as Depth First Search (DFS) or Breadth First Search (BFS) to find connected components in the given undirected graph. We will start the traversal from each unvisited node and mark all the nodes reachable from it. Each connected component will be identified by a separate traversal from an unvisited node.
Here are the steps for the algorithm:
- Create an adjacency list representation of the graph.
- Initialize a set to keep track of visited nodes.
- Initialize a variable to count the number of connected components.
- Iterate through all the nodes in the graph.
- For each unvisited node, perform a DFS or BFS traversal to mark all nodes reachable from it as visited. Increment the count of connected components.
- Return the count of connected components.
Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges in the graph. Space Complexity: O(V), where V is the number of vertices in the graph.
:
Solution Code
class Solution {
public int countComponents(int n, int[][] edges) {
List<List<Integer>> adjList = new ArrayList<>();
for (int i = 0; i < n; i++) {
adjList.add(new ArrayList<>());
}
for (int[] edge : edges) {
adjList.get(edge[0]).add(edge[1]);
adjList.get(edge[1]).add(edge[0]);
}
Set<Integer> visited = new HashSet<>();
int count = 0;
for (int i = 0; i < n; i++) {
if (!visited.contains(i)) {
dfs(i, adjList, visited);
count++;
}
}
return count;
}
private void dfs(int node, List<List<Integer>> adjList, Set<Integer> visited) {
visited.add(node);
for (int neighbor : adjList.get(node)) {
if (!visited.contains(neighbor)) {
dfs(neighbor, adjList, visited);
}
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 323 (Number of Connected Components in an Undirected Graph)?
This page provides optimized solutions for LeetCode problem 323 (Number of Connected Components in an Undirected Graph) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 323 (Number of Connected Components in an Undirected Graph)?
The time complexity for LeetCode 323 (Number of Connected Components in an Undirected Graph) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 323 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 323 in Java, C++, or Python.