图的遍历

面试题 04.01. 节点间通路

BFS

from typing import List
from collections import defaultdict, deque


class Solution:
    def findWhetherExistsPath(self, n: int, graph: List[List[int]], start: int, target: int) -> bool:

        dd = defaultdict(set)
        for a, b in graph:
            dd[a].add(b)

        dq = deque()
        dq.append(start)
        mark = set()
        while dq:
            node = dq.popleft()
            if node == target:
                return True

            if node in mark:
                continue

            dq.extend(dd[node])
            mark.add(node)

        return False

DFS

最后更新于

这有帮助吗?