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 db763bd

Browse files
committed
added minimum spanning tree
1 parent a13d3c6 commit db763bd

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

‎basic_algorithm/graph/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
### 图相关算法
1+
# 图相关算法
22

33
Ongoing...
44

5-
[拓扑排序](./topological_sorting.md)
5+
[拓扑排序](./topological_sorting.md)
6+
7+
[最小生成树](./mst.md)

‎basic_algorithm/graph/mst.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# 最小生成树
2+
3+
### [minimum-risk-path](https://www.lintcode.com/problem/minimum-risk-path/description)
4+
5+
> 地图上有 m 条无向边,每条边 (x, y, w) 表示位置 m 到位置 y 的权值为 w。从位置 0 到 位置 n 可能有多条路径。我们定义一条路径的危险值为这条路径中所有的边的最大权值。请问从位置 0 到 位置 n 所有路径中最小的危险值为多少?
6+
7+
**图森面试真题**。最小危险值为最小生成树中 0 到 n 路径上的最大边权。
8+
9+
```Python
10+
# Kruskal's algorithm
11+
class Solution:
12+
def getMinRiskValue(self, n, m, x, y, w):
13+
14+
# Kruskal's algorithm with union-find to construct MST
15+
parent = list(range(n + 1))
16+
17+
def find(x):
18+
if parent[parent[x]] != parent[x]:
19+
parent[x] = find(parent[x])
20+
return parent[x]
21+
22+
def union(x, y):
23+
px, py = find(x), find(y)
24+
if px != py:
25+
parent[px] = py
26+
return True
27+
else:
28+
return False
29+
30+
edges = sorted(zip(x, y, w), key=lambda x: x[2])
31+
32+
mst_edges = []
33+
for edge in edges:
34+
if union(edge[0], edge[1]):
35+
mst_edges.append(edge)
36+
if find(0) == find(n):
37+
break
38+
39+
mst = collections.defaultdict(list)
40+
target = find(0)
41+
for u, v, r in mst_edges:
42+
if find(u) == target and find(v) == target:
43+
mst[u].append((v, r))
44+
mst[v].append((u, r))
45+
46+
# dfs to search route from 0 to n
47+
dfs = [(0, None, float('-inf'))]
48+
while dfs:
49+
v, p, max_risk = dfs.pop()
50+
for a, r in mst[v]:
51+
cur_max = max(max_risk, r)
52+
if a == n:
53+
return cur_max
54+
if a != p:
55+
dfs.append((a, v, cur_max))
56+
```
57+
58+
```Python
59+
# Prim's algorithm
60+
class Solution:
61+
def getMinRiskValue(self, n, m, x, y, w):
62+
63+
# construct graph
64+
adj = collections.defaultdict(list)
65+
for i in range(m):
66+
adj[x[i]].append((y[i], w[i]))
67+
adj[y[i]].append((x[i], w[i]))
68+
69+
# Prim's algorithm with min heap
70+
mst = collections.defaultdict(list)
71+
min_heap = [(r, 0, v) for v, r in adj[0]]
72+
heapq.heapify(min_heap)
73+
74+
while n not in mst:
75+
r, u, v = heapq.heappop(min_heap)
76+
if v not in mst:
77+
mst[u].append((v, r))
78+
mst[v].append((u, r))
79+
for nei, w in adj[v]:
80+
if nei not in mst:
81+
heapq.heappush(min_heap, (w, v, nei))
82+
83+
# dfs to search route from 0 to n
84+
dfs = [(0, None, float('-inf'))]
85+
while dfs:
86+
v, p, max_risk = dfs.pop()
87+
for a, r in mst[v]:
88+
cur_max = max(max_risk, r)
89+
if a == n:
90+
return cur_max
91+
if a != p:
92+
dfs.append((a, v, cur_max))
93+
```
94+

0 commit comments

Comments
(0)

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