0 tayangan

Diunggah oleh Vishal Golcha

- Design Table
- FPTree
- 6 Back Tracking
- AI Architectures
- Discrete Mathematics Asssignment 3
- Party Kit
- Sede2016 Pub
- DecisionTreePrimer-3
- Report Study on Cooperative Data Dissemination_153903015.pdf
- 6.02.AVL_trees
- p340-yi
- Prac Final
- Changes
- 11-Trees_1pps.pdf
- IOSR Journals
- How to Use the BOL Tree
- Data Structure
- Binary.search.tree.c
- Web Data Extraction Applications Survey
- Comparative Report on Programming Languages 2

Anda di halaman 1dari 240

Part Two

problem is the following:

Given a fixed array A and two indices

ij, what is the smallest element out of

A[i], A[i + 1], , A[j 1], A[j]?

31

41

59

26

53

58

97

93

Some Notation

complexity p(n),q(n) if

runtimes:

Blocking Revisited

31

31

41

59

26

26

53

58

97

23

93

23

84

62

62

64

33

27

83

27

Blocking Revisited

This

Thisisisjust

justRMQ

RMQon

on

the

theblock

blockminimums!

minimums!

31

31

41

59

26

26

53

58

97

23

93

23

84

62

62

64

33

27

83

27

Blocking Revisited

31

31

41

59

26

26

53

58

97

23

93

23

84

This

Thisisisjust

justRMQ

RMQ

inside

insidethe

theblocks!

blocks!

62

62

64

33

27

83

27

The Framework

for the block minimums and a p(n),q(n)-time

RMQ solution within each block.

O(n + p(n / b) + (n / b) p(b))

O(q(n / b) + q(b))

31

31

41

59

26

26

53

58

97

23

93

23

84

62

62

64

33

27

83

27

A Useful Observation

O(n log n).

If we use a sparse table as a top

structure, construction time is

O((n/b)log n).

then the construction time is O(n).

Yes!

An Observation

O(n + p(n / b) + (n / b) p(b))

O(q(n / b) + q(b))

p(n) = O(n) and q(n) = O(1).

We can't build an optimal solution out of

the hybrid approach unless we already

have one!

Or can we?

A Key Difference

Solve RMQ on a single array in time

O(n), O(1)

Solve RMQ on a large number of small

arrays with O(1) query time and average

preprocessing time O(n).

Question: Why is this second problem any

easier than the first?

An Observation

10

30

20

40

166

361

261

464

An Observation

10

30

20

40

166

361

261

464

An Observation

10

30

20

40

166

361

261

464

An Observation

10

30

20

40

166

361

261

464

An Observation

10

30

20

40

166

361

261

464

An Observation

10

30

20

40

166

361

261

464

An Observation

10

30

20

40

166

361

261

464

An Observation

10

30

20

40

166

361

261

464

An Observation

10

30

20

40

166

361

261

464

An Observation

10

30

20

40

166

361

261

464

Claim:

Claim:The

Theindices

indicesof

ofthe

the

answers

answersto

toany

anyrange

range

minimum

minimumqueries

querieson

onthese

these

two

twoarrays

arraysare

arethe

thesame.

same.

Modifying RMQ

RMQA(i, j) denote the index of the

minimum value in the range rather than

the value itself.

Observation: If RMQ structures return

indices rather than values, we can use a

single RMQ structure for both of these

arrays:

10

30

20

40

166

361

261

464

Some Notation

We'll say that B and B have the same block

type (denoted B ~ B) if the following holds:

For all 0 i j < b:

RMQB(i, j) = RMQB(i, j)

the same as the RMQ answers for B.

If we precompute RMQ over B, we can reuse

that RMQ structure on B iff B~B.

An Observation

31

41

59

26

53

58

97

23

93

84

62

64

33

83

27

An Observation

31

41

59

26

53

58

97

23

93

84

62

64

33

83

27

An Observation

31

41

59

26

53

58

97

23

93

84

62

64

33

83

27

An Observation

RMQ

Structure

31

41

59

26

53

58

97

23

93

