本文へスキップ
製品デスクトップツールTASTECAD図面の確認TASTEFILESファイル管理TASTEFTPファイル転送TASTESSHリモートワークスペースTASTEZIP圧縮と展開TASTEWINMファイル管理開発ツールTASTECODERust 統合開発環境TASTESTUDIO統合開発・テスト環境TASTEETLデータ統合(ETL)サーバーTASTEREMOTEリモートデスクトップサーバーTASTEGEO空間情報サーバーTASTEMAILメールワークスペース投資TASTESTOCK株式自動売買デスクトップアプリはじめるダウンロードドキュメント購入すべての製品
マーケットプレイス
ニュース
技術ニュース今日の開発ニュース開発ドキュメントプログラミング言語RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWebフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngularサーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Expressアプリ・デスクトップ フレームワークTauriFlutterReact NativeElectronアルゴリズムアルゴリズムの基礎主要アルゴリズム発展アルゴリズム実務のアルゴリズム
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケースコミュニティガイド
協業技術移転ウェブサイト構築デスクトップアプリ構築リリース・決済パイプライン販売パートナーお問い合わせ
サービスリモートデスクトップインストールパッケージのカスタマイズ
TASTEDEV
製品

デスクトップツール

TASTECAD複雑な図面を、軽やかにひらくTASTEFILES必要なファイルだけを、最速でTASTEFTPファイルが行き交う、いちばん端正な道TASTESSHサーバーとあなたを、より確かにつなぐTASTEZIPより速くまとめ、より安全にひらくTASTEWINM2つの画面で整理する、キーボード中心のファイルマネージャー

開発ツール

TASTECODERust アプリの編集・ビルド・デバッグ・テストまでひとつのウィンドウでTASTESTUDIO編集からリモートテスト、承認した AI 修正までTASTEETL値を見ながら、データを運ぶ

サーバー

TASTEREMOTE自社サーバーで運用するリモートデスクトップTASTEGEOGeoServer 互換の空間情報サーバーを Rust で作り直しますTASTEMAILメールの主導権を、もう一度自分の手に

投資

TASTESTOCKいつ・何を。自分で決めたルールどおりに動く株式自動売買

はじめる

ダウンロード全製品の最新版ドキュメント各製品の使い方購入サブスクリプション・永久ライセンスすべての製品全製品を一覧で
すべての製品
マーケットプレイスニュース
技術

ニュース

今日の開発ニュース言語・フレームワーク・GitHub・AI の公式発表開発ドキュメント言語・フレームワーク・アルゴリズムのガイド

プログラミング言語

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

フレームワーク

Webフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngular
サーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
アプリ・デスクトップ フレームワークTauriFlutterReact NativeElectron

アルゴリズム

アルゴリズムの基礎計算量解析 · ソート · 探索 · 基本データ構造 · 木構造とヒープ主要アルゴリズムグラフ探索 · 最短経路と最小全域木 · 動的計画法 · 貪欲法 · 分割統治法 · バックトラッキング発展アルゴリズム文字列アルゴリズム · 数学と整数論 · ビット演算 · セグメント木 · Union-Find実務のアルゴリズムコーディング面接対策 · LRUキャッシュ · レート制限 · コンシステントハッシュ法
開発ドキュメントをすべて見る
コミュニティ
プロジェクトビルダーと作品を探す開発ログ決定と試行錯誤プロンプト再利用できる指示AIツールの問題Claude・Codex・Gemini の問題と回避策ショーケース完成した成果コミュニティガイドオープンにつくる方法
協業
技術移転Rust・egui でネイティブアプリを作るウェブサイト構築10言語、アカウント、ドキュメントとダウンロードデスクトップアプリ構築Rust・egui、WebView なしリリース・決済パイプライン署名・MSI・deb・rpm・AppImage・ハッシュ販売パートナー紹介して決済額の30%お問い合わせ構築・提携・投資のご相談
サービス
リモートデスクトップブラウザから自分のパソコンへインストールパッケージのカスタマイズ必要な機能だけ、設定は事前に
購入
JA
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
ログイン
ログイン
TASTEDEV

小さなツールから、より良い開発へ。

support@tastedev.netTASTEDEVを支援
製品製品ラインナップ購入ドキュメントダウンロードマーケットプレイスサポート
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケース
ポリシー会社概要プライバシーポリシー利用規約サブスクリプション規約返金ポリシーコミュニティガイドライン
アカウントログイン新規登録
訪問者
今日
昨日
累計閲覧
商号
GXSOFT
代表者
CHOI YOUNG GU
事業者登録番号
853-44-00368
所在地
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
電話
+82-10-4404-1523
メール
whitecalvin@tastedev.net
個人情報保護責任者
CHOI YOUNG GU

