Professional Documents
Culture Documents
Lec07 Range Minima
Lec07 Range Minima
Lec07 Range Minima
(ESO207)
Lecture 7:
• Data structure for Range-minima problem
Compact and fast
1
Data structures
AIM:
To organize a data in the memory
so that any query can be answered efficiently.
Example:
Data: A set S of numbers
Query: “Is a number present in S ?”
Important assumption:
No. of queries to be answered will be …
many.
Parameters of Efficiency
• Query time
• Space
• Preprocessing time
3
RANGE-MINIMA Problem
4
Range-Minima Problem
Given: an array A storing numbers,
Aim: a data structure to answer a sequence of queries of the following type
Range-minima() : report the smallest element from A[],…,A[]
Range-Minima() = -6
A 3 5 1 8 19 0 -1 30 99 -6 10 2 40 27 44 67
=4 =11
Range-Minima Problem
Size of B is too large to be kept in RAM.
Solution 1 So we shall haveSolution
to keep most2of it in
the Hard disk drive.
(brute force) (store
Hence it will take a fewallmilliseconds
answers) per
𝒋
query.
Range-minima-trivial()
{ temp ;
min A[]; 𝒊 3
While(temp <= )
{ if (min > A[temp])
min A[temp];
temp temp+1; B
}
return min
}
Time complexity for one query: O() Space : O()
(a few hours for 10 million queries) Impractical 6
Range-Minima Problem
Query:
Report_min(A,) : report smallest element from {A[],…,A[]}
𝟏 𝒊 𝒋 𝒏
A 3.1 29 99 781 41.5 67.4
Aim :
• compact data structure
• O() Query time for any ≤ < ≤ .
Why does O() bound on
space appear so hard to break
if we want O(1) query time?
8
Artificial hurdle
9
Artificial hurdle
𝒊 -1
A 3.1 29 99 781 41.5 67.4
… If we fix the first parameter for all queries, we need O() space. True Fact
11
Range-minima problem:
Breaking the O() barrier using collaboration
An Overview:
12
HOW DOES COLLABORATION WORK
IN THIS PROBLEM ?
13
Range-minima problem:
Breaking the O() barrier using collaboration
We may use the tiny data
structure of index to
answer Range-Minima()
𝒊 𝒋
A
𝟏 𝒊 𝒒 𝒏
stores answers for this range stores answers for this range
DETAILS OF TINY DATA STRUCTURES
15
Range-minima problem :
Details of tiny data structure stored at each
𝟐𝒕
8
4
2
1
A
1 𝒊 𝒏
𝒌 +𝟏
𝟐
𝒌
𝟐
A
1 𝒊 𝒋 𝒏
𝒌
𝒋 −𝟐
We shall use two additional arrays
Definition :
Power-of-2[] : the greatest number of the form such that ≤ .
Examples: Power-of-2[5] = 4,
Power-of-2[19]= 16,
Power-of-2[32]=32.
Definition :
Log[] : the greatest integer such that ≤ .
Examples: Log[5] = 2,
Log[19]= 4,
Log[32]=5.
19
Range-Minima Problem:
Data structure with O( log ) space and O(1) query time
Data Structure:
– × log matrix B where B[][] stores … minimum of {A[],A[+1],…, A[+]}
– Array Power-of-2[]
– Array Log[] 0 1 𝐥𝐨 𝐠 𝟐 𝒏
0
Range-minima-() 1
{ L;
Power-of-2[L]; …
Log[L]; B
If ( = L) return ??;B[][];
else return min( B[][]
?? , ??
B[][]; ); 𝒏− 𝟏
}
20
Theorem:
There is a data structure for range-minima problem that takes
O( log ) space and O(1) query time.
Preprocessing time:
O( log ) : Trivial
O( log ) : Doable with little hints
21
Homework:
Design an O( log ) time algorithm
to build the × log matrix B used in data structure of Range-Minima problem.
To compute B[][], you need to know only two entries from column **.
22
Data structures
(To be discussed in the course)
• Arrays
• Linked Lists
Elementary
• Stacks
• Queues