Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1971 · Graph Fundamentals / DFS-BFS

Find if Path Exists in Graph

LeetCode 1971 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

BFS reachability.

எளிதுDepth-First SearchBreadth-First SearchUnion-FindGraph Theory
LeetCode இல் திற ↗

நான் Find if Path Exists in Graph (LeetCode 1971) ஐ இப்படித் தீர்த்தேன் — Graph Fundamentals / DFS-BFS பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Build the undirected graph and BFS from the source; the destination is reachable exactly when BFS visits it.

Approach

  1. 1

    Adjacency list both ways.

  2. 2

    BFS from source; return True when destination is popped.

நான் எப்படித் தீர்த்தேன்

BFSPythonO(V+E) time
class Solution:
    def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
        adj = defaultdict(list)
        for u,v in edges:
            adj[u].append(v)
            adj[v].append(u)
        q = deque([source])
        visited = set()
        while q:
            node = q.popleft()
            if node==destination:
                return True
            for adj_node in adj[node]:
                if adj_node not in visited:
                    visited.add(adj_node)
                    q.append(adj_node)
        return False
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Find if Path Exists in Graph — அடிக்கடி கேட்கப்படும் கேள்விகள்

Find if Path Exists in Graph பின்னாலுள்ள intuition என்ன?

Build the undirected graph and BFS from the source; the destination is reachable exactly when BFS visits it.

Find if Path Exists in Graph (LeetCode 1971) ஐ எப்படித் தீர்ப்பது?

1. Adjacency list both ways. 2. BFS from source; return True when destination is popped.

Find if Path Exists in Graph இன் time மற்றும் space complexity என்ன?

Time: O(V + E). Space: O(V + E). (Accepted approach: BFS.)

Find if Path Exists in Graph இல் பொதுவான தவறுகள் என்ன?

source == destination is trivially True.