84

62

64

33

83

27

An Observation

RMQ

Structure

31

41

59

26

53

58

97

23

93

84

62

64

33

83

27

An Observation

RMQ

Structure

31

41

59

26

RMQ

Structure

53

58

97

23

93

84

62

64

33

83

27

An Observation

RMQ

Structure

31

41

59

26

RMQ

Structure

53

58

97

23

93

84

RMQ

Structure

62

64

33

83

27

works as follows. For each block:

If an RMQ structure already exists for its block type,

just use that structure.

Otherwise, compute its RMQ structure and store it

for later.

ensures we reuse RMQ structures, but

the blocks aren't so small that we can't efficiently

build the summary structure on top.

able to check whether two blocks have the

same block type.

Problem: Our formal definition of B ~ B is

defined in terms of RMQ.

have to compute RMQ structures on B and B to

decide whether they have the same block type!

two blocks have the same type?

An Initial Idea

and we're looking for the smallest value in

certain ranges, we might look at the

permutation types of the blocks.

31

41

59

16

18

27

18

28

66

73

84

12

66

26

60

22

14

72

99

27

permutation on their elements, then B ~ B.

An Initial Idea

and we're looking for the smallest value in

certain ranges, we might look at the

permutation types of the blocks.

31

1

41

2

59

3

16

2

18

3

3

1

27

2

18

1

28

3

66

1

73

2

84

3

12

3

2

1

5

2

66

3

26

2

6

1

60

3

22

1

14

2

72

2

99

3

27

1

permutation on their elements, then B ~ B.

An Initial Idea

and we're looking for the smallest value in

certain ranges, we might look at the

permutation types of the blocks.

31

1

41

2

59

3

16

2

18

3

3

1

27

2

18

1

28

3

66

1

73

2

84

3

12

3

2

1

5

2

66

3

26

2

6

1

60

3

22

1

14

2

72

2

99

3

27

1

permutation on their elements, then B ~ B.

Some Problems

Problem One: It's possible for two blocks to have

different permutations but the same block type.

All three of these blocks have the same block type

but different permutation types:

permutations of a block is b!.

b has to be absolutely minuscule for b! to be small.

Some Problems

Problem One: It's possible for two blocks to have

different permutations but the same block type.

All three of these blocks have the same block type

but different permutation types:

261 268 161 167 166 167 261 161 268 166 166 268 161 261 167

4

5

1

3

2

3

4

1

5

2

2

5

1

4

3

permutations of a block is b!.

b has to be absolutely minuscule for b! to be small.

Some Problems

Problem One: It's possible for two blocks to have

different permutations but the same block type.

All three of these blocks have the same block type

but different permutation types:

261 268 161 167 166 167 261 161 268 166 166 268 161 261 167

4

5

1

3

2

3

4

1

5

2

2

5

1

4

3

permutations of a block is b!.

Some Problems

Problem One: It's possible for two blocks to have

different permutations but the same block type.

All three of these blocks have the same block type

but different permutation types:

261 268 161 167 166 167 261 161 268 166 166 268 161 261 167

4

5

1

3

2

3

4

1

5

2

2

5

1

4

3

permutations of a block is b!.

An Observation

and B must occur at the same position.

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

75

35

80

85

83

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

75

35

80

85

83

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

75

35

80

85

83

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

75

35

80

85

83

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

14

22

11

43

35

the subarrays to the left and right of the

minimum.

An Observation

and B must occur at the same position.

14

22

11

43

35

the subarrays to the left and right of the

minimum.

Cartesian Trees

array and defined as follows:

For a nonempty array, the root stores the index of

the minimum value. Its left and right children are

Cartesian trees for the subarrays to the left and

right of the minimum.

2

4

0

3

14

55

22

43

11

B~B iff B and B have equal Cartesian trees.

Proof sketch:

() Induction. B and B have equal RMQs, so

corresponding ranges have the same minima.

B~B iff B and B have equal Cartesian trees.

Proof sketch:

corresponding ranges have the same minima.

B~B iff B and B have equal Cartesian trees.

