lecture-27-cs210-2012.pptx
TRANSCRIPT
-
8/11/2019 Lecture-27-CS210-2012.pptx
1/30
Data Structures and Algorithms(CS210/ESO207/ESO211)
Lecture 27 Miscellaneous application of binary trees
1
-
8/11/2019 Lecture-27-CS210-2012.pptx
2/30
Outline of the lecture
Elegant solution for two interesting problem
An important lesson :
Lack of proper understanding of a problem is a big hurdle to solve the problem
2
-
8/11/2019 Lecture-27-CS210-2012.pptx
3/30
Two interesting problems on sequences
3
-
8/11/2019 Lecture-27-CS210-2012.pptx
4/30
What is a sequence ?
A sequence S = , , Can be viewed as a mapping from [ 0, ]. Order does matter.
4
-
8/11/2019 Lecture-27-CS210-2012.pptx
5/30
Problem 1
5
Multi-increment
-
8/11/2019 Lecture-27-CS210-2012.pptx
6/30
-
8/11/2019 Lecture-27-CS210-2012.pptx
7/30
Problem 1Given an initial sequence S = , ,
of numbers, maintain a compact data
structure to perform the following operations efficiently for any 0 < < . ReportElement ( ):
Report the current value of . Multi-Increment ( , , ):
Add to each for each
Trivial solution :
Store S in an array A[0.. -1] such that A[ ] stores the current value of .Multi-Increment ( , , )
{For ( ) A[ ] A[ ]+ ;
}
ReportElement ( ){ return A[ ] }
7
O( ) = O( )
O(1)
-
8/11/2019 Lecture-27-CS210-2012.pptx
8/30
Problem 1Given an initial sequence S = , ,
of numbers, maintain a compact data
structure to perform the following operations efficiently for any 0 < < . ReportElement ( ):
Report the current value of . Multi-Increment ( , , ):
Add to each for each
Trivial solution :
Store S in an array A[0.. -1] such that A[ ] stores the current value of .
Question: the source of difficulty in breaking the O (n) barrier for Multi-Increment () ?Answer: we need to explicitly maintain in S.
Question: who asked/inspired us to maintain S explicitly.Answer: 1. incomplete understanding of the problem
2. conditioning based on incomplete understanding 8
-
8/11/2019 Lecture-27-CS210-2012.pptx
9/30
Towards efficient solution of Problem 1
Assumption: without loss of generality assume is power of 2.
Explore ways to maintain sequence S implicitly such that Multi-Increment ( , , ) is efficient Report ( ) is efficient too.
Main hurdle: To perform Multi-Increment ( , , ) efficiently
9
-
8/11/2019 Lecture-27-CS210-2012.pptx
10/30
How to perform Multi-Increment ( , , ) efficiently ?
A simple idea: Any positive integer can be expressed as a sum of powers of 2.Inference: an O(log n) size set A such that any arbitrary integer upto n can beexpressed as a sum of numbers from A.
Key idea for our problem:
a small set of intervals I such that every interval can be expressed as aunion of very small subset of I.
You are encouraged to explore how this idea might lead to efficient implementation ofMulti-Increment ( , , ).
10
-
8/11/2019 Lecture-27-CS210-2012.pptx
11/30
A small set of intervals
Expressing an interval [ , ] as union of a small set of intervals:For intervals [ , ]with = 0.EXAMPLE: [0 , 12 ]
11
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]
-
8/11/2019 Lecture-27-CS210-2012.pptx
12/30
A small set of intervals
Expressing an interval [ , ] as union of a small set of intervals:For intervals [ , ]with = 0.EXAMPLE: [0 , 12 ]
12
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]
-
8/11/2019 Lecture-27-CS210-2012.pptx
13/30
A small set of intervals
Expressing an interval [ , ] as union of a small set of intervals:For intervals [ , ]with = 0.EXAMPLE: [0 , 12 ]
13
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]
c
-
8/11/2019 Lecture-27-CS210-2012.pptx
14/30
A small set of intervals
Expressing an interval [ , ] as union of a small set of intervals:For general intervals [ , ].EXAMPLE: [3 , 11 ]
14
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]
-
8/11/2019 Lecture-27-CS210-2012.pptx
15/30
Multi-Increment ( , , ) efficiently
Observation: Any interval [ , ] can be expressed as union of O(log n ) basic intervals.Question: What data structure to use ?
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]
-
8/11/2019 Lecture-27-CS210-2012.pptx
16/30
Data Structure for efficient Multi-Increment ( , , )
How to do Multi-Increment (3,11 ,10 ) ?
16
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]
0 0 0 0 0 0 0 0
0 0 0 0
0 0
0
What value should you keepin leaf nodes initially ?
What value should you keepin internal nodes initially ?
4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44Initial
sequence
-
8/11/2019 Lecture-27-CS210-2012.pptx
17/30
Data Structure for efficient Multi-Increment ( , , )
How to do Multi-Increment (3,11 ,10 ) ?
17
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]
4 14 9 27 23 21 29 91 37 25 8 43 2 67 11 44
0 0 0 0 0 0 0 0
0 0 0 0
0 0
0
-
8/11/2019 Lecture-27-CS210-2012.pptx
18/30
Data Structure for efficient Multi-Increment ( , , )
How to do Multi-Increment (3,11 ,10 ) ?
18
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]
4 14 9 27 23 21 29 91 37 25 18 43 2 67 11 44
0 0 0 0 0 0 0 0
0 0 0 0
0 0
0
-
8/11/2019 Lecture-27-CS210-2012.pptx
19/30
Data Structure for efficient Multi-Increment ( , , )
How to do Multi-Increment (3,11 ,10 ) ?
19
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]
4 14 9 27 23 21 29 91 37 25 18 43 2 67 11 44
0 0 0 0 10 0 0 0
0 0 0 0
0 0
0
-
8/11/2019 Lecture-27-CS210-2012.pptx
20/30
Data Structure for efficient Multi-Increment ( , , )
How to do Multi-Increment (3,11 ,10 ) ?
20
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]
4 14 9 27 23 21 29 91 37 25 18 43 2 67 11 44
0 0 0 0 10 0 0 0
0 10 0 0
0 0
0
-
8/11/2019 Lecture-27-CS210-2012.pptx
21/30
Data Structure for efficient Multi-Increment ( , , )
How to do Multi-Increment (3,11 ,10 ) ?
21
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]
4 14 9 27 23 21 29 91 37 25 18 43 2 67 11 44
0 0 0 0 10 0 0 0
0 10 0 0
0 0
0
-
8/11/2019 Lecture-27-CS210-2012.pptx
22/30
Multi-Increment ( , , ) efficiently
Sketch:
1. Let u and v be the leaf nodes corresponding to and .
2. Increment the value stored at u and v.
3. Keep repeating the following step as long as parent (u ) parent (v)Move up by one step simultaneously from u and v - If u is left child of its parent, increment value stored in sibling of u .
- If v is right child of its parent, increment value stored in sibling of v.
22
-
8/11/2019 Lecture-27-CS210-2012.pptx
23/30
Executing Report ( ) efficiently
Sketch:
1. Let u be the leaf nodes corresponding to .2. val 0;3. Keep moving up from u and keep adding the value of all the nodes on the
path to the root to val .4. Return val .
23
What is an efficient implementation of the treedata structure for these two algorithms?
-
8/11/2019 Lecture-27-CS210-2012.pptx
24/30
Exploiting complete binary tree structure
Data structure: An array A of size 2n -1.Copy the sequence S = , , 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) 24
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 ]
-
8/11/2019 Lecture-27-CS210-2012.pptx
25/30
MultiIncrement( , , ) MultiIncrement( , , )
( ) + ; ( ) + ;A( ) A( ) + ;If ( > )
{ A( ) A( ) + ;While( ?? ){
If ( %2=1) A( + ) A( + ) + ;If ( %2=0) A( ) A( ) + ; ( 1)/ 2 ;
( 1)/ 2 ;}
}25
( 1)/ 2 ( 1)/ 2
-
8/11/2019 Lecture-27-CS210-2012.pptx
26/30
Report( )
Report( ) ( ) + ;
val 0;While( ?? ){
val val + A[ ]; ( 1)/ 2 ;
}
return val ;
26
> 0
-
8/11/2019 Lecture-27-CS210-2012.pptx
27/30
The solution of Multi-Increment Problem
Theorem: There exists a data structure of size O( ) for maintaining asequence S = , , such that each Multi-Increment () and Report ()operation takes O(log ) time.
27
-
8/11/2019 Lecture-27-CS210-2012.pptx
28/30
Problem 2
28
Dynamic Range-minima
-
8/11/2019 Lecture-27-CS210-2012.pptx
29/30
Problem 2
Given an initial sequence S = , , of numbers, maintain a compact datastructure 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.
29
-
8/11/2019 Lecture-27-CS210-2012.pptx
30/30
Efficient dynamic range minima
What to store at internal nodes ?How to perform ReportMin ( , ) ?How to perform Update ( , a ) ?
Make sincere attempts to solve the problem. We shall discuss it in next class 30