55
アルゴリズムの設計と解析 教授: 潤和 W4022[email protected] SA広野 史明 A4/A10[email protected]

Analysis of Algorithms 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

Embed Size (px)

Citation preview

Page 1: Analysis of Algorithms 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

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

教授: 黄 潤和 (W4022)[email protected]

SA: 広野 史明 (A4/A10)[email protected]

Page 2: Analysis of Algorithms 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

Contents (L5 – Search trees)

Searching problems

AVL tree

2-3-4 trees (insertion)

2

Page 3: Analysis of Algorithms 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

3

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)

Like Dictionary: Dictionary operations (dynamic data):

find (search)

insert

delete

Page 4: Analysis of Algorithms 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

4

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

Multi-way balanced trees: 2-3 trees, 2-3-4 trees, B trees

Hashing

open hashing (separate chaining)

closed hashing (open addressing)

Page 5: Analysis of Algorithms 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

AVL tree - Balanced binary search tree

平衡2分探索木

An AVL tree (named after inventors Adelson-Velskyand Landis) is a self-balancing binary search tree

特に木構造の一つ。AVL木平衡条件を満たす平衡2分探索木である。左右の部分木の高さの差を多くとも1にする。

5

balanced?

Page 6: Analysis of Algorithms 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

AVL - Good but not Perfect Balance

6

perfect balanced

Page 7: Analysis of Algorithms 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

7

Node Heights

1

00

2

0

6

4 9

81 5

1

height of node = hbalance factor = hleft-hright

0

0

height=2 BF=1-0=1

0

6

4 9

1 5

1

Tree A (AVL) Tree B (AVL)

Count from leaf nodes

Page 8: Analysis of Algorithms 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

8

Node Heights after Insert 7

Tree A (AVL) Tree B (not AVL)

after single rotation

balance factor0-(-1)=1

Page 9: Analysis of Algorithms 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

Work in class

9

AVL木の部分木もAVL木

(a)

(c)

(b)

Page 10: Analysis of Algorithms 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

10

(2,4) Trees

9

10 142 5 7

• B木は多分岐の平衡木(バランス木)である。1 ノードから最大m 個の枝が出るとき、これをオーダー(order) m のB木という。

• B木の中でも特に、オーダー3のものを2-3木、オーダー4のものを2-3-4木 (2, 4) と呼ぶ。

Page 11: Analysis of Algorithms 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

11

2-node 3-node

Page 12: Analysis of Algorithms 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

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

4-node

Page 13: Analysis of Algorithms 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

13

Height of a (2,4) Tree(2,4)木の高さ

Theorem: A (2,4) tree storing n items has height O(log n)

Searching in a (2,4) tree with n items takes O(log n) time

1

2

2h-1

0

items

0

1

h-1

h

depth

Work in ClassHint: refer to the following binary tree,

proof this theorem

Page 14: Analysis of Algorithms 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

14

Height of a (2,4) Tree(2,4)木の高さ

Theorem: A (2,4) tree storing n items has height O(log2 n)

Proof: Let h be the height of a (2,4) tree with n items

Since there are at least 2i items at depth i = 0, … , h - 1 and no items at depth h, we have

n 1 + 2 + 4 + … + 2h-1 = 2h - 1

Thus, h log2 (n + 1)

Searching in a (2,4) tree with n items takes O(log2n) time

1

2

2h-1

0

items

0

1

h-1

h

depth

Page 15: Analysis of Algorithms 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

15

(2,4)Tree: 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

27 32 35

10 15 24

2 8 12 18

10 15 24

2 8 12 27 30 32 3518

v

v

Is it correct?If no, what is the problem?

Page 16: Analysis of Algorithms 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

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によってオーバーフローが広まる

15 20 24

12 27 30 32 3518

v

u

v1 v2 v3 v4 v5

15 20 24 32

12 27 3018

v'

u

v1 v2 v3 v4 v5

35

v"

u u

overflow!how to insert?If it is this case,

Page 17: Analysis of Algorithms 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

17

Page 18: Analysis of Algorithms 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

(2,4) Tree: InsertionInserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 99

2-3-4 tree insert

Page 19: Analysis of Algorithms 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

(2,4) Tree: InsertionInserting 60, 30, 10, 20 ...

... 50, 40 ...

Page 20: Analysis of Algorithms 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

(2,4) Tree: InsertionInserting 50, 40 ...

... 70, ...

Page 21: Analysis of Algorithms 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

Work in class

21

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

Insert 70,please draw (2,4) tree

Page 22: Analysis of Algorithms 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

(2,4) Tree: Insertion – (answer)