Proof sketch:

corresponding ranges have the same minima.

B~B iff B and B have equal Cartesian trees.

Proof sketch:

corresponding ranges have the same minima.

B~B iff B and B have equal Cartesian trees.

Proof sketch:

corresponding ranges have the same minima.

B~B iff B and B have equal Cartesian trees.

Proof sketch:

corresponding ranges have the same minima.

B~B iff B and B have equal Cartesian trees.

Proof sketch:

corresponding ranges have the same minima.

B~B iff B and B have equal Cartesian trees.

Proof sketch:

corresponding ranges have the same minima.

B~B iff B and B have equal Cartesian trees.

Proof sketch:

recursive walk on the Cartesian tree.

B~B iff B and B have equal Cartesian trees.

Proof sketch:

31

41

recursive walk on the Cartesian tree.

59

26

53

58

97

23

93

84

33

64

62

83

27

B~B iff B and B have equal Cartesian trees.

Proof sketch:

recursive walk on the Cartesian tree.

7

3

0

14

4

10

5

1

2

31

41

59

9

6

26

53

58

97

12

8

23

93

11

84

33

64

13

62

83

27

B~B iff B and B have equal Cartesian trees.

Proof sketch:

recursive walk on the Cartesian tree.

7

3

0

14

4

10

5

1

2

31

41

59

9

6

26

53

58

97

12

8

23

93

11

84

33

64

13

62

83

27

B~B iff B and B have equal Cartesian trees.

Proof sketch:

recursive walk on the Cartesian tree.

7

3

0

14

4

10

5

1

2

31

41

59

9

6

26

53

58

97

12

8

23

93

11

84

33

64

13

62

83

27

B~B iff B and B have equal Cartesian trees.

Proof sketch:

recursive walk on the Cartesian tree.

7

3

0

14

4

10

5

1

2

31

41

59

9

6

26

53

58

97

12

8

23

93

11

84

33

64

13

62

83

27

B~B iff B and B have equal Cartesian trees.

Proof sketch:

recursive walk on the Cartesian tree.

7

3

0

14

4

10

5

1

2

31

41

59

9

6

26

53

58

97

12

8

23

93

11

84

33

64

13

62

83

27

B~B iff B and B have equal Cartesian trees.

Proof sketch:

recursive walk on the Cartesian tree.

7

3

0

14

4

10

5

1

2

31

41

59

9

6

26

53

58

97

12

8

23

93

11

84

33

64

13

62

83

27

B~B iff B and B have equal Cartesian trees.

Proof sketch:

recursive walk on the Cartesian tree.

7

3

0

14

4

10

5

1

2

31

41

59

9

6

26

53

58

97

12

8

23

93

11

84

33

64

13

62

83

27

Wednesday at 2:15PM at the start of class.

You can work individually or in pairs.

with both your names on it. You'll each earn the

same score.

If you work individually, we will grade the

problem set out of 19 points. (There are 23

possible points on the problem set).

Code and the Problem Set Policies before

starting this problem set.

Office Hours

Keith: Monday, 3:30PM 5:30PM, location

TBA.

out locations and post them on the

course website as well.

Your Questions

you be and why?

we solve with RMQ?

A

B

C

G

F

CS166

DataStructures

A B C B A D E D F D A G A

A

B

C

D

E

G

F

A B C B A D E D F D A G A

A

B

C

D

E

G

F

A B C B A D E D F D A G A

This

Thisisiscalled

calledan

anEuler

Euler

tour

of

the

tree.

We'll

tour of the tree. We'lltalk

talk

about

aboutthis

thismore

morein

inaafew

few

weeks.

weeks.

A

B

G

F

A B C B A D E D F D A G A

0

A

B

G

F

A B C B A D E D F D A G A

0

for this class?

(so we can print them to take notes on)?

Will slides be posted online after lecture?

Back to CS166!

whether B ~ B by testing whether they

have the same Cartesian tree.

How efficiently can we actually build

these trees?

Cartesian trees:

Recursively build a Cartesian tree with the

elements to the left.

