Skip to content

Commit 200a990

Browse files
committed
leetcode
1 parent fb2a947 commit 200a990

File tree

4 files changed

+367
-0
lines changed

4 files changed

+367
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
3+
-*1971. Find if Path Exists in Graph *-
4+
5+
6+
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.
7+
8+
You want to determine if there is a valid path that exists from vertex source to vertex destination.
9+
10+
Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.
11+
12+
13+
14+
Example 1:
15+
16+
17+
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
18+
Output: true
19+
Explanation: There are two paths from vertex 0 to vertex 2:
20+
- 0 → 1 → 2
21+
- 0 → 2
22+
Example 2:
23+
24+
25+
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
26+
Output: false
27+
Explanation: There is no path from vertex 0 to vertex 5.
28+
29+
30+
Constraints:
31+
32+
1 <= n <= 2 * 105
33+
0 <= edges.length <= 2 * 105
34+
edges[i].length == 2
35+
0 <= ui, vi <= n - 1
36+
ui != vi
37+
0 <= source, destination <= n - 1
38+
There are no duplicate edges.
39+
There are no self edges.
40+
41+
42+
*/
43+
44+
import 'dart:collection';
45+
46+
class A {
47+
bool found = false;
48+
void dfs(Map<int, List<int>> graph, List<bool> visited, int start, int end) {
49+
if (visited[start] || found) return;
50+
visited[start] = true;
51+
//when we found and neighbor which is equal to end point inside the recursion, voooleeey! break and return the true
52+
for (int nei in graph[start] ?? []) {
53+
if (nei == end) {
54+
found = true;
55+
break;
56+
}
57+
if (!visited[nei])
58+
dfs(graph, visited, nei, end); //otherwise deep dig again!
59+
}
60+
}
61+
62+
bool validPath(int n, List<List<int>> edges, int source, int destination) {
63+
if (source == destination) return true;
64+
65+
HashMap<int, List<int>> graph = HashMap();
66+
List<bool> visited = List.filled(n, false);
67+
68+
for (int i = 0; i < n; i++) graph[i] = [];
69+
//construct graph, add bidirectional vertex
70+
for (List<int> edge in edges) {
71+
graph[edge[0]]?.add(edge[1]);
72+
graph[edge[1]]?.add(edge[0]);
73+
}
74+
//start dfs from start point
75+
dfs(graph, visited, source, destination);
76+
return found;
77+
}
78+
}
79+
80+
class B {
81+
List<int> parent = [];
82+
int findParent(int node) {
83+
return parent[node] == node
84+
? node
85+
: (parent[node] = findParent(parent[node]));
86+
}
87+
88+
void makeSameGroup(int u, int v) {
89+
int pu = findParent(u);
90+
int pv = findParent(v);
91+
parent[pu] = pv;
92+
}
93+
94+
bool validPath(int n, List<List<int>> edges, int source, int destination) {
95+
// resizing an array
96+
parent = List.filled(n, 0);
97+
for (int i = 0; i < n; i++) parent[i] = i;
98+
99+
for (List<int> e in edges) {
100+
makeSameGroup(e[0], e[1]);
101+
}
102+
return findParent(source) == findParent(destination);
103+
}
104+
}
105+
106+
107+
108+
109+
110+
//==========================
111+
112+
// class Solution {
113+
// bool validPath(int n, List<List<int>> edges, int source, int destination) {
114+
// UnionFind uf = UnionFind(n);
115+
// for (List<int> edge in edges) {
116+
// // connect two node
117+
// uf.union(edge[0], edge[1]);
118+
// }
119+
120+
// return uf.find(source, destination);
121+
// }
122+
// }
123+
124+
// class UnionFind {
125+
// // parent of node
126+
// late final List<int> parent;
127+
// // rank of disjoint set
128+
// late final List<int> rank;
129+
// // number of disjoint set
130+
// late int count;
131+
132+
// UnionFind(int n) {
133+
// count = n;
134+
// parent = List.filled(n, 0);
135+
// rank = List.filled(n, 0);
136+
// for (int i = 0; i < n; i++) {
137+
// // every node is itself
138+
// parent[i] = i;
139+
// // every node init rank is one
140+
// rank[i] = 1;
141+
// }
142+
// }
143+
144+
// int root(int p) {
145+
// while (parent[p] != p) {
146+
// // compress path
147+
// parent[p] = parent[parent[p]];
148+
// p = parent[p];
149+
// }
150+
151+
// return p;
152+
// }
153+
154+
// int counting() {
155+
// return count;
156+
// }
157+
158+
// bool find(int p, int q) {
159+
// return root(p) == root(q);
160+
// }
161+
162+
// bool union(int p, int q) {
163+
// int rootP = root(p);
164+
// int rootQ = root(q);
165+
// if (rootP == rootQ) {
166+
// return false;
167+
// }
168+
169+
// if (rank[rootP] < rank[rootQ]) {
170+
// parent[rootP] = rootQ;
171+
// rank[rootQ] += rank[rootP];
172+
// } else {
173+
// parent[rootQ] = rootP;
174+
// rank[rootP] += rank[rootQ];
175+
// }
176+
177+
// count--;
178+
179+
// return true;
180+
// }
181+
// }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import "math"
4+
5+
func validPath(n int, edges [][]int, start int, end int) bool {
6+
way := make([]int, n)
7+
for i := range way {
8+
way[i] = i
9+
}
10+
root := func(n int, way []int) int {
11+
for way[n] != n {
12+
n = way[n]
13+
}
14+
return n
15+
}
16+
for _, e := range edges {
17+
root0, root1 := root(e[0], way), root(e[1], way)
18+
minValue := min(root0, root1)
19+
way[e[0]] = minValue
20+
way[e[1]] = minValue
21+
way[root0] = minValue
22+
way[root1] = minValue
23+
}
24+
return root(start, way) == root(end, way)
25+
}
26+
27+
func min(values ...int) int {
28+
minValue := math.MaxInt64
29+
for _, v := range values {
30+
if v < minValue {
31+
minValue = v
32+
}
33+
}
34+
return minValue
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# 🔥 Find if Path Exists in Graph 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 Union Find Algorithm
4+
5+
### Union Find Implementation
6+
7+
```dart
8+
class UnionFind {
9+
// parent of node
10+
late final List<int> parent;
11+
// rank of disjoint set
12+
late final List<int> rank;
13+
// number of disjoint set
14+
late int count;
15+
16+
UnionFind(int n) {
17+
count = n;
18+
parent = List.filled(n, 0);
19+
rank = List.filled(n, 0);
20+
for (int i = 0; i < n; i++) {
21+
// every node is itself
22+
parent[i] = i;
23+
// every node init rank is one
24+
rank[i] = 1;
25+
}
26+
}
27+
28+
int root(int p) {
29+
while (parent[p] != p) {
30+
// compress path
31+
parent[p] = parent[parent[p]];
32+
p = parent[p];
33+
}
34+
35+
return p;
36+
}
37+
38+
int counting() {
39+
return count;
40+
}
41+
42+
bool find(int p, int q) {
43+
return root(p) == root(q);
44+
}
45+
46+
bool union(int p, int q) {
47+
int rootP = root(p);
48+
int rootQ = root(q);
49+
if (rootP == rootQ) {
50+
return false;
51+
}
52+
53+
if (rank[rootP] < rank[rootQ]) {
54+
parent[rootP] = rootQ;
55+
rank[rootQ] += rank[rootP];
56+
} else {
57+
parent[rootQ] = rootP;
58+
rank[rootP] += rank[rootQ];
59+
}
60+
61+
count--;
62+
63+
return true;
64+
}
65+
}
66+
```
67+
68+
#### Final Code
69+
70+
```dart
71+
class Solution {
72+
bool validPath(int n, List<List<int>> edges, int source, int destination) {
73+
UnionFind uf = UnionFind(n);
74+
for (List<int> edge in edges) {
75+
// connect two node
76+
uf.union(edge[0], edge[1]);
77+
}
78+
79+
return uf.find(source, destination);
80+
}
81+
}
82+
```
83+
84+
## Solution - 2
85+
86+
```dart
87+
class Solution {
88+
List<int> parent = [];
89+
int findParent(int node) {
90+
return parent[node] == node
91+
? node
92+
: (parent[node] = findParent(parent[node]));
93+
}
94+
95+
void makeSameGroup(int u, int v) {
96+
int pu = findParent(u);
97+
int pv = findParent(v);
98+
parent[pu] = pv;
99+
}
100+
101+
bool validPath(int n, List<List<int>> edges, int source, int destination) {
102+
// resizing an array
103+
parent = List.filled(n, 0);
104+
for (int i = 0; i < n; i++) parent[i] = i;
105+
106+
for (List<int> e in edges) {
107+
makeSameGroup(e[0], e[1]);
108+
}
109+
return findParent(source) == findParent(destination);
110+
}
111+
}
112+
```
113+
114+
## Solution - 3 DFS
115+
116+
```dart
117+
class Solution {
118+
bool found = false;
119+
void dfs(Map<int, List<int>> graph, List<bool> visited, int start, int end) {
120+
if (visited[start] || found) return;
121+
visited[start] = true;
122+
//when we found and neighbor which is equal to end point inside the recursion, voooleeey! break and return the true
123+
for (int nei in graph[start] ?? []) {
124+
if (nei == end) {
125+
found = true;
126+
break;
127+
}
128+
if (!visited[nei])
129+
dfs(graph, visited, nei, end); //otherwise deep dig again!
130+
}
131+
}
132+
133+
bool validPath(int n, List<List<int>> edges, int source, int destination) {
134+
if (source == destination) return true;
135+
136+
HashMap<int, List<int>> graph = HashMap();
137+
List<bool> visited = List.filled(n, false);
138+
139+
for (int i = 0; i < n; i++) graph[i] = [];
140+
//construct graph, add bidirectional vertex
141+
for (List<int> edge in edges) {
142+
graph[edge[0]]?.add(edge[1]);
143+
graph[edge[1]]?.add(edge[0]);
144+
}
145+
//start dfs from start point
146+
dfs(graph, visited, source, destination);
147+
return found;
148+
}
149+
}
150+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
170170
- [**1143.** Longest Common Subsequence](LongestCommonSubsequence/longest_common_subsequence.dart)
171171
- [**150.** Evaluate Reverse Polish Notation](EvaluateReversePolishNotation/evaluate_reverse_polish_notation.dart)
172172
- [**739.** Daily Temperatures](DailyTemperatures/daily_temperatures.dart)
173+
- [**1971.* Find if Path Exists in Graph](FindIfPathExistsInGraph/find_if_path_exists_in_graph.dart)
173174

174175
## Reach me via
175176

0 commit comments

Comments
 (0)