[Leetcode Daily Notes] 547. Number of Provinces (Python)

topic

There are n cities, some of which are connected to each other, and some of them are not connected. If city a and city b are directly connected, and city b is directly connected to city c, then city a and city c are indirectly connected.

A province is a group of directly or indirectly connected cities, and the group does not contain other unconnected cities.

Give you an nxn matrix isConnected, where isConnected[i][j] = 1 means that the i-
th city is directly connected to the j- th city, and isConnected[i][j] = 0 means that the two are not directly connected.

Returns the number of provinces in the matrix.
Insert picture description here

Problem-solving ideas

And check set

This question is actually to find the number of connected components in the graph. The first thing that comes to mind is to check the set

Depth first search

The idea of ​​depth-first search is very intuitive. Traverse all cities. For each city, if the city has not been visited yet, then start the depth-first search from the city, through the matrix isConnected \textit{isConnected}isConnected obtains which cities are directly connected to the city, these cities and the city belong to the same connected component, and then continue the depth-first search for these cities until all cities of the same connected component are visited, you can get a province . After traversing all cities, you can get the total number of connected components, that is, the total number of provinces.

Breadth first search

For each city, if the city has not been visited, the breadth-first search is started from the city until all cities in the same connected component are visited, and a province can be obtained.

Code

And check set

class Solution:
    def findCircleNum(self, isConnected: List[List[int]]) -> int:
        '''
        并查集:
        1.初始化每个节点的状态
        2.查找每个节点的父节点
        3.合并
        4.找到所有父节点为自己的节点,就是连通分量的个数
        '''
        l = len(isConnected)
        ans = 0
        # 初始化每个节点
        parent = [i for i in range(l)]
        def union(parent,i,j):
            parent[find(parent,i)] = find(parent,j)
        def find(parent,i):
            while parent[i] != i:
                parent[i] = parent[parent[i]]
                i = parent[i]
            return i
        for i in range(l):
            for j in range(l):
                if isConnected[i][j] == 1:
                    union(parent,i,j)
        for i in range(l):
            if parent[i] == i:
                ans += 1
        return ans

Depth first search

class Solution:
    def findCircleNum(self, isConnected: List[List[int]]) -> int:
        def dfs(i: int):
            for j in range(provinces):
                if isConnected[i][j] == 1 and j not in visited:
                    visited.add(j)
                    dfs(j)
        
        provinces = len(isConnected)
        visited = set()
        circles = 0

        for i in range(provinces):
            if i not in visited:
                dfs(i)
                circles += 1
        
        return circles

Breadth first search

class Solution:
    def findCircleNum(self, isConnected: List[List[int]]) -> int:
        provinces = len(isConnected)
        visited = set()
        circles = 0
        
        for i in range(provinces):
            if i not in visited:
                Q = collections.deque([i])
                while Q:
                    j = Q.popleft()
                    visited.add(j)
                    for k in range(provinces):
                        if isConnected[j][k] == 1 and k not in visited:
                            Q.append(k)
                circles += 1
        
        return circles

Guess you like

Origin blog.csdn.net/qq_36477513/article/details/112303069