Recursively build a Cartesian tree with the

elements to the right.

Return the overall tree.

identifying the minimum at whatever position it

occupies, then

recursively processing the left and right halves

on the array.

depends on where the minima are.

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

93

84

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

0

93

3

84

33

64

5

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

0

93

3

84

33

64

5

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

0

93

3

84

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

0

93

3

84

33

64

62

83

63

Observation

Observation1:

1:This

Thisnew

new

node

nodecannot

cannotend

endup

upas

asthe

the

left

leftchild

childof

ofany

anynode

nodein

inthe

the

tree.

tree.

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

0

93

3

84

Observation

Observation2:

2:This

Thisnew

new

node

nodewill

willend

endup

upon

onthe

theright

right

spine

spineof

ofthe

thetree.

tree.

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

0

93

3

84

33

64

62

83

63

Observation

Observation3:

3:Cartesian

Cartesian

trees

treesare

aremin-heaps

min-heapswith

with

respect

respectto

tothe

theelements

elementsin

in

the

theoriginal

originalarray.

array.

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

0

93

3

84

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

5

6

93

84

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

5

6

93

84

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

5

6

93

84

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

93

84

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

3

5

93

84

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

3

5

93

84

33

64

62

83

63

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

3

5

93

84

33

64

62

83

63

55

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

3

5

93

84

33

64

62

83

63

55

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

3

5

93

84

33

64

62

83

7

63

55

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

1

3

5

93

84

33

64

62

83

63

55

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

7

1

4

3

5

93

84

33

64

62

83

63

55

A Better Approach

tree over an array of length k in time O(k).

High-Level Idea: Build a Cartesian tree for the

first element, then the first two, then the first

three, then the first four, etc.

2

7

1

4

3

5

93

84

33

64

62

83

63

55

A Stack-Based Algorithm

on the right spine of the tree.

To insert a new node:

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

Set the new node's parent to

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

To insert a new node:

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

Set the new node's parent to

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

To insert a new node:

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

Set the new node's parent to

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

To insert a new node:

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

32

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

32

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

32

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

45

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

32

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

32

45

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

32

45

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

32

45

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

16

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

32

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

16

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

To insert a new node:

or the top node has a lower

value than the current value.

Set the new node's left child

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

16

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

To insert a new node:

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

16

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

16

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

16

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

18

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

16

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

16

18

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

16

18

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

16

18

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

9

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

16

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

9

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

To insert a new node:

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

9

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

To insert a new node:

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

33

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

33

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

33

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

33

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

A Stack-Based Algorithm

on the right spine of the tree.

33

or the top node has a lower

value than the current value.

16

to be the last value popped

(or null if nothing was

popped).

32

33

18

45

be the top node on the stack

(or null if the stack is empty).

Push the new node onto the

stack.

32

45

16

18

33

might have to pop everything off the stack.

the number of stack operations performed when that

node was processed.

Total number of stack operations is at most 2n.

linear time, we can test if two blocks

have the same type in linear time.

Goal: Choose a block size that's small

enough that there are duplicated blocks,

but large enough that the top-level RMQ

can be computed efficiently.

So how many Cartesian trees are there?

for an array of length b is at most 4b.

In case you're curious, the actual number is

which is roughly

for an array of length b is at most 4b.

In case you're curious, the actual number is

1 2b

b+1 b

( )

which is roughly

for an array of length b is at most 4b.

In case you're curious, the actual number is

1 2b

b+1 b

( )

which is roughly

4b

b3/2

Proof Approach

Cartesian trees is capable of producing a

Cartesian tree for every possible input

array.

Therefore, if we can count the number of

possible executions of that algorithm, we

can count the number of Cartesian trees.

Using a simple counting scheme, we can

show that there are at most 4b possible

executions.

The Insight

stack-based algorithm is uniquely determined by the

sequence of pushes and pops made on the stack.

There are at most 2b stack operations during the

execution of the algorithm: b pushes and no more than

b pops.

Can represent the execution as a 2b-bit number, where

1 means push and 0 means pop. We'll pad the end

