On RAM PRIORITY QUEUES

Preview:

DESCRIPTION

On RAM PRIORITY QUEUES. MIKKEL THORUP. Objective. Sorting is a basic technique for a lot of algorithms. e.g. find the minimum edge of the graph, scheduling, …. We want to decrease the time of sorting, and the author presented a new data structure to implement sorting. Abstract. - PowerPoint PPT Presentation

Citation preview

On RAM PRIORITY QUEUES

MIKKEL THORUP

Objective

Sorting is a basic technique for a lot of algorithms.

e.g. find the minimum edge of the graph, scheduling, ….

We want to decrease the time of sorting, and the author presented a new data structure to implement sorting.

Abstract First Result:

Implement a queue supporting find-min in constant time, delete-min in O(nloglogn)

Speedup Dijkstra’s Algorithm with this queue

Second Result:Sorting is equivalent to priority queue

Machine Model A model of computation is a simplied abstraction of a

computer that runs a program. The running time of an algorithm in the model is the n

umber of elementary steps needed to complete an implementation of the algorithm.

Turing Machine Comparison Model Random Access Machine

RAM modelRandom Access Machine (RAM)

A CPU

An potentially unbounded bank of memory cells, each of which can hold arbitrary number of character

The RAM ModelEach “simple” operation (+, -, =, if, call procedure) takes exactly 1 step.Basically, any basic operation that takes a small, fixed amount of time we assume to take just one step.

We measure the run time of an algorithm by counting the number of steps it takes.

We allow ourselves to use keys and segments of keys as addresses.

Bucket Sort

Bucket-sort sorts in linear time on the average with some assumptions.

Assumption: Input elements are evenly distributed over the interval [0,1).

Data structures used:

Input: A[1…n] where 1][0 iAAuxiliary: B[0…n-1] is a set of buckets (implemented as linked list).Each of B[i] is a pointer to a sorted listed of A[j]’s that fall into bucket i.

Bucket Sort

BUCKET SORT(A)

1. n ← length[A]

2. for i ← 1 to n

3. do insert A[i] into list

4. for i ← 1 to n-1

5. do sort list B[i] with INSERTION SORT

6. concatenate the lists B[0], B[1], …, B[n-1] together in order

.59

.32

.52

.02

.11

.45

A:

1

2

3

4

5

6

10

B:

.90

0

1

2

3

4

5

9

.02 /

.11 /

.32 /

.59 / .59 /

.45 /

.90 /

.52

10n:

Bucket SortTime Complexity:Let ni be the random variable denoting number of elements placed inn bucket B[i]. Since insertion sort runs in quadratic time, the expected time to sort bucket B[i] is . ])[()]([ 22

ii nEOnOE

)][()]([1

0

21

0

2

n

ii

n

ii nEOnOE

][][][ 22iii nEnVarnE

The probability of the element falling in bucket B[i] is p = 1/n.

1][ npnE i npnpnVar i11)1(][

nn12111 2

Sorting in O(nloglogn) time

Use “packed sort” and “range reduction” to achieve the goal.

We will introduce “packed sort” and “range reduction” in next slides.

Packed SortIdea: Packed sorting saves on integer sorting by packing several integers into a single word and operating simultaneously on all of them at unit cost.

It stores keys in the so-called word representation, i.e., k to a word, where k = Θ(lognloglogn).

one word

our integer

ω

ω/k length(b) <= nn logloglog/

Packed SortThere is a subroutine to merge two sorted sequences, each consisting of k keys and given in the word representation, in O(logk) time.

1 2 5 6 3 4 7 8

3 7 11 15

O(logk)

It can handle k keys at a cost of O(logk), it saves a factor of Θ(k/logk) relative to standard merge sort, so that the total time needed comes to O(nlogn logk/k) = O(n).

Packed Sort

)logloglog

logloglogloglog(

)logloglog

)logloglog(log()/(log

nn

nnO

nn

nnOkkO

)log

1(

nO

)()log

log()/loglog( nO

n

nnOkknnO

Don’t Care!

T(n,b) is the worst-case time needed to sort n integers of b bits each.T(n,b) = O(n)

Range ReductionIdea: In O(n) time we can reduce by about half the number bits in the integers to be sorted.

slice origin integer to small pieces

slice origin integer to small pieces

do Packed Sort

)()2,(),( nObnTbnT

Range ReductionTime Complexity:

We apply the range reduction times, at a total cost O(nloglogn)

nloglog2

)loglog(

)log(

)()2,(),(

nnO

bnO

nObnTbnT

nb log

Fast Basic Priority QueueThe goal is to create a priority queue supporting find-min in constant time and insert and delete-min in O(loglogn) time, where n is the current number of keys in the queue.

We implement priority queue with short keys first.

There is a priority queue with capacity for n (ω/lognloglogn ) bit keys, supporting insert, find-min, and delete-min on integers in constant time per operation.

Preliminaries

n : the number of the input keys

ω : the bits of the address of memory

nlogx[i] : the (i+1)th bit from the left

0 1 … i2

length = i +1

x

Preliminaries

22]1[ nx

i20 1 … … n-2 n-1

memory

x

x = 12]0[ nx

12]2[... nx

32]2[ nx02]1[ nx

12][... inix

1

0

]1[2n

i

i inxx

Preliminaries

][]...[]..[ jxixjix

)1()( jiix

x

x[i]

length = i length = ω-(j-i+1)

ω

Preliminariesflip bit i

1

x[i]

ω

ω-i-1

0…0 0…0 1 << (ω-1-i )

x[i] ⊕

expo(x) xlog

10…0

ω-expo(x)-1

Recommended