Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Priority Queues and Heaps, Thesis of Data Structures and Algorithms

An overview of priority queues and heaps, important data structures in computer science. It discusses the motivation for using priority queues, the operations needed, and how they can be implemented using different data structures. The document then introduces binary heaps, a specialized data structure that can efficiently support key priority queue operations. It explains the properties of binary heaps, including the complete tree structure and heap order property, and describes how they can be implemented using an array. The document also includes sample code for basic heap operations. Overall, this provides a comprehensive introduction to priority queues and heaps, widely used in various applications.

Typology: Thesis

2022/2023

Uploaded on 11/06/2022

oezguen-guelsevinc
oezguen-guelsevinc 🇹🇷

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Today's Agenda
Priority Queues and Heaps
Priority Queues and Heaps
Motivation
Recall the FindMin operation : Find the smallest ( or highest priority) item quickly,
Some Applications:
Operating system needs to schedule jobs according to priority instead of
FIFO
Find student with highest grade, employee with highest salary etc.
Priority Queues
Think of a priority queue as a kind of bag that holds priorities. You can put one in,
and you can take out the current highest priority. (Priorities can be any Comparable
values; in our examples, we'll just use numbers.)
A priority queue is different from a "normal" queue, because instead of being a
"first-in-first-out" data structure, values come out in order by priority.
A priority queue might be used, for example, to handle the jobs sent to the Computer
Science Department's printer: Jobs sent by the department chair should be printed
first, then jobs sent by professors, then those sent by graduate students, and finally
those sent by undergraduates. The values put into the priority queue would be the
priority of the sender (e.g., using 4 for the chair, 3 for professors, 2 for grad
students, and 1 for undergrads), and the associated information would be the
document to print. Each time the printer is free, the job with the highest priority
would be removed from the print queue, and printed. (Note that it is OK to have
multiple jobs with the same priority; if there is more than one job with the same
highest priority when the printer is free, then any one of them can be selected.)
The operations that need to be provided for a priority queue are shown in the following
table, assuming that just priorities (no associated information) are to be stored in a priority
queue.
Priority Queue ADT
Priority Queue should efficiently do
PriorityQ()
Create an empty priority queue
FindMin()
Returns minimum value but does not delete it
DeleteMin()
pf3
pf4
pf5

Partial preview of the text

Download Priority Queues and Heaps and more Thesis Data Structures and Algorithms in PDF only on Docsity!

Today's Agenda

  • Priority Queues and Heaps

Priority Queues and Heaps

Motivation

  • Recall the FindMin operation : Find the smallest ( or highest priority) item quickly,
  • Some Applications:
    • Operating system needs to schedule jobs according to priority instead of

FIFO

  • Find student with highest grade, employee with highest salary etc.

Priority Queues

  • Think of a priority queue as a kind of bag that holds priorities. You can put one in,

and you can take out the current highest priority. (Priorities can be any Comparable

values; in our examples, we'll just use numbers.)

  • A priority queue is different from a "normal" queue, because instead of being a

"first-in-first-out" data structure, values come out in order by priority.

  • A priority queue might be used, for example, to handle the jobs sent to the Computer

Science Department's printer: Jobs sent by the department chair should be printed

first, then jobs sent by professors, then those sent by graduate students, and finally

those sent by undergraduates. The values put into the priority queue would be the

priority of the sender (e.g., using 4 for the chair, 3 for professors, 2 for grad

students, and 1 for undergrads), and the associated information would be the

document to print. Each time the printer is free, the job with the highest priority

would be removed from the print queue, and printed. (Note that it is OK to have

multiple jobs with the same priority; if there is more than one job with the same

highest priority when the printer is free, then any one of them can be selected.)

The operations that need to be provided for a priority queue are shown in the following

table, assuming that just priorities (no associated information) are to be stored in a priority

queue.

Priority Queue ADT

  • Priority Queue should efficiently do
    • PriorityQ()
      • Create an empty priority queue
    • FindMin()
      • Returns minimum value but does not delete it
    • DeleteMin()
  • Returns minimum value and deletes it
  • Insert( k )
  • Inserts the key value k to the priority queue
  • Size() and IsEmpty()

List implementation of a Priority Queue

A priority queue can be implemented using many of the data structures that we've already

studied (an array, a linked list, or a binary search tree). However, those data structures do

not provide the most efficient operations. To make all of the operations very efficient, we'll

use a new data structure called a heap.

  • What if we use unsorted lists
    • FindMin and DeleteMin are O ( n )
      • Indeed, we have to go through the whole list
    • Insert( k ) is O^ (^1 )
  • What if we use sorted lists
    • FindMin and DeleteMin are O ( 1 )
      • Be careful if we want both Min and Max (circular array or doubly

linked list)

  • Insert( k ) is O^ (^ n )
  • Note that, in this case, we need sorting algorithms to sort the given list.

Binary Search Tree (BST) implementation of a Priority Queue

  • Worst case (degenerate tree)
    • FindMin(), DeleteMin() and Insert( k ) are all O^ (^ n )
  • Best case (completely balanced BST)
    • FindMin(), DeleteMin and Insert( k ) are all O ( log n )
  • Balanced BSTs (such as AVL)
    • FindMin(), DeleteMin() and Insert( k ) are all O^ (^ log^ n )

Binary Heaps

  • We can do better than Balanced BST?
    • With limited requirements: Insert, FindMin, DeleteMin.
    • The goals are:
      • FindMin is O ( 1 )
      • Insert is O^ (^ log^ n )
      • DeleteMin is O ( log n )

Binary Heaps

  • A binary heap is a binary tree (NOT a BST) that is:
  • Not complete tree, so it is not heap
  • Complete tree, but min (or max) heap order is broken Array Implementation of Heaps
  • Root node = A^ [^1 ], (note indexing starts with 1, not zero)
  • Children of A [ i ] are A [ 2 i ] and, A [ 2 i + 1 ]
  • Keep track of current size n (number of nodes) FindMin() and DeleteMin() Operations
  • FindMin()
  • It is easy. Just return the root value A [ 1 ]
  • Runtime is O ( 1 )
  • DeleteMin()
  • Delete and return the root node. Maintain the Structure Property
  • We now have a “Hole” at the root
  • Need to fill the hole with another value
  • When we get done, the tree will have one less node and must still be complete display(HTML("
    ")) <IPython.core.display.HTML object> Maintain the Heap Property
  • The last value has lost its node
  • we need to find a new place for it DeleteMin(): Percolate Down
  • Keep comparing with children A [ 2 i ] and A [ 2 i + 1 ]
  • Copy smaller child up and go down one level
  • Done if both children are ≥ item or reached a leaf node
  • What is the run time?
    • It is height of the tree, O^ (^ log^ n ). Insertion: Insert( k )
  • Add a value to the tree
  • Structure and heap order properties must still be correct when we are done Maintain the Structure Property
  • The only valid place for a new node in a complete tree is at the end of the array
  • We need to decide on the correct value for the new node, and adjust Maintain the Heap Property
  • Where does the new value go?
  • We search on the path from the new place to the root to find the correct place for it

in the tree

Insertion: Percolate Up

  • Start at last node and keep comparing with parent A^ [^ i /^2 ]
  • If parent larger, copy parent down and go up one level
  • Done if parent item or reached top node A [ 1 ] Insertion is done
  • What is the run time?
  • Again, it is height of the tree, O^ (^ log^ n ). class BinHeap: def init(self): self.heapList = [ 0 ] self.currentSize = 0 def percUp(self,i): while i // 2 > 0 : if self.heapList[i] < self.heapList[i // 2 ]: tmp = self.heapList[i // 2 ] self.heapList[i // 2 ] = self.heapList[i] self.heapList[i] = tmp i = i // 2

position pass def rightChild(self, position): # returns the position of the right child for the node currently at position pass bh = BinHeap() bh.buildHeap([ 2 , 4 , 6 , 7 , 5 ]) print(bh.parent( 4 )) 2 print(bh.delMin()) 2 print(bh.heapList[ 1 :bh.currentSize+ 1 ]) [4, 5, 6, 7] print(bh.delMin()) print(bh.delMin()) print(bh.delMin()) print(bh.delMin()) 4 5 6 7 bh1 = BinHeap() bh1.buildHeap([ 9 , 5 , 6 , 2 , 3 ]) print(bh1.heapList[ 1 :bh1.currentSize+ 1 ]) [2, 3, 6, 5, 9]