with 0's (pretend we pop everything from the stack.)

particular block.

there are at most 4b possible Cartesian trees.

32

45

16

18

33

32

45

16

18

33

32

32

45

16

18

33

32

32

45

16

18

33

32

32

32

45

16

18

33

32

32

32

45

16

18

33

32

32

45

32

45

16

18

33

32

32

45

32

45

16

18

33

32

45

32

45

32

45

16

18

33

1 1

32

45

32

45

32

45

16

18

33

1 1

32

45

32

45

16

32

45

16

18

33

1 1

32

32

45

16

32

45

16

18

33

1 1 0

32

45

16

32

45

16

18

33

1 1 0 0

16

32

45

32

45

16

18

33

1 1 0 0

16

16

32

45

32

45

16

18

33

1 1 0 0 1

16

16

32

45

32

45

16

18

33

1 1 0 0 1

16

16

32

45

18

32

45

16

18

33

1 1 0 0 1

16

16

32

18

45

32

45

16

18

33

1 1 0 0 1

16

18

16

32

18

45

32

45

16

18

33

1 1 0 0 1 1

16

18

16

32

18

45

32

45

16

18

33

1 1 0 0 1 1

16

18

16

32

18

45

9

32

45

16

18

33

1 1 0 0 1 1

16

16

32

18

45

9

32

45

16

18

33

1 1 0 0 1 1 0

16

32

18

45

9

32

45

16

18

33

1 1 0 0 1 1 0 0

9

16

32

18

45

32

45

16

18

33

1 1 0 0 1 1 0 0

9

9

16

32

18

45

32

45

16

18

33

1 1 0 0 1 1 0 0 1

9

9

16

32

18

45

32

45

16

18

33

1 1 0 0 1 1 0 0 1

9

9

16

32

18

45

33

32

45

16

18

33

1 1 0 0 1 1 0 0 1

9

9

16

32

33

18

45

32

45

16

18

33

1 1 0 0 1 1 0 0 1

9

33

9

16

32

33

18

45

32

45

16

18

33

1 1 0 0 1 1 0 0 1 1

9

33

9

16

32

33

18

45

32

45

16

18

33

1 1 0 0 1 1 0 0 1 1

9

9

16

32

33

18

45

32

45

16

18

33

1 1 0 0 1 1 0 0 1 1 0

9

16

32

33

18

45

32

45

16

18

33

1 1 0 0 1 1 0 0 1 1 0 0

9

16

32

33

18

45

32

45

16

18

33

1 1 0 0 1 1 0 0 1 1 0 0

Cartesian tree we can just simulate the

stack!

27

18

28

18

28

45

90

45

23

53

60

28

74

71

35

27

18

28

18

28

45

90

45

23

53

60

28

74

71

35

27

18

28

27

18

28

45

90

45

23

53

60

28

74

71

35

27

18

28

27

18

28

45

90

45

23

53

60

28

74

71

35

27

1 0

18

28

18

28

45

90

45

23

53

60

28

74

71

35

27

18

28

18

1 0 1

18

28

45

90

45

23

53

60

28

74

71

35

27

18

28

18

1 0 1

18

28

45

90

45

23

53

60

28

74

71

35

27

18

28

18

1 0 1 1

18

28

28

45

90

45

23

53

60

28

74

71

35

27

18

28

18

1 0 1 1

18

28

28

45

90

45

23

53

60

28

74

71

35

27

18

28

18

1 0 1 1 0

18

28

45

90

45

23

53

60

28

74

71

35

27

18

28

18

1 0 1 1 0 1

18

18

28

45

90

45

23

53

60

28

74

71

35

27

18

28

18

1 0 1 1 0 1

18

18

28

45

90

45

23

53

60

28

74

71

35

27

18

28

18

18

1 0 1 1 0 1 1

18

28

45

28

90

45

23

53

60

28

74

71

35

27

18

28

18

18

1 0 1 1 0 1 1

18

28

45

28

90

45

23

53

60

28

74

71

35

27

18

28

18

18

18

1 0 1 1 0 1 1 1

28

45

