Lecture 6-cs345-2014

31
Design and Analysis of Algorithms (CS345/CS345A) Lecture 6 Augmented BST for Dynamic sequences Orthogonal range searching 1

Transcript of Lecture 6-cs345-2014

Page 1: Lecture 6-cs345-2014

Design and Analysis of Algorithms(CS345/CS345A)

Lecture 6

Augmented BST for • Dynamic sequences

• Orthogonal range searching

1

Page 2: Lecture 6-cs345-2014

The fundamental question we answered in last class

Question: What makes BST pervasive in the world of data structures ?

Answer: Augmentation : “storing extra fields at each node”

: Additional information

Page 3: Lecture 6-cs345-2014

DATA STRUCTURES FORDYNAMIC SEQUENCES

3

Page 4: Lecture 6-cs345-2014

Dynamic Sequence

4

OR

OR

Page 5: Lecture 6-cs345-2014

Representing sequence using a BST

8

4

1 2

1

3

1 1

: size field

Page 6: Lecture 6-cs345-2014

6

u

Page 7: Lecture 6-cs345-2014

7

T

We need to do the rebalancing of

height later on.

Page 8: Lecture 6-cs345-2014

Example 2: sequence of numbers

8

Page 9: Lecture 6-cs345-2014

Performing Add(T,i,j,x)

9

iT

View the entire tree T from perspective of the paths from i

and j to the root. How will T look like ?

j

LCA(i,j)

Page 10: Lecture 6-cs345-2014

Performing Add(T,i,j,x)

10

T

j

i

LCA(i,j)

What are the nodes whose values has to be incremented by x ?

Page 11: Lecture 6-cs345-2014

Performing Add(T,i,j,x)

11

T

What are the nodes whose values has to be incremented by x ?

j

i

LCA(i,j)

Page 12: Lecture 6-cs345-2014

Performing Add(T,i,j,x)

12

T

What are the nodes whose values has to be incremented by x ?

j

i

LCA(i,j)

Page 13: Lecture 6-cs345-2014

Performing Add(T,i,j,x)

13

Page 14: Lecture 6-cs345-2014

Key observations about the data structure

14

Page 15: Lecture 6-cs345-2014

Homework

15

Page 16: Lecture 6-cs345-2014

Example 2: sequence of bits

16

Page 17: Lecture 6-cs345-2014

Example 3: sequence of numbers

17

Page 18: Lecture 6-cs345-2014

ORTHOGONAL RANGE SEARCHING

Hopefully you have got a fair amount of understanding of augmenting a BST by now. Think over this problem for a while before proceeding

further.

18

Page 19: Lecture 6-cs345-2014

Orthogonal Range searching

19

Rectangle

No. of points in query rectangle

Page 20: Lecture 6-cs345-2014

Orthogonal Range searching

20

Rectangle

Page 21: Lecture 6-cs345-2014

Orthogonal Range searching

21

Page 22: Lecture 6-cs345-2014

22

T

LCA

Page 23: Lecture 6-cs345-2014

• Binary search tree on x-coordinates

23

T

LCA

Page 24: Lecture 6-cs345-2014

• Binary search tree on x-coordinates

24

T

LCA

Page 25: Lecture 6-cs345-2014

• Binary search tree on x-coordinates

25

T

LCA

Page 26: Lecture 6-cs345-2014

How should we augment each node?

26

left right

valueColor-bit

Augment each node v with a pointer to another BSTstoring all points of T(v) according to y-oordinates.

Y-tree

v

BST storing T(v)

according to y-coordinate

Page 27: Lecture 6-cs345-2014

27

T

LCA

Page 28: Lecture 6-cs345-2014

Homework

28

Page 29: Lecture 6-cs345-2014

Space of the data structure

29

T

1

2

3

4

. . .. . .

BST before augmentation

Page 30: Lecture 6-cs345-2014

Space of the data structure

30

T

. . .

. . .

BST before augmentation

Page 31: Lecture 6-cs345-2014

Total time to build the data structure

31

p