#1971

Find if Path Exists in Graph

Easy Adjacency list + BFS

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

An undirected graph has n vertices and an edge list. Decide whether a path exists from source to destination.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: can you get from source to destination?

Why this works: build the adjacency list and BFS from the source; if you reach the destination, a path exists.

Example: edges [0-1, 1-2, 2-0], 0 → 2

visit 0 → neighbours 1, 2 → found 2 → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Adding each undirected edge in only one direction.

Solve on LeetCode ↗
Complexity
  • Time: O(V + E).
  • Space: O(V + E).
Topic: Graphs
🎬 Episodes 74–85: video coming soon