Docsity

Dynamic Order Statistics - Introduction to Algorithms - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Dynamic Order Statistics - Introduction to Algorithms - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Algorithms

Dynamic Order Statistics

Review: Choosing A Hash Function

● Choosing the hash function well is crucial

■ Bad hash function puts all elements in same slot

■ A good hash function:

○ Should distribute keys uniformly into slots
○ Should not depend on patterns in the data

● We discussed three methods:

■ Division method

■ Multiplication method

■ Universal hashing

Review: Universal Hashing

● A family of hash functions ς is said to be

universal if:

■ With a random hash function from ς, the chance of

a collision between x and y is exactly 1/ m ( x ≠ y )

● We can use this to get good expected performance:

■ Choose h from a universal family of hash functions

■ Hash n keys into a table of m slots, n ≤ m

■ Then the expected number of collisions involving

a particular key x is less than 1

Review: A Universal Hash Function

● Choose table size m to be prime

● Decompose key x into r +1 bytes, so that

x = { x 0 , x 1 , …, x r }

■ Only requirement is that max value of byte < m

■ Let a = { a 0 , a 1 , …, a r } denote a sequence of r+ 1

elements chosen randomly from {0, 1, …, m - 1}

■ Define corresponding hash function h a ∈ ς:

■ With this definition, ς has m r+^1 members

r
i

ha x aixi m

mod

Review: Order Statistic Trees

● OS Trees augment red-black trees:

■ Associate a size field with each node in the tree

■ x->size records the size of subtree rooted at x,

including x itself:

M 8

C 5

P 2

Q 1

A 1

F 3

D 1

H 1

Selection On OS Trees

M 8

C 5

P 2

Q 1

A

1

F 3

D 1

H 1

How can we use this property

to select the i th element of the set?

OS-Select Example

● Example: show OS-Select( root , 5):

M 8

C 5

P 2

Q 1

A 1

F 3

D 1

H 1

OS-Select(x, i)

{

r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r);

}

OS-Select Example

● Example: show OS-Select( root , 5):

M 8

C 5

P 2

Q 1

A 1

F 3

D 1

H 1

OS-Select(x, i)

{

r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r);

}

i = 5 r = 6

OS-Select Example

● Example: show OS-Select( root , 5):

M 8

C 5

P 2

Q 1

A 1

F 3

D 1

H 1

OS-Select(x, i)

{

r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r);

}

i = 5 r = 6

i = 5 r = 2

i = 3 r = 2

OS-Select Example

● Example: show OS-Select( root , 5):

M 8

C 5

P 2

Q 1

A 1

F 3

D 1

H 1

OS-Select(x, i)

{

r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r);

}

i = 5 r = 6

i = 5 r = 2

i = 3 r = 2

i = 1 r = 1

OS-Select

OS-Select(x, i)

{

r = x->left->size + 1;

if (i == r) return x;

else if (i < r)

return OS-Select(x->left, i);

else

return OS-Select(x->right, i-r);

}

● What will be the running time?

Determining The

Rank Of An Element

M 8

C 5

P 2

Q 1

A

1

F 3

D 1

H 1

What is the rank of this element?

Determining The

Rank Of An Element

M 8

C 5

P 2

Q 1

A

1

F 3

D 1

H 1

Of the root? What’s the pattern here?

Determining The

Rank Of An Element

M 8

C 5

P 2

Q 1

A

1

F 3

D 1

H 1

What about the rank of this element?