0% found this document useful (0 votes)
43 views30 pages

Data Structures and Algorithms: (CS210/ESO207/ESO211)

This lecture discusses efficient solutions to two problems involving sequences using binary trees. It presents a non-trivial solution to the first problem of multi-incrementing elements in a sequence range. The solution represents any sequence as a union of intervals in a binary tree to allow incrementing a range in O(log n) time instead of O(n) for the trivial array solution. It explains how to express any interval as a union of O(log n) basic intervals to efficiently update the sequence.

Uploaded by

Moazzam Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views30 pages

Data Structures and Algorithms: (CS210/ESO207/ESO211)

This lecture discusses efficient solutions to two problems involving sequences using binary trees. It presents a non-trivial solution to the first problem of multi-incrementing elements in a sequence range. The solution represents any sequence as a union of intervals in a binary tree to allow incrementing a range in O(log n) time instead of O(n) for the trivial array solution. It explains how to express any interval as a union of O(log n) basic intervals to efficiently update the sequence.

Uploaded by

Moazzam Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Data Structures and Algorithms

(CS210/ESO207/ESO211)
Lecture 27

Miscellaneous application of binary trees

1
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
Two interesting problems on sequences
3
What is a sequence ?



A sequence S =
0
, ,
1

Can be viewed as a mapping from [0, ].
Order does matter.

4
Problem 1
5
Multi-increment
Problem 1
Given an initial sequence S =
0
, ,
1
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

Example:
Let the initial sequence be S = 14, 12, 23, 12, 111, 51, 321, -40
After Multi-Increment(2,6,10), S becomes
14, 12, 33, 22, 121, 61, 331, -40
After Multi-Increment(0,4,25), S becomes
39, 37, 58, 47, 146, 61, 331, -40
After Multi-Increment(2,5,31), S becomes
39, 37, 89, 78, 177, 92, 331, -40
6
Problem 1
Given an initial sequence S =
0
, ,
1
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)
Problem 1
Given an initial sequence S =
0
, ,
1
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
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
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 be
expressed 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 a
union of very small subset of I.

You are encouraged to explore how this idea might lead to efficient implementation of
Multi-Increment(, , ).

10
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

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]
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

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]
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

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]
c
A small set of intervals






Expressing an interval [, ] as union of a small set of intervals:
For general intervals [, ].
EXAMPLE: [3, 11]
14

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]
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

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]
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,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 keep
in leaf nodes initially ?
What value should you keep
in internal nodes initially ?
4 14 9 17 23 21 29 91 37 25 8 33 2 67 11 44
Initial
sequence
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,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
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,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
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,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
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,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
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,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
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
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 tree
data structure for these two algorithms?
Exploiting complete binary tree structure






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)
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]
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
Report()
Report( )
( ) + ;
val 0;
While( ?? )
{
val val + A[];
( 1)/2 ;
}
return val;


26
> 0
The solution of Multi-Increment Problem




Theorem: There exists a data structure of size O() for maintaining a
sequence S =
0
, ,
1
such that each Multi-Increment() and Report()
operation takes O(log ) time.

27
Problem 2
28
Dynamic Range-minima
Problem 2

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.






29
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

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

You might also like