aritrabanik assistantprofessoraritra/rangetrees.pdf · 2019. 6. 16. · aritra banik-5ptslide ideas...

33
Range Trees Aritra Banik 1 Assistant Professor National Institute of Science Education and Research 1 Slide ideas borrowed from Marc van Kreveld and Subhash Suri Range Trees 1/19

Upload: others

Post on 20-Jan-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Range Trees

Aritra Banik1

Assistant ProfessorNational Institute of Science Education and Research

1Slide ideas borrowed from Marc van Kreveld and Subhash SuriRange Trees 1/19

Page 2: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2-dimensional Range Searching

1

2

3

4

5

6

7

9

10

11

12

13

14

15

16

(x1, y1)

(x2, y2)

x1 x2

y1

y2

Range Trees 2/19

Page 3: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2-dimensional Range Searching

1

2

3

4

5

6

7

9

10

11

12

13

14

15

16

(x1, y1)

(x2, y2)

x1 x2

y1

y2

Range Trees 2/19

Page 4: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2-dimensional Range Searching

1

2

3

4

5

6

7

9

10

11

12

13

14

15

16

(x1, y1)

(x2, y2)

x1 x2

y1

y2

Range Trees 2/19

Page 5: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

1-dimensional Range Tree

A 1-dimensional range query with [34, 80]

5 10 22 28 34 36

40

50 55 62 65

70

80 90

5 22 34 50 62 80

10 36 55 70

28 65

40

White nodes: never visited by the queryGrey nodes: visited by the query, unclear if they lead tooutputBlack nodes: Visited by the query, whole subtree is output

Range Trees 3/19

Page 6: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2-dimensional Range Searching

(1, 4)

(2, 11)

(3, 18)

(9, 3)

(7, 12)

(14, 18)

(15, 23)

(12, 14)

(11, 11)

(16, 13)

(18, 21)

(16, 2)

(21, 11)

(20, 4)

(4, 9)

(19, 16)

4 19

9

16

(23, 13)

Range Trees 4/19

Page 7: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2-dimensional Range Searching

(1, 4)

(2, 11)

(3, 18)

(9, 3)

(7, 12)

(14, 18)

(15, 23)

(12, 14)

(11, 11)

(16, 13)

(18, 21)

(16, 2)

(21, 11)

(20, 4)

(4, 9)

(19, 16)

4 19

9

16

(23, 13)

1 2

3 4 7 9 11 12 14 15 16 17 19 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

Range Trees 5/19

Page 8: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2-dimensional Range Searching

(1, 4)

(2, 11)

(3, 18)

(9, 3)

(7, 12)

(14, 18)

(15, 23)

(12, 14)

(11, 11)

(16, 13)

(18, 21)

(16, 2)

(21, 11)

(20, 4)

(4, 9)

(19, 16)

4 19

9

16

(23, 13)

1 2

3 4 7 9 11 12 14 15 16 17 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

19

Range Trees 5/19

Page 9: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2-dimensional Range Searching

(1, 4)

(2, 11)

(3, 18)

(9, 3)

(7, 12)

(14, 18)

(15, 23)

(12, 14)

(11, 11)

(16, 13)

(18, 21)

(16, 2)

(21, 11)

(20, 4)

(4, 9)

(19, 16)

4 19

9

16

(23, 13)

1 2

3 4 7 9 11 12 14 15 16 17 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

19

Range Trees 6/19

Page 10: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2-dimensional Range Searching

(1, 4)

(2, 11)

(3, 18)

(9, 3)

(7, 12)

(14, 18)

(15, 23)

(12, 14)

(11, 11)

(16, 13)

(18, 21)

(16, 2)

(21, 11)

(20, 4)

(4, 9)

(19, 16)

4 19

9

16

(23, 13)

11 14 183

143

11

Range Trees 6/19

Page 11: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2-dimensional Range Searching

1 2

3 4 7 9 11 12 14 15 16 17 19 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

Range Trees 7/19

Page 12: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Storage of 2D range trees

By level: On each level, any point is stored exactly once. Soall associated trees on one level together have O(n) sizeBy point: For any point, it is stored in the associatedstructures of its search path. So it is stored in O(log n) ofthem

Range Trees 8/19

Page 13: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Construction algorithm

Algorithm 1 Build2DRangeTree(P)1: Construct the associated structure: Build a binary search tree

Ty with the y-coordinates in P2: if P contains only one point then3: Create a leaf v storing this point, and make T assoc the as-

sociated structure of v .4: else5: Split P into Pleft and Pright , WRT the median x-coordinate,

xmid

