|
| 1 | +# LeetCode 1971. Find if Path Exists in Graph's Solution |
| 2 | +LeetCode problem link: [1971. Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph) |
| 3 | + |
| 4 | +## LeetCode problem description |
| 5 | +There is a **bi-directional** graph with `n` vertices, where each vertex is labeled from `0` to `n - 1` (**inclusive**). The edges in the graph are represented as a 2D integer array `edges`, where each `edges[i] = [ui, vi]` denotes a bi-directional edge between vertex `ui` and vertex `vi`. Every vertex pair is connected by **at most one** edge, and no vertex has an edge to itself. |
| 6 | + |
| 7 | +You want to determine if there is a **valid path** that exists from vertex `source` to vertex `destination`. |
| 8 | + |
| 9 | +Given `edges` and the integers `n`, `source`, and `destination`, return `true` _if there is a **valid path** from `source` to `destination`, or `false` otherwise_. |
| 10 | + |
| 11 | +### Example 1 |
| 12 | + |
| 13 | +``` |
| 14 | +Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2 |
| 15 | +Output: true |
| 16 | +Explanation: There are two paths from vertex 0 to vertex 2: |
| 17 | +- 0 → 1 → 2 |
| 18 | +- 0 → 2 |
| 19 | +``` |
| 20 | + |
| 21 | +### Example 2 |
| 22 | + |
| 23 | +``` |
| 24 | +Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5 |
| 25 | +Output: false |
| 26 | +Explanation: There is no path from vertex 0 to vertex 5. |
| 27 | +``` |
| 28 | + |
| 29 | +### Constraints |
| 30 | +- `1 <= n <= 2 * 10**5` |
| 31 | +- `0 <= edges.length <= 2 * 10**5` |
| 32 | +- `edges[i].length == 2` |
| 33 | +- `0 <= ui, vi <= n - 1` |
| 34 | +- `ui != vi` |
| 35 | +- `0 <= source, destination <= n - 1` |
| 36 | +- There are no duplicate edges. |
| 37 | +- There are no self edges. |
| 38 | + |
| 39 | +## Intuition |
| 40 | +The island problem can be abstracted into a **graph theory** problem. This is an **undirected graph**: |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +And this graph may have multiple **connected components**. At first, we start from `source` vertex which belongs to one of the `connected components`. |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +We need to find if there is a path from `source` to `destination`. This question is equivalent to determine if `source` and `destination` vertices belong to the same `connected component`. |
| 49 | + |
| 50 | +### Breadth-First Search |
| 51 | +* There are two major ways to explore a `connected component`: **Breadth-First Search** and **Depth-First Search**. |
| 52 | +* For **Depth-First Search**, there are two ways to make it: `Recursive` and `Iterative`. Please see [200. Number of Islands (Depth-First Search)](../1-1000/200-number-of-islands.md). |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +* As shown in the figure above, **breadth-first search** can be thought of as visiting vertices in rounds and rounds. Actually, whenever you see a question is about |
| 57 | + getting `shortest` or `least` of something of a graph, `breadth-first search` would probably help. |
| 58 | + |
| 59 | +* `breadth-first search` emphasizes first-in-first-out, so a **queue** is needed. |
| 60 | + |
| 61 | +## Approach |
| 62 | +1. Starting at the `source` vertex, find all the vertices of the `connected component` by `breadth-first search`. |
| 63 | +1. In order to conduct `breadth-first search`, we need to know the adjacent vertices of a vertex. So we need a `map` `vertex_to_adjacent_vertices`. We can initialize the `map` by transforming `edges`. |
| 64 | +1. We need to mark all vertices on the same connected component as vertex `source` as `visited` because visited vertices don't need to be visited again. |
| 65 | +1. Once vertex `destination` is encountered, return `true`. |
| 66 | + |
| 67 | +## Complexity |
| 68 | +* Time: `O(n)`. |
| 69 | +* Space: `O(n)`. |
| 70 | + |
| 71 | +## Python |
| 72 | +```python |
| 73 | +class Solution: |
| 74 | + def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: |
| 75 | + vertex_queue = deque([source]) |
| 76 | + visited_vertices = set([source]) |
| 77 | + |
| 78 | + vertex_to_adjacent_vertices = defaultdict(list) |
| 79 | + for vertex0, vertex1 in edges: |
| 80 | + vertex_to_adjacent_vertices[vertex0].append(vertex1) |
| 81 | + vertex_to_adjacent_vertices[vertex1].append(vertex0) |
| 82 | + |
| 83 | + while vertex_queue: |
| 84 | + vertex = vertex_queue.popleft() |
| 85 | + |
| 86 | + if vertex == destination: |
| 87 | + return True |
| 88 | + |
| 89 | + for adjacent_vertex in vertex_to_adjacent_vertices[vertex]: |
| 90 | + if adjacent_vertex not in visited_vertices: |
| 91 | + vertex_queue.append(adjacent_vertex) |
| 92 | + visited_vertices.add(adjacent_vertex) # Mark visited as soon as `vertex_queue.append(adjacent_vertex)`. Otherwise it may have performance issue! |
| 93 | + |
| 94 | + return False |
| 95 | +``` |
| 96 | + |
| 97 | +## Java |
| 98 | +```java |
| 99 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 100 | +``` |
| 101 | + |
| 102 | +## C++ |
| 103 | +```cpp |
| 104 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 105 | +``` |
| 106 | + |
| 107 | +## JavaScript |
| 108 | +```javascript |
| 109 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 110 | +``` |
| 111 | + |
| 112 | +## C# |
| 113 | +```c# |
| 114 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 115 | +``` |
| 116 | + |
| 117 | +## Go |
| 118 | +```go |
| 119 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 120 | +``` |
| 121 | + |
| 122 | +## Ruby |
| 123 | +```ruby |
| 124 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 125 | +``` |
| 126 | + |
| 127 | +## C |
| 128 | +```c |
| 129 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 130 | +``` |
| 131 | + |
| 132 | +## Kotlin |
| 133 | +```kotlin |
| 134 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 135 | +``` |
| 136 | + |
| 137 | +## Swift |
| 138 | +```swift |
| 139 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 140 | +``` |
| 141 | + |
| 142 | +## Rust |
| 143 | +```rust |
| 144 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 145 | +``` |
| 146 | + |
| 147 | +## Other languages |
| 148 | +``` |
| 149 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 150 | +``` |
0 commit comments