Number of Connected Components in an Undirected Graph

Problem Id: 323 Difficulty: Medium


Intuition

Solution


class Solution:
    def countComponents(self, n: int, edges: List[List[int]]) -> int:
        graph = Graph(n)
        for p, q in edges:
            graph.add_edge(p, q)

        components = ConnectedComponents(graph)
        return components.components