6: vleft=Build2DRangeTree(Pleft)7: vright=Build2DRangeTree(Pright)8: Create a node v storing xmid , make vleft the left child of v ,

make vright the right child of v , and make Ty the associatedstructure of v

9: return v10: end if

Range Trees 9/19

Page 14: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Efficiency of construction

T (n) = 2 · T (n/2) + O(n log n)

The construction algorithm takes O(n log2 n) time

Range Trees 10/19

Page 15: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Efficiency of construction

T (n) = 2 · T (n/2) + O(n log n)

The construction algorithm takes O(n log2 n) time

Range Trees 10/19

Page 16: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Query algorithm

Query [4 : 19]× [y1 : y2]

1 2

3 4 7 9 11 12 14 15 16 17 19 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

Range Trees 11/19

Page 17: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Query algorithm

Query [4 : 19]× [y1 : y2]

1 2

3 4 7 9 11 12 14 15 16 17 19 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

[y1, y2] [y1, y2]

Range Trees 11/19

Page 18: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Query algorithm

Algorithm 2 2DRangeQuery(T , [x1 : x2]× [y1 : y2])

1: vsplit ← FindSplitNode(T , x1, x2)2: if vsplit is a leaf then3: Check if the point in vsplit must be reported.4: else5: v ← lc(vsplit)6: while v is not a leaf do7: if x1 ≤ value(v) then8: 1DRangeQuery(Ty (rc(v)), [y1 : y2])9: v ← lc(v)

10: else11: v ← rc(v)12: end if13: end whileCheck if the point in v must be reported.14: Similarly, follow the path from rc(vsplit) to x215: end if

Range Trees 12/19

Page 19: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2D range query efficiency

We search in O(log n) associated structures to perform a 1Drange query; at most two per level of the main treeThe query time is O(log2 n + k), where k is the size of theoutput

Theorem

A set of n points in the plane can be preprocessed in O(n log2 n)time into a data structure of O(n log n) size so that any 2D rangequery can be answered in O(log2 n + k) time, where k is thenumber of answers reported.

Recall that a kd-tree has O(n) size and answers queries inO(√n + k) time

Range Trees 13/19

Page 20: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

2D range query efficiency

We search in O(log n) associated structures to perform a 1Drange query; at most two per level of the main treeThe query time is O(log2 n + k), where k is the size of theoutput

Theorem

A set of n points in the plane can be preprocessed in O(n log2 n)time into a data structure of O(n log n) size so that any 2D rangequery can be answered in O(log2 n + k) time, where k is thenumber of answers reported.

Recall that a kd-tree has O(n) size and answers queries inO(√n + k) time

Range Trees 13/19

Page 21: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Higher dimensional range trees

TheoremA set of n points in in d-dimensional space can be preprocessed inO(n logd n) time into a data structure of O(n logd n) size so thatany 2D range query can be answered in O(logd n + k) time, wherek is the number of answers reported.

Recall that a kd-tree has O(n) size and answers queries inO(n1−1/d + k) time

Range Trees 14/19

Page 22: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Range searching

Can we do better?We can improve the query time of a 2D range tree fromO(log2 n) to O(log n) by a technique called fractionalcascading.The idea illustrated best by a different query problem:Suppose that we have a collection of sets S1 . . . Sm , where|S1| = n and where Si+1 ⊂ Si

We want a data structure that can report for a query numberx , the smallest value greater than equals to x in all setsS1, ...,Sm

Range Trees 15/19

Page 23: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Range searching

Can we do better?We can improve the query time of a 2D range tree fromO(log2 n) to O(log n) by a technique called fractionalcascading.The idea illustrated best by a different query problem:Suppose that we have a collection of sets S1 . . . Sm , where|S1| = n and where Si+1 ⊂ Si

We want a data structure that can report for a query numberx , the smallest value greater than equals to x in all setsS1, ...,Sm

Range Trees 15/19

Page 24: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Range searching

Can we do better?We can improve the query time of a 2D range tree fromO(log2 n) to O(log n) by a technique called fractionalcascading.The idea illustrated best by a different query problem:Suppose that we have a collection of sets S1 . . . Sm , where|S1| = n and where Si+1 ⊂ Si

We want a data structure that can report for a query numberx , the smallest value greater than equals to x in all setsS1, ...,Sm

Range Trees 15/19

Page 25: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Fractional Cascading

The idea illustrated best by a different query problem:Suppose that we have a collection of sets S1 . . . Sm , where|S1| = n and where Si+1 ⊂ Si

We want a data structure that can report for a query numberx , the smallest value greater than equals to x in all setsS1, ...,Sm.Say x = 5