28

90

45

45

23

53

60

28

74

71

35

27

18

28

18

18

18

1 0 1 1 0 1 1 1

28

45

28

90

45

45

23

53

60

28

74

71

35

27

18

28

18

18

28

18

1 0 1 1 0 1 1 1 1

45

28

90

45

45

23

90

53

60

28

74

71

35

27

18

28

18

18

28

18

1 0 1 1 0 1 1 1 1

45

28

90

45

45

23

90

53

60

28

74

71

35

27

18

28

18

18

28

18

1 0 1 1 0 1 1 1 1 0

45

28

90

45

45

23

53

60

28

74

71

35

27

18

28

18

18

18

28

45

28

1 0 1 1 0 1 1 1 1 0 1

90

45

45

23

45

53

60

28

74

71

35

27

18

28

18

18

18

28

45

28

1 0 1 1 0 1 1 1 1 0 1

90

45

45

23

45

53

60

28

74

71

35

27

18

28

18

18

18

28

45

28

1 0 1 1 0 1 1 1 1 0 1 0

90

45

45

23

53

60

28

74

71

35

27

18

28

18

18

18

28

45

90

28

1 0 1 1 0 1 1 1 1 0 1 0 0

45

23

53

60

28

74

71

35

27

18

28

18

18

28

45

90

18

1 0 1 1 0 1 1 1 1 0 1 0 0 0

45

23

53

60

28

74

71

35

27

18

28

18

18

18

28

45

90

45

23

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1

23

53

60

28

74

71

35

27

18

28

18

18

18

28

45

90

45

23

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1

23

53

60

28

74

71

35

27

18

28

18

18

18

28

45

23

90

45

53

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1

23

53

60

28

74

71

35

27

18

28

18

18

18

28

45

23

90

45

53

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1

23

53

60

28

74

71

35

27

18

28

18

18

18

28

45

23

90

53

45

23

60

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1

53

60

28

74

71

35

27

18

28

18

18

18

28

45

23

90

53

45

23

60

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1

53

60

28

74

71

35

27

18

28

18

18

18

28

45

23

90

45

23

53

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0

53

60

28

74

71

35

27

18

28

18

18

18

28

45

90

45

23

53

23

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0

60

28

74

71

35

27

18

28

18

18

18

28

45

23

90

45

23

53

28

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1

60

28

74

71

35

27

18

28

18

18

18

28

45

23

90

45

23

53

28

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1

60

28

74

71

35

27

18

28

18

18

18

28

45

23

90

28

45

23

53

60

74

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1

28

74

71

35

27

18

28

18

18

18

28

45

23

90

28

45

23

53

60

74

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1

28

74

71

35

27

18

28

18

18

18

28

45

23

90

45

23

53

60

28

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0

28

74

71

35

27

18

28

18

18

18

28

45

23

90

28

45

23

53

60

28

71

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1

74

71

35

27

18

28

18

18

18

28

45

23

90

28

45

23

53

60

28

71

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1

74

71

35

27

18

28

18

18

18

28

45

23

90

45

23

53

60

28

28

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0

74

71

35

27

18

28

18

18

18

28

45

23

90

28

45

23

53

60

28

74

35

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1

71

35

27

18

28

18

18

18

28

45

23

90

45

23

53

60

28

74

28

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0

71

35

27

18

28

18

18

18

28

45

90

45

23

53

60

28

74

71

23

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0

35

27

18

28

18

18

28

45

90

45

23

53

60

28

74

71

18

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0

35

27

18

28

18

28

45

90

45

23

53

60

28

74

71

35

18

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0

27

18

28

18

28

45

90

45

23

53

60

28

74

71

35

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0

27

18

28

18

28

45

90

45

23

53

60

28

74

71

35

1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0

Finishing Things Up

the Cartesian tree number of a block in time

O(b) and without actually building the tree.

Gives a simple and efficient linear-time

algorithm for testing whether two blocks have

the same block type.

And, we bounded the number of Cartesian

trees at 4b using this setup!

variation on) the following RMQ data structure.

