Analysis of Algorithms

Similar documents
Analysis of Algorithms

AtCoder Regular Contest 073 Editorial Kohei Morita(yosupo) A: Shiritori if python3 a, b, c = input().split() if a[len(a)-1] == b[0] and b[len(

Page 1 of 6 B (The World of Mathematics) November 20, 2006 Final Exam 2006 Division: ID#: Name: 1. p, q, r (Let p, q, r are propositions. ) (10pts) (a

h23w1.dvi

PowerPoint Presentation

25 II :30 16:00 (1),. Do not open this problem booklet until the start of the examination is announced. (2) 3.. Answer the following 3 proble

RR-US470 (RQCA1588).indd

A5 PDF.pwd


fx-9860G Manager PLUS_J

soturon.dvi

p _08森.qxd

24 Depth scaling of binocular stereopsis by observer s own movements

CPP46 UFO Image Analysis File on yucatan091206a By Tree man (on) BLACK MOON (Kinohito KULOTSUKI) CPP46 UFO 画像解析ファイル yucatan091206a / 黒月樹人 Fig.02 Targe

MIDI_IO.book

Microsoft PowerPoint - kougi10.ppt



16_.....E...._.I.v2006

A5 PDF.pwd

untitled

2

2 3

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

Microsoft PowerPoint - algo ppt [互換モード]

1) 1) Props of preparation. Wire Scale Spike Cutter Hammer Tape Marker (, ) Rope(For Sezing) 2) 2) Marking A 22 A point Position of twenty-two times t

Diskette Drive Installation

4.1 % 7.5 %

Diskette Drive Installation

L1 What Can You Blood Type Tell Us? Part 1 Can you guess/ my blood type? Well,/ you re very serious person/ so/ I think/ your blood type is A. Wow!/ G

C. S2 X D. E.. (1) X S1 10 S2 X+S1 3 X+S S1S2 X+S1+S2 X S1 X+S S X+S2 X A. S1 2 a. b. c. d. e. 2

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

NSR-500 Installation Guide

n 2 n (Dynamic Programming : DP) (Genetic Algorithm : GA) 2 i


untitled


国土技術政策総合研究所 研究資料

1 ( 8:12) Eccles. 1:8 2 2

How to read the marks and remarks used in this parts book. Section 1 : Explanation of Code Use In MRK Column OO : Interchangeable between the new part

How to read the marks and remarks used in this parts book. Section 1 : Explanation of Code Use In MRK Column OO : Interchangeable between the new part

How to read the marks and remarks used in this parts book. Section 1 : Explanation of Code Use In MRK Column OO : Interchangeable between the new part

Quiz 1 ID#: Name: 1. p, q, r (Let p, q and r be propositions. Determine whether the following equation holds or not by completing the truth table belo

1 Fig. 1 Extraction of motion,.,,, 4,,, 3., 1, 2. 2.,. CHLAC,. 2.1,. (256 ).,., CHLAC. CHLAC, HLAC. 2.3 (HLAC ) r,.,. HLAC. N. 2 HLAC Fig. 2

