"""Offline algorithm verification lab for Chapter 34 (union-find-basic)."""
import json
VERSION = "CPython 3.12.14 offline DSA verifier (union-find-basic)"
class UnionFindInit:
"""Initial id-array representation of disjoint sets."""
def __init__(self, n: int):
assert n >= 0
self.count = n
self.id = [i for i in range(n)]
def distinct_groups(self) -> int:
return len(set(self.id))
def groups_map(self) -> dict:
groups = {}
for elem, gid in enumerate(self.id):
groups.setdefault(gid, []).append(elem)
return groups
def case_union_find_init_disjoint_sets():
uf = UnionFindInit(10)
expected = {
"count": 10,
"initial_id": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"distinct_groups": 10,
"all_self_identified": True,
}
actual = {
"count": uf.count,
"initial_id": uf.id,
"distinct_groups": uf.distinct_groups(),
"all_self_identified": all(uf.id[i] == i for i in range(10)),
}
return {
"name": "union_find_init_disjoint_sets",
"summary": "验证并查集初始化后每个元素 id[i] == i,共形成 n 个互不相交的单元素集合",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_partition_representation_examples():
# Example A: 0-4 belong to group 0, 5-9 belong to group 1
uf_a = UnionFindInit(10)
uf_a.id = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
# Example B: even indices (0,2,4,6,8) belong to group 0; odd indices (1,3,5,7,9) belong to group 1
uf_b = UnionFindInit(10)
uf_b.id = [0 if i % 2 == 0 else 1 for i in range(10)]
expected = {
"example_a_groups": {0: [0, 1, 2, 3, 4], 1: [5, 6, 7, 8, 9]},
"example_b_groups": {0: [0, 2, 4, 6, 8], 1: [1, 3, 5, 7, 9]},
"example_a_count": 2,
"example_b_count": 2,
}
actual = {
"example_a_groups": uf_a.groups_map(),
"example_b_groups": uf_b.groups_map(),
"example_a_count": uf_a.distinct_groups(),
"example_b_count": uf_b.distinct_groups(),
}
return {
"name": "partition_representation_examples",
"summary": "验证两组经典数组划分:连续段 {0..4}/{5..9} 与奇偶分组 {0,2,4,6,8}/{1,3,5,7,9}(澄清源文偶发笔误)",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_disjoint_set_connectivity_check():
uf = UnionFindInit(10)
uf.id = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
connected = lambda p, q: uf.id[p] == uf.id[q]
reflexive = all(connected(i, i) for i in range(10))
symmetric = all(connected(i, j) == connected(j, i) for i in range(10) for j in range(10))
transitive = all(
(not (connected(i, j) and connected(j, k))) or connected(i, k)
for i in range(10)
for j in range(10)
for k in range(10)
)
expected = {
"reflexive": True,
"symmetric": True,
"transitive": True,
"0_and_8_connected": True,
"0_and_1_connected": False,
"1_and_9_connected": True,
}
actual = {
"reflexive": reflexive,
"symmetric": symmetric,
"transitive": transitive,
"0_and_8_connected": connected(0, 8),
"0_and_1_connected": connected(0, 1),
"1_and_9_connected": connected(1, 9),
}
return {
"name": "disjoint_set_connectivity_check",
"summary": "验证基于集合编号的连通判定满足等价关系的自反性、对称性与传递性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_union_find_init_disjoint_sets(),
case_partition_representation_examples(),
case_disjoint_set_connectivity_check(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线数据结构与算法验证脚本(标准库实现,无网络依赖)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线数据结构与算法验证脚本(标准库实现,无网络依赖)。
入口文件:projects/data-structures-union-find-basic-lab/explore-union-find-basic.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-union-find-basic.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线数据结构与算法验证脚本(标准库实现,无网络依赖)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联44章) · 含当前章节或实例 · mbskycom202612a2641.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2678.zip