Docsity

Order Statistics and Selection Algorithm: Finding the Kth Order Statistic, Thesis of Engineering

Partial preview of the text

Download Order Statistics and Selection Algorithm: Finding the Kth Order Statistic and more Thesis Engineering in PDF only on Docsity!

Divide-and-Conquer Algorithms

Part Four

Announcements

ā—

Problem Set 2 due right now.

ā— Can submit by Monday at 2:15PM using one late period. ā—

Problem Set 3 out, due July 22.

ā— (^) Play around with divide-and-conquer algorithms and recurrence relations! ā— Covers material up through and including today's lecture.

Outline for Today

ā—

The Selection Problem

ā— A problem halfway between searching and sorting. ā—

A Linear-Time Selection Algorithm

ā— (^) A nonobvious algorithm with a nontrivial runtime. ā—

The Substitution Method

ā— (^) Solving recurrences the Master Theorem can't handle.

Order Statistics

ā— Given a collection of data, the k th order statistic is the k th smallest value in the data set. ā— For the purposes of this course, we'll use zero-indexing, so the smallest element would be given by the 0 th order statistic. ā— To give a robust definition: the k th order statistic is the element that would appear at position k if the data were sorted.

An Initial Solution

ā—

Any ideas how to solve this?

ā—

Here is one simple solution:

ā— Sort the array. ā— Return the element at the k th position. ā—

Unless we know something special about

the array, this will run in time O( n log n ).

ā—

Can we do better?

A Useful Subroutine: Partition

ā— Given an input array, a partition algorithm chooses some element p (called the pivot ), then rearranges the array so that ā— All elements less than or equal to p are before p. ā— (^) All elements greater p are after p. ā— p is in the position it would occupy if the array were sorted. ā— The algorithm then returns the index of p. ā— We'll talk about how to choose which element should be the pivot later; right now, assume the algorithm chooses one arbitrarily.

Partitioning an Array

Partitioning and Selection

ā— There is a close connection between partitioning and the selection problem. ā— Let k be the desired index and p be the pivot index after a partition step. Then: ā— If p = k , return A[ k ]. ā— If p > k , recursively select element k from the elements before the pivot. ā— If p < k , recursively select element ( k – p – 1) from the elements after the pivot.

Some Facts

ā—

The partitioning algorithm on an array of

length n can be made to run in time Θ( n ).

ā— Check the Problem Set Advice handout for an outline of an algorithm to do this. ā—

Partitioning algorithms give no

guarantee about which element is

selected as the pivot.

ā—

Each recursive call does Θ( n ) work, then

makes a recursive call on a smaller array.

Analyzing the Runtime

ā— The runtime of our algorithm depends on our choice of pivot. ā— In the best-case, if we pick a pivot that ends up at position k , the runtime is Θ( n ). ā— In the worst case, we pick always pick pivot that is the minimum or maximum value in the array. The runtime is given by this recurrence:

T(1) = Θ(1)

T( n ) = T( n – 1) + Θ( n )

T(1) = Θ(1)

T( n ) = T( n – 1) + Θ( n )

The Story So Far

ā—

If we have no control over the pivot in

the partition step, our algorithm has

runtime Ī©( n ) and O( n

2

ā—

Using heapsort, we could guarantee

O( n log n ) behavior.

ā—

Can we improve our worst-case bounds?

Finding a Good Pivot

ā—

Recall: We recurse on one of the two

pieces of the array if we don't

immediately find the element we want.

ā—

A good pivot should split the array so

that each piece is some constant fraction

of the size of the array.

ā— (^) (Those sizes don't have to be the same, though.)

Analyzing the Runtime

ā—

Our algorithm

ā— Recursively calls itself on the first 2/3 of the array. ā— Runs a partition step. ā— Then, either immediately terminates, or recurses in a piece of size n / 3 or a piece of size 2 n / 3. ā—

This gives the following recurrence:

T(1) = Θ(1)

T( n ) ≤ 2T(2 n / 3) + Θ( n )

T(1) = Θ(1)

T( n ) ≤ 2T(2 n / 3) + Θ( n )

Analyzing the Runtime

ā—

We have the following recurrence:

ā—

Can we apply the Master Theorem?

ā—

What are a , b , and d?

ā— a = 2 , b = 3 / 2 , and d = 1.

ā— Since log

3 / 2

2 > 1, the runtime is

T(1) = Θ(1)

T( n ) ≤ 2T(2 n / 3) + Θ( n )

T(1) = Θ(1)

T( n ) ≤ 2T(2 n / 3) + Θ( n )

O( n

log 3 / 2 2

) ā‰ˆ O( n