137. Tenancy specific information (a) Amount of deposit paid. (insert amount of deposit paid; in the case of a joint tenancy it should be the total am

はじめに

How to read the marks and remarks used in this parts book. Section 1 : Explanation of Code Use In MRK Column OO : Interchangeable between the new part

memo

I N S T R U M E N T A T I O N & E L E C T R I C A L E Q U I P M E N T Pressure-resistant gasket type retreat method effective bulk compressibility Fro

揃 Lag [hour] Lag [day] 35

PowerPoint Presentation

〈企業特集:検査機器・試薬・技術の新たな展開〉新規マイコプラズマ抗原検査キット—プロラスト®Myco

Microsoft Word - NonGenTree.doc

B : Find Symmetries (A, B) (A + 1, B + 1) A + 1 B + 1 N + k k (A, B) (0, 0), (0, 1),..., (0.N 1) N O(N 2 ) N O(N 3 ) 2


58 10


_Y05…X…`…‘…“†[…h…•



Hospitality-mae.indd

H8000操作編

LC304_manual.ai

Z7000操作編_本文.indb

Microsoft PowerPoint - 06graph3.ppt [互換モード]

161 J 1 J 1997 FC 1998 J J J J J2 J1 J2 J1 J2 J1 J J1 J1 J J 2011 FIFA 2012 J 40 56

Introduction Purpose This training course describes the configuration and session features of the High-performance Embedded Workshop (HEW), a key tool

Microsoft Word - Win-Outlook.docx

27 1 NP NP-completeness of Picross 3D without segment-information and that with height of one

elemmay09.pub

# let st1 = {name = "Taro Yamada"; id = };; val st1 : student = {name="taro Yamada"; id=123456} { 1 = 1 ;...; n = n } # let string_of_student {n



C H H H C H H H C C CUTION:These telephones are for use in Japan only. They cannot be used in other countries because of differences in voltages, tele

浜松医科大学紀要

00.\...ec5


1..FEM FEM 3. 4.

取説_VE-PV11L(応用編)

2

Sport and the Media: The Close Relationship between Sport and Broadcasting SUDO, Haruo1) Abstract This report tries to demonstrate the relationship be

How to read the marks and remarks used in this parts book. Section 1 : Explanation of Code Use In MRK Column OO : Interchangeable between the new part

Introduction Purpose This training course demonstrates the use of the High-performance Embedded Workshop (HEW), a key tool for developing software for

RQT8189-S.indd


kut-paper-template.dvi

.N..

L3 Japanese (90570) 2008


_’¼Œì

,,,,., C Java,,.,,.,., ,,.,, i

46

II

Microsoft PowerPoint - IntroAlgDs pptx

TH-42PAS10 TH-37PAS10 TQBA0286

2 3

九州大学学術情報リポジトリ Kyushu University Institutional Repository 看護師の勤務体制による睡眠実態についての調査 岩下, 智香九州大学医学部保健学科看護学専攻 出版情報 : 九州大学医学部保健学

SPSS


DI DI

Transcription:

アルゴリズムの設計と解析 黄潤和 佐藤温 (TA) 2013.4~

Contents (L3 Search trees) Searching problems AVL tree 2-3-4 trees Red-Black trees 2

Searching Problems Problem: Given a (multi)set S of keys and a search key K, find an occurrence of K in S, if any Searching must be considered in the context of: file size (internal vs. external) dynamics of data (static vs. dynamic) Dictionary operations (dynamic data): find (search) insert delete 3

Taxonomy of Searching Algorithms List searching sequential search binary search interpolation search Tree searching binary search tree (pre-, post-, in- order search) binary balanced trees: AVL trees, red-black trees multiway balanced trees: 2-3 trees, 2-3-4 trees, B trees Hashing open hashing (separate chaining) closed hashing (open addressing) 4

AVL tree - Balanced binary search tree 平衡 2 分探索木 特に木構造の一つ AVL 木平衡条件を満たす平衡 2 分探索木である 左右の部分木の高さの差を多くとも 1 にする balanced? 5

AVL - Good but not Perfect Balance perfect balanced 6

Node Heights Tree A (AVL) 6 1 0 4 9 0 0 1 5 height=2 BF=1-0=1 Tree B (AVL) 2 1 6 1 4 9 0 0 0 1 5 8 height of node = h balance factor = h left -h right 7

Node Heights after Insert 7 Tree A (AVL) Tree B (not AVL) after single rotation 8

Work in class AVL 木の部分木も AVL 木 9

Work in class (answer) yes no yes 10

(2,4) Trees 多分岐の平衡木 ( バランス木 ) である 1 ノードから最大 m 個の枝が出るとき これをオーダー (order) m の B 木という B 木の中でも特に オーダー 3 のものを 2-3 木 オーダー 4 のものを 2-3-4 木 (2, 4) と呼ぶ 9 2 5 7 10 14 11

Features of (2,4) Trees 4-nodes can have 3 items and 4 children Depending on the number of children, an internal node of a (2,4) tree is called a 2-node, 3-node or 4-node 12

Height of a (2,4) Tree (2,4) 木の高さ Theorem: A (2,4) tree storing n items has height O(log n) Proof: Let h be the height of a (2,4) tree with n items Since there are at least 2 i items at depth i = 0,, h 1 and no items at depth h, we have n 1 + 2 + 4 + + 2 h 1 = 2 h 1 Thus, h log (n + 1) Searching in a (2,4) tree with n items takes O(log n) time depth 0 1 h 1 h items 1 2 2 h 1 0 13

Work in class Theorem: A (2,4) tree storing n items has height O(log n) Proof: (Do it again by yourself - DIY ) 14

Insertion 挿入 We insert a new item at the parent v of the leaf reached by searching for k We preserve the depth property but We may cause an overflow (i.e., node v may become a 5-node) ノード数が 5 になってしまいオーバーフロー Example: inserting key 30 causes an overflow 10 15 24 2 8 12 18 v 27 32 35 10 15 24 2 8 12 18 27 30 32 35 v 15

Overflow and Split オーバーフローと分裂 We handle an overflow at a 5-node v with a split operation: オーバーフローを解決するために分裂を行う The overflow may propagate to the parent node u 親であるノード u によってオーバーフローが広まる u 15 20 24 v 12 18 27 30 32 35 u 15 20 24 32 v' 12 18 27 30 overflow! v" 35 u v 1 v 2 v 3 v 4 v 5 u v 1 v 2 v 3 v 4 v 5

(2,4) Tree: Insertion Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 100

(2,4) Tree: Insertion Inserting 60, 30, 10, 20...... 50, 40...

(2,4) Tree: Insertion Inserting 50, 40...... 70,...

(2,4) Tree: Insertion Inserting 70...... 80, 15...

(2,4) Tree: Insertion Inserting 80, 15...... 90...

(2,4) Tree: Insertion Inserting 90...... 100...

(2,4) Tree: Insertion Inserting 100...

Work in class Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 100 Draw (2,4) tree (Do it again by yourself - DIY) 24

(2,4) Tree: Insertion Procedure Splitting 4-nodes during Insertion

(2,4) Tree: Insertion Procedure Splitting a 4-node whose parent is a 2-node during insertion

(2,4) Tree: Insertion Procedure Splitting a 4-node whose parent is a 3-node during insertion

28

Insertion in 2-3-4 trees 1. 挿入するノードを見つける 挿入する値より小さい値は左側 大きい値は右側に位置するような葉ノードを見つける 2. 挿入の準備をする 2 ノードもしくは木全体でアイテムがなければなにもしない 3 ノードならば 親ノードのアイテムが 2 つ以下ならばなにもしない 親ノードのアイテムが 3 つならば挿入前に親ノードの分裂を行なう 4 ノードならば挿入前に分裂を行なう 3. 挿入する 分裂 根ノードであれば 真ん中の値を新たに根ノードとし 残りの 2 つの値はそれぞれその子ノードとなる 根ノードでなければ 真ん中の値を親ノードに移動し 残りの 2 つの値はそれぞれその親ノードの子ノードとなる 29

Analysis of Insertion 挿入の分析 Algorithm insertitem(k, o) 1. We search for key k to locate the insertion node v 2. We add the new item (k, o) at node v 3. while overflow(v) if isroot(v) create a new empty root above v v split(v) Let T be a (2,4) tree with n items n 個の値を持つ 2-4 木 T で考察 Tree T has O(log n) height Step 1 takes O(log n) time because we visit O(log n) nodes Step 2 takes O(1) time Step 3 takes O(log n) time because each split takes O(1) time and we perform O(log n) splits Thus, an insertion in a (2,4) tree takes O(log n) time 30

2-3-4 Tree: Deletion Deletion procedure: items are deleted at the leafs swap item of internal node with inorder successor result

2-3-4 Tree: Deletion Note: a 2-node leaf creates a problem (1-node, underflow ) Solution: on the way from the root down to the leaf - turn 2-nodes (except root) into 3-nodes Case 1: an adjacent sibling has 2 or 3 items "steal" item from sibling by rotating items and moving subtree

2-3-4 Tree: Deletion Turning a 2-node into a 3-node... Case 2: each adjacent sibling has only one item "steal" item from parent and merge node with sibling (note: parent has at least two items, unless it is the root)

Deletion - more example Example: to delete key 24, we replace it with 27 (inorder successor) 10 15 24 2 8 12 18 27 32 35 10 15 27 2 8 12 18 32 35 34

Deletion - more example the adjacent siblings of v are 2-nodes merge v with an adjacent sibling w and move an item from u to the merged node v' After merging, the underflow may propagate to the parent u u 9 14 w 2 5 7 10 v u 2 5 7 9 10 14 v' 13/4/24 16 時 22 分 (2,4) Trees 35

Deletion - more example an adjacent sibling w of v is a 3-node or a 4-node Transfer operation: 1. we move a child of w to v 2. we move an item from u to v 3. we move an item from w to u After a transfer, no underflow occurs 2 u 4 9 w 6 8 v 3. 2. u 4 8 w v 2 6 9 1. 36

Deletion in 2-3-4 trees 1. 削除する値のあるノードを見つける 2. もし葉ノードにあれば削除する 3. もし内部ノードにあれば その値の次に大きい値である葉ノードと値を交換する 4. もしルートとその 2 つの子ノードが全て 2 ノードならば その 3 つのノードを統合する 5. 2 ノードの値を削除する時 となりの兄弟ノードに 3 もしくは 4 ノードがあれば回転により値を移動してくる となりの兄弟ノードが全て 2 ノードならば その内の一つと親ノードにある間の値と統合した後に削除する 37

Analysis of Deletion 削除の分析 Let T be a (2,4) tree with n items Tree T has O(log n) height 木 T の高さは O(log n) In a deletion operation We visit O(log n) nodes to locate the node from which to delete the item 削除するために O(log n) のノードを訪れる We handle an underflow with a series of O(log n) fusions, followed by at most one transfer Each fusion and transfer takes O(1) time 合体と移動 : O(1) Thus, deleting an item from a (2,4) tree takes O(log n) time (2,4) 木での削除の時間 : O(log n) 13/4/24 16 時 22 分 (2,4) Trees 38

2-3-4 Tree: Deletion Practice Delete 32, 35, 40, 38, 39, 37, 60

Exercise 3-1 Consider the following sequence of keys: (2,3,7,9). Insert the items with this set of keys in the order given into the (2,4) tree below. Draw the tree after each removal. 12 キー配列について考える : (2,3,7,9) このキーのセットを図の (2,4) 木に挿入しなさい 5,10 15 それぞれの挿入後の (2,4) 木を描きなさい 1,4 6, 8 11 13,14 17 13/4/24 16 時 22 分 Red-Black Trees 40

Exercise 3-2 Consider the following sequence of keys: (4, 12, 13, 14). Remove the items with this set of keys in the order given from the (2,4) tree below. Draw the tree after each removal. 12 キー配列について考える : (4, 12, 13, 14) このキーのセットを図の (2,4) 木に削除しなさい 5,10 15 それぞれの削除後の (2,4) 木を描きなさい 4 6, 8 11 13,14 17 13/4/24 16 時 22 分 Red-Black Trees 41