Top Banner
BLG 335E – Analysis of Algorithms I Fall 2013, Recitation 5 11.12.2013 R.A. Atakan Aral [email protected] – Research Lab 1 R.A. Doğan Altan [email protected] – Research Lab 3
55
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Analysis of Algorithms - 5

BLG 335E – Analysis of Algorithms IFall 2013, Recitation 5

11.12.2013

R.A. Atakan [email protected] – Research Lab 1

R.A. Doğan [email protected] – Research Lab 3

Page 2: Analysis of Algorithms - 5

Question 1

• Insert the following sequence of numbers into a 2-3-4 tree

– {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

Page 3: Analysis of Algorithms - 5

Solution 1

• 2-3-4 tree: Perfect balance by allowing 1, 2, or 3 keys per node:

– 2-node: one key, two children.

– 3-node: two keys, three children.

– 4-node: three keys, four children.

• Every path from root to leaf has the same length.

Page 4: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

53

Page 5: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

27 53

Page 6: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

27 53 75

Page 7: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split

53

7527

Page 8: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split

53

25 27 75

Page 9: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

70 75

53

25 27

Page 10: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

70 7525 27 41

53

Page 11: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split

27 53

70 7525 41

Page 12: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split

27 53

38 41 70 7525

Page 13: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

16 25

27 53

38 41 70 75

Page 14: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

16 25

27 53

59 70 7538 41

Page 15: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

16 25 36 38 41

27 53

59 70 75

Page 16: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split

16 25

27 53 70

36 38 41 59 75

Page 17: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split

16 25

27 53 70

73 7536 38 41 59

Page 18: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

16 25

27 53 70

73 7536 38 41 59 65

Page 19: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

16 25 59 60 65

27 53 70

73 7536 38 41

Page 20: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split that propagates up to the root

53

27 38

16 25 73 7536

70

59 60 6541

Page 21: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split that propagates up to the root

53

27 38

16 25 41 46 73 7536

70

59 60 65

Page 22: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split

53

27 38

16 25

60 70

41 46 73 756536 59

Page 23: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48} causes a split

53

27 38

16 25

60 70

55 5941 46 73 756536

Page 24: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

53

27 38

33 3616 25

60 70

55 5941 46 73 7565

Page 25: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

53

27 38

33 3616 25

60 70

65 6855 5941 46 73 75

Page 26: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

53

27 38

33 3616 25

60 70

65 6855 59 73 75 7941 46

Page 27: Analysis of Algorithms - 5

Solution 1 (Cont.)

• {53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, 48}

53

27 38

33 3616 25 41 46 48

60 70

65 6855 59 73 75 79

Page 28: Analysis of Algorithms - 5

Question 2

• 2-3-4 trees are balanced and can be searched in 𝑂(𝑙𝑜𝑔𝑛), but they have different node structures.

• To get 2-3-4 tree advantages in a binary tree format, we can represent it as a red-black tree.

• Convert the following 2-3-4 tree to a red-black tree

53

27 38

16 25 41 46 73 7536

70

59 60 65

Page 29: Analysis of Algorithms - 5

Solution 2

• Properties of a red-black tree:

– the root is always black

– black condition: every path from the root to a leaf node has the same number of black nodes

– red condition: every red node has a black parent

Page 30: Analysis of Algorithms - 5

Solution 2 (Cont.)

• 2-nodes: can be represented with a black node

• 4-nodes: center value becomes the parent (black) and the others become children (red)

X Y Z

X

A B

X

A B

A B C D

X

Y

Z

A B C D

Page 31: Analysis of Algorithms - 5

Solution 2 (Cont.)

• 3-nodes:

• Note:1.Red-black trees are not unique

2.However, the corresponding 2-3-4 tree is unique

X Y

A B C

X

Y

A B

C

X

YA

B C

or

Page 32: Analysis of Algorithms - 5

Solution 2 (Cont.)

• Top-down conversion algorithm (start at the root):1. Apply red-black tree representation to each node2. Repeat for next level…

53

