-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1971.py
204 lines (169 loc) · 6.55 KB
/
1971.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# [ LeetCode ] 1971. Find if Path Exists in Graph
def solution(
n: int, edges: list[list[int]], source: int, destination: int\
) -> bool:
from collections import deque, defaultdict
if source == destination:
return True
graph: dict[int, set[int]] = defaultdict(set)
for u, v in edges:
graph[u].add(v)
graph[v].add(u)
queue: deque = deque([source])
visited: set[int] = set([source])
while queue:
vertex: int = queue.popleft()
conncted_vertices: set[int] = graph[vertex] - visited
for connected_vertex in conncted_vertices:
if connected_vertex == destination:
return True
queue.append(connected_vertex)
visited.add(connected_vertex)
return False
def another_solution(
n: int, edges: list[list[int]], source: int, destination: int
) -> bool:
from collections import defaultdict
if source == destination:
return True
graph: dict[int, set[int]] = defaultdict(set)
for u, v in edges:
graph[u].add(v)
graph[v].add(u)
stack: list[int] = [source]
visited: set[int] = set([source])
while stack:
vertex: int = stack.pop()
conncted_vertices: set[int] = graph[vertex] - visited
for connected_vertex in conncted_vertices:
if connected_vertex == destination:
return True
stack.append(connected_vertex)
visited.add(connected_vertex)
return False
def disjoint_set_quick_union(
n: int, edges: list[list[int]], source: int, destination: int
) -> bool:
class UnionFind:
def __init__(self, size: int) -> None:
self.root: list[int] = [ vertex for vertex in range(size) ]
def find(self, vertex: int) -> int:
while vertex != self.root[vertex]:
vertex: int = self.root[vertex]
return vertex
def union(self, u: int, v: int) -> int:
u_root: int = self.find(u)
v_root: int = self.find(v)
if u_root != v_root:
self.root[v_root]: int = u_root
def is_connected(self, u: int, v:int) -> bool:
return self.find(u) == self.find(v)
union_find: UnionFind = UnionFind(n)
for u, v in edges:
union_find.union(u, v)
return union_find.is_connected(source, destination)
def disjoint_set_union_by_rank(
n: int, edges: list[list[int]], source: int, destination: int
) -> bool:
class UnionFind:
BASE_HEIGHT: int = 1
def __init__(self, size: int) -> None:
self.root: list[int] = [ vertex for vertex in range(size) ]
self.rank: list[int] = [
UnionFind.BASE_HEIGHT for _ in range(size)
]
def find(self, vertex: int) -> int:
while vertex != self.root[vertex]:
vertex = self.root[vertex]
return vertex
def union(self, u: int, v: int) -> None:
u_root: int = self.find(u)
v_root: int = self.find(v)
if u_root != v_root:
u_rank: int = self.rank[u_root]
v_rank: int = self.rank[v_root]
if u_rank > v_rank:
self.root[v_root]: int = u_root
elif u_rank < v_rank:
self.root[u_root]: int = v_root
else:
self.root[v_root]: int = u_root
self.rank[u_root] += 1
def is_connected(self, u: int, v: int) -> bool:
return self.find(u) == self.find(v)
union_find: UnionFind = UnionFind(n)
for u, v in edges:
union_find.union(u, v)
return union_find.is_connected(source, destination)
def disjoint_set_path_compression_and_union_by_rank_optimization(
n: int, edges: list[list[int]], source: int, destination: int
) -> bool:
class UnionFind:
BASE_HEIGHT: int = 1
def __init__(self, size: int) -> None:
self.root: list[int] = [ vertex for vertex in range(size) ]
self.rank: list[int] = [
UnionFind.BASE_HEIGHT for _ in range(size)
]
def find(self, vertex: int) -> int:
if vertex == self.root[vertex]:
return vertex
self.root[vertex]: int = self.find(self.root[vertex])
return self.root[vertex]
def union(self, u: int, v: int) -> None:
u_root: int = self.find(u)
v_root: int = self.find(v)
if u_root != v_root:
u_rank: int = self.rank[u_root]
v_rank: int = self.rank[v_root]
if u_rank > v_rank:
self.root[v_root]: int = u_root
elif u_rank < v_rank:
self.root[u_root]: int = v_root
else:
self.root[v_root]: int = u_root
self.rank[u_root] += 1
def is_connected(self, u: int, v: int) -> bool:
return self.find(u) == self.find(v)
union_find: UnionFind = UnionFind(n)
for u, v in edges:
union_find.union(u, v)
return union_find.is_connected(source, destination)
if __name__ == "__main__":
cases: list[dict[str, dict[str, int | list[list[int]]] | bool]] = [
{
"input": {
"n": 1,
"edges": [],
"source": 0,
"destination": 0
},
"output": True
},
{
"input": {
"n": 3,
"edges": [[0, 1], [1, 2], [2, 0]],
"source": 0,
"destination": 2
},
"output": True
},
{
"input": {
"n": 6,
"edges": [[0, 1], [0, 2], [3, 5], [5, 4], [4, 3]],
"source": 0,
"destination": 5
},
"output": False
}
]
for case in cases:
assert case["output"] == solution(**case["input"])
assert case["output"] == another_solution(**case["input"])
assert case["output"] == disjoint_set_quick_union(**case["input"])
assert case["output"] == disjoint_set_union_by_rank(**case["input"])
assert case["output"] == disjoint_set_path_compression_and_union_by_rank_optimization(
**case["input"]
)