Two Mergeable Data Structures

Similar documents
ENGG1410-F Tutorial 6

<4D F736F F D20C4A3B0E632A3A8D3EFD1D4CEC4D7D6BCECB2E9B8C4A3A92E646F63>

A B C D E A B C F A C. D F. A. B. C. D. E. F.

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

BC04 Module_antenna__ doc

Fuzzy Highlight.ppt

中国人民大学商学院本科学年论文

彩圖 6 彩圖 7 彩圖 8 3

1對外華語文詞彙教學的策略研究_第三次印).doc

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

Microsoft PowerPoint - STU_EC_Ch08.ppt

20

中国科学技术大学学位论文模板示例文档


論 文 摘 要 本 文 乃 係 兩 岸 稅 務 爭 訟 制 度 之 研 究, 蓋 稅 務 爭 訟 在 行 訴 訟 中 一 直 占 有 相 當 高 的 比 例, 惟 其 勝 訴 率 一 直 偏 低, 民 87 年 10 月 28 日 行 訴 訟 法 經 幅 修 正 後, 審 級 部 分 由 一 級 一

Logitech Wireless Combo MK45 English

Microsoft Word - 先玉335 copy.doc

Microsoft PowerPoint - Discovering Organizational Structure.pptx

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

Analysis of Cultural Elements of Meinong s Paper Umbrella Painting Abstract Meinong paper umbrellas are a traditional industrial art for the Hakka peo

東吳大學

Microsoft PowerPoint - ch6 [相容模式]

<4D F736F F D205F FB942A5CEA668B443C5E9BB73A740B5D8A4E5B8C9A552B1D0A7F75FA6BFB1A4ACFC2E646F63>

symmetrical cutting patterns with various materials for visual designing; ii. This part combined costumes, bags and oilpaper umbrellas with the tradit

穨control.PDF

% GIS / / Fig. 1 Characteristics of flood disaster variation in suburbs of Shang

WWW PHP

論文集29-1_前6P.indd

m m m ~ mm

untitled

LEETCODE leetcode.com 一 个 在 线 编 程 网 站, 收 集 了 IT 公 司 的 面 试 题, 包 括 算 法, 数 据 库 和 shell 算 法 题 支 持 多 种 语 言, 包 括 C, C++, Java, Python 等 2015 年 3 月 份 加 入 了 R

2015年4月11日雅思阅读预测机经(新东方版)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

國立中山大學學位論文典藏

Microsoft PowerPoint - Model Checking a Lazy Concurrent List-Based Set Algorithm.ppt [Compatibility Mode]

2005硕士论文模版

Open topic Bellman-Ford算法与负环

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

A VALIDATION STUDY OF THE ACHIEVEMENT TEST OF TEACHING CHINESE AS THE SECOND LANGUAGE by Chen Wei A Thesis Submitted to the Graduate School and Colleg

第一章 前言

Introduction to Hamilton-Jacobi Equations and Periodic Homogenization


AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING


Microsoft PowerPoint - Lecture7II.ppt

Microsoft Word - ED-774.docx

K301Q-D VRT中英文说明书141009

硕 士 学 位 论 文 论 文 题 目 : 北 岛 诗 歌 创 作 的 双 重 困 境 专 业 名 称 : 中 国 现 当 代 文 学 研 究 方 向 : 中 国 新 诗 研 究 论 文 作 者 : 奚 荣 荣 指 导 老 师 : 姜 玉 琴 2014 年 12 月

1.ai

地質調査研究報告/Bulletin of the Geological Survey of Japan

931.indd

論法院作成出版品禁止發行之衡量標準

Microsoft PowerPoint _代工實例-1

論文格式

Microsoft Word - 专论综述1.doc


