Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cae48e7

Browse files
committed
Merge pull request #380 from 0xff-dev/1971
Add solution and test-cases for problem 1971
2 parents b359bda + acc2b3a commit cae48e7

File tree

5 files changed

+120
-23
lines changed

5 files changed

+120
-23
lines changed

‎leetcode/1901-2000/1971.Find-if-Path-Exists-in-Graph/README.md‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [1971.Find if Path Exists in Graph][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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 u<sub>i</sub> and vertex v<sub>i</sub>. Every vertex pair is connected by **at most one** edge, and no vertex has an edge to itself.
5+
6+
You want to determine if there is a **valid path** that exists from vertex `source` to vertex `destination`.
7+
8+
Given `edges` and the integers `n`, `source`, and `destination`, return `true` if there is a **valid path** from `source` to `destination`, or `false` otherwise.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![example1](./validpath-ex1.png)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
16+
Output: true
17+
Explanation: There are two paths from vertex 0 to vertex 2:
18+
- 0 → 1 → 2
19+
- 0 → 2
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
24+
![example2](./validpath-ex2.png)
1925

20-
### 思路1
21-
> ...
22-
Find if Path Exists in Graph
23-
```go
2426
```
25-
27+
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
28+
Output: false
29+
Explanation: There is no path from vertex 0 to vertex 5.
30+
```
2631

2732
## 结语
2833

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,81 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, edges [][]int, source int, destination int) bool {
4+
graph := make(map[int]map[int]struct{})
5+
for _, e := range edges {
6+
if _, ok := graph[e[0]]; !ok {
7+
graph[e[0]] = make(map[int]struct{})
8+
}
9+
if _, ok := graph[e[1]]; !ok {
10+
graph[e[1]] = make(map[int]struct{})
11+
}
12+
13+
graph[e[0]][e[1]] = struct{}{}
14+
graph[e[1]][e[0]] = struct{}{}
15+
}
16+
17+
visited := map[int]struct{}{}
18+
visited[source] = struct{}{}
19+
found := false
20+
var dfs func(int, *bool)
21+
dfs = func(source int, found *bool) {
22+
if *found {
23+
return
24+
}
25+
for rel := range graph[source] {
26+
if _, ok := visited[rel]; ok {
27+
continue
28+
}
29+
if rel == destination {
30+
*found = true
31+
return
32+
}
33+
visited[rel] = struct{}{}
34+
dfs(rel, found)
35+
delete(visited, rel)
36+
}
37+
}
38+
dfs(source, &found)
39+
return found
40+
}
41+
42+
func Solution1(n int, edges [][]int, source int, destination int) bool {
43+
if source == destination {
44+
return true
45+
}
46+
graph := make(map[int]map[int]struct{})
47+
for _, e := range edges {
48+
if _, ok := graph[e[0]]; !ok {
49+
graph[e[0]] = make(map[int]struct{})
50+
}
51+
if _, ok := graph[e[1]]; !ok {
52+
graph[e[1]] = make(map[int]struct{})
53+
}
54+
55+
graph[e[0]][e[1]] = struct{}{}
56+
graph[e[1]][e[0]] = struct{}{}
57+
}
58+
59+
queue := []int{source}
60+
61+
visited := map[int]struct{}{}
62+
visited[source] = struct{}{}
63+
64+
for len(queue) > 0 {
65+
nq := make([]int, 0)
66+
for _, item := range queue {
67+
for rel := range graph[item] {
68+
if rel == destination {
69+
return true
70+
}
71+
72+
if _, ok := visited[rel]; !ok {
73+
visited[rel] = struct{}{}
74+
nq = append(nq, rel)
75+
}
76+
}
77+
}
78+
queue = nq
79+
}
80+
return false
581
}

‎leetcode/1901-2000/1971.Find-if-Path-Exists-in-Graph/Solution_test.go‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,37 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
n int
14+
edges [][]int
15+
source, destination int
1416
expect bool
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", 3, [][]int{
19+
{0, 1},
20+
{1, 2},
21+
{2, 0},
22+
}, 0, 2, true},
23+
{"TestCase2", 6, [][]int{
24+
{0, 1},
25+
{0, 2},
26+
{3, 5},
27+
{5, 4},
28+
{4, 3},
29+
}, 0, 5, false},
1930
}
2031

2132
// 开始测试
2233
for i, c := range cases {
2334
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
35+
got := Solution(c.n, c.edges, c.source, c.destination)
2536
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
37+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
38+
c.expect, got, c.n, c.edges, c.source, c.destination)
39+
}
40+
got = Solution1(c.n, c.edges, c.source, c.destination)
41+
if !reflect.DeepEqual(got, c.expect) {
42+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
43+
c.expect, got, c.n, c.edges, c.source, c.destination)
2844
}
2945
})
3046
}
4.31 KB
Loading[フレーム]
7.37 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /