analysis of algorithms...color the incoming edge of the new node red 4. add two new leaves, and...

48
アルゴリズムの設計と解析 潤和、佐藤 温(TA2013.4

Upload: others

Post on 10-May-2020

22 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

アルゴリズムの設計と解析

黄 潤和、佐藤 温(TA) 2013.4~

Page 2: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

Contents (L4 – Search trees) Searching problems AVL tree 2-3-4 trees

(quick review of 2-3-4 tree’s insertion) explain 2-3-4 deletion

Red-Black tree

2

Page 3: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

Insertion in 2-3-4 trees 1. 挿入するノードを見つける

挿入する値より小さい値は左側、大きい値は右側に位置するような葉ノードを見つける

2. 挿入の準備をする 2ノードもしくは木全体でアイテムがなければなにもしない 3ノードならば

親ノードのアイテムが2つ以下ならばなにもしない 親ノードのアイテムが3つならば挿入前に親ノードの分裂を行なう

4ノードならば挿入前に分裂を行なう 3. 挿入する

分裂 根ノードであれば、真ん中の値を新たに根ノードとし、残りの2つ

の値はそれぞれその子ノードとなる 根ノードでなければ、真ん中の値を親ノードに移動し、残りの2つ

の値はそれぞれその親ノードの子ノードとなる 3

Page 4: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

4

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.

キー配列について考える: (2,3,7,9)。 このキーのセットを図の(2,4)木に挿入しなさい。 それぞれの挿入後の(2,4)木を描きなさい。

11 1,4

12

13,14

5,10

6, 8

15

17

Page 5: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

5

10 65 15 60 20 55

25 50 30 45

35 40

例:Insertion of 75, 5, 70, 10, 65, 15, 60, 20, 55, 25, 50, 30, 45, 35, 40, 80

80

Read this page at home

Page 6: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

Work in class (家で自習)

6

Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 99 Draw (2,4) tree

Page 7: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

(2,4) Tree: Insertion (例の通り)

Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 99

Page 8: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

2-3-4 Tree: Deletion Deletion procedure:

• items are deleted at the leafs swap item of internal node with inorder successor

result

Page 9: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

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

e.g. deleting node is 40

Page 10: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

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) e.g. The nodes, 10 and 40

Page 11: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

11

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

9 14

2 5 7 10

u

v 9

10 14

u

v' w 2 5 7

Page 12: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

12

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

4 9

6 8 2

u

v w 4 8

6 2 9

u

v w

Deletion - more example

1.

2.

3.

Page 13: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

(2,4) tree Deletion Algorithm I

1. Locate node n, which contains item I

2. If node n is not a leaf swap I with inorder successor

deletion always begins at a leaf

3. If leaf node n contains another item, just delete item I else

Deleting item I:

Page 14: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

Deletion in 2-3-4 trees 1. 削除する値のあるノードを見つける 2. もし葉ノードにあれば削除する もし内部ノードにあれば、その値の次に大きい値 である葉ノードと値を交換する 3. もしルートとその2つの子ノードが全て2ノードならば、 その3つのノードを統合する 2ノードの値を削除する時

となりの兄弟ノードに3もしくは4ノードがあれば回転により値を移動してくる

となりの兄弟ノードが全て2ノードならば、その内の一つと親ノードにある間の値と統合した後に削除する

14

Page 15: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

15

Deletion – work in class

Example: to delete key 24, we replace it with 27 (inorder successor)

27 32 35

10 15 24

2 8 12 18

?

Page 16: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

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

Page 17: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

17

99 90 15

80 70 40 50

20 10 30 60

From the root down to the leaf 1. find the node in which item I is in and then check the following cases 2. if (the node is leaf) 3. delete 4. if (the node is not leaf) 5. swap 6. if (the root and two children are 2-nodes) 7. merge1 8. if (sibling node has 2 or 3 items) 9. right/left rotate 10. if (sibling nodes are 2-nodes) 11. merge2

3. delete 99 is in the lead node

5. swap: swap item of internal node with inorder successor

7. merge1 combine all three elements into the root.

9. right/left rotate: parent item to the right/left child and the left/right child to its parent

7.merge1 and delete 15 2-node (30, 50, 70)

merge and delete 10 a 2-node (10, 30, 60)

9. right rotate and delete 40 2-node (20, 30, 40)

11.merge2 and delete 50 2-node (30, 50, 60)

11. merge2 combine all three elements as a child node.

(2,4) Tree: Deletion procedure

11.merge2 and delete 80 2-node (60, 70, 80)

Page 18: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

18

-70

(50,60,70) 9.right rotate (70.75) 5.swap (70) delete

-30

(30,50,60) 9.left rotate (30.35) 5.swap (30) delete

(35.40) 5.swap (35) delete

-10

(60,50,75) 11.merge2 (65,75,80) 11.merge2 (75) delete

-75

Work in class: delete 80, 40, 45

-35

(10,20,40) 9.left rotate (5,10.15) 11.merge2 (10) delete

Page 19: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

2-3-4 Tree: Deletion Practice

Delete 32, 35, 40, 38, 39, 37, 60

result

Page 21: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

21

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.

キー配列について考える: (4, 12, 13, 14)。 このキーのセットを図の(2,4)木に削除しなさい。 それぞれの削除後の(2,4)木を描きなさい。

11 4

12

13,14

5,10

6, 8

15

17

Page 22: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

22

Red-Black Trees

video lecture http://videolectures.net/mit6046jf05_demaine_lec10/

Page 23: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

23

Outline What is Red-Black?

赤黒木とは? From (2,4) trees to red-black trees 2-4木から赤黒木へ Red-black tree 赤黒木 Insertion 挿入

restructuring 再構築 recoloring 再色付け

Deletion 削除 restructuring 再構築 recoloring 再色付け adjustment 調整

Page 24: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

24

Red-Black Tree 赤黒木

A red-black tree can also be defined as a binary search tree that satisfies the following properties: 赤黒木は以下の特性を満たす二分探索木として定義することができる。

9

15 4

6 2 12

7

21

Page 25: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

25

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)

Page 26: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

26

Page 27: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

27

How?

OK x x

Page 28: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

28

From (2,4) to Red-Black Trees 2-4木から赤黒木へ

A red-black tree is a representation of a (2,4) tree by means of a binary tree whose nodes are colored red or black (2,4)木を赤と黒の2つの色をもった2分木で表す In comparison with its associated (2,4) tree, a red-black tree has same logarithmic time performance 同じ実装時間(対数) simpler implementation with a single node type ノードのシンプルな表現

2 6 7 3 5 4

4 6

2 7

5

3

3

5 OR

Page 29: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

29

Compared to (2,4) Tree Let each node have d (2, 3, or 4) children so that we have search time O(logdN) since the height of a (2,4) tree is log (N+1) when d is 2 (binary tree) and 1/2log (N+1) when d is 4.

How about (2,4) trees? •Balanced -- good •O(log N) search time -- good •Different node structure -- not good

Can we get the advantages of (2,4) in a binary tree? Red-Black trees

Page 30: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

The properties of Red-black tree?

30

N=7 L=N+1=7+1=8 H=4 B=2

L=N+1=8= 4B - maximum 1/2log22B=(B=2) - minimum

log22B=(H=4) - minimum

Page 31: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

31

Insertion 挿入 1. Perform a standard search to find the leaf where the key should be added 2. Replace the leaf with an internal node with the new key 3. Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have two consecutive red edges! (This is a problem! need to solve it!) Solution: We must reorganize tree to remove that violation. What must be done depends on the sibling of the parent.

Violation! (Double red edges)

Page 32: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

32

restructuring(): Remedying a Double Red

重なる赤の改善法 Rotation right rotation left rotation double rotations

Left rotation

(left-right) double rotations

Page 33: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

33

recoloring(): Bottom-up rebalancing

