Succinct Data Structures
Auto-completion as our target application
Rossano Venturini
[email protected]
Dataset?
Dataset? All the past queries
Dataset?
Searches?
All the past queries
Dataset?
Prefix search Searches?
All the past queries
Dataset?
Prefix search Searches?
All the past queries
Data structure?
Dataset?
Prefix search Searches?
All the past queries
Data structure? Trie
Dataset?
Prefix search Searches?
All the past queries Data structure? Trie
How to find top-k efficiently?
Trie
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) }
Trie
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
Trie
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
Trie
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
Trie
O(n) nodes
O(n log n + m log σ) bits of space
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
Trie
O(n) nodes
O(n log n + m log σ) bits of space Find all the strings prefixed by
any pattern P in O(|P|) time
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
Trie
O(n) nodes
O(n log n + m log σ) bits of space Find all the strings prefixed by
any pattern P in O(|P|) time
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
Trie
O(n) nodes
O(n log n + m log σ) bits of space Find all the strings prefixed by
any pattern P in O(|P|) time
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Scan to find the maximum!
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Scan to find the maximum!
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Scan to find the maximum!
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Scan to find the maximum!
O(n) query time :-(
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Scan to find the maximum!
O(n) query time :-(
Better ideas?
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Augment each node with the max (and string id )
within its subtree!
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Augment each node with the max (and string id )
within its subtree!
4,3
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Augment each node with the max (and string id )
within its subtree!
4,3 6,5
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Augment each node with the max (and string id )
within its subtree!
4,3 6,5
6,5
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Augment each node with the max (and string id )
within its subtree!
4,3 6,5
6,5 2,1
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Augment each node with the max (and string id )
within its subtree!
4,3 6,5
6,5 2,1
7,0
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Augment each node with the max (and string id )
within its subtree!
4,3 6,5
6,5 2,1
7,0
Preprocessing time: O(n)
!
Extra space: O(n log n) bits
!
Query time: O(1)
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Augment each node with the max (and string id )
within its subtree!
4,3 6,5
6,5 2,1
7,0
Preprocessing time: O(n)
!
Extra space: O(n log n) bits
!
Query time: O(1)
Solving Top-k?
!
!
!
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Augment each node with the max (and string id )
within its subtree!
4,3 6,5
6,5 2,1
7,0
Preprocessing time: O(n)
!
Extra space: O(n log n) bits
!
Query time: O(1)
Solving Top-k?
!
!
!
Finding Top-1
Solving Top-k?
!
- Extra space: O(k*n*log n) bits :-(
- You must know k at building time! :-(
n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) }
n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
7 2 1 4 1 6 2
S
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) }
n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Assume you have a Data Structure on top of S answering in O(1) by using O(n) bits
!
RMQ(i,j) = position of the maximum in the
range S[i,j]
S
7 2 1 4 1 6 2Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) }
n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Assume you have a Data Structure on top of S answering in O(1) by using O(n) bits
!
RMQ(i,j) = position of the maximum in the
range S[i,j]
S
7 2 1 4 1 6 2Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) }
n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Assume you have a Data Structure on top of S answering in O(1) by using O(n) bits
!
RMQ(i,j) = position of the maximum in the
range S[i,j]
S
7 2 1 4 1 6 2RMQ(3,6) = 5
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) }
n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Assume you have a Data Structure on top of S answering in O(1) by using O(n) bits
!
RMQ(i,j) = position of the maximum in the
range S[i,j]
S
7 2 1 4 1 6 2RMQ(3,6) = 5 Can you solve Top-2?
Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) }
n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Assume you have a Data Structure on top of S answering in O(1) by using O(n) bits
!
RMQ(i,j) = position of the maximum in the
range S[i,j] Can you solve Top-2?
S
7 2 1 4 1 6 2Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) }
n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
P = c
How to find Top-1?
Assume you have a Data Structure on top of S answering in O(1) by using O(n) bits
!
RMQ(i,j) = position of the maximum in the
range S[i,j] Can you solve Top-2?
S
7 2 1 4 1 6 2Finding Top-1
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) }
Finding Top-k
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
5
3
6
1 1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
It can be built top-down with RMQ
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
Results 1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
Results 1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
10
Results 1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
Results 1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
Results
10
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
Results
10
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
10
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
9
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
9 8
7
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
9 8
7 7
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
9 8
7
8 7
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
9 8
7
8 77
7
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
9 8
7
8 77
7 7
7
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
9 8
7
8 77
7 7
7
Claim: we “touch” at most 2k nodes.
Query time O(k log k) 1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
9 8
7
8 77
7 7
7
Claim: we “touch” at most 2k nodes.
Query time O(k log k)
Important: the cartesian tree is not built!
1
Finding Top-k
3 5 1 7 1 6 10
S
… 9 8 7 1 4 2 …10
7 9
8 5
3
6
1 7
4
1 2
Cartesian Tree
How to find Top-k?
Visit the node starting from the root and try to insert each visited node in a
max-Heap storing at most k elements.
!
Extract (and report) the maximum from the heap and visit its children.
max-Heap k=4
9 7
Results
7 10
9 8
7
8 77
7 7
7
Claim: we “touch” at most 2k nodes.
Query time O(k log k)
Important: the cartesian tree is not built!
1 Assume you have a Data Structure on top of S answering in O(1) by using O(n) bits
!
RMQ(i,j) = position of the maximum in the range S[i,j]
Range Maximum Query (1)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Range Maximum Query (1)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n2 log n) bits Query time: O(1)
Range Maximum Query (1)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n2 log n) bits Query time: O(1)
Precompute the answer to any possible query.
!
There are O(n2) distinct queries!
Range Maximum Query (1)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n2 log n) bits
Query time: O(1)
M[i,j] = RMQ(i,j)
Precompute the answer to any possible query.
!
There are O(n2) distinct queries!
Range Maximum Query (1)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n2 log n) bits Query time: O(1)
M
0 1 2 3 4 5 6 7 8 9 10 110 1
5 2 3 4
6 7 8 9 10 11
M[i,j] = RMQ(i,j)
Precompute the answer to any possible query.
!
There are O(n2) distinct queries!
Range Maximum Query (1)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n2 log n) bits Query time: O(1)
M
0 1 2 3 4 5 6 7 8 9 10 110 1
5 2 3 4
6 7 8 9 10 11
M[i,j] = RMQ(i,j)
Precompute the answer to any possible query.
!
There are O(n2) distinct queries!
3
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits Query time: O(1)
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits Query time: O(1)
Maximum in a interval is the
max between the maxima of any its subintervals
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits Query time: O(1)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
Maximum in a interval is the
max between the maxima of any its subintervals
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
Maximum in a interval is the
max between the maxima of any its subintervals
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
?
Maximum in a interval is the
max between the maxima of any its subintervals
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
?
Maximum in a interval is the
max between the maxima of any its subintervals
9=1+23
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
?
Maximum in a interval is the
max between the maxima of any its subintervals
9=1+23
6
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
Maximum of a interval is the
max between the maxima of any its subintervals
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
Maximum of a interval is the
max between the maxima of any its subintervals
RMQ(1,7) =
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
Maximum of a interval is the
max between the maxima of any its subintervals
argmax(S[M[1,1+22]], S[M[7-22,7]]) = 6 RMQ(1,7) =
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
Maximum of a interval is the
max between the maxima of any its subintervals
argmax(S[M[1,1+22]], S[M[7-22,7]]) = 6 RMQ(1,7) =
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
3
Maximum of a interval is the
max between the maxima of any its subintervals
argmax(S[M[1,1+22]], S[M[7-22,7]]) = 6 RMQ(1,7) =
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
3
Maximum of a interval is the
max between the maxima of any its subintervals
argmax(S[M[1,1+22]], S[M[7-22,7]]) = 6 RMQ(1,7) =
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
3
Maximum of a interval is the
max between the maxima of any its subintervals
argmax(S[M[1,1+22]], S[M[7-22,7]]) = 6 RMQ(1,7) =
6
Range Maximum Query (2)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Space: O(n log2 n) bits
Query time: O(1)
M[i,j] = RMQ(i,i+2
j)
Precompute the answer to every interval of size a power of 2.
!
There are O(log n) possible
intervals starting at any position i.
M
0 1 2 3 40 1
5 2 3 4
6 7 8 9 10 11
3
Maximum of a interval is the
max between the maxima of any its subintervals
argmax(S[M[1,1+22]], S[M[7-22,7]]) = 6 RMQ(1,7) =
where len = log (j-i+1)
RMQ(i,j) = argmax(S[M[i,i+2len]], S[M[j-2len,j]])
6
Range Maximum Query (3)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
RMQ(1,10) = ?
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
RMQ(1,10) = ?
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
RMQ(1,10) = ?
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
RMQ(1,10) = ?
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
RMQ(1,10) = ?
O(1) time
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
RMQ(1,10) = ?
O(1) time
O(log n) time O(log n) time
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
RMQ(1,10) = ?
O(1) time
O(log n) time O(log n) time
Space: O(n log n) bits Query time: O(1)
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
RMQ(1,10) = ?
O(1) time
O(log n) time O(log n) time
Space: O(n log n) bits Query time: O(1)
O(1) time O(1) time
Range Maximum Query (3)
Space: O(n log n) bits Query time: O(log n)
3 5 1 7 1 6 10
S
9 8 7 1 40 1 2 3 4 5 6 7 8 9 10 11
log n
R
5 7 10 7Use the previous solution on R!
Space: ? bits Query time: O(1)
Space: O(n log n) bits Query time: O(1)
RMQ(1,10) = ?
O(1) time
O(log n) time O(log n) time
Space: O(n log n) bits Query time: O(1)
O(1) time O(1) time
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
Summary
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
Summary
Find the node “prefixed” by P
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
Summary
Find the node “prefixed” by P O(|P|) time
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
Summary
Find the node “prefixed” by P O(|P|) time O(m log σ + n log m) bits
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
Summary
Find the node “prefixed” by P O(|P|) time O(m log σ + n log m) bits
Compute the top-k strings
D = { ab (7), bab (2), bca (1), cab (4), cac (1), cbac (6), cbba (2) } n = |D|, m total length of strings in D
7
2 1
4 1 6 2
ab b
ab ca
c
a b
ba ac
b c
Summary
Find the node “prefixed” by P O(|P|) time O(m log σ + n log m) bits
Compute the top-k strings O(k log k) time