Inserting 70 ...

... 80, 15 ...

Page 23: Analysis of Algorithms 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

(2,4) Tree: InsertionInserting 80, 15 ...

... 90 ...

Page 24: Analysis of Algorithms 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

Work in class

24

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

Insert 90,please draw (2,4) tree

Page 25: Analysis of Algorithms 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

(2,4) Tree: Insertion – (answer)

Inserting 90 ...

... 99..

Page 26: Analysis of Algorithms 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

(2,4) Tree: InsertionInserting 99 ...

80 90 99

Page 27: Analysis of Algorithms 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

(2,4) Tree: InsertionInserting 99 ...

Page 28: Analysis of Algorithms 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

28

Page 29: Analysis of Algorithms 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

29

Understand it from the Python code

Page 30: Analysis of Algorithms 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

(2,4) Tree: Insertion Procedure

Splitting 4-nodes

pItemC

In parent levelpItemB

Page 31: Analysis of Algorithms 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

(2,4) Tree: Insertion Procedure

Splitting a 4-node whose parent is a 2-node during insertion

Page 32: Analysis of Algorithms 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

(2,4) Tree: Insertion Procedure

Splitting a 4-node whose parent is a 3-node during insertion

Page 33: Analysis of Algorithms 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

33

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 nitemsn個の値を持つ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

Page 34: Analysis of Algorithms 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

2-3-4 Tree - Insertion process implementation in python

34

Page 35: Analysis of Algorithms 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

35

Page 36: Analysis of Algorithms 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

36

Page 37: Analysis of Algorithms 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

37

Page 38: Analysis of Algorithms 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

38

Page 39: Analysis of Algorithms 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

39

Page 40: Analysis of Algorithms 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

40

Page 41: Analysis of Algorithms 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

41

Page 42: Analysis of Algorithms 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

2-3-4 tree demohttp://www.cs.unm.edu/~rlpm/499/ttft.html

42

2-3-4-tree.jar

http://stackoverflow.com/questions/15047935/234-tree-python

http://www.clear.rice.edu/comp212/01-fall/lectures/33/

https://tbc-python.fossee.in/convert-notebook/Sams_Teach_Yourself_Data_Structures_and_Algorithms_Analysis_in_24_Hours/chapter20_1.ipynb

Page 43: Analysis of Algorithms 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

43

Exercise 5-1 Consider the following sequence of keys: (7,9, 2, 3). Insert the items with this set of keys in the order given into the (2,4) tree below. Draw the tree after each insertion.

キー配列について考える: (2,3,7,9)。

このキーのセットを図の(2,4)木に挿入しなさい。

それぞれの挿入後の(2,4)木を描きなさい。

111,4

12

13,14

5,10

6, 8

15

17

Page 44: Analysis of Algorithms 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

Exercise 5-2 5.2.1 Understand 2-3-4 tree and run the example implementation in Python

(given in this slide)

5.2.2 (optional) Improve the program so that it have GUI for insertion

and can display 2-3-4 tree.

Page 45: Analysis of Algorithms 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

2-3-4 Tree: DeletionDeletion procedure:

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

result

Delete, then handle problem

Page 46: Analysis of Algorithms 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

2-3-4 Tree: DeletionDeletion procedure:

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

result

90

60 70 80 100

pull down 70merge60,70,80

Prevent problem, then delete

Page 47: Analysis of Algorithms 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

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

Page 48: Analysis of Algorithms 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

2-3-4 Tree: DeletionTurning a 2-node into a 4-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)

Page 49: Analysis of Algorithms 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

49

Deletion - more example

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

27 32 35

10 15 24

2 8 12 18

32 35

10 15 27

2 8 12 18

Swap and delete

Page 50: Analysis of Algorithms 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

50

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'w2 5 7

Delete and handle underflow(merge)

Page 51: Analysis of Algorithms 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

51

an adjacent sibling w of v is a 3-node or a 4-node Transfer operation:

1. we move a child of w to v2. we move an item from u to v3. we move an item from w to u

After a transfer, no underflow occurs

4 9

6 82

u

vw

4 8

62 9

u

vw

Deletion - more example

1.

2.

3.

rotation

Page 52: Analysis of Algorithms 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

18/5/14 8時56分 (2,4) Trees 52

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 53: Analysis of Algorithms 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

2-3-4 Tree: Deletion Practice

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

Page 54: Analysis of Algorithms 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

2-3-4 Tree: Deletion Practice(solution)

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

result

Page 55: Analysis of Algorithms 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

55

Exercise 5-3 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)木を描きなさい。

114

12

13,14

5,10

6, 8

15

17