木の平衡の改善法 Problem

When incoming edge of p is red and its sibling is also red. “rotation” does not work!

double red edges!

problem!

Bottom-up recoloring: From bottom leaf nodes, black-red-black-…… •The black depth remains unchanged for all of the descendants of g •This process will continue upward beyond g if necessary: rename g as n and repeat.

Page 34: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

34

Some examples Insert “C U B S”

Double rotation

Right rotation

Page 35: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

35

Some examples…continue…

Recoloring Left rotation

Page 36: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

36

Analysis of Insertion 挿入の分析

Recall that a red-black tree 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 we perform O(log n) recoloring, each

taking O(1) time, and at most one restructuring

taking O(1) time

Thus, an insertion in a red-black tree takes O(log n) time

Algorithm insertItem(k, o) 1. We search for key k to locate the

insertion node z 2. We add the new item (k, o) at

node z and color z red 3. while doubleRed(z) if isBlack(sibling(parent(z)))

z ← restructuring(z) return

else { sibling(parent(z) is red } z ← recoloring(z)

Page 37: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

37

Deletion 削除 To perform operation remove(k), we first execute the deletion algorithm for binary search trees 最初に2分探索木の削除アルゴリズムを用いる Let v be the internal node removed, w the external node removed, and r the sibling of w

内部ノード v を削除すると、外部ノードの w と r も削除される。 If either v of r was red, we color r black and we are done Else (v and r were both black) we color r double black, which is a

violation of the internal property requiring a reorganization of the tree Example where the deletion of 8 causes a double black:

Double black! Violation: Black height is changed

Solution?

Page 38: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

38

Deletion – a node without external children

No double black

Page 39: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

Deletion – a node with external children

39

Remove v 1. If v was red, color u black, else, color u double black. 2. If double black edge exists, perform action for 3 cases: •Case 1: black sibling s with a red child •Case 2: black sibling s with black children •Case 3: red sibling s

if else

Page 40: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

Case 1: black sibling s with a red child

40

• If sibling s is black and one of its children is red, perform a restructuring

Leaf node in black

successor node of p

successor external node of p

Why Z here, not S?

Page 41: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

Case 2: black sibling s with black children

41

• If sibling and its children are black, perform recoloring If parent becomes double black, continue upward

1. recoloring from leaf nodes 2. continue upward

Continue to solve the double black edge by recoloring

Page 42: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

Case 3: red sibling s

42

• If sibling s is red, perform an adjustment then its sibling is black (Case 3 becomes Case 1 or Case 2)

V’s sibling becomes black, Which becomes Case 1&2

Page 43: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

43

Some examples Delete 9, 8, 7

Page 44: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

44

Q1: Which case? • case 1 • case 2 • case 3

Q2: What operation? • restructuring • recoloring • adjustment

One more example

Page 45: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

45

Analysis of Deletion 挿入の分析

Recall that a red-black tree 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 we perform at most one restructuring

taking O(1) time O(log n) recoloring, O(log n) adjustmen,

Thus, an deletion in a red-black tree takes O(log n) time

Algorithm deleteItem(k, o) 1. We search for key k to locate the node v 2. We delete node v and 3. while doubleBlack(v) if isBlack(sibling(v)) if isRed(sibling(oneOfChildren(v)))

restructuring() else recoloring()

else { sibling(v) is red } adjustment()

Page 46: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

Time Complexity of Red-Black Trees

46

Page 48: Analysis of Algorithms...Color the incoming edge of the new node red 4. Add two new leaves, and color their incoming edges black 5. If the parent had an incoming red edge, we now have

48

Exercise 4-1 Consider the following sequence of keys: (18, 30, 25, 12, 14). Insert the

items with this set of keys in the order given into the red-black tree below. Draw the tree after each insertion.

キー配列について考える: (18, 30, 25, 12, 14)。 このキーのセットを図の赤黒木に挿入しなさい。 それぞれの挿入後の赤黒木を描きなさい。