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 625b934

Browse files
committed
added union find
1 parent 0c09215 commit 625b934

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- [链表](./data_structure/linked_list.md)
2828
- [栈和队列](./data_structure/stack_queue.md)
2929
- [优先级队列(堆)](./data_structure/heap.md)
30+
- [并查集](./data_structure/union_find.md)
3031
- [二进制](./data_structure/binary_op.md)
3132

3233
### 基础算法篇 🐮

‎data_structure/union_find.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# 并查集
2+
3+
用于处理不相交集合 (disjoint sets) 合并及查找的问题。
4+
5+
### [redundant-connection](https://leetcode-cn.com/problems/redundant-connection/)
6+
7+
```Python
8+
class Solution:
9+
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
10+
11+
parent = list(range(len(edges) + 1))
12+
13+
def find(x):
14+
if parent[parent[x]] != parent[x]:
15+
parent[x] = find(parent[x])
16+
return parent[x]
17+
18+
def union(x, y):
19+
px, py = find(x), find(y)
20+
if px == py:
21+
return False
22+
parent[px] = py
23+
return True
24+
25+
for u, v in edges:
26+
if not union(u, v):
27+
return [u, v]
28+
```
29+
30+
### [accounts-merge](https://leetcode-cn.com/problems/accounts-merge/)
31+
32+
```Python
33+
class Solution:
34+
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
35+
36+
parent = []
37+
38+
def find(x):
39+
if parent[parent[x]] != parent[x]:
40+
parent[x] = find(parent[x])
41+
return parent[x]
42+
43+
def union(x, y):
44+
parent[find(x)] = find(y)
45+
return
46+
47+
email2name = {}
48+
email2idx = {}
49+
i = 0
50+
for acc in accounts:
51+
for email in acc[1:]:
52+
email2name[email] = acc[0]
53+
if email not in email2idx:
54+
parent.append(i)
55+
email2idx[email] = i
56+
i += 1
57+
union(email2idx[acc[1]], email2idx[email])
58+
59+
result = collections.defaultdict(list)
60+
for email in email2name:
61+
result[find(email2idx[email])].append(email)
62+
63+
return [[email2name[s[0]]] + sorted(s) for s in result.values()]
64+
```
65+
66+
67+
68+
### Kruskal's algorithm
69+
70+
### [minimum-risk-path](https://www.lintcode.com/problem/minimum-risk-path/description)
71+
72+
> 地图上有 m 条无向边,每条边 (x, y, w) 表示位置 m 到位置 y 的权值为 w。从位置 0 到 位置 n 可能有多条路径。我们定义一条路径的危险值为这条路径中所有的边的最大权值。请问从位置 0 到 位置 n 所有路径中最小的危险值为多少?
73+
74+
**图森面试真题**。最小危险值为最小生成树中 0 到 n 路径上的最大边权。
75+
76+
```Python
77+
# Kruskal's algorithm
78+
class Solution:
79+
def getMinRiskValue(self, N, M, X, Y, W):
80+
81+
# Kruskal's algorithm with union-find to construct MST
82+
parent = list(range(N + 1))
83+
84+
def find(x):
85+
if parent[parent[x]] != parent[x]:
86+
parent[x] = find(parent[x])
87+
return parent[x]
88+
89+
def union(x, y):
90+
px, py = find(x), find(y)
91+
if px != py:
92+
parent[px] = py
93+
return True
94+
else:
95+
return False
96+
97+
edges = sorted(zip(W, X, Y))
98+
99+
MST_edges = []
100+
for edge in edges:
101+
if union(edge[1], edge[2]):
102+
MST_edges.append(edge)
103+
if find(0) == find(N):
104+
break
105+
106+
MST = collections.defaultdict(list)
107+
target = find(0)
108+
for w, u, v in MST_edges:
109+
if find(u) == target and find(v) == target:
110+
MST[u].append((v, w))
111+
MST[v].append((u, w))
112+
113+
# dfs to search route from 0 to n
114+
dfs = [(0, None, float('-inf'))]
115+
while dfs:
116+
v, p, max_w = dfs.pop()
117+
for n, w in MST[v]:
118+
cur_max_w = max(max_w, w)
119+
if n == N:
120+
return cur_max_w
121+
if n != p:
122+
dfs.append((n, v, cur_max_w))
123+
```

0 commit comments

Comments
(0)

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