classSolution:defpathExists(self,n,edges,source,destination):""" :type n: int :type edges: List[List[int]] :type source: int :type destination: int :rtype: bool """adjacency_list=defaultdict(list)# adjacency_list = {i: [] for i in range(n)}foru,vinedges:adjacency_list[u].append(v)adjacency_list[v].append(u)visited=set()defdfs(node):ifnode==destination:returnTruevisited.add(node)forneighbourinadjacency_list[node]:ifneighbournotinvisited:ifdfs(neighbour):returnTruereturnFalsereturndfs(source)
defpathExists(self,n,edges,source,destination):""" :type n: int :type edges: List[List[int]] :type source: int :type destination: int :rtype: bool """adjacency_list=defaultdict(list)# adjacency_list = {i: [] for i in range(n)}foru,vinedges:adjacency_list[u].append(v)adjacency_list[v].append(u)visited=set()defdfs(node):ifnode==destination:returnTruevisited.add(node)forneighbourinadjacency_list[node]:ifneighbournotinvisited:ifdfs(neighbour):returnTruereturnFalsereturndfs(source)