25
CSC 172 DATA STRUCTURES

CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert

Embed Size (px)

Citation preview

CSC 172 DATA STRUCTURES

Priority QueuesModel

Set with priorities associatedwith elementsPriorities are comparable by a < operator

OperationsInsert an element with a given priorityDeletemax or Deletemin Find and remove from the set the element with highest priority

PriorityQueue Interface

public Interface PriorityQueue {int size();boolean isEmpty();void add(Object element);Object getMax();Object deleteMax();

}

Sorted Linked List implementation

Deletemax is O(1) – take first elementInsert is O(n) – on average, go halfway down the

list

Partially Ordered Tree

A binary tree with elements and priorities at nodesSatisfying the partially ordered tree (POT)

propertyThe priority at any node >= the priority of its childrenIn Weiss, the VALUE of the key is <= values of children,

interpretation is up to user.

Balanced POT

A POT in which all nodes exist, down to a certain level, and for one level below that, “extra” nodes may exist, as far left as possible

9

6 3

4 1 3 2

1

Insertion on a Balanced POT1. Place new element in the leftmost possible

place2. “Bubble up” new node by repeatedly

exchanging it with its parent if it has higher priority than its parent

Restores the POT property Maintains balanced property O(log n) because an n-node balanced tree has no

more than 1 + log2n levels

Deletemax on Balanced POT

1. Select element at root2. Replace root element by rightmost element at

lowest level3. Bubble down root by repeatedly exchanging it

with the larger of its children, as long as it is smaller than either

Restores POT property Preserves balance O(log n) because O(1) at each step along a path of

length at most log2n

Heap Data StructureRepresent a balanced POT by an array An nodes represented by A[1] through A[n]A[0] not usedArray holds elements level-by-level

9

6 3

4 1 3 2

1 12314369

A node represented byA[k] has parent A[k/2]

Children of A[k] are:A[2*k]A[2*k+1]

Bubble up in Heap Array

At position k:1. If k == 1, then done2. Otherwise, compare A[k] with A[k/2].

If A[k] is smaller, then done Otherwise swap and repeat at k/2

Bubble down in Heap Array

At positition k1. If 2k > n, then done2. Otherwise, see if A[k] is smaller than A[2k] or

A[2k + 1] (if 2k+1 <= n) If not, done If so, swap with larger and repeat

Insert in Heap Array

1. Put new element in A[n+1] 2. Add 1 to n3. Bubble up

Insert on heap – O(log n)

Put new element in A[n+1] O(1) Add 1 to n O(1) Bubbleup O(log n)

Deletemax in Heap Array

1. Take A[1]2. Replace it by A[n]3. Subtract 1 from n4. Bubble down

Deletemax in heap array – O(log n)

Take A[1] O(1) Replace it by A[n] O(1) Subtract 1 from n O(1) Bubble down O(log n)

POP QUIZ

What heap do you get if you 1. Insert {1,8,2,7,3,6,4}2. Deletemax3. Deletemax4. Insert {5,9}5. Deletemax

Make a Heap

1. Simplest idea: insert N items: O(n) average, O(nlogn) worst case.

2. Heapify array A by bubbling down A[k] for:k = n/2, n/2-1, n/2-2,…,1 in orderHow long does this take?

Might seems like O(n log n) but really O(n)1. Repeatedly deletemax until all are selectedPriority is reverse of sorted ordern deletemaxes @ O(log n) each == O(n log n)

Big-Oh of Heapify

Bubble down n/2, n/2-1, n/2-2,…,1 in order

9

7 6

5

4 2

4

3 2

3

1 2

6

3 5

532421236534679

Big-Oh of Heapify

Bubble down n/2, n/2-1, n/2-2,…,1 in order

9

7 6

5

4 2

4

3 2

3

1 2

6

3 5

532421236534679

Big-Oh of Heapify

Bubble down n/2, n/2-1, n/2-2,…,1 in order

9

7 6

5

4 2

4

3 2

3

1 2

6

3 5

532421236534679

Big-Oh of Heapify

Bubble down n/2, n/2-1, n/2-2,…,1 in order

9

7 6

5

4 2

4

3 2

3

1 2

6

3 5

532421236534679

Big-Oh of Heapify

Max. number of swaps at each level

532421236534679

No swapsAt most one each

At most two each

Big-Oh of Heapify

0123…

n/2n/4n/8n/16…

∑i=1

log2n

i n2i1

=n2

∑i=1

log2n i2i

Big-Oh of Heapify

0123…

n/2n/4n/8n/16…

∑i=1

log2n

i n2i1

n2∑i=1

∞ i

2i

(n/2)(1/2+2/4+3/8+4/16…..)

=n2

∑i=1

log2n i2i <

Big-Oh of Heapify

1/2+2/4+3/8+4/16…..

1/2+(1/4+1/4)+(1/8+1/8+1/8) +…..

1/2+ 1/4 + 1/8 + … = 1

1/4 + 1/8 +….. = 1/2 1/8 +….. = 1/4

….. = …

So, O(2*n/2) = O(n)