[ 13 年 12 月 06 日, 下 午 6 点 24 分 ] Intel Hosts 新 加 入 的 同 学 们, 快 去 听 听 在 线 宣 讲 会 哦, 同 时 完 成 页 面 下 方 有 奖 调 查, 就 有 资 格 参 与 大 奖 抽 取 啦! [ 13 年 12 月 06 日, 下 午

Microsoft Word 電子報x

Microsoft Word - 澎湖田調報告_璉謙組.doc


Stochastic Processes (XI) Hanjun Zhang School of Mathematics and Computational Science, Xiangtan University 508 YiFu Lou talk 06/


國立中山大學學位論文典藏.PDF

參 加 第 二 次 pesta 的 我, 在 是 次 交 流 營 上 除 了, 與 兩 年 沒 有 見 面 的 朋 友 再 次 相 聚, 加 深 友 誼 外, 更 獲 得 與 上 屆 不 同 的 體 驗 和 經 歴 比 較 起 香 港 和 馬 來 西 亞 的 活 動 模 式, 確 是 有 不 同 特


Microsoft Word - ICF的編碼指引-new sjl.doc


致 谢 开 始 这 篇 致 谢 的 时 候, 以 为 这 是 最 轻 松 最 愉 快 的 部 分, 而 此 时 心 头 却 充 满 了 沉 甸 甸 的 回 忆 和 感 恩, 一 时 间 竟 无 从 下 笔 虽 然 这 远 不 是 一 篇 完 美 的 论 文, 但 完 成 这 篇 论 文 要 感 谢


什么是函数式编程?

3戴文鋒-人文.indd

第1章 簡介

The Idea Changing of Father & Son between Two Dynasty Chou Wan-Yu Kao* Abstract Chinese is proud of its country with courtesy and justice. The courtes

ebook39-5

untitled

浙生竟委(2016)1号文件

Some experiences in working with Madagascar: installa7on & development Tengfei Wang, Peng Zou Tongji university

龍華科技大學數位典藏論文

演算法導入、ソート、データ構造、ハッシュ

闲 旅 游 现 已 成 为 城 市 居 民 日 常 生 活 的 重 要 部 分 袁 它 的 出 现 标 志 着 现 代 社 会 文 明 的 进 步 遥 据 国 外 学 者 预 测 袁 2015 年 左 右 袁 发 达 国 家 将 陆 续 进 入 野 休 闲 时 代 冶 袁 发 展 中 国 家 也 将

2008年1月11日に岩手県釜石沖で発生した地震(M4.7)について

A dissertation for Master s degree Metro Indoor Coverage Systems Analysis And Design Author s Name: Sheng Hailiang speciality: Supervisor:Prof.Li Hui,

1.2 资 金 的 管 理 1.1 权 利 义 务 来 源 MOU 1.3 数 据 的 使 用 和 保 护 2 国 际 空 间 站 资 源 分 配 方 案 54

2

Transcription:

Two Mergeable Data Structures Disjoint-Set 并查集 & Leftist-Tree 左偏树 1

Disjoint-Set(Union-Find Set) 并查集 N distinct elements into a collection of disjoint sets. Op1: Find which set a given element belong in I.e. Judge if two elements are in the same set Op2: Unite two sets N 个不同的元素组成不相交集合 操作 1: 寻找给定元素所属集合 ( 判断两个元素是否在同一集合 ) 操作 2: 合并两个集合 2

An Example of Disjoint-Set Operation Disjoint sets Initialization {a} {b} {c} {d} {e} {f} Merge(a,b) {a,b} {c} {d} {e} {f} Query(a,c) Query(a,b) False True Merge(b,e) {a,b,e} {c} {d} {f} Merge(c,f) {a,b,e} {c,f} {d} Query(a,e) Query(c,b) True False Merge(b,f) {a,b,c,e,f} {d} Query(a,e) Query(d,e) True False 3

Naive Algorithm Assign each set a label. 给集合编号 Op Element {a} {b} {c} {d} {e} {f} 1 2 3 4 5 6 Merge(a,b) 1 1 3 4 5 6 Merge(b,e) 1 1 3 4 1 6 Merge(c,f) 1 1 3 4 1 3 Merge(b,f) 1 1 1 4 1 1 4

Naive Algorithm Assign each set a label. 给集合编号 Op Element {a} {b} {c} {d} {e} {f} 1 2 3 4 5 6 Merge(a,b) 1 1 3 4 5 6 Merge(b,e) 1 1 3 4 1 6 Merge(c,f) 1 1 3 4 1 3 Merge(b,f) 1 1 1 4 1 1 Query(a,e) 5

Naive Algorithm Assign each set a label. 给集合编号 Op Element {a} {b} {c} {d} {e} {f} 1 2 3 4 5 6 Merge(a,b) 1 1 3 4 5 6 Merge(b,e) 1 1 3 4 1 6 Merge(c,f) 1 1 3 4 1 3 Merge(b,f) 1 1 1 4 1 1 Query O(1); Merge O(N) 6

First Look Tree Structure Init: a b c d e f Merge(a,b) a b c d e f Merge(b,e) a c d f b e 7

First Look Tree Structure a c d f b e Merge(c,f) a c d b e f Merge(b,f) b a e c d f 8

First Look Tree Structure Merge(b,f) Attach f s tree as the direct subtree of b s 将 f 所在树挂为 b 所在树的直接子树 Par[i] indicates i s father node; Par[i]=i for roots a d b e c f 9

First Look Tree Structure Query(b,f) Simply compare the roots of b s tree and f s tree 简单比较 b 和 f 所在树的根节点是否相同 a d b e c f 10

First Look Tree Structure Weakness Merge(c,d), Merge(b,c), Merge(a,b) Query(d,*) a b c O(N)! d 11

First Look Tree Structure Weakness Merge(c,d), Merge(b,c), Merge(a,b) Query(d,*) a Merge O(1); Query O(N) b c O(N)! d 12

First Look Tree Structure Weakness Merge(c,d), Merge(b,c), Merge(a,b) Query(d,*) a Merge O(N); Query O(N) b c O(N)! d 13

Improve One Union by Rank For each node, maintain a Rank that is an upper bound on the height of that subtree 每个点维护一个 Rank 表示子树最大可能高度 Root with smaller rank is made to point to root with larger rank in Merge operation. 较小 Rank 的树连到较大 Rank 树的根部 14

Improve One Union by Rank New One LINK(x, y) If Rank[x]>Rank[y] Else par[y] x Par[x] y If Rank[x]=Rank[y] Rank[y]++ Old One LINK(x, y) par[y] x 15

Improve One Union by Rank 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 2 2 1 2 3 1 3 2 3 3 4 16

Improve One Union by Rank GET_PAR(a) If Par[a]=a Return a Else Return GET_PAR(par[a]) Query(a,b) Return GET_PAR(a)==GET_PAR(b) Merge(a,b) LINK( GET_PAR(a), GET_PAR(b) ) 17

Improve One Union by Rank GET_PAR(a) O(log 2 N) If Par[a]=a Return a Else Return GET_PAR(par[a]) Query(a,b) O(log 2 N) Return GET_PAR(a)==GET_PAR(b) Merge(a,b) O(log 2 N) LINK( GET_PAR(a), GET_PAR(b) ) 18

Improve Two Path Compression In GET_PAR method, make each node on the find path directly point to the root 将 GET_PAR 中查找路径上的节点直接指向根 a GET_PAR(d) a b b c d c d 19

Improve Two Path Compression New Code GET_PAR(a) If Par[a]!=a Par[a] =GET_PAR(par[a]) Return par[a] Old Code GET_PAR(a) If Par[a]=a Else Return a Return GET_PAR(par[a]) 20

Complexity Amortized cost of GET_PAR operation O(α(n)) GET_PAR 函数的平摊复杂度为 O(α(n)) α(n) =0, if 0<=n<=2 =1, if n=3 =2, if 4<=n<=7 =3, if 8<=n<=2047 =4, if 2048<=n<=A 4 (1) 2 22...2 2048 21

Complexity Amortized cost of GET_PAR operation O(α(n)) GET_PAR 函数的平摊复杂度为 O(α(n)) Amortized analysis is a tool for analyzing algorithms that perform a sequence of similar operations. 平摊分析是一种分析一串类似操作的总体效率的思想 Op3 Op4 Op7 Op8 Op1 Op2 Op5 Op6 Op9 22

Practical Use 23

Practical Use int get_par(int u) { if (par[a]!=a) par[a] = get_par(par[a]); return par[a]; } int link(int x, int y) { } int par[]; int rank[]; if (rank[x]>rank[y]) par[y]=x; else par[x]=y; if (rank[x]==rank[y]) rank[y]++; int query(int a,int b) { return get_par(a)==get_par(b); } void merge(int a,int b) { link(get_par(a), get_par(b) } 24

Practical Use int get_par(int u) { if (par[a]!=a) par[a] = get_par(par[a]); return par[a]; } int link(int x, int y) { par[y]=x; } int par[]; int query(int a,int b) { return get_par(a)==get_par(b); } void merge(int a,int b) { link(get_par(a), get_par(b) } 25

Practical Use int get_par(int u) { if (par[a]!=a) par[a] = get_par(par[a]); return par[a]; } int par[]; int query(int a,int b) { return get_par(a)==get_par(b); } void merge(int a,int b) { par[get_par(a)] = get_par(b); } 26

Practical Use int get_par(int u) { return par[a]==a? a : par[a]=get_par(par[a]); } int par[]; int query(int a,int b) { return get_par(a)==get_par(b); } void merge(int a,int b) { par[get_par(a)] = get_par(b); } 27

Exercise 银河英雄传说 题目大意 : M i j: 让第 i 号战舰所在的整个战舰队列, 作为一个整体 ( 头在前尾在后 ) 接至第 j 号战舰所在的战舰队列的尾部 C i j: 询问电脑, 杨威利的第 i 号战舰与第 j 号战舰当前是否在同一列中, 如果在同一列中, 那么它们之间布置有多少战舰 National Olympiad in Informatics 2002 天津 28

Exercise 银河英雄传说 可以把每列划分成一个集合, 那么, 舰队的合并 查询就是对集合的合并和查询 这样就是一个很典型的并查集算法的模型 与普通并查集的区别是, 此处需要记录每个点相对当前父节点的相对位置, 用来回答查询操作中, 两艘之间布置有多少战舰的问题 29

Improve Two Path Compression In GET_PAR method, make each node on the find path directly point to the root 将 GET_PAR 中查找路径上的节点直接指向根 a 3 b 1 GET_PAR(d) a 3 b 4 c 8 d c 4 d 30

Leftist-Tree 左偏树 是一个二叉堆 31

Lestist Tree 左偏树 Classical Heap Leftist Tree Binomial Heap Fibonacci Heap Initialization O(n) O(n) O(n) O(n) Insert O(logn) O(logn) O(logn) O(1) Get Top O(1) O(1) O(logn) O(1) Remove Top O(logn) O(logn) O(logn) O(logn) Remove Any O(logn) O(logn) O(logn) O(logn) Merge O(n) O(logn) O(logn) O(1) Coding Difficulty Low Medium High Very High 32

Definition Every node has a count dist on the distance to the nearest external node(on its own subtree). In addition to the heap property, leftist trees are kept so the right descendant of each node has shorter distance to a leaf. 每个结点记录自身子树上到达最近外结点距离 dist 除了堆所具有性质以外, 左偏树保证右孩子的 dist 小于左孩子 33

Definition 34

Merge Operation: Merge(A, B) A B Simplest case: either tree is empty (A=NULL or B=NULL). Just return the other tree. 如果其中一棵树为空, 直接返回另一棵 If A==NULL Return B If B==NULL Return A 35

Merge Operation: Merge(A, B) A B Suppose A s root has larger key. Simply merge B and the right subtree of A. 设 A 根节点键值更大, 将 A 右子树和 B 合并 If Key[A] < Key[B] Swap(A,B) Right[A] Merge(Right[A], B) 36

Merge Operation: Merge(A, B) A Swap Right(A) and Left(A) when necessary 当需要时交换 Right(A) 及 Left(A) If dist[left[a]] < dist[right[a]] Swap(left[A],right[A]) 37

Merge Operation: Merge(A, B) A Update dist(a) If Right[A]==NULL dist[a] 0 Else Dist[A] dist[right[a]] + 1 38

Other Operations Insert(A, x) Merge(A, tree of x) RemoveTop(A) Merge(Left[A], Right[A]) 39

Lestist Tree 左偏树 Classical Heap Leftist Tree Binomial Heap Fibonacci Heap Initialization O(n) O(n) O(n) O(n) Insert O(logn) O(logn) O(logn) O(1) Get Top O(1) O(1) O(logn) O(1) Remove Top O(logn) O(logn) O(logn) O(logn) Remove Any O(logn) O(logn) O(logn) O(logn) Merge O(n) O(logn) O(logn) O(1) Coding Difficulty Low Medium High Very High 40

Mixture of Disj-Set & Leftist Tree Merge(Node a, Node b) Merge two heaps containing a/b respectively 将包含 a/b 的两个堆合并 FindMax (Node a) Acquire the maximum element in the heap of a 求 a 所在堆中的最大元素 1 6 4 5 3 2 41

Applications Medical Science: 疾病监控 Biology: 细菌扩散 Math: 等价类 42

References http://www.dgp.toronto.edu/people/jamesst ewart/378notes/10leftist/ Introduction to Algorithms (2 nd Edition) Thanks! zhuzeyuan@hotmail.com 朱泽园基科 62 43