1 3 6 10 12 18 19 25 28 32 40 55 61 67

6 12

18

25

25

67

67

67

55

55

55 6118 25 28 32 401

6128 32 40

Range Trees 16/19

Page 26: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Fractional Cascading

The idea illustrated best by a different query problem:Suppose that we have a collection of sets S1 . . . Sm , where|S1| = n and where Si+1 ⊂ Si

We want a data structure that can report for a query numberx , the smallest value greater than equals to x in all setsS1, ...,Sm.Say x = 5

1 3 6 10 12 18 19 25 28 32 40 55 61 67

6 12

18

25

25

67

67

67

55

55

55 6118 25 28 32 401

6128 32 40

Range Trees 16/19

Page 27: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Fractional Cascading

Now we do "the same" on the associated structures of a2-dimensional range treeNote that in every associated structure, we search with thesame values y1 and y2.Replace all associated structures (y trees) with sorted listsFor every list element (and leaf of the associated structure ofthe root), store two pointers to the appropriate list elements inthe lists of the left child and of the right child

Range Trees 17/19

Page 28: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Fractional Cascading

Query (4, 8)− (19, 30)

1 2

3 4 7 9 11 12 14 15 16 17 19 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

(1, 90), (2, 30), (3, 8), (4, 12), (7, 14), (9, 7), (11, 20), (12, 19), (14, 21),(15, 4), (16, 1), (17, 6), (18, 5), (19, 60), (20, 3), (21, 11), (23, 40)

(4, 8)− (19, 30)

1 3 4 5 6 7 8 9 11 12 14 19 20 21 30 40 60

Range Trees 18/19

Page 29: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Fractional Cascading

Query (4, 8)− (19, 30)

1 2

3 4 7 9 11 12 14 15 16 17 19 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

(1, 90), (2, 30), (3, 8), (4, 12), (7, 14), (9, 7), (11, 20), (12, 19), (14, 21),(15, 4), (16, 1), (17, 6), (18, 5), (19, 60), (20, 3), (21, 11), (23, 40)

(4, 8)− (19, 30)

1 3 4 5 6 7 8 9 11 12 14 19 20 21 30 40 60

Range Trees 18/19

Page 30: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Fractional Cascading

Query (4, 8)− (19, 30)

1 2

3 4 7 9 11 12 14 15 16 17 19 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

(1, 90), (2, 30), (3, 8), (4, 12), (7, 14), (9, 7), (11, 20), (12, 19), (14, 21),(15, 4), (16, 1), (17, 6), (18, 5), (19, 60), (20, 3), (21, 11), (23, 40)

(4, 8)− (19, 30)

1 3 4 5 6 7 8 9 11 12 14 19 20 21 30 40 60

7 8 9 12 14 19 20 21 30

Range Trees 18/19

Page 31: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Fractional Cascading

Query (4, 8)− (19, 30)

1 2

3 4 7 9 11 12 14 15 16 17 19 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

(1, 90), (2, 30), (3, 8), (4, 12), (7, 14), (9, 7), (11, 20), (12, 19), (14, 21),(15, 4), (16, 1), (17, 6), (18, 5), (19, 60), (20, 3), (21, 11), (23, 40)

(4, 8)− (19, 30)

1 3 4 5 6 7 8 9 11 12 14 19 20 21 30 40 60

7 8 9 12 14 19 20 21 30

7 1920 21

Range Trees 18/19

Page 32: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Fractional Cascading

Query (4, 8)− (19, 30)

1 2

3 4 7 9 11 12 14 15 16 17 19 20 21 23181

2 4

3

9 12

11

7

15 17

16

19 21

20

18

14

(1, 90), (2, 30), (3, 8), (4, 12), (7, 14), (9, 7), (11, 20), (12, 19), (14, 21),(15, 4), (16, 1), (17, 6), (18, 5), (19, 60), (20, 3), (21, 11), (23, 40)

(4, 8)− (19, 30)

1 3 4 5 6 7 8 9 11 12 14 19 20 21 30 40 60

7 8 9 12 14 19 20 21 30

7 1920 21

8 9 12 14 30

Range Trees 18/19

Page 33: AritraBanik AssistantProfessoraritra/RangeTrees.pdf · 2019. 6. 16. · Aritra Banik-5ptSlide ideas borrowed from Marc van Kreveld and Subhash Suri Created Date: 6/6/2019 4:15:59

Higher dimensional range trees

TheoremA set of n points in in d-dimensional space can be preprocessed inO(n logd n) time into a data structure of O(n logd n) size so thatany 2D range query can be answered in O(logd−1 n + k) time,where k is the number of answers reported.

Range Trees 19/19