© 2026 TASTEDEV製品情報は検証済みの開発ソースに基づいています。リリース・改善中

Union-Find ドキュメント
Union-Find
Concepts: Disjoint Sets and Union-FindHow It Works: Tracing find and unionImplementation: Building a DSU ClassComplexity: Inverse Ackermann in Plain WordsPractice ProblemsReal-World Use and Pitfalls
ディスカッション

Union-Find ガイド · 2/6

How It Works: Tracing find and union

この章は現在、英語でのみ提供しています。

This chapter follows a small example with eight elements (0 to 7) and shows how the parent array changes. First we see how trees degrade without optimizations, then how union by size and path compression fix the problem.

Initial state

Every element starts in its own set, so each one is its own parent. All sizes are 1.

Element01234567
parent01234567
size11111111

The problem with unoptimized merging

Suppose union always hangs the second root under the first. Calling union(1, 0), union(2, 1) and union(3, 2) puts 0 under 1, 1 under 2 and 2 under 3, producing a chain 0 → 1 → 2 → 3. Now find(0) has to climb three links to reach the root 3. With n elements, a single find can cost O(n) in the worst case.

parent = list(range(8))

def find(x):
    steps = 0
    while parent[x] != x:
        x = parent[x]
        steps += 1
    return x, steps

def naive_union(a, b):
    ra, _ = find(a)
    rb, _ = find(b)
    if ra != rb:
        parent[rb] = ra

for i in range(1, 4):
    naive_union(i, i - 1)   # 0 → 1 → 2 → 3
print(find(0))  # (3, 3): three steps to root 3

Union by size

The fix is to hang the smaller tree under the larger one. Then whenever an element's depth grows by one, the set it belongs to at least doubles in size. A set can never exceed n elements, so no depth can exceed log2 n. Union by rank, which compares an upper bound on height instead of the size, gives the same guarantee.

Let us run the following sequence on the same eight elements, merging by size. Each union first calls find on both arguments to get their roots.

StepOperationRoots comparedResult
1union(0, 1)size 1 vs 1, ties keep the first rootparent[1] = 0, size[0] = 2
2union(2, 3)size 1 vs 1parent[3] = 2, size[2] = 2
3union(1, 3)root 0 (size 2) vs root 2 (size 2)parent[2] = 0, size[0] = 4
この章について質問がありますか?
前の章Concepts: Disjoint Sets and Union-Find次の章Implementation: Building a DSU Class
4union(4, 5)size 1 vs 1parent[5] = 4, size[4] = 2
5union(6, 4)root 6 (size 1) vs root 4 (size 2)parent[6] = 4, size[4] = 3
6union(7, 1)root 7 (size 1) vs root 0 (size 4)parent[7] = 0, size[0] = 5

After step 6 there are two sets, {0, 1, 2, 3, 7} and {4, 5, 6}. The path from element 3 is 3 → 2 → 0, length 2, well within the maximum depth of 3 that eight elements allow.

Path compression

Calling find(3) walks 3 → 2 → 0 and discovers the root 0. Path compression then points every node on that path directly at the root. The next find(3) needs only one hop.

def find(x):
    root = x
    while parent[root] != root:      # pass 1: locate the root
        root = parent[root]
    while parent[x] != root:         # pass 2: point visited nodes at the root
        parent[x], x = root, parent[x]
    return root

Compression happens automatically inside find, so callers never have to think about it. The more often the same elements are queried, the shorter their paths become and the bigger the payoff.

A popular one-pass variant is path halving: while walking up, link each node to its grandparent. The code is shorter and the performance guarantee is the same.

def find_halving(x):
    while parent[x] != x:
        parent[x] = parent[parent[x]]  # skip to the grandparent
        x = parent[x]
    return x

Before and after compression

Element01234567
parent before find(3)00024440
parent after find(3)00004440

parent[3] changed from 2 to 0. Compression only flattens the shape of a tree; it never changes which set an element belongs to. Sizes only matter at roots, so they need no update during compression.

Summary

Without optimizations, union-find trees can degrade into long chains. Union by size or rank, which hangs the smaller tree under the larger, bounds tree height by log n, and path compression or path halving flattens trees on every find. The two techniques are independent and work best together.

コメント 0件

人気のコメント順新しい順

ログイン · ログインするとコメントできます。