27 38

16 25 41 46 73 7536

70

59 60 65

Page 33: Analysis of Algorithms - 5

Solution 2 (Cont.)

53

27 38

16 25 41 46 73 7536

70

59 60 65

Page 34: Analysis of Algorithms - 5

53

27

16 25 41 46 73 7536 59 60 65

7038

Solution 2 (Cont.)

Page 35: Analysis of Algorithms - 5

53

27

7336

7038

25

16

46

41

7560

6559

Solution 2 (Cont.)

Page 36: Analysis of Algorithms - 5

Question 3

• Insert the following sequence of numbers into a red-black tree

– {2, 1, 4, 5, 9, 3, 6, 7}

Page 37: Analysis of Algorithms - 5
Page 38: Analysis of Algorithms - 5

Solution 3

{2, 1, 4, 5, 9, 3, 6, 7}

2

Page 39: Analysis of Algorithms - 5

{2, 1, 4, 5, 9, 3, 6, 7}

2

1

Solution 3 (Cont.)

Page 40: Analysis of Algorithms - 5

{2, 1, 4, 5, 9, 3, 6, 7}

2

1 4

Solution 3 (Cont.)

Page 41: Analysis of Algorithms - 5

Solution 3 (Cont.)

{2, 1, 4, 5, 9, 3, 6, 7} recoloring

2

1 4

5

2

1 4

5

2

1 4

5

case1 case0

Page 42: Analysis of Algorithms - 5

Solution 3 (Cont.)

{2, 1, 4, 5, 9, 3, 6, 7} recoloring and rotation

2

1 4

5

9

2

1 4

5

9

2

1 5

94

case3step1

case3step2

Page 43: Analysis of Algorithms - 5

Solution 3 (Cont.)

{2, 1, 4, 5, 9, 3, 6, 7} recoloring

2

1 5

94

3

2

1 5

94

3

case1

Page 44: Analysis of Algorithms - 5

Solution 3 (Cont.)

{2, 1, 4, 5, 9, 3, 6, 7}

2

1 5

94

3 6

Page 45: Analysis of Algorithms - 5

Solution 3 (Cont.)

{2, 1, 4, 5, 9, 3, 6, 7} recoloring

2

1 5

94

3 6

7

2

1 5

94

3 6

7

case2step1

Page 46: Analysis of Algorithms - 5

Solution 3 (Cont.)

{2, 1, 4, 5, 9, 3, 6, 7} 2 rotations

2

1 5

94

3 6

7

2

1 5

94

3 7

6

2

1 5

74

3 6 9

case2step2

case2step3

Page 47: Analysis of Algorithms - 5

Question 4

• Delete 90, 80 and 70 from the following red-black tree in the given order

65

50 80

90706010

62

Page 48: Analysis of Algorithms - 5
Page 49: Analysis of Algorithms - 5

Solution 4 (Cont.)

• Delete 90, 80 and 70

65

50 80

90706010

62

65

50 80

706010

62

Page 50: Analysis of Algorithms - 5

Solution 4 (Cont.)

• Delete 90, 80 and 70

65

50 80

706010

62

65

50 80

706010

62

case2

Page 51: Analysis of Algorithms - 5

Solution 4 (Cont.)

• Delete 90, 80 and 70

65

50 80

706010

62

65

50 70

6010

62

absorb black

Page 52: Analysis of Algorithms - 5

Solution 4 (Cont.)

• Delete 90, 80 and 70

65

50 70

6010

62

65

50

6010

62

Page 53: Analysis of Algorithms - 5

Solution 4 (Cont.)

• Delete 90, 80 and 70

65

50

6010

62

50

10 65

60

62

case1

Page 54: Analysis of Algorithms - 5

Solution 4 (Cont.)

• Delete 90, 80 and 70

50

10 65

60

62

50

10 65

62

60

case4

Page 55: Analysis of Algorithms - 5

Solution 4 (Cont.)

• Delete 90, 80 and 70

50

10 65

62

60

50

10 62

60 65

case3