Use a hybrid approach with block size b (we'll choose b

later), a sparse table as a top RMQ structure, and the

full precomputation data structure for the blocks.

However, make the following modifications:

index corresponds to the Cartesian tree number. Initially,

the array is empty.

When computing the RMQ for a particular block, first

compute its Cartesian tree number t.

If there's an RMQ structure for t in the array, use it.

Otherwise, compute the RMQ structure for the current

block, store it in the array and index t, then use it.

Fischer-Heun structure?

mins takes time O(n) regardless of b.

Creating the sparse table takes time O((n / b) log n).

Computing the Cartesian tree number of each block

takes time O(n) in total.

is O(4b b2):

Total runtime:

O(n + (n / b) log n + 4b b2)

The runtime is

O(n + (n / b) log n + 4b b2)

(n / b) log n = O(n)

4b b2 = n1/2 (log n)2 = o(n)

= O(n + n + n1/2 (log n)2) = O(n)

Practical Concerns

efficient; preprocessing is relatively fast.

In practice, the O(n), O(log n) hybrid is a

bit faster:

O(1) are high.

Constant factor in the hybrid approach's O(n)

and O(log n) are very low.

Wait a Minute...

numbers will fit into individual machine words!

If b = log n, then each Cartesian tree number

will have log n bits.

Cartesian tree numbers will fit into a machine

word if n fits into a machine word.

In the transdichotomous machine model, we

assume the problem size always fits into a machine

word.

the Method of Four Russians.

Idea:

Using the fact that there can only be polynomially

many different blocks of size (log n), evaluate the

blocks more efficiently than evaluating each one

independently.

Combine the results together using a top-level

structure on an input of size (n / log n).

factors off of runtimes.

Different intuitions produced different runtimes.

Build data structures out of other data structures.

Many modern data structures use other data structures

as building blocks, and it's very evident here.

See the Method of Four Russians. This trick looks like

magic the first few times you see it and shows up in lots

of places.

Explore modern data structures. This is relatively

recent data structure (2005), and I wanted to show you

that the field is still very active!

So what's next?

Next Time

Balanced Trees

B-Trees

structures around.

Data structures for storing sorted information on

disk.

Red/Black Trees

me!

- Design TableDiunggah olehvkms
- FPTreeDiunggah olehVimal Victor
- 6 Back TrackingDiunggah olehSuraj Shrestha
- AI ArchitecturesDiunggah olehdagush
- Discrete Mathematics Asssignment 3Diunggah olehChung Chee Yuen
- Party KitDiunggah olehClaudiaNila
- Sede2016 PubDiunggah olehkopa shamsu
- DecisionTreePrimer-3Diunggah olehtsar_philip2010
- Report Study on Cooperative Data Dissemination_153903015.pdfDiunggah olehWahab Kukaswadia
- 6.02.AVL_treesDiunggah olehPratyush Kshirsagar
- p340-yiDiunggah olehKlara Sess
- Prac FinalDiunggah olehMohammed Jeelan
- ChangesDiunggah olehKoby Ampah
- 11-Trees_1pps.pdfDiunggah olehClark Fan
- IOSR JournalsDiunggah olehInternational Organization of Scientific Research (IOSR)
- How to Use the BOL TreeDiunggah olehtopschat9355
- Data StructureDiunggah olehali_afzal89
- Binary.search.tree.cDiunggah olehfarhanhubble
- Web Data Extraction Applications SurveyDiunggah olehjose_dias_58
- Comparative Report on Programming Languages 2Diunggah olehGilzpo
- Data StructuresDiunggah olehYuchen
- Desalgo Preliminary Exam Reviewer 1(6)Diunggah olehRafael Nobleza Masallo
- Weil Crush Sc06Diunggah olehcypha11
- Data Structure Lec32 HandoutDiunggah olehJʋŋaiɗ Aĸɓar
- Graph and TreeDiunggah olehRiaz Mir
- XCLS++: A new algorithm to improve XCLS+ for clustering XML documentsDiunggah olehijitcajournal
- Test Bank Landmarks in Humanity 3rd EditionDiunggah olehRichard1Lauritsen
- Chevalier Iwpse2007Diunggah olehAnonymous sBIHbx7
- plan and packageDiunggah olehsushil@ird
- ConstructingELHDiunggah olehSookara Kushal

