data structures and algorithmsΒ Β· dynamic range minima problem given an initial sequence s =...
TRANSCRIPT
Data Structures and Algorithms (CS210A)
Semester I β 2014-15
Lecture 31 β’ Magical applications of Binary trees -II
1
RECAP OF LAST LECTURE
2
Intervals
S = {[π, π], 0 β€ π β€ π < π}
Question: Can we have a small set XβS of intervals s.t.
every interval in S can be expressed as a union of a few intervals from X ?
Answer: yes
3
0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7 8,8 9,9 β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ . [15,15]
Hierarchy of intervals
4
0,3 [4,7] 8,11 [12,15]
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
[8,15] [0,7]
[0,15]
0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7 8,8 9,9 β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ . [15,15]
Hierarchy of intervals
Observation: There are 2π intervals such that
any interval [π, π] can be expressed as union of O(log π) basic intervals
5
0,3 [4,7] 8,11 [12,15]
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
[8,15] [0,7]
[0,15]
0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7 8,8 9,9 β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ . [15,15]
Hierarchy of intervals
Observation: There are 2π intervals such that
any interval [π, π] can be expressed as union of O(log π) basic intervals
6
0,3 [4,7] 8,11 [12,15]
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
[8,15] [0,7]
[0,15]
0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7 8,8 9,9 β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ . [15,15]
Relation to a sequence ?
Which data structure emerges ?
7
π₯0 π₯1 π₯2 π₯3 π₯4 π₯5 π₯6 π₯7 π₯8 π₯9 π₯10 π₯11 π₯12 π₯13 π₯14 π₯15
0,3 [4,7] 8,11 [12,15]
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
[8,15] [0,7]
[0,15]
0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7 8,8 9,9 β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ . [15,15]
Sequence
How to perform Operation on an interval ?
8
π₯0 π₯1 π₯2 π₯3 π₯4 π₯5 π₯6 π₯7 π₯8 π₯9 π₯10 π₯11 π₯12 π₯13 π₯14 π₯15
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
0,3 [4,7] 8,11 [12,15]
[0,7] [8,15]
[0,15]
What value should you keep in internal nodes ?
A Binary tree
How to perform Operation on an interval ?
9
π₯0 π₯1 π₯2 π₯3 π₯4 π₯5 π₯6 π₯7 π₯8 π₯9 π₯10 π₯11 π₯12 π₯13 π₯14 π₯15
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
0,3 [4,7] 8,11 [12,15]
[0,7] [8,15]
[0,15]
Problem 2
10
Dynamic Range-minima
Dynamic Range Minima Problem
Given an initial sequence S = βΊ π₯0, β¦, π₯π;1 β» of numbers, maintain a compact data structure to perform the following operations efficiently for any 0 β€ π < π < π.
β’ ReportMin(π, π):
Report the minimum element from {π₯π | for each π β€ π β€ π}
β’ Update(π, a):
a becomes the new value of π₯π.
Example:
Let the initial sequence be S = βΊ 14, 12, 3, 49, 4, 21, 322, -40 β»
ReportMin(π, π) returns
ReportMin(π, π) returns
Update(π, 19) update S to
ReportMin(π, π) returns
ReportMin(π, π) returns
11
βΊ 14, 12, 19, 49, 4, 21, 322, -40 β»
3
3
4
12
Dynamic Range Minima Problem
Given an initial sequence S = βΊ π₯0, β¦, π₯π;1 β» of numbers, maintain a compact data structure to perform the following operations efficiently for any 0 β€ π < π < π.
β’ ReportMin(π, π):
Report the minimum element from {π₯π | for each π β€ π β€ π}
β’ Update(π, a):
a becomes the new value of π₯π.
AIM:
β’ O(π) size data structure.
β’ ReportMin(π, π) in O(log π) time.
β’ Update(π, a) in O(log π) time.
12
Hierarchy of intervals
Observation: There are 2π intervals such that
any interval [π, π] can be expressed as union of O(log π) basic intervals
13
π₯0 π₯1 π₯2 π₯3 π₯4 π₯5 π₯6 π₯7 π₯8 π₯9 π₯10 π₯11 π₯12 π₯13 π₯14 π₯15
0,3 [4,7] 8,11 [12,15]
0,1 2,3 4,5 6,7 8,9 10,11 12,13 [14,15]
[8,15] [0,7]
[0,15]
0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7 8,8 9,9 β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ . [15,15]
Data structure for dynamic range minima
Question: What should be stored in an internal node v ?
Answer: minimum value stored in subtree(v).
14
π₯0 π₯1 π₯2 π₯3 π₯4 π₯5 π₯6 π₯7 π₯8 π₯9 π₯10 π₯11 π₯12 π₯13 π₯14 π₯15
4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44 Initial
sequence
4 9 21 29 25 8 2 11
2 8 21 4
2
4 2
Data structure for dynamic range minima
How to do Report-Min(2,10) ?
15
π₯0 π₯1 π₯2 π₯3 π₯4 π₯5 π₯6 π₯7 π₯8 π₯9 π₯10 π₯11 π₯12 π₯13 π₯14 π₯15
4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44 Initial
sequence
2
4 2
2 8 21 4
4 9 21 29 25 8 2 11
Data structure for dynamic range minima
How to do Report-Min(2,10) ?
16
π₯0 π₯1 π₯2 π₯3 π₯4 π₯5 π₯6 π₯7 π₯8 π₯9 π₯10 π₯11 π₯12 π₯13 π₯14 π₯15
4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44 Initial
sequence
2
4 2
2 8 21 4
4 9 21 29 25 8 2 11
Data structure for dynamic range minima
Data structure: An array A of size 2n-1.
Copy the sequence S = βΊ π₯0, β¦, π₯π;1 β» into ??
Leaf node corresponding to π₯π = ??
How to check if a node is left child or right child of its parent ?
(if index of the node is odd, then the node is left child, else the node is right child) 17
0
1 2
3 4 5 6
7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
A[(π β 1) + π]
A[π β 1]β¦A[2π β 2]
4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44 Initial
sequence
4 9 21 29 25 8 2 11
2 8 21 4
4 2
2
Update(π, π)
Update(π, π)
π (π β π) + π ;
A[π] π ;
π (π β π)/π ;
While( ?? )
{
}
18
Homework
Report-Min(π,π) Report-Min(π,π)
π (π β π) + π ;
π π β π + π ;
min A(π);
If (π > π)
{ If (A(π)< min) min A(π);
While( ?? )
{
If(π%2=1 and A(π + π)< min) min A(π + π);
If(π%2=0 and A(π β π)< min) min A(π β π);
π (π β 1)/2 ;
π (π β 1)/2 ;
}
}
return min;
21
(π β 1)/2 <> (π β 1)/2
Proof of correctness
Let T be the tree data structure for Dynamic Range-minima problem.
Let u be any node in T.
Question:
What can we say about value(u) after a series of operations ?
Answer:
After every operation:
value(u) is minimum among all values stored in the leaf nodes of subtree(u).
23
Another interesting problem on sequences
24
Practice Problem Given an initial sequence S = βΊ π₯0, β¦, π₯π;1 β» of π numbers,
maintain a compact data structure to perform the following operations efficiently :
β’ Report_min(π , π):
Report the minimum element from {π₯π , β¦ π₯_π}.
β’ Multi-Increment(π, π, β):
Add β to each π₯π for each π β€ π β€ π
Example:
Let the initial sequence be S = βΊ 14, 12, 3, 12, 111, 51, 321, -40 β»
Report_min(π , π):
returns 3
Multi-Increment(2,6,10):
S becomes βΊ 14, 12, 13, 22, 121, 61, 331, -40 β»
Report_min(π , π):
returns 12
25
An challenging problem on sequences
26
For winter vacation
(not for the exam)
β Problem Given an initial sequence S = βΊ π₯0, β¦, π₯π;1 β» of π numbers,
maintain a compact data structure to perform the following operations efficiently :
β’ Report_min(π , π):
Report the minimum element from {π₯π , β¦ π₯_π}.
β’ Multi-Increment(π, π, β):
Add β to each π₯π for each π β€ π β€ π
β’ Rotate(π , π):
π₯π β π₯π , π₯π:1 β π₯π;1, β¦.
Example:
Let the initial sequence be S = βΊ 14, 12, 23, 19, 111, 51, 321, -40 β»
After Rotate(1,6), S becomes
βΊ 14, 321, 51, 111, 19, 23, 12, -40 β»
27
Problem 4
28
A data structure for sets
Sets under operations Given: a collection of π singleton sets {π}, {π}, {π}, β¦ {π β π}
Aim: a compact data structure to perform
β’ Union(π, π):
Unite the two sets containing π and π.
β’ Same_sets(π, π):
Determine if π and π belong to the same set.
Trivial Solution 1
Keep an array Label[] such that
Label[π]=Label[π] if and only if π and π belong to the same set.
Same_sets(π, π):
check if Label[π]=Label[π] ?
Union(π, π):
For each π β€ π < π
if ( ? ) ? 29
O(1) time
O(π) time
Label[π]= Label[π] Label[π] Label[π])
Sets under operations Given: a collection of π singleton sets {π}, {π}, {π}, β¦ {π β π}
Aim: a compact data structure to perform
β’ Union(π, π):
Unite the two sets containing π and π.
β’ Same_sets(π, π):
Determine if π and π belong to the same set.
Trivial Solution 2
Treat the problem as a graph problem: ??
β’ V = {0,β¦, π β π}, E = empty set initially.
β’ A set a connected component.
β’ Keep array Label[] such that Label[π]=Label[π] iff π and π belong to the same component.
Union(π, π) :
add an edge (π, π) and
recompute connected components using BFS/DFS.
30
Connected component
O(π) time
Sets under operations Given: a collection of π singleton sets {π}, {π}, {π}, β¦ {π β π}
Aim: a compact data structure to perform
β’ Union(π, π):
Unite the two sets containing π and π.
β’ Same_sets(π, π):
Determine if π and π belong to the same set.
Efficient solution:
β’ A data structure which supports each operation in O(log π) time.
β’ An additional heuristic
time complexity of an operation : practically O(1).
31