Priority Queue - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Priority Queue

Description:

Definition: a dynamic set of elements, where each element is associated with some 'priority' ... A sorting algorithm with a running time of O(n log n) ... – PowerPoint PPT presentation

Number of Views:223
Avg rating:3.0/5.0
Slides: 12
Provided by: Udi8
Category:

less

Transcript and Presenter's Notes

Title: Priority Queue


1
Priority Queue
  • Definition
  • a dynamic set of elements, where each element is
    associated with some priority. Elements are
    entered into the queue as they come, but are
    removed from the queue according to their
    priority.
  • Operations
  • insert(s,x) - inserts the element x into the
    set.
  • Maximium (s) - returns the element of s with th
    largest key
  • Extract-Max(s) - removes and returns the element
    of S with the largest key..
  • Application
  • A scheduler of jobs on a shared computer

2
Heap Sort
  • A sorting algorithm with a running time of O(n
    log n)
  • The basic idea is to maintain a priority queue ,
    and then each extracting each time the maximal
    element at the head of the queue.
  • The goal
  • To find the most efficinet implemntatoin ( data
    structure ) for the priority queue and thus for
    the heapsort.

3
Heap
  • We could have used
  • Linear list - insert O(1) , delete O(n)
  • Ordered list - insert O(n) ,
    delete O(1)
  • Binary Search Tree - insert and delete O(log n)
    - the tree might get unbalanced and it suppotes
    many unneeded operations.
  • The implementation used is called a binary heap,
    which is the most commonly used.It is an array
    implementing a partially ordered binary tree.

4
Partially Ordered Trees (POT)
  • A data structure with efficient algorithm to
    deal with priority queues.
  • Definition of POT
  • A labeled, binary tree
  • label contains a priority field (a field with
    total order)
  • for every node n in POT
  • n.priority gt n.left.priority of all nodes in
    n.left-subtree (n.left)
  • n.priority gt n.right.priority of all nodes in
    n.right-subtree (n.right)
  • The priority of the root is always the maximal
    among all nodes in the tree, recursively.
  • What are the operation needed ?

5
Partially Ordered Trees (POT)
  • The operations performed on the tree must not
    effect its properties
  • Insert -
  • Insert at the bottom of the tree, maintaining the
    completeproperty.
  • Restore the POT property using bubble-up.
  • Delete-Max -
  • Delete the root (node with maximal priority)
  • Replace (the root) with the last element in tree
    (to maintain completeness)
  • Restore the POT property using bubble-down.

6
Partially Ordered Trees (POT)

void bubble-up (Node n) if n.parent
null or n.key lt n.parent.key return
else swap node n with n.parent bubble-up(n.p
arent)
void bubble-down (Node n) If (n.left
n.right null) return Node m
(n.left.key, n.right.key) if n.key gt
m.key return else swap nodes n,
m bubble-down (m)
T(n) O(h), h O(lg(n))
Why log(n)?
How to implement ?
7
Heap
  • The binary heap data structure is an array object
    that can be viewed as a complete partially
    ordered binary tree .
  • For each node in position I in the array

left(i) return 2i right(i) return
2i1 parent(i) return i/2
8
Heap
  • The heap is an efficient implementation of the
    balanced partially ordered tree that represents
    the priority queue.
  • It allows us to use the operations of the array (
    retrieving elements according to their index ) ,
    which are efficient and fast.

What are the operations needed ?
9
Heap OperationsHeapify - BubbleDown
heapify (A, i) l left (i) // indexes
of arrays used as pointers r right (i)
if l lt heap-sizeA and Al gt
Ai largest l else largest i
if r lt heap-size A and Ar gt Alargest
largest r if largest ! i exchange
Ai with Alargest heapify A, largest
// recursively correct subtree
T(n) O(h) O(lg n)
10
Heap OperationsBuildHeap
Given an unordered array A, transform A into a
Heap. (use heapify recursively, bottom up).
Build-heap (A) heap-sizeA lengthA
for j length A / 2 downto 1
heapify(A, j)
Why not the entire length?
11
Heap OperationsHeapSort
  • Heapsort (A)
  • build-heap(A)
  • for k lengthA downto 2
  • exchange A1 with Ak
  • heapsizeA heapsizeA - 1
  • heapify (A,1)

Delete-max
Why O (n log n)?
Write a Comment
User Comments (0)
About PowerShow.com