- Mipt 2014 Burunduk1.EnDiunggah olehVishal Golcha
- Assignment 1Diunggah olehVishal Golcha
- PhysicaA284p131 Grigorenko GarciaDiunggah olehVishal Golcha
- Chapt_4_Klepp_remaining.pdfDiunggah olehVishal Golcha
- Introductory Linear Algebra (an Applied First Course), 8e, Bernard KolmanDiunggah olehgxsteph
- Answers-Ch.4.pdfDiunggah olehVishal Golcha
- DataScienceWeekly DataScientistInterviews Vol1 April2014Diunggah olehclungaho7109
- Teach Like a ChampionDiunggah olehVishal Golcha
- microprocessor lab manualDiunggah olehVishal Golcha
- MATH F113 -Chapter-3.pdfDiunggah olehVishal Golcha
- GMAT HandbookDiunggah olehRach3ch
- GamesDiunggah olehVishal Golcha
- Crossword Prelims _ Gateway 2011_KeyDiunggah olehVishal Golcha
- lecture4aDiunggah olehVishal Golcha
- Practice Problem sheet Data .xlsDiunggah olehVishal Golcha
- Chapt_4_Klepp_part1.pdfDiunggah olehVishal Golcha
- MATH F113 -Chapter-4.pdfDiunggah olehVishal Golcha

- Introduction Custom GisDiunggah olehsilesjl
- Beginners Python Cheat Sheet Pcc AllDiunggah olehTelmo Joao Magalhaes Oliveira
- EE36 Data Structures and AlgorithmDiunggah olehksu2006
- Application Controlled Physical Memory using External Page Cache ManagementDiunggah olehAntoniouNikos
- COMSC-210 Lecture Topic 7, Stacks and Queues.pdfDiunggah olehAnonymous sx2B8I8F
- Engel Cl ManualDiunggah olehHari Manikandan
- 1982 Clark Geometry EngineDiunggah olehDharm Maharaja
- C Program to implement Infix to Postfix Expression conversion algorithmDiunggah olehSaiyasodharan
- BCA-108 DSDiunggah olehKumar Sumit
- 8122420214_DataCDiunggah olehArun
- Chapter 3Diunggah olehAnonymous rY5FB0dz
- DuplicateDiunggah olehPuneet Dhiman
- 2 marks ESDiunggah olehJoshua Duffy
- 8085 Microprocessor Architecture PPTDiunggah olehJuhil H Patel
- CCUJ - Conformance checking between UML and JavaDiunggah olehSreehari B S
- Data StructuresDiunggah olehkarthikavilan
- PIC16F527Diunggah olehMarcelo Souto Maior
- System of “Analysis of Intersections Paths” for Signature RecognitionDiunggah olehAI Coordinator - CSC Journals
- Peeks_and_Pokes_for_the_Commodore_64.pdfDiunggah olehmado demano
- How to Simulate Recursive Functions Using Stack and While-loop to Avoid the Stack-overflow - CodeProjectDiunggah olehalexandrelmmm
- Stack OrganizationDiunggah olehShyam Vamsi
- 24608384-Exploit-writing-tutorial-part-1-Stack-Based-OverflowsDiunggah olehbalderdashbalderdash
- 11 Instruction Set of 8086Diunggah olehkingmeno
- Computer Graphics ExamplesDiunggah olehmonisani
- ATMEGA128-16AUDiunggah olehNarintorn Kanamoon
- Data Structure Using c NotesDiunggah olehZain Shoaib Mohammad
- Data Load BidyutDiunggah olehanima1982
- DataSturucture Objective-Questions IIISemDiunggah olehRohit Tiwari
- Operating System and Computer Architecture (CT049-3-1-OS)Diunggah olehCk Vin
- Hw03 Stack CheckbalancedDiunggah olehnguyentran263