Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4...

17
0 Simon Gog: Advanced Data Structures Institute of Theoretical Informatics Algorithmics Institute of Theoretical Informatics - Algorithmics Advanced Data Structures Simon Gog – [email protected] KIT – University of the State of Baden-Wuerttemberg and National Research Center of the Helmholtz Association www.kit.edu

Transcript of Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4...

Page 1: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

0 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

Institute of Theoretical Informatics - Algorithmics

Advanced Data StructuresSimon Gog – [email protected]

KIT – University of the State of Baden-Wuerttemberg andNational Research Center of the Helmholtz Association www.kit.edu

Page 2: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

Range minimum queries (RMQs)

1 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

DefinitionGiven an array A of length n containing elements from a totally orderedset. A range minimum query rmqA(`, r ) returns the position of theminimal element in the sub-array A[`, r ]:

rmqA(`, r ) = arg min`≤k≤r

A[k ]

Example

A = 80

21

42

73

14

95

36

57

78

49

610

411

312

113

414

815

Page 3: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

Range minimum queries (RMQs)

1 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

DefinitionGiven an array A of length n containing elements from a totally orderedset. A range minimum query rmqA(`, r ) returns the position of theminimal element in the sub-array A[`, r ]:

rmqA(`, r ) = arg min`≤k≤r

A[k ]

Example

A = 80

21

42

73

14

95

36

57

78

49

610

411

312

113

414

815

rmq(2,6) = 4

Page 4: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

Range minimum queries (RMQs)

1 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

DefinitionGiven an array A of length n containing elements from a totally orderedset. A range minimum query rmqA(`, r ) returns the position of theminimal element in the sub-array A[`, r ]:

rmqA(`, r ) = arg min`≤k≤r

A[k ]

Example

A = 80

21

42

73

14

95

36

57

78

49

610

411

312

113

414

815

rmq(5,9) = 6

Page 5: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

Range minimum queries (RMQs)

1 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

DefinitionGiven an array A of length n containing elements from a totally orderedset. A range minimum query rmqA(`, r ) returns the position of theminimal element in the sub-array A[`, r ]:

rmqA(`, r ) = arg min`≤k≤r

A[k ]

Example

A = 80

21

42

73

14

95

36

57

78

49

610

411

312

113

414

815

rmq(0,15) = 4

Page 6: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

Overview

2 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

Notation: Complexity of an algorithm is denoted with 〈f (n),g(n)〉 ,where f (n) is preprocessing time and g(n) query time.Different solutions:

naïve approach 1: 〈O(n2),O(1)〉 using O(n2) words of spacenaïve approach 2: 〈O(1),O(n)〉〈O(n),O(log n)〉 using O(n) words of space〈O(n log n),O(1)〉 using O(n log n) words of space〈O(n log log n),O(1)〉 using O(n log log n) words of space〈O(n),O(1)〉 using O(n) words of space〈O(n),O(1)〉 using 4n + o(n) bits of space〈O(n),O(1)〉 using 2n + o(n) bits of space

LiteratureM.A. Bender, M. Farach-Colton: The LCA Problem Revisited. (LATIN2000) K. Sadakane: Compressed Suffix Trees with Full Functionality.(TCS 2007)

Page 7: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n),O(log n)〉 – solution #1

3 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

A = 8

0

2

1

4

2

7

3

1

4

9

5

3

6

5

7

7

8

4

9

6

10

4

11

3

12

1

13

4

14

8

15

1 2 4 6 9 11 13 14

1 4 9 13

4 13

4

rmq(1,5) = 4

Page 8: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n),O(log n)〉 – solution #1

3 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

A = 8

0

2

1

4

2

7

3

1

4

9

5

3

6

5

7

7

8

4

9

6

10

4

11

3

12

1

13

4

14

8

15

1 2 4 6 9 11 13 14

1 4 9 13

4 13

4

rmq(1,5) = 4

Page 9: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n),O(log n)〉 – solution #1

3 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

A = 8

0

2

1

4

2

7

3

1

4

9

5

3

6

5

7

7

8

4

9

6

10

4

11

3

12

1

13

4

14

8

15

1 2 4 6 9 11 13 14

1 4 9 13

4 13

4

rmq(1,5) = 4

Page 10: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n),O(log n)〉 – solution #1

3 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

A = 8

0

2

1

4

2

7

3

1

4

9

5

3

6

5

7

7

8

4

9

6

10

4

11

3

12

1

13

4

14

8

15

1 2 4 6 9 11 13 14

1 4 9 13

4 13

4

rmq(1,5) = 4

Page 11: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n),O(log n)〉 – solution #1

4 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

Store index of minimum in binary interval tree.Tree has O(n) nodes.Follow all nodes which overlap with the query interval but are not fullycontained in it (at most 2 per level).So not more than 2 log n such nodes in total.Select all children of these nodes which are fully contained in thequery interval.From these nodes select the index with minimal value.

Page 12: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n log n),O(1)〉 – solution #2

5 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

For each item A[i ] store an array Mi [0, log n].Mi [j ] = rmqA(i , i + 2j − 1)Space is O(n log n) wordsHow long does pre-computation take?

QueryingFind the largest k with 2k ≤ `− r + 1. Then

rmqA(`, r ) ={

Mi [k ] if A[Mi [k ]] < A[Mj−2k+1[k ]]Mj−2k+1[k ] otherwise

Question: How can k be determined in constant time?

Page 13: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n log log n),O(1)〉 solution

6 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

Split A into t = nlog n blocks B0, ...,Bt−1. B spans O(log n) items of A.

Create an array S[0, t − 1] with S[i ] = min{x ∈ Bi}Build rmq structure #2 for SFor each block Bi of O(log n) elements build rmq structure #2Total space: O(n) + O(n log log n)

Querying

Determine blocks B`′ , Br ′ which contain ` and rCalculate m = rmqS(`

′ + 1, r ′ − 1)Let k0, k1, k2 be the results of the RMQs in blocks `′, r ′, and m relativeto AReturn arg minki

A[ki ] for 0 ≤ k ≤ 3

Page 14: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n),O(1)〉 solution

7 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

DefinitionThe Cartesian Tree C of an array is defined as follows:

The root of C is the minimum element of the array and is labeled withits positionRemoving the root splits the array into two piecesThe left and right children of the root are recursively constructedCartesian trees of the left and right subarray

Exercise: How to construct C in linear time?

Page 15: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n),O(1)〉 solution

8 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

Solution overview:

Partition the array into blocks of size sEach block corresponds to a Cartesian Tree of size sRepresent the Cartesian Tree via LOUDS (2s− 1 bits)For each of the 22s−1 bit pattern and the s2 possible RMQ queriesstore the result in a table P („Four Fussians Trick”)I.e. P requires O(22ss2) words space

For s = log n4 P requires o(n) words of space

Page 16: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

LCA & ±1RMQ

9 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

See chalkboard presentation.

Page 17: Advanced Data Structuresalgo2.iti.kit.edu/gog/7_ads.pdf · 4 2 7 3 1 4 9 5 3 6 5 7 7 8 4 9 6 10 4 11 3 12 1 13 4 14 8 15 rmq(5,9) = 6. Range minimum queries (RMQs) 1 Simon Gog: Advanced

〈O(n),O(1)〉 solution using 4n + o(n)bits

10 Simon Gog:Advanced Data Structures

Institute of Theoretical InformaticsAlgorithmics

See chalkboard presentation.